New rxSimpleTest example
[anna.git] / example / diameter / rxSimpleTest / MyDiameterEntity.cpp
diff --git a/example/diameter/rxSimpleTest/MyDiameterEntity.cpp b/example/diameter/rxSimpleTest/MyDiameterEntity.cpp
new file mode 100755 (executable)
index 0000000..8daec6e
--- /dev/null
@@ -0,0 +1,221 @@
+// 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>
+#include <anna/diameter.comm/ClientSession.hpp>
+#include <anna/diameter.comm/OriginHost.hpp>
+//#include <anna/diameter/helpers/base/functions.hpp>
+#include <anna/diameter/helpers/nas/defines.hpp>
+
+// Process
+#include <MyDiameterEngine.hpp>
+#include <MyDiameterEntity.hpp>
+#include <MyLocalServer.hpp>
+#include <rxSimpleTest.hpp>
+#include <anna/testing/TestManager.hpp>
+
+
+void MyDiameterEntity::eventRequestRetransmission(const anna::diameter::comm::ClientSession* clientSession, anna::diameter::comm::Message *request) throw() {
+
+  LOGMETHOD(anna::TraceMethod tm("rxSimpleTest::MyDiameterEntity", "eventRequestRetransmission", ANNA_FILE_LOCATION));
+
+  // Base class:
+  Entity::eventRequestRetransmission(clientSession, request); // warning trace
+
+  // Performance stats:
+  RxSimpleTest& my_app = static_cast <RxSimpleTest&>(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);
+  );
+}
+
+
+void MyDiameterEntity::eventRequest(anna::diameter::comm::ClientSession *clientSession, const anna::DataBlock &message)
+throw(anna::RuntimeException) {
+  LOGMETHOD(anna::TraceMethod tm("rxSimpleTest::MyDiameterEntity", "eventRequest", ANNA_FILE_LOCATION));
+  // Performance stats:
+  RxSimpleTest& my_app = static_cast <RxSimpleTest&>(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
+  (
+    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);
+  );
+}
+
+void MyDiameterEntity::eventResponse(const anna::diameter::comm::Response &response)
+throw(anna::RuntimeException) {
+  LOGMETHOD(anna::TraceMethod tm("rxSimpleTest::MyDiameterEntity", "eventResponse", ANNA_FILE_LOCATION));
+  RxSimpleTest& my_app = static_cast <RxSimpleTest&>(anna::app::functions::getApp());
+  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_c = static_cast<const anna::diameter::comm::ClientSession *>(response.getSession());
+  anna::diameter::comm::ClientSession *clientSession = const_cast<anna::diameter::comm::ClientSession *>(clientSession_c);
+  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(isOK) {
+    LOGDEBUG(
+      std::string msg = "Received response for diameter message:  ";
+      msg += anna::diameter::functions::commandIdAsPairString(request_cid);
+      anna::Logger::debug(msg, ANNA_FILE_LOCATION);
+    );
+
+    if(request_cid == anna::diameter::helpers::base::COMMANDID__Capabilities_Exchange_Request) {
+      // Build AAR and send it:
+      anna::diameter::codec::Message aar;
+
+      // From file would be:
+      //aar.loadXML("./aar.xml");
+      aar.setApplicationId(anna::diameter::helpers::APPID__3GPP_Rx); // 16777236
+      aar.setId("AA-Request");
+
+      // Session-Id
+      Avp *sessionId = aar.addAvp("Session-Id");
+      sessionId->getUTF8String()->setValue("ocs3;1332774430;1;1332774430");
+
+      // Auth-Application-Id
+      Avp *authApplicationId = aar.addAvp("Auth-Application-Id");
+      authApplicationId->getUnsigned32()->setValue(16777236);
+
+      // Origin-Host
+      Avp *originHost = aar.addAvp("Origin-Host");
+      originHost->getDiameterIdentity()->setValue("thehost.theOriginRealm.com");
+
+      // Origin-Realm
+      Avp *originRealm = aar.addAvp("Origin-Realm");
+      originRealm->getDiameterIdentity()->setValue("theOriginRealm.com");
+
+      // Destination-Realm
+      Avp *destinationRealm = aar.addAvp("Destination-Realm");
+      destinationRealm->getDiameterIdentity()->setValue("theDestinationRealm.com");
+
+      // Suppported features:
+      Avp *supportedFeatures = aar.addAvp("Supported-Features");
+      supportedFeatures->addAvp("Vendor-Id")->getUnsigned32()->setValue(10415);
+      supportedFeatures->addAvp("Feature-List-ID")->getUnsigned32()->setValue(1);
+      supportedFeatures->addAvp("Feature-List")->getUnsigned32()->setValue(3);
+
+      anna::diameter::comm::Message *msg;
+
+      try {
+        msg = my_node->createCommMessage();
+        msg->setBody(aar.code());
+        /* response = NULL =*/clientSession->send(msg);
+      } catch(anna::RuntimeException &ex) {
+        ex.trace();
+      }
+
+      // release msg
+      my_node->releaseCommMessage(msg);
+    }
+    else if(request_cid == anna::diameter::helpers::nas::COMMANDID__AA_Request) {
+      // Decode
+      anna::diameter::codec::Message aaa;
+      try { aaa.decode(*message); } catch(anna::RuntimeException &ex) { ex.trace(); }
+
+      Avp *resultCode = aaa.getAvp("Result-Code");
+      int rc = resultCode->getUnsigned32()->getValue();
+
+      if (rc != 2001) exit(1);
+      else exit(0);
+    }
+  }
+}
+
+void MyDiameterEntity::eventUnknownResponse(anna::diameter::comm::ClientSession *clientSession, const anna::DataBlock &message)
+throw(anna::RuntimeException) {
+  LOGMETHOD(anna::TraceMethod tm("rxSimpleTest::MyDiameterEntity", "eventUnknownResponse", ANNA_FILE_LOCATION));
+  // Performance stats:
+  RxSimpleTest& my_app = static_cast <RxSimpleTest&>(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
+  (
+    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);
+  );
+}
+
+void MyDiameterEntity::eventDPA(anna::diameter::comm::ClientSession *clientSession, const anna::DataBlock &message)
+throw(anna::RuntimeException) {
+  LOGMETHOD(anna::TraceMethod tm("rxSimpleTest::MyDiameterEntity", "eventDPA", ANNA_FILE_LOCATION));
+  // Performance stats:
+  RxSimpleTest& my_app = static_cast <RxSimpleTest&>(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
+  (
+    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);
+  );
+}
+