First commit
[anna.git] / example / comm / irkClient / main.cpp
1 // ANNA - Anna is Not 'N' Anymore
2 //
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
4 //
5 // https://bitbucket.org/testillano/anna
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
9 // are met:
10 //
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
16 // distribution.
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.
20 //
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.
32 //
33 // Authors: eduardo.ramos.testillano@gmail.com
34 //          cisco.tierra@gmail.com
35
36
37 /*
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.
41
42    Realiza las peticiones mediante un servicio de reparto por indice.
43
44    El cliente de esta aplicacion es: server.p rserver.p => Transporte: comm::Transport.
45 */
46 #include <iostream>
47 #include <ctype.h>
48
49 #include <string.h>
50
51 #include <anna/core/core.hpp>
52 #include <anna/comm/comm.hpp>
53
54 #include <anna/test/Menu.hpp>
55 #include <anna/test/Request.hpp>
56 #include <anna/test/Response.hpp>
57
58 class MyCommunicator : public Communicator {
59 public:   
60    MyCommunicator () : Communicator () {;}
61    
62 private:
63    test::Response a_response;   
64    test::Request a_request;
65    
66    void eventReceiveMessage (ClientSocket&, const Message&) throw (RuntimeException);
67    void eventUser (const char* id, const void* context) throw ();   
68 };
69
70 typedef comm::IndexedDelivery MyService;
71
72 class IRKClient : public anna::comm::Application {
73 public:
74
75    IRKClient ();
76       
77    MyService* getService () const throw () { return a_service; }
78    const test::Menu& getMenu () const throw () { return a_menu; }
79    
80 private:
81    MyCommunicator a_communicator;
82    test::Menu a_menu;
83    MyService* a_service;
84
85    void initialize () throw (RuntimeException);
86    void run () throw (RuntimeException);    
87 };
88
89 using namespace std;
90 using namespace test;
91
92 int main (int argc, const char** argv)
93 {
94    CommandLine& commandLine (CommandLine::instantiate ());
95    IRKClient app;
96    
97    try {
98       commandLine.initialize (argv, argc);
99       commandLine.verify ();
100
101       Logger::setLevel (Logger::Debug); 
102       Logger::initialize ("kclient", new TraceWriter ("file.trace", 4096000));
103  
104       app.start ();
105    }
106    catch (Exception& ex) {
107       cout << ex.asString () << endl;
108    }
109    
110    return 0;
111 }
112
113 IRKClient::IRKClient () : 
114    Application ("kclient", "IRKClient", "1.0.0"),
115    a_menu (&a_communicator)
116 {
117    CommandLine& commandLine (CommandLine::instantiate ());
118       
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 /");
125 }
126
127 void IRKClient::initialize () 
128    throw (RuntimeException)
129 {
130    CommandLine& cl (CommandLine::instantiate ());    
131
132    Network& network = Network::instantiate ();
133
134    int port;
135    
136    const char* ip = cl.getValue ("a");
137    const char mode = toupper (*cl.getValue ("mode"));
138
139    a_service = new MyService ("Service_Arithmetic", true, (mode == 'S') ? MyService::Mode::Strict: MyService::Mode::Flexible);
140
141    port = cl.getIntegerValue ("pp");
142    a_service->attach (network.createServer (ip, port, true));
143
144    port = cl.getIntegerValue ("pm");
145    a_service->attach (network.createServer (ip, port, true));
146
147    port = cl.getIntegerValue ("px");
148    a_service->attach (network.createServer (ip, port, true));
149
150    port = cl.getIntegerValue ("pd");
151    a_service->attach (network.createServer (ip, port, true));
152
153    a_communicator.attach (a_service);
154    a_communicator.attach (&a_menu);
155 }
156
157 void IRKClient::run ()
158    throw (RuntimeException)
159 {   
160    a_menu.paint ();
161    a_communicator.accept ();
162 }
163
164 void MyCommunicator::eventReceiveMessage (ClientSocket&, const Message& message)
165    throw (RuntimeException)
166 {
167    a_response.decode (message.getBody ());
168
169    const Millisecond responseTime = anna::functions::millisecond () - a_response.initTime;
170    
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;
173    
174    static_cast <IRKClient&> (anna::comm::functions::getApp ()).getMenu ().paint ();
175 }
176
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) 
182    throw ()
183 {
184    LOGMETHOD (TraceMethod tm ("MyCommunicator", "eventUser", ANNA_FILE_LOCATION));
185    int index;
186
187    if (anna_strcmp (id, Menu::EventData) == 0) {
188       const Menu::Data* data (reinterpret_cast <const Menu::Data*>  (context));
189       
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 ();
194       
195       MyService* service = static_cast <IRKClient&> (anna::comm::functions::getApp ()).getService ();
196       
197       try {
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;
203          }
204          service->prepare (index);
205          service->send (a_request);
206       }
207       catch (RuntimeException& ex) {
208          ex.trace ();
209       }
210    } 
211 }