Updated license
[anna.git] / example / comm / datagramKClient / 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: 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    comm::DatagramSocket* getServer () const throw () { return a_output; }
72    const test::Menu& getMenu () const throw () { return a_menu; }
73    
74 private:
75    MyCommunicator a_communicator;
76    test::Menu a_menu;
77    comm::DatagramSocket* a_output;
78    comm::DatagramSocket* a_input;
79
80    void initialize () throw (RuntimeException);
81    void run () throw (RuntimeException);    
82 };
83
84 using namespace std;
85 using namespace test;
86
87 int main (int argc, const char** argv)
88 {
89    CommandLine& commandLine (CommandLine::instantiate ());
90    KClient app;
91    
92    try {
93       commandLine.initialize (argv, argc);
94       commandLine.verify ();
95
96       Logger::setLevel (Logger::Debug); 
97       Logger::initialize ("kclient", new TraceWriter ("file.trace", 4096000));
98  
99       app.start ();
100    }
101    catch (Exception& ex) {
102       cout << ex.asString () << endl;
103    }
104    
105    return 0;
106 }
107
108 KClient::KClient () : 
109    Application ("kclient", "KClient", "1.0.0"),
110    a_menu (&a_communicator)
111 {
112    CommandLine& commandLine (CommandLine::instantiate ());
113       
114    commandLine.add ("as", CommandLine::Argument::Optional, "Dirección broadcast en el que servidor atiende peticiones.");
115    commandLine.add ("ps", CommandLine::Argument::Mandatory, "Puerto en el que el servidor atiende las peticiones.");
116    commandLine.add ("a", CommandLine::Argument::Optional, "Dirección broadcast en la que el cliente atiende respuestas.");
117    commandLine.add ("p", CommandLine::Argument::Mandatory, "Puerto en el que atiende las respuestas");
118
119    activateGeneralPublicLicense ();
120 }
121
122 void KClient::initialize () 
123    throw (RuntimeException)
124 {
125    CommandLine& cl (CommandLine::instantiate ());    
126
127    Network& network = Network::instantiate ();
128
129    /* Define el Socket para enviar las respuestas */
130    const comm::Device* device = Network::instantiate ().find (Device::asAddress (cl.getValue ("as")));
131    int port = cl.getIntegerValue ("ps");
132    INetAddress remoteAddress (device, port);
133    a_output = new DatagramSocket (DatagramSocket::WriteOnly, remoteAddress);
134    a_output->connect ();
135
136    cout << "Server Address: " << a_output->asString () << endl << endl;
137
138    /* Define el Socket por el que recibir las respuestas */
139    port = cl.getIntegerValue ("p");
140    device = Network::instantiate ().find (Device::asAddress (cl.getValue ("a")));
141    INetAddress localAddress (device, port);
142    a_input = new DatagramSocket (DatagramSocket::ReadOnly, localAddress);
143    a_communicator.attach (a_input);
144
145    cout << "My Address: " << a_input->asString () << endl << endl;
146
147    a_communicator.attach (&a_menu);
148 }
149
150 void KClient::run ()
151    throw (RuntimeException)
152 {   
153    a_menu.paint ();
154    a_communicator.accept ();
155 }
156
157 void MyCommunicator::eventReceiveMessage (ClientSocket&, const Message& message)
158    throw (RuntimeException)
159 {
160    a_response.decode (message.getBody ());
161
162    const Millisecond responseTime = (Millisecond)anna::functions::microsecond() - a_response.initTime;
163    
164    cout << endl << "ResponseTime: " << responseTime << " us" << endl;
165    cout << "Resultado de la peticion: " << a_response.x << (char) a_response.op << a_response.y  << " = " << a_response.result << endl << endl;
166    
167    static_cast <KClient&> (anna::comm::functions::getApp ()).getMenu ().paint ();
168 }
169
170 //-----------------------------------------------------------------------------------------
171 // Cuando el Menu tiene disponibles todos los datos necesarios para la petición se lo
172 // notifica al comunicador mediante un evento de usuario.
173 //-----------------------------------------------------------------------------------------
174 void MyCommunicator::eventUser (const char* id, const void* context) 
175    throw ()
176 {
177    LOGMETHOD (TraceMethod tm ("MyCommunicator", "eventUser", ANNA_FILE_LOCATION));
178
179    if (anna_strcmp (id, Menu::EventData) == 0) {
180       const Menu::Data* data (reinterpret_cast <const Menu::Data*>  (context));
181       
182       a_request.op = data->a_operation;
183       a_request.x = data->a_op1;
184       a_request.y = data->a_op2;
185       a_request.initTime = anna::functions::microsecond ();
186
187       comm::DatagramSocket* server = static_cast <KClient&> (anna::comm::functions::getApp ()).getServer ();
188       
189       try {
190          server->send (a_request);
191       }
192       catch (RuntimeException& ex) {
193          ex.trace ();
194       }
195    } 
196 }