1 // ANNA - Anna is Not 'N' Anymore
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
5 // https://bitbucket.org/testillano/anna
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 Google Inc. 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 Server* getServer () const throw () { return a_server; }
72 const test::Menu& getMenu () const throw () { return a_menu; }
75 MyCommunicator a_communicator;
79 void initialize () throw (RuntimeException);
80 void run () throw (RuntimeException);
86 int main (int argc, const char** argv)
88 CommandLine& commandLine (CommandLine::instantiate ());
92 commandLine.initialize (argv, argc);
93 commandLine.verify ();
95 Logger::setLevel (Logger::Debug);
96 Logger::initialize ("kclient", new TraceWriter ("file.trace", 4096000));
100 catch (Exception& ex) {
101 cout << ex.asString () << endl;
107 KClient::KClient () :
108 Application ("kclient", "KClient", "1.0.0"),
109 a_menu (&a_communicator)
111 CommandLine& commandLine (CommandLine::instantiate ());
113 commandLine.add ("p", CommandLine::Argument::Mandatory, "Puerto en el que el servidor atiende respuestas.");
114 commandLine.add ("a", CommandLine::Argument::Optional, "Dirección IP Puerto en el que el servidor atiende respuestas.");
115 commandLine.add ("host", CommandLine::Argument::Optional, "Nombre del host en el que atiende el servidor");
116 commandLine.add ("auto", CommandLine::Argument::Optional, "Autoreconexion", false);
117 commandLine.add ("trace", CommandLine::Argument::Optional, "Nivel de trazas (debug,warning, error,...)");
119 activateGeneralPublicLicense ();
122 void KClient::initialize ()
123 throw (RuntimeException)
125 LOGMETHOD (TraceMethod tm ("MyCommunicator", "initialize", ANNA_FILE_LOCATION));
127 CommandLine& cl (CommandLine::instantiate ());
129 Network& network = Network::instantiate ();
131 if (cl.exists ("trace"))
132 Logger::setLevel (Logger::asLevel (cl.getValue ("trace")));
134 const int p = cl.getIntegerValue ("p");
137 a_server = network.createServer (cl.getValue ("a"), p, cl.exists ("auto"));
138 else if (cl.exists ("host")) {
139 a_server = network.resolveServer (cl.getValue ("host"), p, cl.exists ("auto"));
142 throw RuntimeException ("Hay que indicar el argumento 'a' o 'host'", ANNA_FILE_LOCATION);
144 a_communicator.attach (&a_menu);
148 throw (RuntimeException)
150 LOGMETHOD (TraceMethod tm ("MyCommunicator", "run", ANNA_FILE_LOCATION));
153 a_communicator.accept ();
156 void MyCommunicator::eventReceiveMessage (ClientSocket&, const Message& message)
157 throw (RuntimeException)
159 LOGMETHOD (TraceMethod tm ("MyCommunicator", "eventReceiveMessage", ANNA_FILE_LOCATION));
161 a_response.decode (message.getBody ());
163 const Millisecond responseTime = (Millisecond)(anna::functions::microsecond()) - a_response.initTime;
165 cout << endl << "ResponseTime: " << responseTime << " us" << endl;
166 cout << "Resultado de la peticion: " << a_response.x << (char) a_response.op << a_response.y << " = " << a_response.result << endl << endl;
168 static_cast <KClient&> (anna::comm::functions::getApp ()).getMenu ().paint ();
171 //-----------------------------------------------------------------------------------------
172 // Cuando el Menu tiene disponibles todos los datos necesarios para la petición se lo
173 // notifica al comunicador mediante un evento de usuario.
174 //-----------------------------------------------------------------------------------------
175 void MyCommunicator::eventUser (const char* id, const void* context)
178 LOGMETHOD (TraceMethod tm ("MyCommunicator", "eventUser", ANNA_FILE_LOCATION));
180 if (anna_strcmp (id, Menu::EventData) == 0) {
181 const Menu::Data* data (reinterpret_cast <const Menu::Data*> (context));
183 a_request.op = data->a_operation;
184 a_request.x = data->a_op1;
185 a_request.y = data->a_op2;
186 a_request.initTime = anna::functions::microsecond ();
188 Server* server = static_cast <KClient&> (anna::comm::functions::getApp ()).getServer ();
191 server->send (a_request);
193 catch (RuntimeException& ex) {