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 //
11 #include <anna/core/util/CommandLine.hpp>
12 #include <anna/core/tracing/Logger.hpp>
14 #include <anna/comm/Network.hpp>
15 #include <anna/comm/IndexedDelivery.hpp>
16 #include <anna/comm/ServerSocket.hpp>
17 #include <anna/comm/Device.hpp>
19 #include <anna/http/Transport.hpp>
21 #include "Application.hpp"
22 #include "Context.hpp"
28 http4comm::Application::Application () :
29 comm::Application ("http_rserver", "Arithmetic HTTP server", "1.0") ,
30 a_timeController ((Millisecond)30000, (Millisecond)1000),
33 CommandLine& commandLine (CommandLine::instantiate ());
35 commandLine.add ("p", CommandLine::Argument::Mandatory, "Listen port for requests");
36 commandLine.add ("a", CommandLine::Argument::Mandatory, "Listen IP address for requests");
37 commandLine.add ("mode", CommandLine::Argument::Mandatory, "Delivery mode, (S)trict or (F)lexible");
38 commandLine.add ("as", CommandLine::Argument::Mandatory, "Arithmetic servers IP");
39 commandLine.add ("pp", CommandLine::Argument::Mandatory, "Port for + operation");
40 commandLine.add ("pm", CommandLine::Argument::Mandatory, "Port for - operation");
41 commandLine.add ("px", CommandLine::Argument::Mandatory, "Port for * operation");
42 commandLine.add ("pd", CommandLine::Argument::Mandatory, "Port for / operation");
43 commandLine.add ("trace", CommandLine::Argument::Optional, "Trace level (debug, warning, error,...)");
44 commandLine.add ("clone", CommandLine::Argument::Optional, "Clone mode", false);
47 void http4comm::Application::initialize ()
50 using namespace anna::comm;
52 CommandLine& cl (CommandLine::instantiate ());
54 Communicator::WorkMode::_v workMode (Communicator::WorkMode::Single);
56 if (cl.exists ("trace"))
57 Logger::setLevel (Logger::asLevel (cl.getValue ("trace")));
59 if (cl.exists ("clone"))
60 workMode = Communicator::WorkMode::Clone;
62 a_communicator = new Communicator (workMode);
64 int port = cl.getIntegerValue ("p");
65 Network& network = Network::instantiate ();
67 const char* ip = cl.getValue ("a");
68 const char mode = toupper (*cl.getValue ("mode"));
70 const Device* device = Network::instantiate ().find (Device::asAddress (ip));
72 a_serverSocket = new ServerSocket (INetAddress (device, port), false, &http::Transport::getFactory ());
73 a_serverSocket->setReceiverFactory (a_acceptorFactory);
75 a_service = new comm::IndexedDelivery ("Service_Arithmetic", true, (mode == 'S') ? comm::IndexedDelivery::Mode::Strict: comm::IndexedDelivery::Mode::Flexible);
77 ip = cl.getValue ("as");
79 port = cl.getIntegerValue ("pp");
80 a_service->attach (network.createServer (ip, port, true, a_reactorFactory));
82 port = cl.getIntegerValue ("pm");
83 a_service->attach (network.createServer (ip, port, true, a_reactorFactory));
85 port = cl.getIntegerValue ("px");
86 a_service->attach (network.createServer (ip, port, true, a_reactorFactory));
88 port = cl.getIntegerValue ("pd");
89 a_service->attach (network.createServer (ip, port, true, a_reactorFactory));
91 a_communicator->attach (a_service);
92 a_communicator->attach (a_serverSocket);
94 a_context = new http4comm::Context (a_timeController, (Millisecond)2000);
97 void http4comm::Application::run ()
100 a_communicator->accept ();