Remove dynamic exceptions
[anna.git] / example / timex / ArithmeticHTTPServer / Acceptor.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/TraceMethod.hpp>
10
11 #include <anna/app/functions.hpp>
12 #include <anna/app/Application.hpp>
13
14 #include <anna/comm/Communicator.hpp>
15 #include <anna/comm/IndexedDelivery.hpp>
16 #include <anna/comm/CongestionController.hpp>
17
18 #include <anna/http/Request.hpp>
19
20 #include "Application.hpp"
21 #include "Request.hpp"
22 #include "Acceptor.hpp"
23 #include "Context.hpp"
24
25 using namespace std;
26 using namespace anna;
27 using namespace test;
28
29 void http4comm::Acceptor::initialize ()
30    noexcept(false)
31 {
32 }
33
34 void http4comm::Acceptor::evRequest (comm::ClientSocket& clientSocket, const http::Request& request)
35    noexcept(false)
36 {
37    LOGMETHOD (TraceMethod tm ("http4comm::Acceptor", "evRequest", ANNA_FILE_LOCATION));
38
39    comm::CongestionController& ccgg = comm::CongestionController::instantiate ();
40
41    if (ccgg.getAdvice (clientSocket) == comm::CongestionController::Advice::Discard)
42       return;
43
44    const DataBlock& body = request.getBody ();
45
46    if (body.getSize () == 0)
47       throw RuntimeException ("La peticion no incorpora los parametros de operacion", ANNA_FILE_LOCATION);
48
49    LOGINFORMATION (
50       string msg ("Body recibido: ");
51       msg += anna::functions::asString (body);
52       Logger::information (msg, ANNA_FILE_LOCATION);
53    );
54
55    if (comm::Codec::getType (body) != Request::Id) 
56       throw RuntimeException ("El mensaje recibido no es una peticion aritmetica", ANNA_FILE_LOCATION);
57
58    int index;
59
60    a_testRequest.decode (body);
61
62    comm::IndexedDelivery* delivery = static_cast <Application&> (app::functions::getApp ()).getService ();
63
64    try {
65       switch (a_testRequest.op) {
66          case '+': index = 0; break;
67          case '-': index = 1; break;
68          case '*': index = 2; break;
69          case '/': index = 3; break;
70       }
71       delivery->prepare (index);
72       delivery->send (a_testRequest);
73       
74       http4comm::Application& app = static_cast <http4comm::Application&> (app::functions::getApp ());      
75       app.getContext ()->create (a_testRequest.initTime, clientSocket);
76    }
77    catch (RuntimeException& ex) {
78       ex.trace ();
79    }
80 }
81