Remove warnings
[anna.git] / example / comm / datagramKClient / main.cpp
1 // ANNA - Anna is Not Nothingness Anymore                                                         //
2 //                                                                                                //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo                         //
4 //                                                                                                //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite                           //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
7
8
9 /*
10    Establece un manejador externo para controlar el teclado, recoge los parametros de la operacion
11    por este y envia la peticion al servidor que devolvera el resultado de aplicar la operacion 
12    sobre los parametros recogidos.
13
14    El cliente de esta aplicacion es: server.p => Transporte: comm::Transport.
15 */
16 #include <iostream>
17
18 #include <string.h>
19
20 #include <anna/core/core.hpp>
21 #include <anna/comm/comm.hpp>
22
23 #include <anna/test/Menu.hpp>
24 #include <anna/test/Request.hpp>
25 #include <anna/test/Response.hpp>
26
27 class MyCommunicator : public Communicator {
28 public:   
29    MyCommunicator () : Communicator () {;}
30    
31 private:
32    test::Response a_response;   
33    test::Request a_request;
34    
35    void eventReceiveMessage (ClientSocket&, const Message&) throw (RuntimeException);
36    void eventUser (const char* id, const void* context) throw ();   
37 };
38
39 class KClient : public anna::comm::Application {
40 public:
41    KClient ();
42       
43    comm::DatagramSocket* getServer () const throw () { return a_output; }
44    const test::Menu& getMenu () const throw () { return a_menu; }
45    
46 private:
47    MyCommunicator a_communicator;
48    test::Menu a_menu;
49    comm::DatagramSocket* a_output;
50    comm::DatagramSocket* a_input;
51
52    void initialize () throw (RuntimeException);
53    void run () throw (RuntimeException);    
54 };
55
56 using namespace std;
57 using namespace test;
58
59 int main (int argc, const char** argv)
60 {
61    CommandLine& commandLine (CommandLine::instantiate ());
62    KClient app;
63    
64    try {
65       commandLine.initialize (argv, argc);
66       commandLine.verify ();
67
68       Logger::setLevel (Logger::Debug); 
69       Logger::initialize ("kclient", new TraceWriter ("file.trace", 4096000));
70  
71       app.start ();
72    }
73    catch (Exception& ex) {
74       cout << ex.asString () << endl;
75    }
76    
77    return 0;
78 }
79
80 KClient::KClient () : 
81    Application ("kclient", "KClient", "1.0.0"),
82    a_menu (&a_communicator)
83 {
84    CommandLine& commandLine (CommandLine::instantiate ());
85       
86    commandLine.add ("as", CommandLine::Argument::Optional, "Direccion broadcast en el que servidor atiende peticiones.");
87    commandLine.add ("ps", CommandLine::Argument::Mandatory, "Puerto en el que el servidor atiende las peticiones.");
88    commandLine.add ("a", CommandLine::Argument::Optional, "Direccion broadcast en la que el cliente atiende respuestas.");
89    commandLine.add ("p", CommandLine::Argument::Mandatory, "Puerto en el que atiende las respuestas");
90
91    activateGeneralPublicLicense ();
92 }
93
94 void KClient::initialize () 
95    throw (RuntimeException)
96 {
97    CommandLine& cl (CommandLine::instantiate ());    
98
99    /* Define el Socket para enviar las respuestas */
100    const comm::Device* device = Network::instantiate ().find (Device::asAddress (cl.getValue ("as")));
101    int port = cl.getIntegerValue ("ps");
102    INetAddress remoteAddress (device, port);
103    a_output = new DatagramSocket (DatagramSocket::WriteOnly, remoteAddress);
104    a_output->connect ();
105
106    cout << "Server Address: " << a_output->asString () << endl << endl;
107
108    /* Define el Socket por el que recibir las respuestas */
109    port = cl.getIntegerValue ("p");
110    device = Network::instantiate ().find (Device::asAddress (cl.getValue ("a")));
111    INetAddress localAddress (device, port);
112    a_input = new DatagramSocket (DatagramSocket::ReadOnly, localAddress);
113    a_communicator.attach (a_input);
114
115    cout << "My Address: " << a_input->asString () << endl << endl;
116
117    a_communicator.attach (&a_menu);
118 }
119
120 void KClient::run ()
121    throw (RuntimeException)
122 {   
123    a_menu.paint ();
124    a_communicator.accept ();
125 }
126
127 void MyCommunicator::eventReceiveMessage (ClientSocket&, const Message& message)
128    throw (RuntimeException)
129 {
130    a_response.decode (message.getBody ());
131
132    const Millisecond responseTime = (Millisecond)anna::functions::microsecond() - a_response.initTime;
133    
134    cout << endl << "ResponseTime: " << responseTime << " us" << endl;
135    cout << "Resultado de la peticion: " << a_response.x << (char) a_response.op << a_response.y  << " = " << a_response.result << endl << endl;
136    
137    static_cast <KClient&> (anna::comm::functions::getApp ()).getMenu ().paint ();
138 }
139
140 //-----------------------------------------------------------------------------------------
141 // Cuando el Menu tiene disponibles todos los datos necesarios para la peticiĆ³n se lo
142 // notifica al comunicador mediante un evento de usuario.
143 //-----------------------------------------------------------------------------------------
144 void MyCommunicator::eventUser (const char* id, const void* context) 
145    throw ()
146 {
147    LOGMETHOD (TraceMethod tm ("MyCommunicator", "eventUser", ANNA_FILE_LOCATION));
148
149    if (anna_strcmp (id, Menu::EventData) == 0) {
150       const Menu::Data* data (reinterpret_cast <const Menu::Data*>  (context));
151       
152       a_request.op = data->a_operation;
153       a_request.x = data->a_op1;
154       a_request.y = data->a_op2;
155       a_request.initTime = anna::functions::microsecond ();
156
157       comm::DatagramSocket* server = static_cast <KClient&> (anna::comm::functions::getApp ()).getServer ();
158       
159       try {
160          server->send (a_request);
161       }
162       catch (RuntimeException& ex) {
163          ex.trace ();
164       }
165    } 
166 }