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 El cliente de esta aplicacion es: server.p => Transporte: comm::Transport.
20 #include <anna/core/core.hpp>
21 #include <anna/comm/comm.hpp>
23 #include <anna/test/Menu.hpp>
24 #include <anna/test/Request.hpp>
25 #include <anna/test/Response.hpp>
27 class MyCommunicator : public Communicator {
29 MyCommunicator () : Communicator () {;}
32 test::Response a_response;
33 test::Request a_request;
35 void eventReceiveMessage (ClientSocket&, const Message&) throw (RuntimeException);
36 void eventUser (const char* id, const void* context) throw ();
39 class KClient : public anna::comm::Application {
43 comm::DatagramSocket* getServer () const throw () { return a_output; }
44 const test::Menu& getMenu () const throw () { return a_menu; }
47 MyCommunicator a_communicator;
49 comm::DatagramSocket* a_output;
50 comm::DatagramSocket* a_input;
52 void initialize () throw (RuntimeException);
53 void run () throw (RuntimeException);
59 int main (int argc, const char** argv)
61 CommandLine& commandLine (CommandLine::instantiate ());
65 commandLine.initialize (argv, argc);
66 commandLine.verify ();
68 Logger::setLevel (Logger::Debug);
69 Logger::initialize ("kclient", new TraceWriter ("file.trace", 4096000));
73 catch (Exception& ex) {
74 cout << ex.asString () << endl;
81 Application ("kclient", "KClient", "1.0.0"),
82 a_menu (&a_communicator)
84 CommandLine& commandLine (CommandLine::instantiate ());
86 commandLine.add ("as", CommandLine::Argument::Optional, "Direccion broadcast en el que servidor atiende peticiones.");
87 commandLine.add ("ps", CommandLine::Argument::Mandatory, "Puerto en el que el servidor atiende las peticiones.");
88 commandLine.add ("a", CommandLine::Argument::Optional, "Direccion broadcast en la que el cliente atiende respuestas.");
89 commandLine.add ("p", CommandLine::Argument::Mandatory, "Puerto en el que atiende las respuestas");
91 activateGeneralPublicLicense ();
94 void KClient::initialize ()
95 throw (RuntimeException)
97 CommandLine& cl (CommandLine::instantiate ());
99 /* Define el Socket para enviar las respuestas */
100 const comm::Device* device = Network::instantiate ().find (Device::asAddress (cl.getValue ("as")));
101 int port = cl.getIntegerValue ("ps");
102 INetAddress remoteAddress (device, port);
103 a_output = new DatagramSocket (DatagramSocket::WriteOnly, remoteAddress);
104 a_output->connect ();
106 cout << "Server Address: " << a_output->asString () << endl << endl;
108 /* Define el Socket por el que recibir las respuestas */
109 port = cl.getIntegerValue ("p");
110 device = Network::instantiate ().find (Device::asAddress (cl.getValue ("a")));
111 INetAddress localAddress (device, port);
112 a_input = new DatagramSocket (DatagramSocket::ReadOnly, localAddress);
113 a_communicator.attach (a_input);
115 cout << "My Address: " << a_input->asString () << endl << endl;
117 a_communicator.attach (&a_menu);
121 throw (RuntimeException)
124 a_communicator.accept ();
127 void MyCommunicator::eventReceiveMessage (ClientSocket&, const Message& message)
128 throw (RuntimeException)
130 a_response.decode (message.getBody ());
132 const Millisecond responseTime = (Millisecond)anna::functions::microsecond() - a_response.initTime;
134 cout << endl << "ResponseTime: " << responseTime << " us" << endl;
135 cout << "Resultado de la peticion: " << a_response.x << (char) a_response.op << a_response.y << " = " << a_response.result << endl << endl;
137 static_cast <KClient&> (anna::comm::functions::getApp ()).getMenu ().paint ();
140 //-----------------------------------------------------------------------------------------
141 // Cuando el Menu tiene disponibles todos los datos necesarios para la peticiĆ³n se lo
142 // notifica al comunicador mediante un evento de usuario.
143 //-----------------------------------------------------------------------------------------
144 void MyCommunicator::eventUser (const char* id, const void* context)
147 LOGMETHOD (TraceMethod tm ("MyCommunicator", "eventUser", ANNA_FILE_LOCATION));
149 if (anna_strcmp (id, Menu::EventData) == 0) {
150 const Menu::Data* data (reinterpret_cast <const Menu::Data*> (context));
152 a_request.op = data->a_operation;
153 a_request.x = data->a_op1;
154 a_request.y = data->a_op2;
155 a_request.initTime = anna::functions::microsecond ();
157 comm::DatagramSocket* server = static_cast <KClient&> (anna::comm::functions::getApp ()).getServer ();
160 server->send (a_request);
162 catch (RuntimeException& ex) {