1 // ANNA - Anna is Not Nothingness Anymore
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
5 // http://redmine.teslayout.com/projects/anna-suite
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
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
17 // * Neither the name of the copyright holder 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.
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.
33 // Authors: eduardo.ramos.testillano@gmail.com
34 // cisco.tierra@gmail.com
38 Ejemplo de programa servidor. Atiende peticiones aritmeticas, el protocolo de transporte
39 sera el comm::Transport y el contenido del mensaje sera el resutaldo de un comm::Codec con los
40 (x, y, op) -> El resultado sera estos tres componente mas result.
42 Ejemplo de uso del sistema de receiveres, que son capaces de tratar N peticiones de forma
43 totalmetne simultanea en caso de estar en un entorno MT.
45 Para poder probar el sistema de congestion se puede indicar un numero de milisegundos de
46 retardo aplicados a cada contestacion.
48 Los clientes pueden ser: client.p o kclient.p
52 #include <anna/core/core.hpp>
53 #include <anna/comm/comm.hpp>
55 #include <anna/xml/Node.hpp>
56 #include <anna/xml/Attribute.hpp>
58 #include <anna/app/functions.hpp>
60 #include <anna/test/Request.hpp>
61 #include <anna/test/Response.hpp>
62 #include <anna/test/Communicator.hpp>
67 class MyCommunicator : public test::Communicator {
69 MyCommunicator (Communicator::WorkMode::_v workMode) : test::Communicator () {;}
72 class MyReceiver : public Receiver {
74 static const char* className () throw () { return "MyReceiver"; }
79 MyCommunicator* a_communicator;
81 MyReceiver () : Receiver ("MyReceiver") { ; }
82 void initialize () throw (RuntimeException);
83 void apply (comm::ClientSocket &, const Message&) throw (RuntimeException);
85 friend class Allocator <MyReceiver>;
88 class ArithmeticServer : public comm::Application {
91 ~ArithmeticServer () { delete a_communicator; }
94 MyCommunicator* a_communicator;
95 ReceiverFactoryImpl <MyReceiver> a_receiverFactory;
96 comm::ServerSocket* a_serverSocket;
98 void initialize () throw (RuntimeException);
99 void run () throw (RuntimeException);
100 xml::Node* asXML (xml::Node* app) const throw ();
101 void signalTerminate () throw (RuntimeException);
105 using namespace anna::comm;
107 int main (int argc, const char** argv)
109 CommandLine& commandLine (CommandLine::instantiate ());
110 ArithmeticServer app;
115 commandLine.initialize (argv, argc);
116 commandLine.verify ();
118 Logger::setLevel (Logger::Debug);
119 string traceFile ("server.");
120 traceFile += anna::functions::asString ((int) getpid ());
121 traceFile += ".trace";
122 Logger::initialize ("arithmeticServer", new TraceWriter (traceFile.c_str (),4096000));
126 catch (Exception& ex) {
127 cout << ex.asString () << endl;
133 ArithmeticServer::ArithmeticServer () :
134 Application ("arithmeticServer", "Servidor de operaciones (iRS)", "1.0"),
135 a_communicator (NULL)
137 CommandLine& commandLine (CommandLine::instantiate ());
139 commandLine.add ("p", CommandLine::Argument::Mandatory, "Puerto en el que atender peticiones");
140 commandLine.add ("a", CommandLine::Argument::Mandatory, "Direccin IP en la que atender");
141 commandLine.add ("d", CommandLine::Argument::Mandatory, "Retardo aplicado a la contestacio");
142 commandLine.add ("maxpending", CommandLine::Argument::Optional, "Nº máximo de bytes en la cola de entrada");
143 commandLine.add ("limit", CommandLine::Argument::Mandatory, "% de ocupacion que permitimos");
144 commandLine.add ("r", CommandLine::Argument::Optional, "Indicador de reuso de direccin", false);
145 commandLine.add ("n", CommandLine::Argument::Optional, "Numero de mensajes a servir", true);
146 commandLine.add ("trace", CommandLine::Argument::Optional, "Nivel de trazas (debug,warning, error,...)");
147 commandLine.add ("clone", CommandLine::Argument::Optional, "Aplica el metodo de clonado en el tratamiento de mensajes", false);
148 commandLine.add ("chunksize", CommandLine::Argument::Optional, "Tamaño del chunk de lectura");
151 //-----------------------------------------------------------------------------------------
152 // Inicializa el servidor de sockets.
153 //-----------------------------------------------------------------------------------------
154 void ArithmeticServer::initialize ()
155 throw (RuntimeException)
157 LOGMETHOD (TraceMethod tm ("ArithmeticServer", "initialize", ANNA_FILE_LOCATION));
159 CommandLine& cl (CommandLine::instantiate ());
161 int port = cl.getIntegerValue ("p");
162 const comm::Device* device = Network::instantiate ().find (Device::asAddress (cl.getValue ("a")));
164 if (cl.exists ("trace"))
165 Logger::setLevel (Logger::asLevel (cl.getValue ("trace")));
167 if (cl.exists ("chunksize"))
168 comm::Communicator::setReceivingChunkSize (cl.getIntegerValue ("chunksize"));
170 a_serverSocket = new ServerSocket (INetAddress (device, port), cl.exists ("r"));
171 a_serverSocket->setCategory (777);
172 a_serverSocket->setReceiverFactory (a_receiverFactory);
174 comm::Communicator::WorkMode::_v workMode = (cl.exists ("clone")) ? comm::Communicator::WorkMode::Clone: comm::Communicator::WorkMode::Single;
176 a_communicator = new MyCommunicator (workMode);
179 //-----------------------------------------------------------------------------------------
180 // Atiende las peticiones.
181 // Cuando hay un nuevo mensaje invocar�a Communicator::eventReceiveMessage
182 //-----------------------------------------------------------------------------------------
183 void ArithmeticServer::run ()
184 throw (RuntimeException)
186 LOGMETHOD (TraceMethod tm ("ArithmeticServer", "run", ANNA_FILE_LOCATION));
188 CommandLine& cl (CommandLine::instantiate ());
190 a_communicator->attach (a_serverSocket);
191 a_communicator->setDelay ((Millisecond)cl.getIntegerValue ("d"));
193 if (cl.exists ("n") == true)
194 a_communicator->setMaxMessage (cl.getIntegerValue ("n"));
196 CongestionController& ccgg = CongestionController::instantiate ();
198 ccgg.setLimit (cl.getIntegerValue ("limit"));
200 if (cl.exists ("maxpending"))
201 ccgg.setMaxPendingBytes (cl.getIntegerValue ("maxpending"));
203 a_communicator->accept ();
206 xml::Node* ArithmeticServer::asXML (xml::Node* app) const
209 xml::Node* node = app::Application::asXML (app);
211 node->createAttribute ("MaxMessage", a_communicator->getMaxMessage ());
212 node->createAttribute ("Message", a_communicator->getMessage ());
217 void ArithmeticServer::signalTerminate ()
218 throw (RuntimeException)
220 a_communicator->terminate ();
221 comm::Application::signalTerminate ();
224 void MyReceiver::initialize ()
225 throw (RuntimeException)
227 a_communicator = app::functions::component <MyCommunicator> (ANNA_FILE_LOCATION);
230 void MyReceiver::apply (ClientSocket& clientSocket, const Message& message)
231 throw (RuntimeException)
233 LOGMETHOD (TraceMethod tm ("MyReceiver", "apply", ANNA_FILE_LOCATION));
235 if (a_communicator->canContinue (clientSocket) == false)
238 a_request.decode (message.getBody ());
240 a_communicator->delay ();
242 a_response.x = a_request.x;
243 a_response.y = a_request.y;
244 a_response.initTime = a_request.initTime;
246 switch (a_response.op = a_request.op) {
248 a_response.result = a_request.x + a_request.y;
251 a_response.result = a_request.x - a_request.y;
254 a_response.result = a_request.x * a_request.y;
257 a_response.result = (a_request.y != 0) ? (a_request.x / a_request.y): 0;
262 string msg = anna::functions::asString ("%d %c %d = %d", a_request.x, a_request.op, a_request.y, a_response.result);
263 msg += anna::functions::asText (" | InitTime: ", a_response.initTime);
264 Logger::information (msg, ANNA_FILE_LOCATION);
268 clientSocket.send (a_response);
270 catch (Exception& ex) {