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 Realiza las peticiones mediante un servicio de reparto por indice.
44 El cliente de esta aplicacion es: server.p rserver.p => Transporte: comm::Transport.
51 #include <anna/core/core.hpp>
52 #include <anna/comm/comm.hpp>
54 #include <anna/test/Menu.hpp>
55 #include <anna/test/Request.hpp>
56 #include <anna/test/Response.hpp>
58 class MyCommunicator : public Communicator {
60 MyCommunicator () : Communicator () {;}
63 test::Response a_response;
64 test::Request a_request;
66 void eventReceiveMessage (ClientSocket&, const Message&) throw (RuntimeException);
67 void eventUser (const char* id, const void* context) throw ();
70 typedef comm::IndexedDelivery MyService;
72 class IRKClient : public anna::comm::Application {
77 MyService* getService () const throw () { return a_service; }
78 const test::Menu& getMenu () const throw () { return a_menu; }
81 MyCommunicator a_communicator;
85 void initialize () throw (RuntimeException);
86 void run () throw (RuntimeException);
92 int main (int argc, const char** argv)
94 CommandLine& commandLine (CommandLine::instantiate ());
98 commandLine.initialize (argv, argc);
99 commandLine.verify ();
101 Logger::setLevel (Logger::Debug);
102 Logger::initialize ("kclient", new TraceWriter ("file.trace", 4096000));
106 catch (Exception& ex) {
107 cout << ex.asString () << endl;
113 IRKClient::IRKClient () :
114 Application ("kclient", "IRKClient", "1.0.0"),
115 a_menu (&a_communicator)
117 CommandLine& commandLine (CommandLine::instantiate ());
119 commandLine.add ("a", CommandLine::Argument::Mandatory, "Direccion en el que el servidor atiende respuestas.");
120 commandLine.add ("mode", CommandLine::Argument::Mandatory, "Modo de reparto (S)trict o (F)lexible");
121 commandLine.add ("pp", CommandLine::Argument::Mandatory, "Puerto del servidor de +");
122 commandLine.add ("pm", CommandLine::Argument::Mandatory, "Puerto del servidor de -");
123 commandLine.add ("px", CommandLine::Argument::Mandatory, "Puerto del servidor de *");
124 commandLine.add ("pd", CommandLine::Argument::Mandatory, "Puerto del servidor de /");
127 void IRKClient::initialize ()
128 throw (RuntimeException)
130 CommandLine& cl (CommandLine::instantiate ());
132 Network& network = Network::instantiate ();
136 const char* ip = cl.getValue ("a");
137 const char mode = toupper (*cl.getValue ("mode"));
139 a_service = new MyService ("Service_Arithmetic", true, (mode == 'S') ? MyService::Mode::Strict: MyService::Mode::Flexible);
141 port = cl.getIntegerValue ("pp");
142 a_service->attach (network.createServer (ip, port, true));
144 port = cl.getIntegerValue ("pm");
145 a_service->attach (network.createServer (ip, port, true));
147 port = cl.getIntegerValue ("px");
148 a_service->attach (network.createServer (ip, port, true));
150 port = cl.getIntegerValue ("pd");
151 a_service->attach (network.createServer (ip, port, true));
153 a_communicator.attach (a_service);
154 a_communicator.attach (&a_menu);
157 void IRKClient::run ()
158 throw (RuntimeException)
161 a_communicator.accept ();
164 void MyCommunicator::eventReceiveMessage (ClientSocket&, const Message& message)
165 throw (RuntimeException)
167 a_response.decode (message.getBody ());
169 const Millisecond responseTime = anna::functions::millisecond () - a_response.initTime;
171 cout << endl << "ResponseTime: " << responseTime << " ms" << endl;
172 cout << "Resultado de la peticion: " << a_response.x << (char) a_response.op << a_response.y << " = " << a_response.result << endl << endl;
174 static_cast <IRKClient&> (anna::comm::functions::getApp ()).getMenu ().paint ();
177 //-----------------------------------------------------------------------------------------
178 // Cuando el Menu tiene disponibles todos los datos necesarios para la peticiĆ³n se lo
179 // notifica al comunicador mediante un evento de usuario.
180 //-----------------------------------------------------------------------------------------
181 void MyCommunicator::eventUser (const char* id, const void* context)
184 LOGMETHOD (TraceMethod tm ("MyCommunicator", "eventUser", ANNA_FILE_LOCATION));
187 if (anna_strcmp (id, Menu::EventData) == 0) {
188 const Menu::Data* data (reinterpret_cast <const Menu::Data*> (context));
190 a_request.op = data->a_operation;
191 a_request.x = data->a_op1;
192 a_request.y = data->a_op2;
193 a_request.initTime = anna::functions::millisecond ();
195 MyService* service = static_cast <IRKClient&> (anna::comm::functions::getApp ()).getService ();
198 switch (data->a_operation) {
199 case '+': index = 0; break;
200 case '-': index = 1; break;
201 case '*': index = 2; break;
202 case '/': index = 3; break;
204 service->prepare (index);
205 service->send (a_request);
207 catch (RuntimeException& ex) {