Remove dynamic exceptions
[anna.git] / source / test / Communicator.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 <iostream>
10
11 #include <anna/core/mt/Guard.hpp>
12 #include <anna/core/functions.hpp>
13 #include <anna/core/tracing/Logger.hpp>
14
15 #include <anna/comm/ClientSocket.hpp>
16 #include <anna/comm/CongestionController.hpp>
17
18 #include <anna/test/Communicator.hpp>
19
20 using namespace std;
21 using namespace anna;
22
23 bool test::Communicator::canContinue (const comm::ClientSocket& clientSocket)
24    noexcept(false)
25 {
26    Guard guard (this, "test::Communicator::canContinue");
27
28    if (a_initTime == 0)
29       a_initTime = anna::functions::millisecond ();
30
31    if (a_messageCounter > 0)
32       if (a_messageCounter == a_maxMessage && hasRequestedStop () == false) {
33          terminate ();
34          return false;
35       }
36
37    using namespace anna::comm;
38
39    CongestionController& congestionController = CongestionController::instantiate ();
40
41    a_messageCounter ++;
42
43    if (congestionController.getAdvice (clientSocket) == CongestionController::Advice::Discard)
44       return false;
45
46    a_successCounter ++;
47
48    return true;
49 }
50
51 void test::Communicator::delay ()
52    noexcept(false)
53 {
54   if (a_delay > 0) {
55       int random = (a_delay > 10) ? (rand () % (a_delay / 10)): 0;
56       int sign = rand () % 2;
57    
58       if (sign == 0)
59          random *= -1;
60
61       const Microsecond init = anna::functions::hardwareClock ();
62
63       anna::functions::sleep (a_delay + (Millisecond)random);
64
65       if (true) {
66          Guard guard (this, "test::Communicator::delay");   
67          a_avgDelay += anna::functions::hardwareClock () - init;
68       }
69    }
70 }
71
72 void  test::Communicator::terminate ()
73    
74 {
75    if (hasRequestedStop () == true)
76       return;
77
78    requestStop ();
79
80    const Millisecond serviceTime = anna::functions::millisecond () - a_initTime;
81
82    LOGNOTICE (
83       const int workload = (serviceTime == 0) ? 0: a_messageCounter * 1000 / serviceTime;   
84       string msg (anna::functions::asText ("Tiempo de servicio: ", (int) serviceTime));
85       msg += anna::functions::asText (" ms | Carga: ", workload);
86       msg += anna::functions::asText (" msg/seg | Mensajes recibidos: ", a_messageCounter);
87       msg += anna::functions::asText (" | Mensajes tratados: ", a_successCounter);
88       Logger::notice (msg, ANNA_FILE_LOCATION);      
89       cout << msg << endl << endl;
90       
91       msg = "Retardo medio (us) | ";
92       msg += a_avgDelay.asString ();
93       Logger::notice (msg, ANNA_FILE_LOCATION);      
94       cout << msg << endl << endl;
95    );
96 }
97
98 //----------------------------------------------------------------------------------------
99 // (1) Dos handlers, uno el ServerSocket y otro el ClientSocket que va a cerran =>
100 // si es asi termina
101 //----------------------------------------------------------------------------------------
102 void test::Communicator::eventOverQuota (const comm::ClientSocket&) 
103    
104 {
105    int counter = 0;
106
107    for (handler_iterator ii = handler_begin (), maxii = handler_end (); ii != maxii; ii ++)
108       counter ++;
109
110    LOGNOTICE (
111       Logger::notice (anna::functions::asText ("Handlers: ", counter), ANNA_FILE_LOCATION);
112    );
113       
114    if (counter == 2)  // (1)
115       terminate ();
116 }