1 // ANNA - Anna is Not Nothingness Anymore //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo //
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 //
10 Envia una peticion de interseccion sobre HTTP/XML para ihttp_server
12 El cliente de esta aplicacion es: ihttp_server.p => Transporte: http::Transport.
18 #include <anna/core/core.hpp>
19 #include <anna/comm/comm.hpp>
21 #include <anna/app/functions.hpp>
23 #include <anna/http/Request.hpp>
24 #include <anna/http/Response.hpp>
25 #include <anna/http/Handler.hpp>
26 #include <anna/http/Transport.hpp>
27 #include <anna/http/functions.hpp>
29 #include <anna/http/wims20/ClientSide.hpp>
31 class MyHandler : public http::Handler {
33 MyHandler () : http::Handler ("ihttp_client::MyHandler") {;}
36 http::Response a_httpResponse;
38 void evRequest (ClientSocket&, const http::Request&) throw (RuntimeException) {;}
39 void evResponse (ClientSocket&, const http::Response&) throw (RuntimeException);
42 class MyCommunicator : public Communicator {
44 MyCommunicator () : Communicator (), a_httpRequest ()
46 a_httpRequest.setMethod (http::Method::Type::Get);
47 a_httpRequest.setURI ("ihttp_client");
51 MyHandler a_httpHandler;
52 http::Request a_httpRequest;
54 void eventReceiveMessage (ClientSocket &, const Message&) throw (RuntimeException);
55 void eventStartup () throw (RuntimeException);
58 class IHTTPClient : public anna::comm::Application {
62 Server* getServer () const throw () { return a_server; }
65 MyCommunicator a_communicator;
68 void initialize () throw (RuntimeException);
69 void run () throw (RuntimeException);
74 int main (int argc, const char** argv)
76 CommandLine& commandLine (CommandLine::instantiate ());
79 http::functions::initialize ();
82 commandLine.initialize (argv, argc);
83 commandLine.verify ();
85 Logger::setLevel (Logger::Debug);
86 Logger::initialize ("ihttp_client", new TraceWriter ("file.trace", 4096000));
90 catch (Exception& ex) {
91 cout << ex.asString () << endl;
97 IHTTPClient::IHTTPClient () :
98 Application ("wims20_client", "IHTTPClient", "2.0.0")
100 CommandLine& commandLine (CommandLine::instantiate ());
102 commandLine.add ("p", CommandLine::Argument::Mandatory, "Port to attend the answers.");
103 commandLine.add ("a", CommandLine::Argument::Mandatory, "Direccion IP Puerto en el que el servidor atiende respuestas.");
104 commandLine.add ("domain", CommandLine::Argument::Mandatory, "Domain indicado en la peticion WIMS 2.0");
105 commandLine.add ("op", CommandLine::Argument::Mandatory, "operacion a realizar (sum,sub,mul,div)");
106 commandLine.add ("x", CommandLine::Argument::Mandatory, "Primer operador");
107 commandLine.add ("y", CommandLine::Argument::Mandatory, "Segundo operador");
108 commandLine.add ("op", CommandLine::Argument::Mandatory, "operacion a realizar (sum,sub,mul,div)");
109 commandLine.add ("trace", CommandLine::Argument::Optional, "Nivel de trazas (debug,warning, error,...)");
110 commandLine.add ("path", CommandLine::Argument::Optional, "Path indicado en la peticion WIMS 2.0");
111 commandLine.add ("m", CommandLine::Argument::Optional, "Metodo HTTP a usar");
114 void IHTTPClient::initialize ()
115 throw (RuntimeException)
117 CommandLine& cl (CommandLine::instantiate ());
119 Network& network = Network::instantiate ();
121 if (cl.exists ("trace"))
122 Logger::setLevel (Logger::asLevel (cl.getValue ("trace")));
124 Host* host = network.find_host("host000");
125 host->assign (network.find (Device::asAddress (cl.getValue ("a"))));
126 a_server = host->createServer ("http_server", cl.getIntegerValue ("p"), true, &http::Transport::getFactory ());
129 void IHTTPClient::run ()
130 throw (RuntimeException)
132 a_communicator.accept ();
135 void MyCommunicator::eventStartup ()
136 throw (RuntimeException)
138 CommandLine& cl (CommandLine::instantiate ());
140 string domain = cl.getValue ("domain");
142 http::wims20::ClientSide* wims20Request = NULL;
144 if (cl.exists ("path")) {
145 string path = cl.getValue ("path");
146 wims20Request = new http::wims20::ClientSide (domain, path);
149 wims20Request = new http::wims20::ClientSide (domain);
151 wims20Request->setServiceID ("math");
152 wims20Request->setGUID ("user@tid.es");
153 wims20Request->addOtherLevel (cl.getValue ("op"));
154 wims20Request->setParameter ("X", cl.getIntegerValue ("x"));
155 wims20Request->setParameter ("Y", cl.getIntegerValue ("y"));
157 http::Header* header = a_httpRequest.createHeader (http::Header::Type::Connection);
158 header->setValue (" Keep-Alive ");
160 if (cl.exists ("m")) {
161 http::Method::Type::_v type = http::Method::Type::asEnumEx (cl.getValue ("m"));
162 a_httpRequest.setMethod (type);
165 wims20Request->codeOn (a_httpRequest);
167 static_cast <IHTTPClient&> (anna::comm::functions::getApp ()).getServer ()->send (a_httpRequest);
170 void MyCommunicator::eventReceiveMessage (ClientSocket& clientSocket, const Message& message)
171 throw (RuntimeException)
173 LOGMETHOD (TraceMethod tm ("MyCommunicator", "eventReceiveMessage", ANNA_FILE_LOCATION));
175 if (clientSocket.support (http::Transport::className ()) == false)
178 a_httpHandler.apply (clientSocket, message);
181 void MyHandler::evResponse (ClientSocket& clientSocket, const http::Response& response)
182 throw (RuntimeException)
184 cout << "HTTP StatusCode: " << response.getStatusCode () << endl;
185 cout << "HTTP Text: " << response.getReasonPhrase () << endl;
187 app::functions::component <Communicator> (ANNA_FILE_LOCATION)->requestStop ();