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"
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
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));
29 Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
30 CommandLine& cl(anna::CommandLine::instantiate());
32 anna::diameter::CommandId cid = anna::diameter::codec::functions::getCommandId(message);
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);
45 if(my_app.logEnabled()) my_app.writeLogFile(message, "recvfe", clientSession->asString());
47 // Lookup reacting answers list:
49 anna::diameter::codec::Message *answer_message = a_reactingAnswers.getMessage(code);
52 my_app.getCommunicator()->prepareAnswer(answer_message, message);
55 G_commMsgSent2e.setBody(answer_message->code());
56 /* response = NULL =*/clientSession->send(&G_commMsgSent2e);
58 if(my_app.logEnabled()) my_app.writeLogFile(*answer_message, "sent2e", clientSession->asString());
59 } catch(anna::RuntimeException &ex) {
62 if(my_app.logEnabled()) my_app.writeLogFile(*answer_message, "send2eError", clientSession->asString());
65 // Pop front the reacting answer:
66 a_reactingAnswers.nextMessage(code);
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);
77 // not found: forward to client (if exists)
79 anna::diameter::comm::LocalServer *localServer = my_app.getDiameterLocalServer();
81 if(localServer && (cid != anna::diameter::helpers::base::COMMANDID__Capabilities_Exchange_Request) /* don't forward CER */) {
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);
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);
95 } catch(anna::RuntimeException &ex) {
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);
117 anna::diameter::CommandId request_cid = request->getCommandId();
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);
132 //if (isApplicationMessage)
133 LOGWARNING(anna::Logger::warning("Diameter entity unavailable for Diameter Request", ANNA_FILE_LOCATION));
137 //if (isApplicationMessage)
138 LOGWARNING(anna::Logger::warning("Context Expired for Diameter Request which was sent to the entity", ANNA_FILE_LOCATION));
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());
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);
152 bool alreadyDecodedOnG_codecMsg = false;
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;
161 // Forward to client:
162 anna::diameter::comm::LocalServer *localServer = my_app.getDiameterLocalServer();
164 if(localServer && (request_cid != anna::diameter::helpers::base::COMMANDID__Capabilities_Exchange_Request) /* don't forward CEA */) {
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);
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
174 if(my_app.logEnabled()) {
175 if(alreadyDecodedOnG_codecMsg)
176 my_app.writeLogFile(G_codecMsg, (success ? "fwd2c" : "fwd2cError"), detail);
178 my_app.writeLogFile(*message, (success ? "fwd2c" : "fwd2cError"), detail);
180 } catch(anna::RuntimeException &ex) {
187 if(isOK || contextExpired) my_app.sendBurstMessage();
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());
196 anna::diameter::CommandId cid = anna::diameter::codec::functions::getCommandId(message);
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);
209 if(my_app.logEnabled()) my_app.writeLogFile(message, "recvfe-ans-unknown", clientSession->asString());
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());
218 anna::diameter::CommandId cid = anna::diameter::codec::functions::getCommandId(message);
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);
231 if(my_app.logEnabled()) my_app.writeLogFile(message, "recvfe", clientSession->asString());