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 Network& network = Network::instantiate ();
101 /* Define el Socket para enviar las respuestas */
102 const comm::Device* device = Network::instantiate ().find (Device::asAddress (cl.getValue ("as")));
103 int port = cl.getIntegerValue ("ps");
104 INetAddress remoteAddress (device, port);
105 a_output = new DatagramSocket (DatagramSocket::WriteOnly, remoteAddress);
106 a_output->connect ();
108 cout << "Server Address: " << a_output->asString () << endl << endl;
110 /* Define el Socket por el que recibir las respuestas */
111 port = cl.getIntegerValue ("p");
112 device = Network::instantiate ().find (Device::asAddress (cl.getValue ("a")));
113 INetAddress localAddress (device, port);
114 a_input = new DatagramSocket (DatagramSocket::ReadOnly, localAddress);
115 a_communicator.attach (a_input);
117 cout << "My Address: " << a_input->asString () << endl << endl;
119 a_communicator.attach (&a_menu);
123 throw (RuntimeException)
126 a_communicator.accept ();
129 void MyCommunicator::eventReceiveMessage (ClientSocket&, const Message& message)
130 throw (RuntimeException)
132 a_response.decode (message.getBody ());
134 const Millisecond responseTime = (Millisecond)anna::functions::microsecond() - a_response.initTime;
136 cout << endl << "ResponseTime: " << responseTime << " us" << endl;
137 cout << "Resultado de la peticion: " << a_response.x << (char) a_response.op << a_response.y << " = " << a_response.result << endl << endl;
139 static_cast <KClient&> (anna::comm::functions::getApp ()).getMenu ().paint ();
142 //-----------------------------------------------------------------------------------------
143 // Cuando el Menu tiene disponibles todos los datos necesarios para la peticiĆ³n se lo
144 // notifica al comunicador mediante un evento de usuario.
145 //-----------------------------------------------------------------------------------------
146 void MyCommunicator::eventUser (const char* id, const void* context)
149 LOGMETHOD (TraceMethod tm ("MyCommunicator", "eventUser", ANNA_FILE_LOCATION));
151 if (anna_strcmp (id, Menu::EventData) == 0) {
152 const Menu::Data* data (reinterpret_cast <const Menu::Data*> (context));
154 a_request.op = data->a_operation;
155 a_request.x = data->a_op1;
156 a_request.y = data->a_op2;
157 a_request.initTime = anna::functions::microsecond ();
159 comm::DatagramSocket* server = static_cast <KClient&> (anna::comm::functions::getApp ()).getServer ();
162 server->send (a_request);
164 catch (RuntimeException& ex) {