Remove dynamic exceptions
[anna.git] / example / timex / ArithmeticHTTPServer / Reactor.cpp
1 // ANNA - Anna is Not Nothingness Anymore                                                         //
2 //                                                                                                //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo                         //
4 //                                                                                                //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite                           //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
7
8
9 #include <anna/core/tracing/Logger.hpp>
10 #include <anna/core/tracing/TraceMethod.hpp>
11
12 #include <anna/app/functions.hpp>
13
14 #include <anna/comm/ClientSocket.hpp>
15 #include <anna/comm/Communicator.hpp>
16
17 #include "Application.hpp"
18 #include "Reactor.hpp"
19 #include "Context.hpp"
20 #include "Transaction.hpp"
21
22 using namespace std;
23 using namespace anna;
24 using namespace test;
25
26 /**
27  * Recupera la respuesta del servidor que usa ANNA.comm y lo transfiere al
28  * cliente original que usaba el protocolo HTTP.
29  */
30 void http4comm::Reactor::apply (comm::ClientSocket&, const comm::Message& message) 
31    noexcept(false)
32 {
33    LOGMETHOD (TraceMethod tm ("http4comm::Reactor", "apply", ANNA_FILE_LOCATION));
34
35    comm::Communicator* communicator = app::functions::component <comm::Communicator> (ANNA_FILE_LOCATION);
36    
37    http4comm::Application& app = static_cast <http4comm::Application&> (app::functions::getApp ());      
38    Context* context = app.getContext ();   
39   
40    a_response.decode (message.getBody ());
41
42    // Como cookie entre extremos del gateway usa el inittime de la peticiĆ³n
43    Transaction* transaction = static_cast <http4comm::Transaction*> (context->find (a_response.initTime));
44
45    comm::ClientSocket* clientSocket = transaction->getClientSocket ();
46
47    if (communicator->isUsable (clientSocket) == false) {
48       LOGWARNING (
49          string msg (transaction->asString ());
50          msg += " | Response ignoreb because of ClientSocket";
51          Logger::warning (msg, ANNA_FILE_LOCATION);
52       )
53       return;
54    }
55
56    a_httpResponse.setStatusCode (200);
57    a_httpResponse.setBody (message.getBody ());
58
59    try {
60       clientSocket->send (a_httpResponse);
61    }
62    catch (RuntimeException& ex) {
63       ex.trace ();
64    }
65
66    context->close (transaction);
67 }
68
69