Revert "Remove mysql and oracle resources for anna-ericsson project"
[anna.git] / example / diameter / launcher / MyLocalServer.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/codec/Engine.hpp>
14 #include <anna/diameter.comm/Response.hpp>
15 #include <anna/diameter.comm/ClientSession.hpp>
16 #include <anna/diameter.comm/Server.hpp>
17 #include <anna/diameter.comm/OriginHost.hpp>
18
19 // Process
20 #include <MyLocalServer.hpp>
21 #include <MyDiameterEngine.hpp>
22 #include <MyDiameterEntity.hpp>
23 #include <Launcher.hpp>
24 #include <anna/testing/TestManager.hpp>
25
26
27 void MyLocalServer::eventRequest(anna::diameter::comm::ServerSession *serverSession, const anna::DataBlock &message)
28 throw(anna::RuntimeException) {
29   LOGMETHOD(anna::TraceMethod tm("launcher::MyLocalServer", "eventRequest", ANNA_FILE_LOCATION));
30   // Performance stats:
31   Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
32   anna::diameter::comm::OriginHost *my_node = my_app.getOriginHost(getEngine()->getOriginHostName());
33   anna::diameter::codec::Engine *codecEngine = my_node->getCodecEngine();
34
35   // CommandId:
36   anna::diameter::CommandId cid = anna::diameter::codec::functions::getCommandId(message);
37   LOGDEBUG
38   (
39     std::string msg = "Request received: ";
40     msg += anna::diameter::functions::commandIdAsPairString(cid);
41     msg += " | DiameterServer: ";
42     msg += anna::functions::socketLiteralAsString(serverSession->getAddress(), serverSession->getPort());
43     msg += " | EventTime: ";
44     msg += anna::time::functions::currentTimeAsString();
45     anna::Logger::debug(msg, ANNA_FILE_LOCATION);
46   );
47
48   // Write reception
49   if(my_node->logEnabled()) my_node->writeLogFile(message, "recvfc", serverSession->asString());
50
51   // If no answer is programmed and entity is configured, the failed request would be forwarded even being wrong (delegates at the end point)
52   int code = cid.first;
53   anna::diameter::codec::Message *programmed_answer = a_reactingAnswers.getMessage(code);
54   bool programmed = (programmed_answer != NULL);
55
56   anna::diameter::comm::Entity *entity = my_node->getEntity();
57   if(!programmed && entity) {  // forward condition (no programmed answer + entity available)
58     anna::diameter::comm::Message *msg = my_node->createCommMessage();
59     msg->forwardEndToEnd(); // end-to-end will be kept
60     msg->setBody(message);
61     msg->setRequestServerSessionKey(serverSession->getKey());
62     bool success = entity->send(msg);
63
64     // Detailed log:
65     if(my_node->logEnabled()) {
66       anna::diameter::comm::Server *usedServer = entity->getLastUsedResource();
67       anna::diameter::comm::ClientSession *usedClientSession = usedServer ? usedServer->getLastUsedResource() : NULL;
68       std::string detail = usedClientSession ? usedClientSession->asString() : "<null client session>"; // esto no deberia ocurrir
69       my_node->writeLogFile(message, (success ? "fwd2e" : "fwd2eError"), detail); // forwarded
70     }
71
72
73     // Testing:
74     anna::testing::TestManager::instantiate().receiveDiameterMessage(message, serverSession);
75
76     return;
77   }
78
79   // Error analisys:
80   bool analysisOK = true; // by default
81   anna::diameter::codec::Message *answer_message = NULL;
82   anna::diameter::codec::Message codecMsg;
83   anna::diameter::codec::Message codecAnsMsg;
84
85   CommandLine& cl(anna::CommandLine::instantiate());
86   if(!cl.exists("ignoreErrors")) {  // Error analysis
87     answer_message = (anna::diameter::codec::Message *)&codecAnsMsg;
88     answer_message->clear();
89
90     // Decode
91     try { codecMsg.decode(message, answer_message); } catch(anna::RuntimeException &ex) { ex.trace(); }
92
93     answer_message->setStandardToAnswer(codecMsg, my_node->getCommEngine()->getOriginHostName(), my_node->getCommEngine()->getOriginRealmName());
94     analysisOK = (answer_message->getResultCode() == anna::diameter::helpers::base::AVPVALUES__Result_Code::DIAMETER_SUCCESS);
95   }
96
97   // Programmed answer only when all is ok
98   if(analysisOK) {
99     if(programmed) {
100       answer_message = programmed_answer;
101       // Prepare answer:
102       my_app.getCommunicator()->prepareAnswer(answer_message, message);
103     } else {
104
105       // Testing:
106       anna::testing::TestManager::instantiate().receiveDiameterMessage(message, serverSession);
107
108       return; // nothing done
109     }
110   }
111
112   anna::diameter::codec::Engine::ValidationMode::_v backupVM = codecEngine->getValidationMode();
113
114   if(!analysisOK)
115     codecEngine->setValidationMode(anna::diameter::codec::Engine::ValidationMode::Never);
116
117   anna::diameter::comm::Message *msg;
118   try {
119     msg = my_node->createCommMessage();
120     msg->setBody(answer_message->code());
121     /* response = NULL =*/serverSession->send(msg);
122
123     if(my_node->logEnabled()) my_node->writeLogFile(*answer_message, "sent2c", serverSession->asString());
124   } catch(anna::RuntimeException &ex) {
125     ex.trace();
126     if(my_node->logEnabled()) my_node->writeLogFile(*answer_message, "send2cError", serverSession->asString());
127   }
128
129   // release msg
130   my_node->releaseCommMessage(msg);
131
132   // Restore validation mode
133   codecEngine->setValidationMode(backupVM);
134
135   // Pop front the reacting answer:
136   if(analysisOK && programmed) a_reactingAnswers.nextMessage(code);
137
138   // Testing:
139   anna::testing::TestManager::instantiate().receiveDiameterMessage(message, serverSession);
140 }
141
142 void MyLocalServer::eventResponse(const anna::diameter::comm::Response &response)
143 throw(anna::RuntimeException) {
144   LOGMETHOD(anna::TraceMethod tm("launcher::MyLocalServer", "eventResponse", ANNA_FILE_LOCATION));
145   Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
146   anna::diameter::comm::OriginHost * my_node = my_app.getOriginHost(getEngine()->getOriginHostName());
147   anna::diameter::comm::ClassCode::_v code = response.getClassCode();
148   anna::diameter::comm::Response::ResultCode::_v result = response.getResultCode();
149   anna::diameter::comm::Message* request = const_cast<anna::diameter::comm::Message*>(response.getRequest());
150   const anna::DataBlock* message = response.getMessage();
151   const anna::diameter::comm::ServerSession *serverSession = static_cast<const anna::diameter::comm::ServerSession *>(response.getSession());
152   bool contextExpired = (result == anna::diameter::comm::Response::ResultCode::Timeout);
153   bool isUnavailable = (result == anna::diameter::comm::Response::ResultCode::DiameterUnavailable);
154   bool isOK = (result == anna::diameter::comm::Response::ResultCode::Success);
155
156   // CommandId:
157   anna::diameter::CommandId request_cid = request->getCommandId();
158   LOGDEBUG
159   (
160     std::string msg = "Response received for original diameter request: ";
161     msg += anna::diameter::functions::commandIdAsPairString(request_cid);
162     msg += " | Response: ";
163     msg += response.asString();
164     msg += " | LocalServer: ";
165     msg += anna::functions::socketLiteralAsString(serverSession->getAddress(), serverSession->getPort());
166     msg += " | EventTime: ";
167     msg += anna::time::functions::currentTimeAsString();
168     anna::Logger::debug(msg, ANNA_FILE_LOCATION);
169   );
170
171   if(isUnavailable) {
172     //if (isApplicationMessage)
173     LOGWARNING(anna::Logger::warning("Diameter client unavailable for Diameter Request", ANNA_FILE_LOCATION));
174   }
175
176   if(contextExpired) {
177     //if (isApplicationMessage)
178     LOGWARNING(anna::Logger::warning("Context Expired for Diameter Request which was sent to the client", ANNA_FILE_LOCATION));
179
180     if(request_cid != anna::diameter::helpers::base::COMMANDID__Capabilities_Exchange_Request) {  // don't trace CEA
181       if(my_node->logEnabled()) my_node->writeLogFile(*request, "req2c-expired", serverSession->asString());
182     }
183   }
184
185   if(isOK) {
186     LOGDEBUG(
187       std::string msg = "Received response for diameter message:  ";
188       msg += anna::diameter::functions::commandIdAsPairString(request_cid);
189       anna::Logger::debug(msg, ANNA_FILE_LOCATION);
190     );
191
192     // Write reception
193     if(my_node->logEnabled()) my_node->writeLogFile(*message, "recvfc", serverSession->asString());
194
195     // This is not very usual, but answers could arrive from clients:
196     anna::diameter::comm::Entity *entity = my_node->getEntity();
197
198     if(entity) {
199       anna::diameter::comm::ClientSession *usedClientSession = my_node->getCommEngine()->findClientSession(request->getRequestClientSessionKey());
200       std::string detail;
201
202       if(my_node->logEnabled()) detail = usedClientSession ? usedClientSession->asString() : "<null client session>";  // this should not happen
203       anna::diameter::comm::Message *msg;
204
205       try {
206         msg = my_node->createCommMessage();
207         msg->forwardEndToEnd(); // end-to-end will be kept
208         msg->setBody(*message);
209
210         // Metodo 1:
211         if(usedClientSession) /* response = NULL =*/usedClientSession->send(msg);
212
213         // Metodo 2:
214         //msg->setRequestClientSessionKey(request->getRequestClientSessionKey());
215         //bool success = entity->send(msg);
216
217         if(my_node->logEnabled()) my_node->writeLogFile(*message, "fwd2e", detail);  // forwarded
218       } catch(anna::RuntimeException &ex) {
219         ex.trace();
220         if(my_node->logEnabled()) my_node->writeLogFile(*message, "fwd2eError", detail);  // forwarded
221       }
222
223       // release msgs
224       my_node->releaseCommMessage(msg);
225       my_node->releaseCommMessage(request);
226     }
227   }
228
229   // Testing:
230   if(isOK) anna::testing::TestManager::instantiate().receiveDiameterMessage(*message, serverSession);
231 }
232
233 void MyLocalServer::eventUnknownResponse(anna::diameter::comm::ServerSession *serverSession, const anna::DataBlock &message)
234 throw(anna::RuntimeException) {
235   LOGMETHOD(anna::TraceMethod tm("launcher::MyLocalServer", "eventUnknownResponse", ANNA_FILE_LOCATION));
236   // Performance stats:
237   Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
238   anna::diameter::comm::OriginHost *my_node = my_app.getOriginHost(getEngine()->getOriginHostName());
239   // CommandId:
240   anna::diameter::CommandId cid = anna::diameter::codec::functions::getCommandId(message);
241   LOGDEBUG
242   (
243     std::string msg = "Out-of-context response received from client: ";
244     msg += anna::diameter::functions::commandIdAsPairString(cid);
245     msg += " | DiameterServer: ";
246     msg += anna::functions::socketLiteralAsString(serverSession->getAddress(), serverSession->getPort());
247     msg += " | EventTime: ";
248     msg += anna::time::functions::currentTimeAsString();
249     anna::Logger::debug(msg, ANNA_FILE_LOCATION);
250   );
251
252   if(my_node->logEnabled()) my_node->writeLogFile(message, "recvfc-ans-unknown", serverSession->asString());
253 }
254
255 void MyLocalServer::eventDPA(anna::diameter::comm::ServerSession *serverSession, const anna::DataBlock &message)
256 throw(anna::RuntimeException) {
257   LOGMETHOD(anna::TraceMethod tm("launcher::MyLocalServer", "eventDPA", ANNA_FILE_LOCATION));
258   // Performance stats:
259   Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
260   anna::diameter::comm::OriginHost *my_node = my_app.getOriginHost(getEngine()->getOriginHostName());
261   // CommandId:
262   anna::diameter::CommandId cid = anna::diameter::codec::functions::getCommandId(message);
263   LOGDEBUG
264   (
265     std::string msg = "Disconnect-Peer-Answer response received from client: ";
266     msg += anna::diameter::functions::commandIdAsPairString(cid);
267     msg += " | DiameterServer: ";
268     msg += anna::functions::socketLiteralAsString(serverSession->getAddress(), serverSession->getPort());
269     msg += " | EventTime: ";
270     msg += anna::time::functions::currentTimeAsString();
271     anna::Logger::debug(msg, ANNA_FILE_LOCATION);
272   );
273
274   // Write reception
275   if(my_node->logEnabled()) my_node->writeLogFile(message, "recvfc", serverSession->asString());
276
277   // Testing:
278   anna::testing::TestManager::instantiate().receiveDiameterMessage(message, serverSession);
279 }