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 Este proceso, a diferencia de kclient, abre una nueva conexion por cada peticion que tiene que lanzar.
44 El cliente de esta aplicacion es: server.p => Transporte: comm::Transport.
50 #include <anna/core/core.hpp>
52 #include <anna/comm/Application.hpp>
53 #include <anna/comm/Communicator.hpp>
54 #include <anna/comm/INetAddress.hpp>
55 #include <anna/comm/ClientSocket.hpp>
56 #include <anna/comm/Network.hpp>
57 #include <anna/comm/Device.hpp>
59 #include <anna/test/Menu.hpp>
60 #include <anna/test/Request.hpp>
61 #include <anna/test/Response.hpp>
63 class MyCommunicator : public comm::Communicator {
65 class ClientSocketAllocator {
67 static comm::INetAddress* st_inetAddress;
68 static comm::ClientSocket* create () throw () { return new comm::ClientSocket (*st_inetAddress); }
69 static void destroy (comm::ClientSocket* clientSocket) throw () { delete clientSocket; }
73 test::Response a_response;
74 test::Request a_request;
75 comm::INetAddress a_inetAddress;
76 Recycler <comm::ClientSocket, ClientSocketAllocator> a_clientSockets;
78 void do_initialize () throw (RuntimeException);
80 void eventReceiveMessage (comm::ClientSocket&, const comm::Message&) throw (RuntimeException);
81 void eventBreakConnection (const comm::ClientSocket&) throw ();
82 void eventUser (const char* id, const void* context) throw ();
85 class KXClient : public anna::comm::Application {
89 const test::Menu& getMenu () const throw () { return a_menu; }
92 MyCommunicator a_communicator;
95 void initialize () throw (RuntimeException);
96 void run () throw (RuntimeException);
100 using namespace test;
102 comm::INetAddress* MyCommunicator::ClientSocketAllocator::st_inetAddress = NULL;
104 int main (int argc, const char** argv)
106 CommandLine& commandLine (CommandLine::instantiate ());
110 commandLine.initialize (argv, argc);
111 commandLine.verify ();
113 Logger::setLevel (Logger::Debug);
114 Logger::initialize ("kclient", new TraceWriter ("file.trace", 4096000));
118 catch (Exception& ex) {
119 cout << ex.asString () << endl;
125 KXClient::KXClient () :
126 Application ("kclient", "KXClient", "1.0.0"),
127 a_menu (&a_communicator)
129 CommandLine& commandLine (CommandLine::instantiate ());
131 commandLine.add ("p", CommandLine::Argument::Mandatory, "Puerto en el que el servidor atiende respuestas.");
132 commandLine.add ("a", CommandLine::Argument::Mandatory, "Direccion IP Puerto en el que el servidor atiende respuestas.");
135 void KXClient::initialize ()
136 throw (RuntimeException)
138 a_communicator.attach (&a_menu);
141 void KXClient::run ()
142 throw (RuntimeException)
145 a_communicator.accept ();
148 //--------------------------------------------------------------------------------------------
149 // Crea la direccion a la que se conectaran los ClientSocket para enviar las peticiones.
150 //--------------------------------------------------------------------------------------------
151 void MyCommunicator::do_initialize ()
152 throw (RuntimeException)
154 CommandLine& cl (CommandLine::instantiate ());
156 using namespace anna::comm;
158 Network& network = Network::instantiate ();
160 Device* device = network.find (Device::asAddress (cl.getValue ("a")));
162 a_inetAddress.setAddress (device);
163 a_inetAddress.setPort (cl.getIntegerValue ("p"));
165 ClientSocketAllocator::st_inetAddress = &a_inetAddress;
168 void MyCommunicator::eventReceiveMessage (comm::ClientSocket&, const comm::Message& message)
169 throw (RuntimeException)
171 a_response.decode (message.getBody ());
173 cout << endl << "Resultado de la peticion: " << a_response.x << (char) a_response.op << a_response.y << " = " << a_response.result << endl << endl;
175 static_cast <KXClient&> (anna::comm::functions::getApp ()).getMenu ().paint ();
178 //-----------------------------------------------------------------------------------------
179 // Cuando el servidor remoto cierra el socket => debemos liberar este extremo para poder
180 // reutilizar la instancia (no la conexion).
181 //-----------------------------------------------------------------------------------------
182 void MyCommunicator::eventBreakConnection (const comm::ClientSocket& clientSocket)
185 a_clientSockets.release (&clientSocket);
188 //-----------------------------------------------------------------------------------------
189 // Cuando el Menu tiene disponibles todos los datos necesarios para la peticiĆ³n se lo
190 // notifica al comunicador mediante un evento de usuario.
191 //-----------------------------------------------------------------------------------------
192 void MyCommunicator::eventUser (const char* id, const void* context)
195 LOGMETHOD (TraceMethod tm ("MyCommunicator", "eventUser", ANNA_FILE_LOCATION));
197 if (anna_strcmp (id, Menu::EventData) == 0) {
198 const Menu::Data* data (reinterpret_cast <const Menu::Data*> (context));
200 a_request.op = data->a_operation;
201 a_request.x = data->a_op1;
202 a_request.y = data->a_op2;
204 comm::ClientSocket* clientSocket = a_clientSockets.create ();
207 attach (clientSocket);
208 clientSocket->send (a_request);
210 catch (RuntimeException& ex) {
211 a_clientSockets.release (clientSocket);