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/tracing/TraceMethod.hpp>
40 #include <anna/core/tracing/Logger.hpp>
41 #include <anna/core/functions.hpp>
43 #include <anna/xml/Node.hpp>
45 #include <anna/comm/Communicator.hpp>
46 #include <anna/comm/Server.hpp>
47 #include <anna/comm/ClientSocket.hpp>
53 * Se invoca desde el handler::RemoteConnection::finalize y debe estar protegido por la SSCC del Communicator
55 void comm::ConnectionRecover::annotateFault(comm::Server* server)
57 if(server->autoRecovery() == false)
60 if(find(a_breaks.begin(), a_breaks.end(), server) != a_breaks.end())
63 if(a_isRunning == false) {
64 a_nextTime = functions::millisecond() + a_communicator.getRecoveryTime();
68 a_breaks.push_back(server);
69 a_recovering = a_breaks.begin();
70 string msg("comm::ConnectionRecover::fault | ");
71 msg += server->asString();
72 msg += anna::functions::asText(" | NextTime: ", a_nextTime);
73 Logger::error(msg, ANNA_FILE_LOCATION);
77 * Se invoca desde el Communicator y en MT debe estar protegido por la SSCC del Communicator
79 void comm::ConnectionRecover::tryRecover()
81 LOGMETHOD(TraceMethod traceMethod(Logger::Local7, "comm::ConnectionRecover", "tryRecover", ANNA_FILE_LOCATION));
82 Millisecond now(functions::millisecond());
87 if(a_recovering == a_breaks.end())
90 Millisecond maxTime = now + a_communicator.getTryingConnectionTime();
91 break_iterator beginning = a_recovering;
94 while(now < maxTime && a_breaks.size() > 0) {
95 server = *a_recovering;
100 Logger::write(Logger::Notice, "Recover", server->asString(), ANNA_FILE_LOCATION)
102 a_recovering = a_breaks.erase(a_recovering);
103 } catch(Exception& ex) {
107 if(a_recovering == a_breaks.end())
108 a_recovering = a_breaks.begin();
110 if(a_recovering == beginning)
113 now = functions::millisecond();
116 a_isRunning = (a_breaks.size() > 0);
117 a_nextTime = now + a_communicator.getRecoveryTime();
119 string msg("comm::ConnectionRecover::recover | Running: ");
120 msg += anna::functions::asString(a_isRunning);
121 msg += anna::functions::asText(" | N: ", (int) a_breaks.size());
122 msg += anna::functions::asText(" | NextTime: ", a_nextTime);
123 Logger::debug(msg, ANNA_FILE_LOCATION);
128 bool comm::ConnectionRecover::contains (comm::Server* server) const
131 Guard guard (a_mutex, "comm::ConnectionRecover::contains");
132 return std::find (a_breaks.begin (), a_breaks.end (), server) != a_breaks.end ();
135 void comm::ConnectionRecover::erase (comm::Server* server)
138 Guard guard (a_mutex, "comm::ConnectionRecover::erase");
140 vector <Server*>::iterator ii = find (a_breaks.begin (), a_breaks.end (), server);
142 if (ii == a_breaks.end ())
145 if (ii == a_recovering)
146 if ((a_recovering = a_breaks.erase (ii)) == a_breaks.end ())
147 a_recovering = a_breaks.begin ();
149 a_isRunning = (a_breaks.size () > 0);
153 xml::Node* comm::ConnectionRecover::asXML(xml::Node* parent) const
154 throw(RuntimeException) {
155 xml::Node* connectionRecover = parent->createChild("comm.ConnectionRecover");
156 connectionRecover->createAttribute("Active", functions::asString(a_isRunning));
158 for(std::vector <Server*>::const_iterator ii = a_breaks.begin(), maxii = a_breaks.end(); ii != maxii; ii ++)
159 (*ii)->asXML(connectionRecover);
161 return connectionRecover;