Updated license
[anna.git] / example / comm / rrkClient / main.cpp
1 // ANNA - Anna is Not Nothingness 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    El cliente de esta aplicacion es: service.p => Transporte: comm::Transport.
43 */
44 #include <iostream>
45
46 #include <string.h>
47
48 #include <anna/core/core.hpp>
49 #include <anna/comm/comm.hpp>
50
51 #include <anna/test/Menu.hpp>
52 #include <anna/test/Request.hpp>
53 #include <anna/test/Response.hpp>
54
55 class MyCommunicator : public Communicator {
56 public:   
57    MyCommunicator () : Communicator () {;}
58    
59 private:
60    test::Response a_response;   
61    test::Request a_request;
62    
63    void eventReceiveMessage (ClientSocket&, const Message&) throw (RuntimeException);
64    void eventUser (const char* id, const void* context) throw ();   
65 };
66
67 class RRKClient : public anna::comm::Application {
68 public:
69    RRKClient ();
70       
71    Service* getService () const throw () { return a_service; }
72    const test::Menu& getMenu () const throw () { return a_menu; }
73    
74 private:
75    MyCommunicator a_communicator;
76    test::Menu a_menu;
77    Service* a_service;
78
79    void initialize () throw (RuntimeException);
80    void run () throw (RuntimeException);    
81 };
82
83 using namespace std;
84 using namespace test;
85
86 int main (int argc, const char** argv)
87 {
88    CommandLine& commandLine (CommandLine::instantiate ());
89    RRKClient app;
90    
91    try {
92       commandLine.initialize (argv, argc);
93       commandLine.verify ();
94
95       Logger::setLevel (Logger::Debug); 
96       Logger::initialize ("kclient", new TraceWriter ("file.trace", 4096000));
97  
98       app.start ();
99    }
100    catch (Exception& ex) {
101       cout << ex.asString () << endl;
102    }
103    
104    return 0;
105 }
106
107 RRKClient::RRKClient () : 
108    Application ("kclient", "RRKClient", "1.0.0"),
109    a_menu (&a_communicator)
110 {
111    CommandLine& commandLine (CommandLine::instantiate ());
112       
113    commandLine.add ("p", CommandLine::Argument::Mandatory, "Puerto en el que el servidor atiende respuestas.");
114    commandLine.add ("a", CommandLine::Argument::Mandatory, "Puertos en los que hay servidores atendiendo peticiones");
115    commandLine.add ("trace", CommandLine::Argument::Optional, "Nivel de trazas (debug,warning, error,...)");
116 }
117
118 void RRKClient::initialize () 
119    throw (RuntimeException)
120 {
121    CommandLine& cl (CommandLine::instantiate ());    
122
123    if (cl.exists ("trace"))
124       Logger::setLevel (Logger::asLevel (cl.getValue ("trace")));
125
126    Network& network = Network::instantiate ();
127
128    Tokenizer ports (cl.getValue ("p"), ",");
129    int port;
130
131    a_service = new comm::RoundRobinDelivery ("Service_Arithmetic", true);
132
133    for (Tokenizer::const_iterator ii = ports.begin (), maxii = ports.end (); ii != maxii; ii ++) {
134       port = atoi (Tokenizer::data (ii));
135       a_service->attach (network.createServer (cl.getValue ("a"), port, true));
136    }
137
138    a_communicator.attach (a_service);
139    a_communicator.attach (&a_menu);
140 }
141
142 void RRKClient::run ()
143    throw (RuntimeException)
144 {   
145    a_menu.paint ();
146    a_communicator.accept ();
147 }
148
149 void MyCommunicator::eventReceiveMessage (ClientSocket&, const Message& message)
150    throw (RuntimeException)
151 {
152    LOGMETHOD (TraceMethod tm (Logger::Local7, "MyCommunicator", "eventReceiveMessage", ANNA_FILE_LOCATION));
153
154    a_response.decode (message.getBody ());
155
156    const Millisecond responseTime = anna::functions::millisecond () - a_response.initTime;
157    
158    cout << endl << "ResponseTime: " << responseTime << " ms" << endl;
159    cout << "Resultado de la peticion: " << a_response.x << (char) a_response.op << a_response.y  << " = " << a_response.result << endl << endl;
160    
161    static_cast <RRKClient&> (anna::comm::functions::getApp ()).getMenu ().paint ();
162 }
163
164 //-----------------------------------------------------------------------------------------
165 // Cuando el Menu tiene disponibles todos los datos necesarios para la peticiĆ³n se lo
166 // notifica al comunicador mediante un evento de usuario.
167 //-----------------------------------------------------------------------------------------
168 void MyCommunicator::eventUser (const char* id, const void* context) 
169    throw ()
170 {
171    LOGMETHOD (TraceMethod tm (Logger::Local7, "MyCommunicator", "eventUser", ANNA_FILE_LOCATION));
172
173    if (anna_strcmp (id, Menu::EventData) == 0) {
174       const Menu::Data* data (reinterpret_cast <const Menu::Data*>  (context));
175       
176       a_request.op = data->a_operation;
177       a_request.x = data->a_op1;
178       a_request.y = data->a_op2;
179       a_request.initTime = anna::functions::millisecond ();
180       
181       Service* service = static_cast <RRKClient&> (anna::comm::functions::getApp ()).getService ();
182       
183       try {
184          service->send (a_request);
185       }
186       catch (RuntimeException& ex) {
187          ex.trace ();
188       }
189    } 
190 }
191