Updated license
[anna.git] / example / timex / ArithmeticHTTPServer / Acceptor.cpp
1 // ANNA - Anna is Not Nothingness Anymore
2 //
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
4 //
5 // https://bitbucket.org/testillano/anna
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
9 // are met:
10 //
11 //     * Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 //     * Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following disclaimer
15 // in the documentation and/or other materials provided with the
16 // distribution.
17 //     * Neither the name of Google Inc. nor the names of its
18 // contributors may be used to endorse or promote products derived from
19 // this software without specific prior written permission.
20 //
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 //
33 // Authors: eduardo.ramos.testillano@gmail.com
34 //          cisco.tierra@gmail.com
35
36
37 #include <anna/core/tracing/TraceMethod.hpp>
38
39 #include <anna/app/functions.hpp>
40 #include <anna/app/Application.hpp>
41
42 #include <anna/comm/Communicator.hpp>
43 #include <anna/comm/IndexedDelivery.hpp>
44 #include <anna/comm/CongestionController.hpp>
45
46 #include <anna/http/Request.hpp>
47
48 #include "Application.hpp"
49 #include "Request.hpp"
50 #include "Acceptor.hpp"
51 #include "Context.hpp"
52
53 using namespace std;
54 using namespace anna;
55 using namespace test;
56
57 void http4comm::Acceptor::initialize ()
58    throw (RuntimeException)
59 {
60 }
61
62 void http4comm::Acceptor::evRequest (comm::ClientSocket& clientSocket, const http::Request& request)
63    throw (RuntimeException)
64 {
65    LOGMETHOD (TraceMethod tm ("http4comm::Acceptor", "evRequest", ANNA_FILE_LOCATION));
66
67    comm::CongestionController& ccgg = comm::CongestionController::instantiate ();
68
69    if (ccgg.getAdvice (clientSocket) == comm::CongestionController::Advice::Discard)
70       return;
71
72    const DataBlock& body = request.getBody ();
73
74    if (body.getSize () == 0)
75       throw RuntimeException ("La peticion no incorpora los parametros de operacion", ANNA_FILE_LOCATION);
76
77    LOGINFORMATION (
78       string msg ("Body recibido: ");
79       msg += anna::functions::asString (body);
80       Logger::information (msg, ANNA_FILE_LOCATION);
81    );
82
83    if (comm::Codec::getType (body) != Request::Id) 
84       throw RuntimeException ("El mensaje recibido no es una peticion aritmetica", ANNA_FILE_LOCATION);
85
86    int index;
87
88    a_testRequest.decode (body);
89
90    comm::IndexedDelivery* delivery = static_cast <Application&> (app::functions::getApp ()).getService ();
91
92    try {
93       switch (a_testRequest.op) {
94          case '+': index = 0; break;
95          case '-': index = 1; break;
96          case '*': index = 2; break;
97          case '/': index = 3; break;
98       }
99       delivery->prepare (index);
100       delivery->send (a_testRequest);
101       
102       http4comm::Application& app = static_cast <http4comm::Application&> (app::functions::getApp ());      
103       app.getContext ()->create (a_testRequest.initTime, clientSocket);
104    }
105    catch (RuntimeException& ex) {
106       ex.trace ();
107    }
108 }
109