Remove dynamic exceptions
[anna.git] / source / test / Control.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 #include <anna/core/util/DelayMeter.hpp>
15 #include <anna/core/tracing/TraceMethod.hpp>
16
17 #include <anna/xml/Node.hpp>
18 #include <anna/xml/Attribute.hpp>
19
20 #include <anna/app/Application.hpp>
21
22 #include <anna/comm/Communicator.hpp>
23 #include <anna/comm/CongestionController.hpp>
24
25 #include <anna/test/Control.hpp>
26
27 using namespace std;
28 using namespace anna;
29
30 typedef Guard MyGuard;
31 //typedef Guard <test::Control> MyGuard;
32
33 test::Control::Control (comm::Communicator* engine) :
34    a_engine (*engine),
35    a_maxMessage (-1),
36    a_initTime (0),
37    a_avgDelay ("AvgDelay"),
38    a_avgLatency ("AvgLatency"),
39    a_delay (0)
40 {;}
41
42 bool test::Control::canContinue (const comm::socket::Client&)
43    noexcept(false)
44 {
45    LOGMETHOD (TraceMethod ttmm (Logger::Local7, "test::Control::canContinue", ANNA_FILE_LOCATION));
46
47    comm::CongestionController& congestionController = comm::CongestionController::instantiate ();
48
49    if (a_maxMessage != -1 && congestionController.getMessageCounter () >= a_maxMessage) {
50       stop ();
51       return false;
52    }
53
54    MyGuard guard (this);
55
56    if (a_initTime == 0)
57       a_initTime = anna::Millisecond::getTime ();
58
59    return true;
60 }
61
62 void test::Control::delay ()
63    noexcept(false)
64 {
65    LOGMETHOD (TraceMethod ttmm (Logger::Local7, "test::Control::delay", ANNA_FILE_LOCATION));
66
67   if (a_delay > 0) {
68       Millisecond random = Millisecond ((a_delay > 10) ? (rand () % (a_delay / 10)): 0);
69       Millisecond delay (a_delay);
70
71       int sign = rand () % 2;
72
73       if (sign == 0)
74          delay -= random;
75       else
76          delay += random;
77
78       DelayMeter <Microsecond> meter;
79
80       anna::functions::sleep (delay);
81
82       if (true) {
83          MyGuard guard (this);
84          a_avgDelay += meter.getValue();
85       }
86    }
87 }
88
89 // Aproximación del valor que lleva en mensaje en cola I/O
90 Millisecond test::Control::latency (const Millisecond& init)
91    noexcept(false)
92 {
93    LOGMETHOD (TraceMethod ttmm (Logger::Local7, "test::Control::latency", ANNA_FILE_LOCATION));
94
95    const Millisecond now = anna::Millisecond::getTime();
96
97    const Millisecond _latency = now - init;
98
99    if (_latency > 0) {
100       MyGuard guard (this);
101       a_avgLatency += _latency;
102    }
103
104    return now;
105 }
106
107 void  test::Control::report ()
108    
109 {
110    const Millisecond serviceTime = anna::Millisecond::getTime () - a_initTime;
111
112    comm::CongestionController& congestionController = comm::CongestionController::instantiate ();
113
114    const int workload = (serviceTime == 0) ? 0: congestionController.getMessageCounter () * 1000 / serviceTime;
115    string msg (anna::functions::asText ("Time of service: ", (int) serviceTime));
116    msg += anna::functions::asText (" ms | Workload: ", workload);
117    msg += anna::functions::asText (" msg/sec | Received Messages: ", congestionController.getMessageCounter ());
118    msg += anna::functions::asText (" | Processed Messages: ", congestionController.getSuccessCounter ());
119    Logger::notice (msg, ANNA_FILE_LOCATION);
120    cout << msg << endl << endl;
121
122    msg = "Average delay (us): ";
123    msg += a_avgDelay.asString ();
124    Logger::notice (msg, ANNA_FILE_LOCATION);
125    cout << msg << endl << endl;
126
127    msg = "Average latency (ms): ";
128    msg += a_avgLatency.asString ();
129    Logger::notice (msg, ANNA_FILE_LOCATION);
130    cout << msg << endl << endl;
131 }
132
133 void test::Control::stop ()
134    noexcept(false)
135 {
136    if (a_engine.hasRequestedStop () == true)
137       return;
138
139    a_engine.requestStop ();
140
141 }
142
143 xml::Node* test::Control::asXML (xml::Node* parent)
144    
145 {
146    xml::Node* result = parent->createChild ("test.Control");
147
148    result->createAttribute ("MaxMessage", a_maxMessage);
149    result->createAttribute ("AvgDelay", a_avgDelay.value ().asString());
150    result->createAttribute ("AvgLatency", a_avgLatency.value ().asString());
151
152    return result;
153 }
154