1 // ANNA - Anna is Not Nothingness Anymore //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo //
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 //
11 #include <anna/core/tracing/TraceMethod.hpp>
12 #include <anna/core/tracing/Logger.hpp>
13 #include <anna/core/functions.hpp>
15 #include <anna/xml/Node.hpp>
17 #include <anna/comm/Communicator.hpp>
18 #include <anna/comm/Server.hpp>
19 #include <anna/comm/ClientSocket.hpp>
25 * Se invoca desde el handler::RemoteConnection::finalize y debe estar protegido por la SSCC del Communicator
27 void comm::ConnectionRecover::annotateFault(comm::Server* server)
29 if(server->autoRecovery() == false)
32 if(find(a_breaks.begin(), a_breaks.end(), server) != a_breaks.end())
35 if(a_isRunning == false) {
36 a_nextTime = functions::millisecond() + a_communicator.getRecoveryTime();
40 a_breaks.push_back(server);
41 a_recovering = a_breaks.begin();
42 string msg("comm::ConnectionRecover::fault | ");
43 msg += server->asString();
44 msg += anna::functions::asText(" | NextTime: ", a_nextTime);
45 Logger::error(msg, ANNA_FILE_LOCATION);
49 * Se invoca desde el Communicator y en MT debe estar protegido por la SSCC del Communicator
51 void comm::ConnectionRecover::tryRecover()
53 LOGMETHOD(TraceMethod traceMethod(Logger::Local7, "comm::ConnectionRecover", "tryRecover", ANNA_FILE_LOCATION));
54 Millisecond now(functions::millisecond());
59 if(a_recovering == a_breaks.end())
62 Millisecond maxTime = now + a_communicator.getTryingConnectionTime();
63 break_iterator beginning = a_recovering;
66 while(now < maxTime && a_breaks.size() > 0) {
67 server = *a_recovering;
72 Logger::write(Logger::Notice, "Recover", server->asString(), ANNA_FILE_LOCATION)
74 a_recovering = a_breaks.erase(a_recovering);
75 } catch(Exception& ex) {
79 if(a_recovering == a_breaks.end())
80 a_recovering = a_breaks.begin();
82 if(a_recovering == beginning)
85 now = functions::millisecond();
88 a_isRunning = (a_breaks.size() > 0);
89 a_nextTime = now + a_communicator.getRecoveryTime();
91 string msg("comm::ConnectionRecover::recover | Running: ");
92 msg += anna::functions::asString(a_isRunning);
93 msg += anna::functions::asText(" | N: ", (int) a_breaks.size());
94 msg += anna::functions::asText(" | NextTime: ", a_nextTime);
95 Logger::debug(msg, ANNA_FILE_LOCATION);
100 bool comm::ConnectionRecover::contains (comm::Server* server) const
103 Guard guard (a_mutex, "comm::ConnectionRecover::contains");
104 return std::find (a_breaks.begin (), a_breaks.end (), server) != a_breaks.end ();
107 void comm::ConnectionRecover::erase (comm::Server* server)
110 Guard guard (a_mutex, "comm::ConnectionRecover::erase");
112 vector <Server*>::iterator ii = find (a_breaks.begin (), a_breaks.end (), server);
114 if (ii == a_breaks.end ())
117 if (ii == a_recovering)
118 if ((a_recovering = a_breaks.erase (ii)) == a_breaks.end ())
119 a_recovering = a_breaks.begin ();
121 a_isRunning = (a_breaks.size () > 0);
125 xml::Node* comm::ConnectionRecover::asXML(xml::Node* parent) const
126 throw(RuntimeException) {
127 xml::Node* connectionRecover = parent->createChild("comm.ConnectionRecover");
128 connectionRecover->createAttribute("Active", functions::asString(a_isRunning));
130 for(std::vector <Server*>::const_iterator ii = a_breaks.begin(), maxii = a_breaks.end(); ii != maxii; ii ++)
131 (*ii)->asXML(connectionRecover);
133 return connectionRecover;