1 // ANNA - Anna is Not Nothingness Anymore
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
5 // http://redmine.teslayout.com/projects/anna-suite
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
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
17 // * Neither the name of the copyright holder 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.
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.
33 // Authors: eduardo.ramos.testillano@gmail.com
34 // cisco.tierra@gmail.com
39 #include <anna/core/mt/Guard.hpp>
40 #include <anna/core/functions.hpp>
41 #include <anna/core/tracing/Logger.hpp>
43 #include <anna/comm/ClientSocket.hpp>
44 #include <anna/comm/CongestionController.hpp>
46 #include <anna/test/Communicator.hpp>
51 bool test::Communicator::canContinue (const comm::ClientSocket& clientSocket)
52 throw (RuntimeException)
54 Guard guard (this, "test::Communicator::canContinue");
57 a_initTime = anna::functions::millisecond ();
59 if (a_messageCounter > 0)
60 if (a_messageCounter == a_maxMessage && hasRequestedStop () == false) {
65 using namespace anna::comm;
67 CongestionController& congestionController = CongestionController::instantiate ();
71 if (congestionController.getAdvice (clientSocket) == CongestionController::Advice::Discard)
79 void test::Communicator::delay ()
80 throw (RuntimeException)
83 int random = (a_delay > 10) ? (rand () % (a_delay / 10)): 0;
84 int sign = rand () % 2;
89 const Microsecond init = anna::functions::hardwareClock ();
91 anna::functions::sleep (a_delay + (Millisecond)random);
94 Guard guard (this, "test::Communicator::delay");
95 a_avgDelay += anna::functions::hardwareClock () - init;
100 void test::Communicator::terminate ()
103 if (hasRequestedStop () == true)
108 const Millisecond serviceTime = anna::functions::millisecond () - a_initTime;
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;
119 msg = "Retardo medio (us) | ";
120 msg += a_avgDelay.asString ();
121 Logger::notice (msg, ANNA_FILE_LOCATION);
122 cout << msg << endl << endl;
126 //----------------------------------------------------------------------------------------
127 // (1) Dos handlers, uno el ServerSocket y otro el ClientSocket que va a cerran =>
129 //----------------------------------------------------------------------------------------
130 void test::Communicator::eventOverQuota (const comm::ClientSocket&)
135 for (handler_iterator ii = handler_begin (), maxii = handler_end (); ii != maxii; ii ++)
139 Logger::notice (anna::functions::asText ("Handlers: ", counter), ANNA_FILE_LOCATION);
142 if (counter == 2) // (1)