+// ANNA - Anna is Not Nothingness Anymore //
+// //
+// (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo //
+// //
+// See project site at http://redmine.teslayout.com/projects/anna-suite //
+// See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
+
+
+// Project
+#include <anna/core/core.hpp>
+#include <anna/diameter/functions.hpp>
+#include <anna/time/functions.hpp>
+#include <anna/diameter.comm/Response.hpp>
+
+// Process
+#include "MyDiameterEntity.hpp"
+#include "Launcher.hpp"
+
+
+void MyDiameterEntity::eventRequest(anna::diameter::comm::ClientSession *clientSession, const anna::DataBlock &message)
+throw(anna::RuntimeException) {
+ LOGMETHOD(anna::TraceMethod tm("launcher::MyDiameterEntity", "eventRequest", ANNA_FILE_LOCATION));
+ // Performance stats:
+ Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
+ CommandLine& cl(anna::CommandLine::instantiate());
+ // CommandId:
+ anna::diameter::CommandId cid = anna::diameter::codec::functions::getCommandId(message);
+ LOGDEBUG
+ (
+ std::string msg = "Request received: ";
+ msg += anna::diameter::functions::commandIdAsPairString(cid);
+ msg += " | DiameterServer: ";
+ msg += anna::functions::socketLiteralAsString(clientSession->getAddress(), clientSession->getPort());
+ msg += " | EventTime: ";
+ msg += anna::time::functions::currentTimeAsString();
+ anna::Logger::debug(msg, ANNA_FILE_LOCATION);
+ );
+
+ // Write reception
+ if(my_app.logEnabled()) my_app.writeLogFile(message, "recvfe", clientSession->asString());
+
+ // Lookup reacting answers list:
+ int code = cid.first;
+ anna::diameter::codec::Message *answer_message = a_reactingAnswers.getMessage(code);
+ if (answer_message) {
+ // Prepare answer:
+ my_app.getCommunicator()->prepareAnswer(answer_message, message);
+
+ try {
+ anna::diameter::comm::Message *msg = my_app.createCommMessage();
+ msg->setBody(answer_message->code());
+ /* response = NULL =*/clientSession->send(msg);
+ my_app.releaseCommMessage(msg);
+
+ if(my_app.logEnabled()) my_app.writeLogFile(*answer_message, "sent2e", clientSession->asString());
+ } catch(anna::RuntimeException &ex) {
+ ex.trace();
+
+ if(my_app.logEnabled()) my_app.writeLogFile(*answer_message, "send2eError", clientSession->asString());
+ }
+
+ // Pop front the reacting answer:
+ a_reactingAnswers.nextMessage(code);
+ return;
+ }
+
+ LOGDEBUG
+ (
+ std::string msg = "No answers programmed (maybe sold out) for request coming from entity: ";
+ msg += anna::diameter::functions::commandIdAsPairString(cid);
+ anna::Logger::debug(msg, ANNA_FILE_LOCATION);
+ );
+
+ // not found: forward to client (if exists)
+ // Forward to client:
+ anna::diameter::comm::LocalServer *localServer = my_app.getDiameterLocalServer();
+
+ if(localServer && (cid != anna::diameter::helpers::base::COMMANDID__Capabilities_Exchange_Request) /* don't forward CER */) {
+ try {
+ anna::diameter::comm::Message *msg = my_app.createCommMessage();
+ msg->updateEndToEnd(false); // end-to-end will be kept
+ msg->setBody(message);
+ msg->setRequestClientSessionKey(clientSession->getKey());
+ bool success = localServer->send(msg);
+
+ // Detailed log:
+ if(my_app.logEnabled()) {
+ anna::diameter::comm::ServerSession *usedServerSession = localServer->getLastUsedResource();
+ std::string detail = usedServerSession ? usedServerSession->asString() : "<null server session>"; // esto no deberia ocurrir
+ my_app.writeLogFile(message, (success ? "fwd2c" : "fwd2cError"), detail);
+ }
+ } catch(anna::RuntimeException &ex) {
+ ex.trace();
+ }
+ }
+}
+
+void MyDiameterEntity::eventResponse(const anna::diameter::comm::Response &response)
+throw(anna::RuntimeException) {
+ LOGMETHOD(anna::TraceMethod tm("launcher::MyDiameterEntity", "eventResponse", ANNA_FILE_LOCATION));
+ Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
+ CommandLine& cl(anna::CommandLine::instantiate());
+ anna::diameter::comm::ClassCode::_v code = response.getClassCode();
+ anna::diameter::comm::Response::ResultCode::_v result = response.getResultCode();
+ anna::diameter::comm::Message* request = const_cast<anna::diameter::comm::Message*>(response.getRequest());
+ const anna::DataBlock* message = response.getMessage();
+ const anna::diameter::comm::ClientSession *clientSession = static_cast<const anna::diameter::comm::ClientSession *>(response.getSession());
+ bool isBindResponse = (code == anna::diameter::comm::ClassCode::Bind);
+ bool isApplicationMessage = (code == anna::diameter::comm::ClassCode::ApplicationMessage);
+ bool contextExpired = (result == anna::diameter::comm::Response::ResultCode::Timeout);
+ bool isUnavailable = (result == anna::diameter::comm::Response::ResultCode::DiameterUnavailable);
+ bool isOK = (result == anna::diameter::comm::Response::ResultCode::Success);
+ // CommandId:
+ anna::diameter::CommandId request_cid = request->getCommandId();
+ LOGDEBUG
+ (
+ std::string msg = "Response received for original diameter request: ";
+ msg += anna::diameter::functions::commandIdAsPairString(request_cid);
+ msg += " | Response: ";
+ msg += response.asString();
+ msg += " | DiameterServer: ";
+ msg += anna::functions::socketLiteralAsString(clientSession->getAddress(), clientSession->getPort());
+ msg += " | EventTime: ";
+ msg += anna::time::functions::currentTimeAsString();
+ anna::Logger::debug(msg, ANNA_FILE_LOCATION);
+ );
+
+ if(isUnavailable) {
+ //if (isApplicationMessage)
+ LOGWARNING(anna::Logger::warning("Diameter entity unavailable for Diameter Request", ANNA_FILE_LOCATION));
+ }
+
+ if(contextExpired) {
+ //if (isApplicationMessage)
+ LOGWARNING(anna::Logger::warning("Context Expired for Diameter Request which was sent to the entity", ANNA_FILE_LOCATION));
+
+ if(request_cid != anna::diameter::helpers::base::COMMANDID__Capabilities_Exchange_Request) { // don't trace CEA
+ if(my_app.logEnabled()) my_app.writeLogFile(*request, "req2e-expired", clientSession->asString());
+ }
+ }
+
+ if(isOK) {
+ LOGDEBUG(
+ std::string msg = "Received response for diameter message: ";
+ msg += anna::diameter::functions::commandIdAsPairString(request_cid);
+ anna::Logger::debug(msg, ANNA_FILE_LOCATION);
+ );
+
+ // Write reception
+ if(request_cid != anna::diameter::helpers::base::COMMANDID__Capabilities_Exchange_Request) { // don't trace CEA
+ if(my_app.logEnabled()) {
+ my_app.writeLogFile(*message, "recvfe", clientSession->asString());
+ }
+ }
+
+ // Forward to client:
+ anna::diameter::comm::LocalServer *localServer = my_app.getDiameterLocalServer();
+
+ if(localServer && (request_cid != anna::diameter::helpers::base::COMMANDID__Capabilities_Exchange_Request) /* don't forward CEA */) {
+ try {
+ anna::diameter::comm::Message *msg = my_app.createCommMessage();
+ msg->updateEndToEnd(false); // end-to-end will be kept
+ msg->setBody(*message);
+ bool success = localServer->send(msg, request->getRequestServerSessionKey());
+ my_app.releaseCommMessage(msg);
+ my_app.releaseCommMessage(request);
+
+ // Detailed log:
+ anna::diameter::comm::ServerSession *usedServerSession = my_app.getMyDiameterEngine()->findServerSession(request->getRequestServerSessionKey());
+ std::string detail = usedServerSession ? usedServerSession->asString() : "<null server session>"; // esto no deberia ocurrir
+
+ if(my_app.logEnabled()) {
+ my_app.writeLogFile(*message, (success ? "fwd2c" : "fwd2cError"), detail);
+ }
+ } catch(anna::RuntimeException &ex) {
+ ex.trace();
+ }
+ }
+ }
+
+ // Triggering burst:
+ if(isOK || contextExpired) my_app.sendBurstMessage();
+}
+
+void MyDiameterEntity::eventUnknownResponse(anna::diameter::comm::ClientSession *clientSession, const anna::DataBlock &message)
+throw(anna::RuntimeException) {
+ LOGMETHOD(anna::TraceMethod tm("launcher::MyDiameterEntity", "eventUnknownResponse", ANNA_FILE_LOCATION));
+ // Performance stats:
+ Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
+ // CommandId:
+ anna::diameter::CommandId cid = anna::diameter::codec::functions::getCommandId(message);
+ LOGDEBUG
+ (
+ std::string msg = "Out-of-context response received from entity: ";
+ msg += anna::diameter::functions::commandIdAsPairString(cid);
+ msg += " | DiameterServer: ";
+ msg += anna::functions::socketLiteralAsString(clientSession->getAddress(), clientSession->getPort());
+ msg += " | EventTime: ";
+ msg += anna::time::functions::currentTimeAsString();
+ anna::Logger::debug(msg, ANNA_FILE_LOCATION);
+ );
+
+ // Write reception
+ if(my_app.logEnabled()) my_app.writeLogFile(message, "recvfe-ans-unknown", clientSession->asString());
+}
+
+void MyDiameterEntity::eventDPA(anna::diameter::comm::ClientSession *clientSession, const anna::DataBlock &message)
+throw(anna::RuntimeException) {
+ LOGMETHOD(anna::TraceMethod tm("launcher::MyDiameterEntity", "eventDPA", ANNA_FILE_LOCATION));
+ // Performance stats:
+ Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
+ // CommandId:
+ anna::diameter::CommandId cid = anna::diameter::codec::functions::getCommandId(message);
+ LOGDEBUG
+ (
+ std::string msg = "Disconnect-Peer-Answer received from entity: ";
+ msg += anna::diameter::functions::commandIdAsPairString(cid);
+ msg += " | DiameterServer: ";
+ msg += anna::functions::socketLiteralAsString(clientSession->getAddress(), clientSession->getPort());
+ msg += " | EventTime: ";
+ msg += anna::time::functions::currentTimeAsString();
+ anna::Logger::debug(msg, ANNA_FILE_LOCATION);
+ );
+
+ // Write reception
+ if(my_app.logEnabled()) my_app.writeLogFile(message, "recvfe", clientSession->asString());
+}