Updated license
[anna.git] / example / timex / ArithmeticHTTPServer / Application.cpp
1 // ANNA - Anna is Not Nothingness Anymore
2 //
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
4 //
5 // https://bitbucket.org/testillano/anna
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
9 // are met:
10 //
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
16 // distribution.
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.
20 //
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.
32 //
33 // Authors: eduardo.ramos.testillano@gmail.com
34 //          cisco.tierra@gmail.com
35
36
37 #include <ctype.h>
38
39 #include <anna/core/util/CommandLine.hpp>
40 #include <anna/core/tracing/Logger.hpp>
41
42 #include <anna/comm/Network.hpp>
43 #include <anna/comm/IndexedDelivery.hpp>
44 #include <anna/comm/ServerSocket.hpp>
45 #include <anna/comm/Device.hpp>
46
47 #include <anna/http/Transport.hpp>
48
49 #include "Application.hpp"
50 #include "Context.hpp"
51
52 using namespace std;
53 using namespace anna;
54 using namespace test;
55
56 http4comm::Application::Application () : 
57    comm::Application ("http_rserver", "Arithmetic HTTP server", "1.0") ,
58    a_timeController ((Millisecond)30000, (Millisecond)1000),
59    a_communicator (NULL)
60 {
61    CommandLine& commandLine (CommandLine::instantiate ());
62       
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);
73 }
74
75 void http4comm::Application::initialize () 
76    throw (RuntimeException)
77 {
78    using namespace anna::comm;
79
80    CommandLine& cl (CommandLine::instantiate ());
81
82    Communicator::WorkMode::_v workMode (Communicator::WorkMode::Single);
83
84    if (cl.exists ("trace"))
85       Logger::setLevel (Logger::asLevel (cl.getValue ("trace")));
86
87    if (cl.exists ("clone"))
88       workMode = Communicator::WorkMode::Clone;
89
90    a_communicator = new Communicator (workMode);
91
92    int port = cl.getIntegerValue ("p");
93    Network& network = Network::instantiate ();
94
95    const char* ip = cl.getValue ("a");
96    const char mode = toupper (*cl.getValue ("mode"));
97
98    const Device* device = Network::instantiate ().find (Device::asAddress (ip));
99
100    a_serverSocket = new ServerSocket (INetAddress (device, port), false, &http::Transport::getFactory ());
101    a_serverSocket->setReceiverFactory (a_acceptorFactory);
102
103    a_service = new comm::IndexedDelivery ("Service_Arithmetic", true, (mode == 'S') ? comm::IndexedDelivery::Mode::Strict: comm::IndexedDelivery::Mode::Flexible);
104
105    ip = cl.getValue ("as");
106
107    port = cl.getIntegerValue ("pp");
108    a_service->attach (network.createServer (ip, port, true, a_reactorFactory));
109
110    port = cl.getIntegerValue ("pm");
111    a_service->attach (network.createServer (ip, port, true, a_reactorFactory));
112
113    port = cl.getIntegerValue ("px");
114    a_service->attach (network.createServer (ip, port, true, a_reactorFactory));
115
116    port = cl.getIntegerValue ("pd");
117    a_service->attach (network.createServer (ip, port, true, a_reactorFactory));
118
119    a_communicator->attach (a_service);
120    a_communicator->attach (a_serverSocket);
121    
122    a_context = new http4comm::Context (a_timeController, (Millisecond)2000);
123 }
124
125 void http4comm::Application::run ()
126    throw (RuntimeException)
127 {
128    a_communicator->accept ();
129 }
130