1 // ANNA - Anna is Not 'N' Anymore
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
5 // https://bitbucket.org/testillano/anna
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 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.
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 Envia una peticion de interseccion sobre HTTP/XML para ihttp_server
40 El cliente de esta aplicacion es: ihttp_server.p => Transporte: http::Transport.
46 #include <anna/core/core.hpp>
47 #include <anna/comm/comm.hpp>
49 #include <anna/app/functions.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/http/wims20/ClientSide.hpp>
59 class MyHandler : public http::Handler {
61 MyHandler () : http::Handler ("ihttp_client::MyHandler") {;}
64 http::Response a_httpResponse;
66 void evRequest (ClientSocket&, const http::Request&) throw (RuntimeException) {;}
67 void evResponse (ClientSocket&, const http::Response&) throw (RuntimeException);
70 class MyCommunicator : public Communicator {
72 MyCommunicator () : Communicator (), a_httpRequest ()
74 a_httpRequest.setMethod (http::Method::Type::Get);
75 a_httpRequest.setURI ("ihttp_client");
79 MyHandler a_httpHandler;
80 http::Request a_httpRequest;
82 void eventReceiveMessage (ClientSocket &, const Message&) throw (RuntimeException);
83 void eventStartup () throw (RuntimeException);
86 class IHTTPClient : public anna::comm::Application {
90 Server* getServer () const throw () { return a_server; }
93 MyCommunicator a_communicator;
96 void initialize () throw (RuntimeException);
97 void run () throw (RuntimeException);
102 int main (int argc, const char** argv)
104 CommandLine& commandLine (CommandLine::instantiate ());
107 http::functions::initialize ();
110 commandLine.initialize (argv, argc);
111 commandLine.verify ();
113 Logger::setLevel (Logger::Debug);
114 Logger::initialize ("ihttp_client", new TraceWriter ("file.trace", 4096000));
118 catch (Exception& ex) {
119 cout << ex.asString () << endl;
125 IHTTPClient::IHTTPClient () :
126 Application ("wims20_client", "IHTTPClient", "2.0.0")
128 CommandLine& commandLine (CommandLine::instantiate ());
130 commandLine.add ("p", CommandLine::Argument::Mandatory, "Puerto en el que el servidor atiende respuestas.");
131 commandLine.add ("a", CommandLine::Argument::Mandatory, "Direccin IP Puerto en el que el servidor atiende respuestas.");
132 commandLine.add ("domain", CommandLine::Argument::Mandatory, "Domain indicado en la petición WIMS 2.0");
133 commandLine.add ("op", CommandLine::Argument::Mandatory, "operación a realizar (sum,sub,mul,div)");
134 commandLine.add ("x", CommandLine::Argument::Mandatory, "Primer operador");
135 commandLine.add ("y", CommandLine::Argument::Mandatory, "Segundo operador");
136 commandLine.add ("op", CommandLine::Argument::Mandatory, "operación a realizar (sum,sub,mul,div)");
137 commandLine.add ("trace", CommandLine::Argument::Optional, "Nivel de trazas (debug,warning, error,...)");
138 commandLine.add ("path", CommandLine::Argument::Optional, "Path indicado en la petición WIMS 2.0");
139 commandLine.add ("m", CommandLine::Argument::Optional, "Método HTTP a usar");
142 void IHTTPClient::initialize ()
143 throw (RuntimeException)
145 CommandLine& cl (CommandLine::instantiate ());
147 Network& network = Network::instantiate ();
149 if (cl.exists ("trace"))
150 Logger::setLevel (Logger::asLevel (cl.getValue ("trace")));
152 Host* host = network.find_host("host000");
153 host->assign (network.find (Device::asAddress (cl.getValue ("a"))));
154 a_server = host->createServer ("http_server", cl.getIntegerValue ("p"), true, &http::Transport::getFactory ());
157 void IHTTPClient::run ()
158 throw (RuntimeException)
160 a_communicator.accept ();
163 void MyCommunicator::eventStartup ()
164 throw (RuntimeException)
166 CommandLine& cl (CommandLine::instantiate ());
168 string domain = cl.getValue ("domain");
170 http::wims20::ClientSide* wims20Request = NULL;
172 if (cl.exists ("path")) {
173 string path = cl.getValue ("path");
174 wims20Request = new http::wims20::ClientSide (domain, path);
177 wims20Request = new http::wims20::ClientSide (domain);
179 wims20Request->setServiceID ("math");
180 wims20Request->setGUID ("user@tid.es");
181 wims20Request->addOtherLevel (cl.getValue ("op"));
182 wims20Request->setParameter ("X", cl.getIntegerValue ("x"));
183 wims20Request->setParameter ("Y", cl.getIntegerValue ("y"));
185 http::Header* header = a_httpRequest.createHeader (http::Header::Type::Connection);
186 header->setValue (" Keep-Alive ");
188 if (cl.exists ("m")) {
189 http::Method::Type::_v type = http::Method::Type::asEnumEx (cl.getValue ("m"));
190 a_httpRequest.setMethod (type);
193 wims20Request->codeOn (a_httpRequest);
195 static_cast <IHTTPClient&> (anna::comm::functions::getApp ()).getServer ()->send (a_httpRequest);
198 void MyCommunicator::eventReceiveMessage (ClientSocket& clientSocket, const Message& message)
199 throw (RuntimeException)
201 LOGMETHOD (TraceMethod tm ("MyCommunicator", "eventReceiveMessage", ANNA_FILE_LOCATION));
203 if (clientSocket.support (http::Transport::className ()) == false)
206 a_httpHandler.apply (clientSocket, message);
209 void MyHandler::evResponse (ClientSocket& clientSocket, const http::Response& response)
210 throw (RuntimeException)
212 cout << "HTTP StatusCode: " << response.getStatusCode () << endl;
213 cout << "HTTP Text: " << response.getReasonPhrase () << endl;
215 app::functions::component <Communicator> (ANNA_FILE_LOCATION)->requestStop ();