Suuports clang compiler
[anna.git] / example / comm / kClient / main.cpp
1 // ANNA - Anna is Not Nothingness Anymore
2 //
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
4 //
5 // http://redmine.teslayout.com/projects/anna-suite
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 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.
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: server.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 KClient : public anna::comm::Application {
68 public:
69    KClient ();
70       
71    Server* getServer () const throw () { return a_server; }
72    const test::Menu& getMenu () const throw () { return a_menu; }
73    
74 private:
75    MyCommunicator a_communicator;
76    test::Menu a_menu;
77    Server* a_server;
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    KClient 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 KClient::KClient () : 
108    Application ("kclient", "KClient", "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::Optional, "Direccion 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,...)");
118
119    activateGeneralPublicLicense ();
120 }
121
122 void KClient::initialize () 
123    throw (RuntimeException)
124 {
125    LOGMETHOD (TraceMethod tm ("MyCommunicator", "initialize", ANNA_FILE_LOCATION));
126
127    CommandLine& cl (CommandLine::instantiate ());    
128
129    Network& network = Network::instantiate ();
130
131   if (cl.exists ("trace"))
132       Logger::setLevel (Logger::asLevel (cl.getValue ("trace")));
133
134    const int p = cl.getIntegerValue ("p");
135
136    if (cl.exists ("a"))
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"));
140    }
141    else 
142       throw RuntimeException ("Hay que indicar el argumento 'a' o 'host'", ANNA_FILE_LOCATION);
143
144    a_communicator.attach (&a_menu);
145 }
146
147 void KClient::run ()
148    throw (RuntimeException)
149 {   
150    LOGMETHOD (TraceMethod tm ("MyCommunicator", "run", ANNA_FILE_LOCATION));
151
152    a_menu.paint ();
153    a_communicator.accept ();
154 }
155
156 void MyCommunicator::eventReceiveMessage (ClientSocket&, const Message& message)
157    throw (RuntimeException)
158 {
159    LOGMETHOD (TraceMethod tm ("MyCommunicator", "eventReceiveMessage", ANNA_FILE_LOCATION));
160
161    a_response.decode (message.getBody ());
162
163    const Millisecond responseTime = (Millisecond)(anna::functions::microsecond()) - a_response.initTime;
164    
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;
167    
168    static_cast <KClient&> (anna::comm::functions::getApp ()).getMenu ().paint ();
169 }
170
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) 
176    throw ()
177 {
178    LOGMETHOD (TraceMethod tm ("MyCommunicator", "eventUser", ANNA_FILE_LOCATION));
179
180    if (anna_strcmp (id, Menu::EventData) == 0) {
181       const Menu::Data* data (reinterpret_cast <const Menu::Data*>  (context));
182       
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 ();
187
188       Server* server = static_cast <KClient&> (anna::comm::functions::getApp ()).getServer ();
189       
190       try {
191          server->send (a_request);
192       }
193       catch (RuntimeException& ex) {
194          ex.trace ();
195       }
196    } 
197 }