Remove global variables. Factory for comm/codec messages in application class (Launcher)
[anna.git] / example / diameter / launcher / MyDiameterEntity.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 // Project
10 #include <anna/core/core.hpp>
11 #include <anna/diameter/functions.hpp>
12 #include <anna/time/functions.hpp>
13 #include <anna/diameter.comm/Response.hpp>
14
15 // Process
16 #include "MyDiameterEntity.hpp"
17 #include "Launcher.hpp"
18
19
20 void MyDiameterEntity::eventRequest(anna::diameter::comm::ClientSession *clientSession, const anna::DataBlock &message)
21 throw(anna::RuntimeException) {
22   LOGMETHOD(anna::TraceMethod tm("launcher::MyDiameterEntity", "eventRequest", ANNA_FILE_LOCATION));
23   // Performance stats:
24   Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
25   CommandLine& cl(anna::CommandLine::instantiate());
26   // CommandId:
27   anna::diameter::CommandId cid = anna::diameter::codec::functions::getCommandId(message);
28   LOGDEBUG
29   (
30     std::string msg = "Request received: ";
31     msg += anna::diameter::functions::commandIdAsPairString(cid);
32     msg += " | DiameterServer: ";
33     msg += anna::functions::socketLiteralAsString(clientSession->getAddress(), clientSession->getPort());
34     msg += " | EventTime: ";
35     msg += anna::time::functions::currentTimeAsString();
36     anna::Logger::debug(msg, ANNA_FILE_LOCATION);
37   );
38
39   // Write reception
40   if(my_app.logEnabled()) my_app.writeLogFile(message, "recvfe", clientSession->asString());
41
42   // Lookup reacting answers list:
43   int code = cid.first;
44   anna::diameter::codec::Message *answer_message = a_reactingAnswers.getMessage(code);
45   if (answer_message) {
46     // Prepare answer:
47     my_app.getCommunicator()->prepareAnswer(answer_message, message);
48
49     try {
50       anna::diameter::comm::Message *msg = my_app.createCommMessage();
51       msg->setBody(answer_message->code());
52       /* response = NULL =*/clientSession->send(msg);
53       my_app.releaseCommMessage(msg);
54
55       if(my_app.logEnabled()) my_app.writeLogFile(*answer_message, "sent2e", clientSession->asString());
56     } catch(anna::RuntimeException &ex) {
57       ex.trace();
58
59       if(my_app.logEnabled()) my_app.writeLogFile(*answer_message, "send2eError", clientSession->asString());
60     }
61
62     // Pop front the reacting answer:
63     a_reactingAnswers.nextMessage(code);
64     return;
65   }
66
67   LOGDEBUG
68   (
69     std::string msg = "No answers programmed (maybe sold out) for request coming from entity: ";
70     msg += anna::diameter::functions::commandIdAsPairString(cid);
71     anna::Logger::debug(msg, ANNA_FILE_LOCATION);
72   );
73
74   // not found: forward to client (if exists)
75   // Forward to client:
76   anna::diameter::comm::LocalServer *localServer = my_app.getDiameterLocalServer();
77
78   if(localServer && (cid != anna::diameter::helpers::base::COMMANDID__Capabilities_Exchange_Request) /* don't forward CER */) {
79     try {
80       anna::diameter::comm::Message *msg = my_app.createCommMessage();
81       msg->updateEndToEnd(false); // end-to-end will be kept
82       msg->setBody(message);
83       msg->setRequestClientSessionKey(clientSession->getKey());
84       bool success = localServer->send(msg);
85
86       // Detailed log:
87       if(my_app.logEnabled()) {
88         anna::diameter::comm::ServerSession *usedServerSession = localServer->getLastUsedResource();
89         std::string detail = usedServerSession ? usedServerSession->asString() : "<null server session>"; // esto no deberia ocurrir
90         my_app.writeLogFile(message, (success ? "fwd2c" : "fwd2cError"), detail);
91       }
92     } catch(anna::RuntimeException &ex) {
93       ex.trace();
94     }
95   }
96 }
97
98 void MyDiameterEntity::eventResponse(const anna::diameter::comm::Response &response)
99 throw(anna::RuntimeException) {
100   LOGMETHOD(anna::TraceMethod tm("launcher::MyDiameterEntity", "eventResponse", ANNA_FILE_LOCATION));
101   Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
102   CommandLine& cl(anna::CommandLine::instantiate());
103   anna::diameter::comm::ClassCode::_v code = response.getClassCode();
104   anna::diameter::comm::Response::ResultCode::_v result = response.getResultCode();
105   anna::diameter::comm::Message* request = const_cast<anna::diameter::comm::Message*>(response.getRequest());
106   const anna::DataBlock* message = response.getMessage();
107   const anna::diameter::comm::ClientSession *clientSession = static_cast<const anna::diameter::comm::ClientSession *>(response.getSession());
108   bool isBindResponse = (code == anna::diameter::comm::ClassCode::Bind);
109   bool isApplicationMessage = (code == anna::diameter::comm::ClassCode::ApplicationMessage);
110   bool contextExpired = (result == anna::diameter::comm::Response::ResultCode::Timeout);
111   bool isUnavailable = (result == anna::diameter::comm::Response::ResultCode::DiameterUnavailable);
112   bool isOK = (result == anna::diameter::comm::Response::ResultCode::Success);
113   // CommandId:
114   anna::diameter::CommandId request_cid = request->getCommandId();
115   LOGDEBUG
116   (
117     std::string msg = "Response received for original diameter request: ";
118     msg += anna::diameter::functions::commandIdAsPairString(request_cid);
119     msg += " | Response: ";
120     msg += response.asString();
121     msg += " | DiameterServer: ";
122     msg += anna::functions::socketLiteralAsString(clientSession->getAddress(), clientSession->getPort());
123     msg += " | EventTime: ";
124     msg += anna::time::functions::currentTimeAsString();
125     anna::Logger::debug(msg, ANNA_FILE_LOCATION);
126   );
127
128   if(isUnavailable) {
129     //if (isApplicationMessage)
130     LOGWARNING(anna::Logger::warning("Diameter entity unavailable for Diameter Request", ANNA_FILE_LOCATION));
131   }
132
133   if(contextExpired) {
134     //if (isApplicationMessage)
135     LOGWARNING(anna::Logger::warning("Context Expired for Diameter Request which was sent to the entity", ANNA_FILE_LOCATION));
136
137     if(request_cid != anna::diameter::helpers::base::COMMANDID__Capabilities_Exchange_Request) {  // don't trace CEA
138       if(my_app.logEnabled()) my_app.writeLogFile(*request, "req2e-expired", clientSession->asString());
139     }
140   }
141
142   if(isOK) {
143     LOGDEBUG(
144       std::string msg = "Received response for diameter message:  ";
145       msg += anna::diameter::functions::commandIdAsPairString(request_cid);
146       anna::Logger::debug(msg, ANNA_FILE_LOCATION);
147     );
148     // Write reception
149     if(request_cid != anna::diameter::helpers::base::COMMANDID__Capabilities_Exchange_Request) {  // don't trace CEA
150       if(my_app.logEnabled()) {
151         my_app.writeLogFile(*message, "recvfe", clientSession->asString());
152       }
153     }
154
155     // Forward to client:
156     anna::diameter::comm::LocalServer *localServer = my_app.getDiameterLocalServer();
157
158     if(localServer && (request_cid != anna::diameter::helpers::base::COMMANDID__Capabilities_Exchange_Request) /* don't forward CEA */) {
159       try {
160         anna::diameter::comm::Message *msg = my_app.createCommMessage();
161         msg->updateEndToEnd(false); // end-to-end will be kept
162         msg->setBody(*message);
163         bool success = localServer->send(msg, request->getRequestServerSessionKey());
164         my_app.releaseCommMessage(msg);
165         my_app.releaseCommMessage(request);
166
167         // Detailed log:
168         anna::diameter::comm::ServerSession *usedServerSession = my_app.getMyDiameterEngine()->findServerSession(request->getRequestServerSessionKey());
169         std::string detail = usedServerSession ? usedServerSession->asString() : "<null server session>"; // esto no deberia ocurrir
170
171         if(my_app.logEnabled()) {
172           my_app.writeLogFile(*message, (success ? "fwd2c" : "fwd2cError"), detail);
173         }
174       } catch(anna::RuntimeException &ex) {
175         ex.trace();
176       }
177     }
178   }
179
180   // Triggering burst:
181   if(isOK || contextExpired) my_app.sendBurstMessage();
182 }
183
184 void MyDiameterEntity::eventUnknownResponse(anna::diameter::comm::ClientSession *clientSession, const anna::DataBlock &message)
185 throw(anna::RuntimeException) {
186   LOGMETHOD(anna::TraceMethod tm("launcher::MyDiameterEntity", "eventUnknownResponse", ANNA_FILE_LOCATION));
187   // Performance stats:
188   Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
189   // CommandId:
190   anna::diameter::CommandId cid = anna::diameter::codec::functions::getCommandId(message);
191   LOGDEBUG
192   (
193     std::string msg = "Out-of-context response received from entity: ";
194     msg += anna::diameter::functions::commandIdAsPairString(cid);
195     msg += " | DiameterServer: ";
196     msg += anna::functions::socketLiteralAsString(clientSession->getAddress(), clientSession->getPort());
197     msg += " | EventTime: ";
198     msg += anna::time::functions::currentTimeAsString();
199     anna::Logger::debug(msg, ANNA_FILE_LOCATION);
200   );
201
202   // Write reception
203   if(my_app.logEnabled()) my_app.writeLogFile(message, "recvfe-ans-unknown", clientSession->asString());
204 }
205
206 void MyDiameterEntity::eventDPA(anna::diameter::comm::ClientSession *clientSession, const anna::DataBlock &message)
207 throw(anna::RuntimeException) {
208   LOGMETHOD(anna::TraceMethod tm("launcher::MyDiameterEntity", "eventDPA", ANNA_FILE_LOCATION));
209   // Performance stats:
210   Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
211   // CommandId:
212   anna::diameter::CommandId cid = anna::diameter::codec::functions::getCommandId(message);
213   LOGDEBUG
214   (
215     std::string msg = "Disconnect-Peer-Answer received from entity: ";
216     msg += anna::diameter::functions::commandIdAsPairString(cid);
217     msg += " | DiameterServer: ";
218     msg += anna::functions::socketLiteralAsString(clientSession->getAddress(), clientSession->getPort());
219     msg += " | EventTime: ";
220     msg += anna::time::functions::currentTimeAsString();
221     anna::Logger::debug(msg, ANNA_FILE_LOCATION);
222   );
223
224   // Write reception
225   if(my_app.logEnabled()) my_app.writeLogFile(message, "recvfe", clientSession->asString());
226 }