First commit
[anna.git] / source / comm / internal / ConnectionRecover.cpp
1 // ANNA - Anna is Not 'N' Anymore
2 //
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
4 //
5 // https://bitbucket.org/testillano/anna
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
9 // are met:
10 //
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
16 // distribution.
17 //     * Neither the name of Google Inc. 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.
20 //
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.
32 //
33 // Authors: eduardo.ramos.testillano@gmail.com
34 //          cisco.tierra@gmail.com
35
36
37 #include <algorithm>
38
39 #include <anna/core/tracing/TraceMethod.hpp>
40 #include <anna/core/tracing/Logger.hpp>
41 #include <anna/core/functions.hpp>
42
43 #include <anna/xml/Node.hpp>
44
45 #include <anna/comm/Communicator.hpp>
46 #include <anna/comm/Server.hpp>
47 #include <anna/comm/ClientSocket.hpp>
48
49 using namespace std;
50 using namespace anna;
51
52 /*
53  * Se invoca desde el handler::RemoteConnection::finalize y debe estar protegido por la SSCC del Communicator
54  */
55 void comm::ConnectionRecover::annotateFault(comm::Server* server)
56 throw() {
57   if(server->autoRecovery() == false)
58     return;
59
60   if(find(a_breaks.begin(), a_breaks.end(), server) != a_breaks.end())
61     return;
62
63   if(a_isRunning == false) {
64     a_nextTime = functions::millisecond() + a_communicator.getRecoveryTime();
65     a_isRunning = true;
66   }
67
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);
74 }
75
76 /*
77  * Se invoca desde el Communicator y en MT debe estar protegido por la SSCC del Communicator
78  */
79 void comm::ConnectionRecover::tryRecover()
80 throw() {
81   LOGMETHOD(TraceMethod traceMethod(Logger::Local7, "comm::ConnectionRecover", "tryRecover", ANNA_FILE_LOCATION));
82   Millisecond now(functions::millisecond());
83
84   if(now < a_nextTime)
85     return;
86
87   if(a_recovering == a_breaks.end())
88     return;
89
90   Millisecond maxTime = now + a_communicator.getTryingConnectionTime();
91   break_iterator beginning = a_recovering;
92   Server* server;
93
94   while(now < maxTime && a_breaks.size() > 0) {
95     server = *a_recovering;
96
97     try {
98       server->connect();
99       LOGNOTICE(
100         Logger::write(Logger::Notice, "Recover", server->asString(), ANNA_FILE_LOCATION)
101       );
102       a_recovering = a_breaks.erase(a_recovering);
103     } catch(Exception& ex) {
104       a_recovering ++;
105     }
106
107     if(a_recovering == a_breaks.end())
108       a_recovering = a_breaks.begin();
109
110     if(a_recovering == beginning)
111       break;
112
113     now = functions::millisecond();
114   }
115
116   a_isRunning = (a_breaks.size() > 0);
117   a_nextTime = now + a_communicator.getRecoveryTime();
118   LOGDEBUG(
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);
124   );
125 }
126
127 /*
128 bool comm::ConnectionRecover::contains (comm::Server* server) const
129    throw ()
130 {
131    Guard guard (a_mutex, "comm::ConnectionRecover::contains");
132    return std::find (a_breaks.begin (), a_breaks.end (), server) != a_breaks.end ();
133 }
134
135 void comm::ConnectionRecover::erase (comm::Server* server)
136    throw ()
137 {
138    Guard guard (a_mutex, "comm::ConnectionRecover::erase");
139
140    vector <Server*>::iterator ii = find (a_breaks.begin (), a_breaks.end (), server);
141
142    if (ii == a_breaks.end ())
143       return;
144
145    if (ii == a_recovering)
146       if ((a_recovering = a_breaks.erase (ii)) == a_breaks.end ())
147          a_recovering = a_breaks.begin ();
148
149    a_isRunning = (a_breaks.size () > 0);
150 }
151 */
152
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));
157
158   for(std::vector <Server*>::const_iterator ii = a_breaks.begin(), maxii = a_breaks.end(); ii != maxii; ii ++)
159     (*ii)->asXML(connectionRecover);
160
161   return connectionRecover;
162 }
163
164
165