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 Establece un manejador externo para controlar el teclado, recoge los parametros de la operacion
39 por este y envia la peticion al servidor que devolvera el resultado de aplicar la operacion
40 sobre los parametros recogidos.
42 El cliente de esta aplicacion es: server.p => Transporte: comm::Transport.
48 #include <anna/core/core.hpp>
49 #include <anna/comm/comm.hpp>
51 #include <anna/test/Menu.hpp>
52 #include <anna/test/Request.hpp>
53 #include <anna/test/Response.hpp>
55 class MyCommunicator : public Communicator {
57 MyCommunicator () : Communicator () {;}
60 test::Response a_response;
61 test::Request a_request;
63 void eventReceiveMessage (ClientSocket&, const Message&) throw (RuntimeException);
64 void eventUser (const char* id, const void* context) throw ();
67 class KClient : public anna::comm::Application {
71 comm::DatagramSocket* getServer () const throw () { return a_output; }
72 const test::Menu& getMenu () const throw () { return a_menu; }
75 MyCommunicator a_communicator;
77 comm::DatagramSocket* a_output;
78 comm::DatagramSocket* a_input;
80 void initialize () throw (RuntimeException);
81 void run () throw (RuntimeException);
87 int main (int argc, const char** argv)
89 CommandLine& commandLine (CommandLine::instantiate ());
93 commandLine.initialize (argv, argc);
94 commandLine.verify ();
96 Logger::setLevel (Logger::Debug);
97 Logger::initialize ("kclient", new TraceWriter ("file.trace", 4096000));
101 catch (Exception& ex) {
102 cout << ex.asString () << endl;
108 KClient::KClient () :
109 Application ("kclient", "KClient", "1.0.0"),
110 a_menu (&a_communicator)
112 CommandLine& commandLine (CommandLine::instantiate ());
114 commandLine.add ("as", CommandLine::Argument::Optional, "Dirección broadcast en el que servidor atiende peticiones.");
115 commandLine.add ("ps", CommandLine::Argument::Mandatory, "Puerto en el que el servidor atiende las peticiones.");
116 commandLine.add ("a", CommandLine::Argument::Optional, "Dirección broadcast en la que el cliente atiende respuestas.");
117 commandLine.add ("p", CommandLine::Argument::Mandatory, "Puerto en el que atiende las respuestas");
119 activateGeneralPublicLicense ();
122 void KClient::initialize ()
123 throw (RuntimeException)
125 CommandLine& cl (CommandLine::instantiate ());
127 Network& network = Network::instantiate ();
129 /* Define el Socket para enviar las respuestas */
130 const comm::Device* device = Network::instantiate ().find (Device::asAddress (cl.getValue ("as")));
131 int port = cl.getIntegerValue ("ps");
132 INetAddress remoteAddress (device, port);
133 a_output = new DatagramSocket (DatagramSocket::WriteOnly, remoteAddress);
134 a_output->connect ();
136 cout << "Server Address: " << a_output->asString () << endl << endl;
138 /* Define el Socket por el que recibir las respuestas */
139 port = cl.getIntegerValue ("p");
140 device = Network::instantiate ().find (Device::asAddress (cl.getValue ("a")));
141 INetAddress localAddress (device, port);
142 a_input = new DatagramSocket (DatagramSocket::ReadOnly, localAddress);
143 a_communicator.attach (a_input);
145 cout << "My Address: " << a_input->asString () << endl << endl;
147 a_communicator.attach (&a_menu);
151 throw (RuntimeException)
154 a_communicator.accept ();
157 void MyCommunicator::eventReceiveMessage (ClientSocket&, const Message& message)
158 throw (RuntimeException)
160 a_response.decode (message.getBody ());
162 const Millisecond responseTime = (Millisecond)anna::functions::microsecond() - a_response.initTime;
164 cout << endl << "ResponseTime: " << responseTime << " us" << endl;
165 cout << "Resultado de la peticion: " << a_response.x << (char) a_response.op << a_response.y << " = " << a_response.result << endl << endl;
167 static_cast <KClient&> (anna::comm::functions::getApp ()).getMenu ().paint ();
170 //-----------------------------------------------------------------------------------------
171 // Cuando el Menu tiene disponibles todos los datos necesarios para la petición se lo
172 // notifica al comunicador mediante un evento de usuario.
173 //-----------------------------------------------------------------------------------------
174 void MyCommunicator::eventUser (const char* id, const void* context)
177 LOGMETHOD (TraceMethod tm ("MyCommunicator", "eventUser", ANNA_FILE_LOCATION));
179 if (anna_strcmp (id, Menu::EventData) == 0) {
180 const Menu::Data* data (reinterpret_cast <const Menu::Data*> (context));
182 a_request.op = data->a_operation;
183 a_request.x = data->a_op1;
184 a_request.y = data->a_op2;
185 a_request.initTime = anna::functions::microsecond ();
187 comm::DatagramSocket* server = static_cast <KClient&> (anna::comm::functions::getApp ()).getServer ();
190 server->send (a_request);
192 catch (RuntimeException& ex) {