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