1 // ANNA - Anna is Not Nothingness Anymore //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo //
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 //
10 Ejemplo de programa servidor. Atiende peticiones aritmeticas, el protocolo de transporte sera HTTP
11 ver http::Transport y el contenido del mensaje sera el resutaldo de un comm::Codec con los
12 (x, y, op) -> El resultado sera estos tres componente mas result.
14 Para poder probar el sistema de congestion se puede indicar un numero de milisegundos de
15 retardo aplicados a cada contestacion.
17 Los clientes pueden ser: http_kclient.p http_client.p
21 #include <anna/core/core.hpp>
23 #include <anna/xml/Node.hpp>
24 #include <anna/xml/Attribute.hpp>
26 #include <anna/comm/comm.hpp>
28 #include <anna/http/Request.hpp>
29 #include <anna/http/Response.hpp>
30 #include <anna/http/Handler.hpp>
31 #include <anna/http/Transport.hpp>
32 #include <anna/http/functions.hpp>
34 #include <anna/test/Request.hpp>
35 #include <anna/test/Response.hpp>
36 #include <anna/test/Communicator.hpp>
40 class MyHandler : public http::Handler {
42 MyHandler () : http::Handler ("http_server::MyHandler") {
43 allocateResponse ()->createHeader (http::Header::Type::Date);
47 test::Request a_testRequest;
48 test::Response a_testResponse;
50 void evRequest (ClientSocket&, const http::Request& request) noexcept(false);
51 void evResponse (ClientSocket&, const http::Response&) noexcept(false) {;}
54 class MyCommunicator : public test::Communicator {
57 a_contexts ("Contexts")
61 ThreadData <MyHandler> a_contexts;
63 void eventReceiveMessage (comm::ClientSocket&, const Message&) noexcept(false);
66 class HTTPArithmeticServer : public comm::Application {
68 HTTPArithmeticServer ();
71 MyCommunicator a_communicator;
72 comm::ServerSocket* a_serverSocket;
74 void initialize () noexcept(false);
75 void run () noexcept(false);
76 xml::Node* asXML (xml::Node* app) const ;
80 using namespace anna::comm;
82 int main (int argc, const char** argv)
84 CommandLine& commandLine (CommandLine::instantiate ());
85 HTTPArithmeticServer app;
87 http::functions::initialize ();
92 commandLine.initialize (argv, argc);
93 commandLine.verify ();
95 Logger::setLevel (Logger::Debug);
96 Logger::initialize ("http_server", new anna::TraceWriter ("file.trace", 4048000));
100 catch (Exception& ex) {
101 cout << ex.asString () << endl;
107 HTTPArithmeticServer::HTTPArithmeticServer () :
108 Application ("http_server", "Servidor HTTP de operaciones aritmeticas", "1.0")
110 CommandLine& commandLine (CommandLine::instantiate ());
112 commandLine.add ("p", CommandLine::Argument::Mandatory, "Puerto en el que atender peticiones");
113 commandLine.add ("a", CommandLine::Argument::Mandatory, "Direccin IP en la que atender");
114 commandLine.add ("d", CommandLine::Argument::Mandatory, "Retardo aplicado a la contestacion");
115 commandLine.add ("r", CommandLine::Argument::Optional, "Indicador de reuso de direccin", false);
116 commandLine.add ("limit", CommandLine::Argument::Mandatory, "% de ocupacion que permitimos");
117 commandLine.add ("n", CommandLine::Argument::Optional, "Numero de mensajes a servir", true);
118 commandLine.add ("trace", CommandLine::Argument::Optional, "Nivel de trazas (debug,warning, error,...)");
119 commandLine.add ("timeout", CommandLine::Argument::Optional, "Milisegundos transcurridos sin actividad para cerrar el socket");
122 void HTTPArithmeticServer::initialize ()
125 CommandLine& cl (CommandLine::instantiate ());
127 int port = cl.getIntegerValue ("p");
128 const comm::Device* device = Network::instantiate ().find (Device::asAddress (cl.getValue ("a")));
130 a_serverSocket = new ServerSocket (INetAddress (device, port), cl.exists ("r"), &http::Transport::getFactory ());
133 void HTTPArithmeticServer::run ()
136 CommandLine& cl (CommandLine::instantiate ());
138 a_communicator.attach (a_serverSocket);
139 a_communicator.setDelay ((Millisecond)cl.getIntegerValue ("d"));
141 if (cl.exists ("n") == true)
142 a_communicator.setMaxMessage (cl.getIntegerValue ("n"));
144 if (cl.exists ("trace"))
145 Logger::setLevel (Logger::asLevel (cl.getValue ("trace")));
147 CongestionController::instantiate ().setLimit (cl.getIntegerValue ("limit"));
149 if (cl.exists ("timeout"))
150 a_communicator.setTimeout ((Millisecond)cl.getIntegerValue ("timeout"));
152 a_communicator.accept ();
155 xml::Node* HTTPArithmeticServer::asXML (xml::Node* app) const
158 xml::Node* node = app::Application::asXML (app);
160 node->createAttribute ("MaxMessage", a_communicator.getMaxMessage ());
161 node->createAttribute ("Message", a_communicator.getMessage ());
166 void MyCommunicator::eventReceiveMessage (ClientSocket& clientSocket, const Message& message)
169 LOGMETHOD (TraceMethod tm ("MyCommunicator", "eventReceiveMessage", ANNA_FILE_LOCATION));
171 if (clientSocket.support (http::Transport::className ()) == false)
174 if (canContinue (clientSocket) == false)
179 MyHandler& httpHandler = a_contexts.get ();
181 httpHandler.apply (clientSocket, message);
184 void MyHandler::evRequest (ClientSocket& clientSocket, const http::Request& request)
187 const DataBlock& body = request.getBody ();
189 if (body.getSize () == 0)
190 throw RuntimeException ("La peticion no incorpora los parametros de operacion", ANNA_FILE_LOCATION);
193 string msg ("Body recibido: ");
194 msg += anna::functions::asString (body);
195 Logger::information (msg, ANNA_FILE_LOCATION);
198 if (Codec::getType (body) != test::Request::Id)
199 throw RuntimeException ("El mensaje recibido no es una peticion aritmetica", ANNA_FILE_LOCATION);
201 a_testRequest.decode (body);
203 a_testResponse.x = a_testRequest.x;
204 a_testResponse.y = a_testRequest.y;
205 a_testResponse.initTime = a_testRequest.initTime;
207 http::Response* response = allocateResponse ();
209 response->setStatusCode (200);
211 switch (a_testResponse.op = a_testRequest.op) {
213 a_testResponse.result = a_testRequest.x + a_testRequest.y;
216 a_testResponse.result = a_testRequest.x - a_testRequest.y;
219 a_testResponse.result = a_testRequest.x * a_testRequest.y;
222 if (a_testRequest.y == 0) {
223 response->setStatusCode (400);
224 response->setReasonPhrase ("Division por cero");
225 a_testResponse.result = 0;
228 a_testResponse.result = a_testRequest.x / a_testRequest.y;
232 response->setBody (a_testResponse.code ());
234 response->find (http::Header::Type::Date)->setValue ("Mon, 30 Jan 2006 14:36:18 GMT");
236 http::Header* keepAlive = response->find ("Keep-Alive");
238 if (keepAlive == NULL)
239 keepAlive = response->createHeader ("Keep-Alive");
241 keepAlive->setValue ("Verificacion del cambio 1.0.7");
245 string msg = anna::functions::asString ("%d %c %d = %d", a_testRequest.x, a_testRequest.op, a_testRequest.y, a_testResponse.result);
246 Logger::information (msg, ANNA_FILE_LOCATION);
250 clientSocket.send (*response);
252 catch (Exception& ex) {