Fix local server for multiple applications
[anna.git] / example / diameter / launcher / MyDiameterEntity.cpp
index 0535160..cba726a 100644 (file)
 #include <anna/diameter/functions.hpp>
 #include <anna/time/functions.hpp>
 #include <anna/diameter.comm/Response.hpp>
+#include <anna/diameter.comm/ClientSession.hpp>
+#include <anna/diameter.comm/OriginHost.hpp>
+#include <anna/diameter/helpers/base/functions.hpp>
+#include <anna/diameter/helpers/dcca/functions.hpp>
 
 // Process
-#include "MyDiameterEntity.hpp"
-#include "Launcher.hpp"
+#include <MyDiameterEngine.hpp>
+#include <MyDiameterEntity.hpp>
+#include <MyLocalServer.hpp>
+#include <Launcher.hpp>
+#include <anna/testing/TestManager.hpp>
 
 
-void MyDiameterEntity::eventRequest(anna::diameter::comm::ClientSession *clientSession, const anna::DataBlock &message)
-throw(anna::RuntimeException) {
+void MyDiameterEntity::eventRequestRetransmission(const anna::diameter::comm::ClientSession* clientSession, anna::diameter::comm::Message *request) {
+
+  LOGMETHOD(anna::TraceMethod tm("launcher::MyDiameterEntity", "eventRequestRetransmission", ANNA_FILE_LOCATION));
+
+  // Base class:
+  Entity::eventRequestRetransmission(clientSession, request); // warning trace
+
+  // Performance stats:
+  Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
+  anna::diameter::comm::OriginHost * my_node = my_app.getOriginHost(getEngine()->getOriginHostName());
+  // CommandId:
+  anna::diameter::CommandId cid = anna::diameter::codec::functions::getCommandId(request->getBody());
+  LOGDEBUG
+  (
+    std::string msg = "Request retransmitted: ";
+    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 retransmission
+  if(my_node->logEnabled()) my_node->writeLogFile(request->getBody(), "retry", clientSession->asString());
+}
+
+
+void MyDiameterEntity::eventRequest(anna::diameter::comm::ClientSession *clientSession, const anna::DataBlock &message, const anna::diameter::comm::OriginHost *myNode)
+noexcept(false) {
   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());
+  anna::diameter::comm::OriginHost * my_node = my_app.getOriginHost(getEngine()->getOriginHostName());
+
   // CommandId:
   anna::diameter::CommandId cid = anna::diameter::codec::functions::getCommandId(message);
   LOGDEBUG
@@ -37,30 +73,33 @@ throw(anna::RuntimeException) {
   );
 
   // Write reception
-  if(my_app.logEnabled()) my_app.writeLogFile(message, "recvfe", clientSession->asString());
+  if(my_node->logEnabled()) my_node->writeLogFile(message, "recvfe", clientSession->asString());
 
   // Lookup reacting answers list:
   int code = cid.first;
-  anna::diameter::codec::Message *answer_message = a_reactingAnswers.getMessage(code);
+  anna::diameter::codec::Message *answer_message = my_node->getReactingAnswers()->getMessage(code);
   if (answer_message) {
     // Prepare answer:
     my_app.getCommunicator()->prepareAnswer(answer_message, message);
+    anna::diameter::comm::Message *msg;
 
     try {
-      anna::diameter::comm::Message *msg = my_app.createCommMessage();
+      msg = my_node->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());
+      if(my_node->logEnabled()) my_node->writeLogFile(*answer_message, "sent2e", clientSession->asString());
     } catch(anna::RuntimeException &ex) {
       ex.trace();
 
-      if(my_app.logEnabled()) my_app.writeLogFile(*answer_message, "send2eError", clientSession->asString());
+      if(my_node->logEnabled()) my_node->writeLogFile(*answer_message, "send2eError", clientSession->asString());
     }
 
+    // release msg
+    my_node->releaseCommMessage(msg);
+
     // Pop front the reacting answer:
-    a_reactingAnswers.nextMessage(code);
+    my_node->getReactingAnswers()->nextMessage(code);
     return;
   }
 
@@ -73,43 +112,45 @@ throw(anna::RuntimeException) {
 
   // not found: forward to client (if exists)
   // Forward to client:
-  anna::diameter::comm::LocalServer *localServer = my_app.getDiameterLocalServer();
+  MyLocalServer *localServer = (MyLocalServer *)my_node->getDiameterServer();
 
   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
+      anna::diameter::comm::Message *msg = my_node->createCommMessage();
+      msg->forwardEndToEnd(); // end-to-end will be kept
       msg->setBody(message);
       msg->setRequestClientSessionKey(clientSession->getKey());
       bool success = localServer->send(msg);
 
       // Detailed log:
-      if(my_app.logEnabled()) {
+      if(my_node->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);
+        std::string detail = usedServerSession ? usedServerSession->asString() : "[null server session]"; // esto no deberia ocurrir
+        my_node->writeLogFile(message, (success ? "fwd2c" : "fwd2cError"), detail);
       }
     } catch(anna::RuntimeException &ex) {
       ex.trace();
     }
   }
+
+  // Testing:
+  anna::testing::TestManager::instantiate().receiveDiameterMessage(message, clientSession);
 }
 
-void MyDiameterEntity::eventResponse(const anna::diameter::comm::Response &response)
-throw(anna::RuntimeException) {
+void MyDiameterEntity::eventResponse(const anna::diameter::comm::Response &response, const anna::diameter::comm::OriginHost *myNode)
+noexcept(false) {
   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::OriginHost *my_node = my_app.getOriginHost(getEngine()->getOriginHostName());
   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
@@ -135,7 +176,7 @@ throw(anna::RuntimeException) {
     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(my_node->logEnabled()) my_node->writeLogFile(*request, "req2e-expired", clientSession->asString());
     }
   }
 
@@ -145,47 +186,56 @@ throw(anna::RuntimeException) {
       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());
+      if(my_node->logEnabled()) {
+        my_node->writeLogFile(*message, "recvfe", clientSession->asString());
       }
     }
 
     // Forward to client:
-    anna::diameter::comm::LocalServer *localServer = my_app.getDiameterLocalServer();
+    MyLocalServer *localServer = (MyLocalServer *)my_node->getDiameterServer();
 
     if(localServer && (request_cid != anna::diameter::helpers::base::COMMANDID__Capabilities_Exchange_Request) /* don't forward CEA */) {
+      anna::diameter::comm::Message *msg;
+
       try {
-        anna::diameter::comm::Message *msg = my_app.createCommMessage();
-        msg->updateEndToEnd(false); // end-to-end will be kept
+        msg = my_node->createCommMessage();
+        msg->forwardEndToEnd(); // 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
+        anna::diameter::comm::ServerSession *usedServerSession = my_node->getCommEngine()->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);
+        if(my_node->logEnabled()) {
+          my_node->writeLogFile(*message, (success ? "fwd2c" : "fwd2cError"), detail);
         }
       } catch(anna::RuntimeException &ex) {
         ex.trace();
       }
+
+      // release msgs
+      my_node->releaseCommMessage(msg);
+      my_node->releaseCommMessage(request);
     }
   }
 
   // Triggering burst:
-  if(isOK || contextExpired) my_app.sendBurstMessage();
+  if(isOK || contextExpired) my_node->sendBurstMessage();
+
+  // Testing:
+  if(isOK) anna::testing::TestManager::instantiate().receiveDiameterMessage(*message, clientSession);
 }
 
-void MyDiameterEntity::eventUnknownResponse(anna::diameter::comm::ClientSession *clientSession, const anna::DataBlock &message)
-throw(anna::RuntimeException) {
+void MyDiameterEntity::eventUnknownResponse(anna::diameter::comm::ClientSession *clientSession, const anna::DataBlock &message, const anna::diameter::comm::OriginHost *myNode)
+noexcept(false) {
   LOGMETHOD(anna::TraceMethod tm("launcher::MyDiameterEntity", "eventUnknownResponse", ANNA_FILE_LOCATION));
   // Performance stats:
   Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
+  anna::diameter::comm::OriginHost * my_node = my_app.getOriginHost(getEngine()->getOriginHostName());
   // CommandId:
   anna::diameter::CommandId cid = anna::diameter::codec::functions::getCommandId(message);
   LOGDEBUG
@@ -200,14 +250,15 @@ throw(anna::RuntimeException) {
   );
 
   // Write reception
-  if(my_app.logEnabled()) my_app.writeLogFile(message, "recvfe-ans-unknown", clientSession->asString());
+  if(my_node->logEnabled()) my_node->writeLogFile(message, "recvfe-ans-unknown", clientSession->asString());
 }
 
-void MyDiameterEntity::eventDPA(anna::diameter::comm::ClientSession *clientSession, const anna::DataBlock &message)
-throw(anna::RuntimeException) {
+void MyDiameterEntity::eventDPA(anna::diameter::comm::ClientSession *clientSession, const anna::DataBlock &message, const anna::diameter::comm::OriginHost *myNode)
+noexcept(false) {
   LOGMETHOD(anna::TraceMethod tm("launcher::MyDiameterEntity", "eventDPA", ANNA_FILE_LOCATION));
   // Performance stats:
   Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
+  anna::diameter::comm::OriginHost * my_node = my_app.getOriginHost(getEngine()->getOriginHostName());
   // CommandId:
   anna::diameter::CommandId cid = anna::diameter::codec::functions::getCommandId(message);
   LOGDEBUG
@@ -222,5 +273,8 @@ throw(anna::RuntimeException) {
   );
 
   // Write reception
-  if(my_app.logEnabled()) my_app.writeLogFile(message, "recvfe", clientSession->asString());
+  if(my_node->logEnabled()) my_node->writeLogFile(message, "recvfe", clientSession->asString());
+
+  // Testing:
+  anna::testing::TestManager::instantiate().receiveDiameterMessage(message, clientSession);
 }