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 Establece un manejador externo para controlar el teclado, recoge los parametros de la operacion
11 por este y envia la peticion al servidor que devolvera el resultado de aplicar la operacion
12 sobre los parametros recogidos.
14 Realiza las peticiones mediante un servicio de reparto por indice.
16 El cliente de esta aplicacion es: server.p rserver.p => Transporte: comm::Transport.
23 #include <anna/core/core.hpp>
24 #include <anna/comm/comm.hpp>
26 #include <anna/test/Menu.hpp>
27 #include <anna/test/Request.hpp>
28 #include <anna/test/Response.hpp>
30 class MyCommunicator : public Communicator {
32 MyCommunicator () : Communicator () {;}
35 test::Response a_response;
36 test::Request a_request;
38 void eventReceiveMessage (ClientSocket&, const Message&) throw (RuntimeException);
39 void eventUser (const char* id, const void* context) throw ();
42 typedef comm::IndexedDelivery MyService;
44 class IRKClient : public anna::comm::Application {
49 MyService* getService () const throw () { return a_service; }
50 const test::Menu& getMenu () const throw () { return a_menu; }
53 MyCommunicator a_communicator;
57 void initialize () throw (RuntimeException);
58 void run () throw (RuntimeException);
64 int main (int argc, const char** argv)
66 CommandLine& commandLine (CommandLine::instantiate ());
70 commandLine.initialize (argv, argc);
71 commandLine.verify ();
73 Logger::setLevel (Logger::Debug);
74 Logger::initialize ("kclient", new TraceWriter ("file.trace", 4096000));
78 catch (Exception& ex) {
79 cout << ex.asString () << endl;
85 IRKClient::IRKClient () :
86 Application ("kclient", "IRKClient", "1.0.0"),
87 a_menu (&a_communicator)
89 CommandLine& commandLine (CommandLine::instantiate ());
91 commandLine.add ("a", CommandLine::Argument::Mandatory, "Direccion en el que el servidor atiende respuestas.");
92 commandLine.add ("mode", CommandLine::Argument::Mandatory, "Modo de reparto (S)trict o (F)lexible");
93 commandLine.add ("pp", CommandLine::Argument::Mandatory, "Puerto del servidor de +");
94 commandLine.add ("pm", CommandLine::Argument::Mandatory, "Puerto del servidor de -");
95 commandLine.add ("px", CommandLine::Argument::Mandatory, "Puerto del servidor de *");
96 commandLine.add ("pd", CommandLine::Argument::Mandatory, "Puerto del servidor de /");
99 void IRKClient::initialize ()
100 throw (RuntimeException)
102 CommandLine& cl (CommandLine::instantiate ());
104 Network& network = Network::instantiate ();
108 const char* ip = cl.getValue ("a");
109 const char mode = toupper (*cl.getValue ("mode"));
111 a_service = new MyService ("Service_Arithmetic", true, (mode == 'S') ? MyService::Mode::Strict: MyService::Mode::Flexible);
113 port = cl.getIntegerValue ("pp");
114 a_service->attach (network.createServer (ip, port, true));
116 port = cl.getIntegerValue ("pm");
117 a_service->attach (network.createServer (ip, port, true));
119 port = cl.getIntegerValue ("px");
120 a_service->attach (network.createServer (ip, port, true));
122 port = cl.getIntegerValue ("pd");
123 a_service->attach (network.createServer (ip, port, true));
125 a_communicator.attach (a_service);
126 a_communicator.attach (&a_menu);
129 void IRKClient::run ()
130 throw (RuntimeException)
133 a_communicator.accept ();
136 void MyCommunicator::eventReceiveMessage (ClientSocket&, const Message& message)
137 throw (RuntimeException)
139 a_response.decode (message.getBody ());
141 const Millisecond responseTime = anna::functions::millisecond () - a_response.initTime;
143 cout << endl << "ResponseTime: " << responseTime << " ms" << endl;
144 cout << "Resultado de la peticion: " << a_response.x << (char) a_response.op << a_response.y << " = " << a_response.result << endl << endl;
146 static_cast <IRKClient&> (anna::comm::functions::getApp ()).getMenu ().paint ();
149 //-----------------------------------------------------------------------------------------
150 // Cuando el Menu tiene disponibles todos los datos necesarios para la peticiĆ³n se lo
151 // notifica al comunicador mediante un evento de usuario.
152 //-----------------------------------------------------------------------------------------
153 void MyCommunicator::eventUser (const char* id, const void* context)
156 LOGMETHOD (TraceMethod tm ("MyCommunicator", "eventUser", ANNA_FILE_LOCATION));
159 if (anna_strcmp (id, Menu::EventData) == 0) {
160 const Menu::Data* data (reinterpret_cast <const Menu::Data*> (context));
162 a_request.op = data->a_operation;
163 a_request.x = data->a_op1;
164 a_request.y = data->a_op2;
165 a_request.initTime = anna::functions::millisecond ();
167 MyService* service = static_cast <IRKClient&> (anna::comm::functions::getApp ()).getService ();
170 switch (data->a_operation) {
171 case '+': index = 0; break;
172 case '-': index = 1; break;
173 case '*': index = 2; break;
174 case '/': index = 3; break;
176 service->prepare (index);
177 service->send (a_request);
179 catch (RuntimeException& ex) {