1 // ANNA - Anna is Not Nothingness Anymore
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
5 // http://redmine.teslayout.com/projects/anna-suite
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
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
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.
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.
33 // Authors: eduardo.ramos.testillano@gmail.com
34 // cisco.tierra@gmail.com
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.
42 El cliente de esta aplicacion es: http_server.p => Transporte: http::Transport.
48 #include <anna/core/core.hpp>
49 #include <anna/comm/comm.hpp>
51 #include <anna/http/Request.hpp>
52 #include <anna/http/Response.hpp>
53 #include <anna/http/Handler.hpp>
54 #include <anna/http/Transport.hpp>
55 #include <anna/http/functions.hpp>
57 #include <anna/test/Menu.hpp>
58 #include <anna/test/Request.hpp>
59 #include <anna/test/Response.hpp>
61 class MyHandler : public http::Handler {
63 MyHandler () : http::Handler ("http_kclient::MyHandler") {;}
66 http::Response a_httpResponse;
67 test::Response a_testResponse;
69 void evRequest (ClientSocket&, const http::Request&) throw (RuntimeException) {;}
70 void evResponse (ClientSocket&, const http::Response&) throw (RuntimeException);
73 class MyCommunicator : public Communicator {
75 MyCommunicator () : Communicator ()
77 a_httpRequest.setMethod (http::Method::Type::Post);
78 a_httpRequest.setURI ("HTTPKClient");
82 MyHandler a_httpHandler;
83 http::Request a_httpRequest;
84 test::Request a_testRequest;
86 void eventReceiveMessage (ClientSocket &, const Message&) throw (RuntimeException);
87 void eventUser (const char* id, const void* context) throw ();
90 class HTTPKClient : public anna::comm::Application {
94 Server* getServer () const throw () { return a_server; }
95 const test::Menu& getMenu () const throw () { return a_menu; }
98 MyCommunicator a_communicator;
102 void initialize () throw (RuntimeException);
103 void run () throw (RuntimeException);
107 using namespace test;
109 int main (int argc, const char** argv)
111 CommandLine& commandLine (CommandLine::instantiate ());
114 http::functions::initialize ();
117 commandLine.initialize (argv, argc);
118 commandLine.verify ();
120 Logger::setLevel (Logger::Debug);
121 Logger::initialize ("http_kclient", new TraceWriter ("file.trace", 4096000));
125 catch (Exception& ex) {
126 cout << ex.asString () << endl;
132 HTTPKClient::HTTPKClient () :
133 Application ("kclient", "HTTPKClient", "1.0.0"),
134 a_menu (&a_communicator)
136 CommandLine& commandLine (CommandLine::instantiate ());
138 commandLine.add ("p", CommandLine::Argument::Mandatory, "Puerto en el que el servidor atiende respuestas.");
139 commandLine.add ("a", CommandLine::Argument::Mandatory, "Direccin IP Puerto en el que el servidor atiende respuestas.");
142 void HTTPKClient::initialize ()
143 throw (RuntimeException)
145 CommandLine& cl (CommandLine::instantiate ());
147 Network& network = Network::instantiate ();
149 Host* host = network.find_host ("host000");
150 host->assign (network.find (Device::asAddress (cl.getValue ("a"))));
151 a_server = host->createServer ("http_server", cl.getIntegerValue ("p"), true, &http::Transport::getFactory ());
153 a_communicator.attach (&a_menu);
156 void HTTPKClient::run ()
157 throw (RuntimeException)
160 a_communicator.accept ();
163 void MyCommunicator::eventReceiveMessage (ClientSocket& clientSocket, const Message& message)
164 throw (RuntimeException)
166 LOGMETHOD (TraceMethod tm ("MyCommunicator", "eventReceiveMessage", ANNA_FILE_LOCATION));
168 if (clientSocket.support (http::Transport::className ()) == false)
171 a_httpHandler.apply (clientSocket, message);
174 //-----------------------------------------------------------------------------------------
175 // Cuando el Menu tiene disponibles todos los datos necesarios para la peticin se lo
176 // notifica al comunicador mediante un evento de usuario.
177 //-----------------------------------------------------------------------------------------
178 void MyCommunicator::eventUser (const char* id, const void* context)
181 LOGMETHOD (TraceMethod tm ("MyCommunicator", "eventUser", ANNA_FILE_LOCATION));
183 if (anna_strcmp (id, Menu::EventData) == 0) {
184 const Menu::Data* data (reinterpret_cast <const Menu::Data*> (context));
186 a_testRequest.op = data->a_operation;
187 a_testRequest.x = data->a_op1;
188 a_testRequest.y = data->a_op2;
189 a_testRequest.initTime = anna::functions::millisecond ();
191 Server* server = static_cast <HTTPKClient&> (anna::comm::functions::getApp ()).getServer ();
194 a_httpRequest.setBody (a_testRequest.code ());
195 server->send (a_httpRequest);
197 catch (RuntimeException& ex) {
203 void MyHandler::evResponse (ClientSocket& clientSocket, const http::Response& response)
204 throw (RuntimeException)
206 if (response.getStatusCode () == 200) {
207 a_testResponse.decode (response.getBody ());
209 const Millisecond responseTime = anna::functions::millisecond () - a_testResponse.initTime;
210 cout << endl << "ResponseTime: " << responseTime << " ms" << endl;
211 cout << endl << "Resultado de la peticion: " << a_testResponse.x << (char) a_testResponse.op << a_testResponse.y << " = " << a_testResponse.result << endl << endl;
214 cout << endl << "Error en la peticion: " << response.getStatusCode () << " (" << response.getReasonPhrase () << ")" << endl << endl;
216 static_cast <HTTPKClient&> (anna::comm::functions::getApp ()).getMenu ().paint ();