App counters including message name and result code if proceed
[anna.git] / source / diameter.comm / Timer.cpp
1 // ANNA - Anna is Not Nothingness Anymore                                                         //
2 //                                                                                                //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo                         //
4 //                                                                                                //
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 //
7
8
9 // Local
10 #include <anna/diameter.comm/Timer.hpp>
11 #include <anna/diameter.comm/Response.hpp>
12 #include <anna/diameter.comm/Session.hpp>
13 #include <anna/diameter.comm/LocalServer.hpp>
14
15 #include <anna/core/tracing/Logger.hpp>
16
17
18 using namespace std;
19 using namespace anna;
20
21 void diameter::comm::Timer::expire(anna::timex::Engine*)
22 throw(anna::RuntimeException) {
23   Response* response = NULL;
24   Session* session = NULL;
25   LocalServer* localServer = NULL;
26
27   switch(getType()) {
28   case Type::ResponseExpiration:
29     response = getResponse();
30     response->setResultCode(Response::ResultCode::Timeout);
31     session = response->getSession();
32     LOGDEBUG(
33       string msg("anna::diameter::comm::Timer::expire | Response: ");
34       msg += response->asString();
35       anna::Logger::debug(msg, ANNA_FILE_LOCATION);
36     );
37     // action:
38     session->expireResponse(response);
39     break;
40   case Type::SessionUnbind:
41     session = getSession();
42
43     if(session) {
44       LOGDEBUG(
45         string msg("anna::diameter::comm::Timer::expire | Session (ending): ");
46         msg += session->asString();
47         anna::Logger::debug(msg, ANNA_FILE_LOCATION);
48       );
49       // action:
50       //session->setOnDisconnect(Session::OnDisconnect::IgnorePendings);
51       session->unbind(true /* immediate */);
52     } else {
53       // La client-session desaparecio (hice un closeSession)
54       LOGDEBUG(anna::Logger::debug("anna::diameter::comm::Timer::expire | Session missing", ANNA_FILE_LOCATION));
55     }
56
57     break;
58   case Type::SessionRecover:
59     session = getSession();
60
61     if(session) {
62       LOGDEBUG(
63         string msg("anna::diameter::comm::Timer::expire | Session (recovering): ");
64         msg += session->asString();
65         anna::Logger::debug(msg, ANNA_FILE_LOCATION);
66       );
67       // action:
68       session->setState(Session::State::Bound);
69     } else {
70       // La client-session desaparecio (hice un closeSession)
71       LOGDEBUG(anna::Logger::debug("anna::diameter::comm::Timer::expire | Session missing", ANNA_FILE_LOCATION));
72     }
73
74     break;
75   case Type::LocalServerAttach:
76     localServer = getLocalServer();
77
78     if(localServer) {
79       LOGDEBUG(
80         string msg("anna::diameter::comm::Timer::expire | LocalServer (attach retry)");
81         //msg += localServer->asString();
82         anna::Logger::debug(msg, ANNA_FILE_LOCATION);
83       );
84       // action:
85       localServer->attach();
86     } else {
87       // LocalServer desaparecio (hice un closeLocalServer)
88       LOGDEBUG(anna::Logger::debug("anna::diameter::comm::Timer::expire | LocalServer missing", ANNA_FILE_LOCATION));
89     }
90
91     break;
92   }
93 }
94
95 const char* diameter::comm::Timer::asText(const Type::_v type)
96 throw() {
97   static const char* text [] = { "ResponseExpiration", "SessionUnbind", "SessionRecover", "LocalServerAttach" };
98   return text [type];
99 }
100
101 string diameter::comm::Timer::asString() const
102 throw() {
103   string result("anna::diameter::comm::Timer { ");
104   result += anna::timex::Transaction::asString();
105   result += " Type: ";
106   result += asText(a_type);
107
108   switch(getType()) {
109   case Type::ResponseExpiration: {
110     const Response* response = getResponse();
111
112     if(response != NULL) {
113       result += " | ";
114       result += response->asString();
115     } else
116       result += " | Response: <null>";
117
118     break;
119   }
120   case Type::SessionUnbind:
121   case Type::SessionRecover: {
122     const Session *session = getSession();
123
124     if(session != NULL) {
125       result += " | ";
126       result += session->asString();
127     } else
128       result += " | Session: <null>"; // puede ocurrir ??
129
130     break;
131   }
132   case Type::LocalServerAttach: {
133     const LocalServer *localServer = getLocalServer();
134
135     if(localServer != NULL) {
136       result += " | ";
137       result += localServer->asString();
138     } else
139       result += " | LocalServer: <null>"; // puede ocurrir ??
140
141     break;
142   }
143   }
144
145   return result += " }";
146 }
147