First commit
[anna.git] / source / test / Communicator.cpp
1 // ANNA - Anna is Not 'N' 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 <iostream>
38
39 #include <anna/core/mt/Guard.hpp>
40 #include <anna/core/functions.hpp>
41 #include <anna/core/tracing/Logger.hpp>
42
43 #include <anna/comm/ClientSocket.hpp>
44 #include <anna/comm/CongestionController.hpp>
45
46 #include <anna/test/Communicator.hpp>
47
48 using namespace std;
49 using namespace anna;
50
51 bool test::Communicator::canContinue (const comm::ClientSocket& clientSocket)
52    throw (RuntimeException)
53 {
54    Guard guard (this, "test::Communicator::canContinue");
55
56    if (a_initTime == 0)
57       a_initTime = anna::functions::millisecond ();
58
59    if (a_messageCounter > 0)
60       if (a_messageCounter == a_maxMessage && hasRequestedStop () == false) {
61          terminate ();
62          return false;
63       }
64
65    using namespace anna::comm;
66
67    CongestionController& congestionController = CongestionController::instantiate ();
68
69    a_messageCounter ++;
70
71    if (congestionController.getAdvice (clientSocket) == CongestionController::Advice::Discard)
72       return false;
73
74    a_successCounter ++;
75
76    return true;
77 }
78
79 void test::Communicator::delay ()
80    throw (RuntimeException)
81 {
82   if (a_delay > 0) {
83       int random = (a_delay > 10) ? (rand () % (a_delay / 10)): 0;
84       int sign = rand () % 2;
85    
86       if (sign == 0)
87          random *= -1;
88
89       const Microsecond init = anna::functions::hardwareClock ();
90
91       anna::functions::sleep (a_delay + (Millisecond)random);
92
93       if (true) {
94          Guard guard (this, "test::Communicator::delay");   
95          a_avgDelay += anna::functions::hardwareClock () - init;
96       }
97    }
98 }
99
100 void  test::Communicator::terminate ()
101    throw ()
102 {
103    if (hasRequestedStop () == true)
104       return;
105
106    requestStop ();
107
108    const Millisecond serviceTime = anna::functions::millisecond () - a_initTime;
109
110    LOGNOTICE (
111       const int workload = (serviceTime == 0) ? 0: a_messageCounter * 1000 / serviceTime;   
112       string msg (anna::functions::asText ("Tiempo de servicio: ", (int) serviceTime));
113       msg += anna::functions::asText (" ms | Carga: ", workload);
114       msg += anna::functions::asText (" msg/seg | Mensajes recibidos: ", a_messageCounter);
115       msg += anna::functions::asText (" | Mensajes tratados: ", a_successCounter);
116       Logger::notice (msg, ANNA_FILE_LOCATION);      
117       cout << msg << endl << endl;
118       
119       msg = "Retardo medio (us) | ";
120       msg += a_avgDelay.asString ();
121       Logger::notice (msg, ANNA_FILE_LOCATION);      
122       cout << msg << endl << endl;
123    );
124 }
125
126 //----------------------------------------------------------------------------------------
127 // (1) Dos handlers, uno el ServerSocket y otro el ClientSocket que va a cerran =>
128 // si es asi termina
129 //----------------------------------------------------------------------------------------
130 void test::Communicator::eventOverQuota (const comm::ClientSocket&) 
131    throw ()
132 {
133    int counter = 0;
134
135    for (handler_iterator ii = handler_begin (), maxii = handler_end (); ii != maxii; ii ++)
136       counter ++;
137
138    LOGNOTICE (
139       Logger::notice (anna::functions::asText ("Handlers: ", counter), ANNA_FILE_LOCATION);
140    );
141       
142    if (counter == 2)  // (1)
143       terminate ();
144 }