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
39 #include <anna/core/util/CommandLine.hpp>
40 #include <anna/core/tracing/Logger.hpp>
42 #include <anna/comm/Network.hpp>
43 #include <anna/comm/IndexedDelivery.hpp>
44 #include <anna/comm/ServerSocket.hpp>
45 #include <anna/comm/Device.hpp>
47 #include <anna/http/Transport.hpp>
49 #include "Application.hpp"
50 #include "Context.hpp"
56 http4comm::Application::Application () :
57 comm::Application ("http_rserver", "Arithmetic HTTP server", "1.0") ,
58 a_timeController ((Millisecond)30000, (Millisecond)1000),
61 CommandLine& commandLine (CommandLine::instantiate ());
63 commandLine.add ("p", CommandLine::Argument::Mandatory, "Listen port for requests");
64 commandLine.add ("a", CommandLine::Argument::Mandatory, "Listen IP address for requests");
65 commandLine.add ("mode", CommandLine::Argument::Mandatory, "Delivery mode, (S)trict or (F)lexible");
66 commandLine.add ("as", CommandLine::Argument::Mandatory, "Arithmetic servers IP");
67 commandLine.add ("pp", CommandLine::Argument::Mandatory, "Port for + operation");
68 commandLine.add ("pm", CommandLine::Argument::Mandatory, "Port for - operation");
69 commandLine.add ("px", CommandLine::Argument::Mandatory, "Port for * operation");
70 commandLine.add ("pd", CommandLine::Argument::Mandatory, "Port for / operation");
71 commandLine.add ("trace", CommandLine::Argument::Optional, "Trace level (debug, warning, error,...)");
72 commandLine.add ("clone", CommandLine::Argument::Optional, "Clone mode", false);
75 void http4comm::Application::initialize ()
76 throw (RuntimeException)
78 using namespace anna::comm;
80 CommandLine& cl (CommandLine::instantiate ());
82 Communicator::WorkMode::_v workMode (Communicator::WorkMode::Single);
84 if (cl.exists ("trace"))
85 Logger::setLevel (Logger::asLevel (cl.getValue ("trace")));
87 if (cl.exists ("clone"))
88 workMode = Communicator::WorkMode::Clone;
90 a_communicator = new Communicator (workMode);
92 int port = cl.getIntegerValue ("p");
93 Network& network = Network::instantiate ();
95 const char* ip = cl.getValue ("a");
96 const char mode = toupper (*cl.getValue ("mode"));
98 const Device* device = Network::instantiate ().find (Device::asAddress (ip));
100 a_serverSocket = new ServerSocket (INetAddress (device, port), false, &http::Transport::getFactory ());
101 a_serverSocket->setReceiverFactory (a_acceptorFactory);
103 a_service = new comm::IndexedDelivery ("Service_Arithmetic", true, (mode == 'S') ? comm::IndexedDelivery::Mode::Strict: comm::IndexedDelivery::Mode::Flexible);
105 ip = cl.getValue ("as");
107 port = cl.getIntegerValue ("pp");
108 a_service->attach (network.createServer (ip, port, true, a_reactorFactory));
110 port = cl.getIntegerValue ("pm");
111 a_service->attach (network.createServer (ip, port, true, a_reactorFactory));
113 port = cl.getIntegerValue ("px");
114 a_service->attach (network.createServer (ip, port, true, a_reactorFactory));
116 port = cl.getIntegerValue ("pd");
117 a_service->attach (network.createServer (ip, port, true, a_reactorFactory));
119 a_communicator->attach (a_service);
120 a_communicator->attach (a_serverSocket);
122 a_context = new http4comm::Context (a_timeController, (Millisecond)2000);
125 void http4comm::Application::run ()
126 throw (RuntimeException)
128 a_communicator->accept ();