1 // ANNA - Anna is Not Nothingness Anymore //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo //
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 //
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>
16 #include "MyDiameterEntity.hpp"
17 #include "Launcher.hpp"
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));
24 Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
25 CommandLine& cl(anna::CommandLine::instantiate());
27 anna::diameter::CommandId cid = anna::diameter::codec::functions::getCommandId(message);
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);
40 if(my_app.logEnabled()) my_app.writeLogFile(message, "recvfe", clientSession->asString());
42 // Lookup reacting answers list:
44 anna::diameter::codec::Message *answer_message = a_reactingAnswers.getMessage(code);
47 my_app.getCommunicator()->prepareAnswer(answer_message, message);
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);
55 if(my_app.logEnabled()) my_app.writeLogFile(*answer_message, "sent2e", clientSession->asString());
56 } catch(anna::RuntimeException &ex) {
59 if(my_app.logEnabled()) my_app.writeLogFile(*answer_message, "send2eError", clientSession->asString());
62 // Pop front the reacting answer:
63 a_reactingAnswers.nextMessage(code);
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);
74 // not found: forward to client (if exists)
76 anna::diameter::comm::LocalServer *localServer = my_app.getDiameterLocalServer();
78 if(localServer && (cid != anna::diameter::helpers::base::COMMANDID__Capabilities_Exchange_Request) /* don't forward CER */) {
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);
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);
92 } catch(anna::RuntimeException &ex) {
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);
114 anna::diameter::CommandId request_cid = request->getCommandId();
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);
129 //if (isApplicationMessage)
130 LOGWARNING(anna::Logger::warning("Diameter entity unavailable for Diameter Request", ANNA_FILE_LOCATION));
134 //if (isApplicationMessage)
135 LOGWARNING(anna::Logger::warning("Context Expired for Diameter Request which was sent to the entity", ANNA_FILE_LOCATION));
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());
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);
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());
155 // Forward to client:
156 anna::diameter::comm::LocalServer *localServer = my_app.getDiameterLocalServer();
158 if(localServer && (request_cid != anna::diameter::helpers::base::COMMANDID__Capabilities_Exchange_Request) /* don't forward CEA */) {
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);
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
171 if(my_app.logEnabled()) {
172 my_app.writeLogFile(*message, (success ? "fwd2c" : "fwd2cError"), detail);
174 } catch(anna::RuntimeException &ex) {
181 if(isOK || contextExpired) my_app.sendBurstMessage();
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());
190 anna::diameter::CommandId cid = anna::diameter::codec::functions::getCommandId(message);
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);
203 if(my_app.logEnabled()) my_app.writeLogFile(message, "recvfe-ans-unknown", clientSession->asString());
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());
212 anna::diameter::CommandId cid = anna::diameter::codec::functions::getCommandId(message);
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);
225 if(my_app.logEnabled()) my_app.writeLogFile(message, "recvfe", clientSession->asString());