Updated license
[anna.git] / source / diameter.comm / Timer.cpp
1 // ANNA - Anna is Not Nothingness 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 // Local
38 #include <anna/diameter.comm/Timer.hpp>
39 #include <anna/diameter.comm/Response.hpp>
40 #include <anna/diameter.comm/Session.hpp>
41 #include <anna/diameter.comm/LocalServer.hpp>
42
43 #include <anna/core/tracing/Logger.hpp>
44
45
46 using namespace std;
47 using namespace anna;
48
49 void diameter::comm::Timer::expire(anna::timex::Engine*)
50 throw(anna::RuntimeException) {
51   Response* response = NULL;
52   Session* session = NULL;
53   LocalServer* localServer = NULL;
54
55   switch(getType()) {
56   case Type::ResponseExpiration:
57     response = getResponse();
58     response->setResultCode(Response::ResultCode::Timeout);
59     session = response->getSession();
60     LOGDEBUG(
61       string msg("anna::diameter::comm::Timer::expire | Response: ");
62       msg += response->asString();
63       anna::Logger::debug(msg, ANNA_FILE_LOCATION);
64     );
65     // action:
66     session->expireResponse(response);
67     break;
68   case Type::SessionUnbind:
69     session = getSession();
70
71     if(session) {
72       LOGDEBUG(
73         string msg("anna::diameter::comm::Timer::expire | Session (ending): ");
74         msg += session->asString();
75         anna::Logger::debug(msg, ANNA_FILE_LOCATION);
76       );
77       // action:
78       //session->setOnDisconnect(Session::OnDisconnect::IgnorePendings);
79       session->unbind(true /* immediate */);
80     } else {
81       // La client-session desaparecio (hice un closeSession)
82       LOGDEBUG(anna::Logger::debug("anna::diameter::comm::Timer::expire | Session missing", ANNA_FILE_LOCATION));
83     }
84
85     break;
86   case Type::SessionRecover:
87     session = getSession();
88
89     if(session) {
90       LOGDEBUG(
91         string msg("anna::diameter::comm::Timer::expire | Session (recovering): ");
92         msg += session->asString();
93         anna::Logger::debug(msg, ANNA_FILE_LOCATION);
94       );
95       // action:
96       session->setState(Session::State::Bound);
97     } else {
98       // La client-session desaparecio (hice un closeSession)
99       LOGDEBUG(anna::Logger::debug("anna::diameter::comm::Timer::expire | Session missing", ANNA_FILE_LOCATION));
100     }
101
102     break;
103   case Type::LocalServerAttach:
104     localServer = getLocalServer();
105
106     if(localServer) {
107       LOGDEBUG(
108         string msg("anna::diameter::comm::Timer::expire | LocalServer (attach retry)");
109         //msg += localServer->asString();
110         anna::Logger::debug(msg, ANNA_FILE_LOCATION);
111       );
112       // action:
113       localServer->attach();
114     } else {
115       // LocalServer desaparecio (hice un closeLocalServer)
116       LOGDEBUG(anna::Logger::debug("anna::diameter::comm::Timer::expire | LocalServer missing", ANNA_FILE_LOCATION));
117     }
118
119     break;
120   }
121 }
122
123 const char* diameter::comm::Timer::asText(const Type::_v type)
124 throw() {
125   static const char* text [] = { "ResponseExpiration", "SessionUnbind", "SessionRecover", "LocalServerAttach" };
126   return text [type];
127 }
128
129 string diameter::comm::Timer::asString() const
130 throw() {
131   string result("anna::diameter::comm::Timer { ");
132   result += anna::timex::Transaction::asString();
133   result += " Type: ";
134   result += asText(a_type);
135
136   switch(getType()) {
137   case Type::ResponseExpiration: {
138     const Response* response = getResponse();
139
140     if(response != NULL) {
141       result += " | ";
142       result += response->asString();
143     } else
144       result += " | Response: <null>";
145
146     break;
147   }
148   case Type::SessionUnbind:
149   case Type::SessionRecover: {
150     const Session *session = getSession();
151
152     if(session != NULL) {
153       result += " | ";
154       result += session->asString();
155     } else
156       result += " | Session: <null>"; // puede ocurrir ??
157
158     break;
159   }
160   case Type::LocalServerAttach: {
161     const LocalServer *localServer = getLocalServer();
162
163     if(localServer != NULL) {
164       result += " | ";
165       result += localServer->asString();
166     } else
167       result += " | LocalServer: <null>"; // puede ocurrir ??
168
169     break;
170   }
171   }
172
173   return result += " }";
174 }
175