Remove dynamic exceptions
[anna.git] / example / timex / ArithmeticHTTPServer / Application.cpp
1 // ANNA - Anna is Not Nothingness Anymore                                                         //
2 //                                                                                                //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo                         //
4 //                                                                                                //
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 //
7
8
9 #include <ctype.h>
10
11 #include <anna/core/util/CommandLine.hpp>
12 #include <anna/core/tracing/Logger.hpp>
13
14 #include <anna/comm/Network.hpp>
15 #include <anna/comm/IndexedDelivery.hpp>
16 #include <anna/comm/ServerSocket.hpp>
17 #include <anna/comm/Device.hpp>
18
19 #include <anna/http/Transport.hpp>
20
21 #include "Application.hpp"
22 #include "Context.hpp"
23
24 using namespace std;
25 using namespace anna;
26 using namespace test;
27
28 http4comm::Application::Application () : 
29    comm::Application ("http_rserver", "Arithmetic HTTP server", "1.0") ,
30    a_timeController ((Millisecond)30000, (Millisecond)1000),
31    a_communicator (NULL)
32 {
33    CommandLine& commandLine (CommandLine::instantiate ());
34       
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);
45 }
46
47 void http4comm::Application::initialize () 
48    noexcept(false)
49 {
50    using namespace anna::comm;
51
52    CommandLine& cl (CommandLine::instantiate ());
53
54    Communicator::WorkMode::_v workMode (Communicator::WorkMode::Single);
55
56    if (cl.exists ("trace"))
57       Logger::setLevel (Logger::asLevel (cl.getValue ("trace")));
58
59    if (cl.exists ("clone"))
60       workMode = Communicator::WorkMode::Clone;
61
62    a_communicator = new Communicator (workMode);
63
64    int port = cl.getIntegerValue ("p");
65    Network& network = Network::instantiate ();
66
67    const char* ip = cl.getValue ("a");
68    const char mode = toupper (*cl.getValue ("mode"));
69
70    const Device* device = Network::instantiate ().find (Device::asAddress (ip));
71
72    a_serverSocket = new ServerSocket (INetAddress (device, port), false, &http::Transport::getFactory ());
73    a_serverSocket->setReceiverFactory (a_acceptorFactory);
74
75    a_service = new comm::IndexedDelivery ("Service_Arithmetic", true, (mode == 'S') ? comm::IndexedDelivery::Mode::Strict: comm::IndexedDelivery::Mode::Flexible);
76
77    ip = cl.getValue ("as");
78
79    port = cl.getIntegerValue ("pp");
80    a_service->attach (network.createServer (ip, port, true, a_reactorFactory));
81
82    port = cl.getIntegerValue ("pm");
83    a_service->attach (network.createServer (ip, port, true, a_reactorFactory));
84
85    port = cl.getIntegerValue ("px");
86    a_service->attach (network.createServer (ip, port, true, a_reactorFactory));
87
88    port = cl.getIntegerValue ("pd");
89    a_service->attach (network.createServer (ip, port, true, a_reactorFactory));
90
91    a_communicator->attach (a_service);
92    a_communicator->attach (a_serverSocket);
93    
94    a_context = new http4comm::Context (a_timeController, (Millisecond)2000);
95 }
96
97 void http4comm::Application::run ()
98    noexcept(false)
99 {
100    a_communicator->accept ();
101 }
102