Testing library separation: now not in launcher but isolated
[anna.git] / example / diameter / launcher / Launcher.cpp
index aebebec..b141865 100644 (file)
 #include <anna/diameter/helpers/base/functions.hpp>
 #include <anna/time/functions.hpp>
 #include <anna/diameter.comm/ApplicationMessageOamModule.hpp>
+#include <anna/testing/defines.hpp>
 #include <anna/xml/xml.hpp>
 #include <Procedure.hpp>
 
 // Process
 #include <Launcher.hpp>
-#include <OriginHost.hpp>
 #include <MyDiameterEngine.hpp>
-#include <TestManager.hpp>
-#include <TestCase.hpp>
+#include <anna/diameter.comm/OriginHost.hpp>
+#include <anna/testing/TestManager.hpp>
+#include <anna/testing/TestCase.hpp>
 
 
 #define SIGUSR2_TASKS_INPUT_FILENAME "sigusr2.in"
@@ -326,11 +327,24 @@ void Launcher::servicesFromXML(const anna::xml::Node* servicesNode, bool eventOp
 
       // Checking command line parameters
       std::string sessionBasedModelsType;
+      anna::diameter::comm::Entity::SessionBasedModelsType::_v sessionBasedModelsTypeEnum;
       if(sessionBasedModelsClientSocketSelection) {
         sessionBasedModelsType = sessionBasedModelsClientSocketSelection->getValue();
-        if((sessionBasedModelsType != "SessionIdHighPart") && (sessionBasedModelsType != "SessionIdOptionalPart") && (sessionBasedModelsType != "RoundRobin")) {
-          throw anna::RuntimeException("Parameter 'sessionBasedModelsClientSocketSelection' only accepts 'SessionIdHighPart'/'SessionIdOptionalPart'/'RoundRobin' as parameter values", ANNA_FILE_LOCATION);
-        }
+       if (sessionBasedModelsType == "RoundRobin") {
+          sessionBasedModelsTypeEnum = anna::diameter::comm::Entity::SessionBasedModelsType::RoundRobin;
+       }
+       else if (sessionBasedModelsType == "SessionIdOptionalPart") {
+          sessionBasedModelsTypeEnum = anna::diameter::comm::Entity::SessionBasedModelsType::SessionIdOptionalPart;
+       }
+       else if (sessionBasedModelsType == "SessionIdHighPart") {
+          sessionBasedModelsTypeEnum = anna::diameter::comm::Entity::SessionBasedModelsType::SessionIdHighPart;
+       }
+       else if (sessionBasedModelsType == "SessionIdLowPart") {
+          sessionBasedModelsTypeEnum = anna::diameter::comm::Entity::SessionBasedModelsType::SessionIdLowPart;
+       }
+       else {
+          throw anna::RuntimeException("Parameter 'sessionBasedModelsClientSocketSelection' only accepts 'SessionIdLowPart'/'SessionIdHighPart'/'SessionIdOptionalPart'/'RoundRobin' as parameter values", ANNA_FILE_LOCATION);
+       }
       }
 
       int retransmissions = retries ? retries->getIntegerValue() : 0;
@@ -338,19 +352,21 @@ void Launcher::servicesFromXML(const anna::xml::Node* servicesNode, bool eventOp
         throw anna::RuntimeException("Parameter 'retries' must be non-negative", ANNA_FILE_LOCATION);
       }
 
-      // Create new Node instance /////////////////////////////////////////////////////////////////
-      a_workingNode = new OriginHost(originHost->getValue(), applicationId, bpd);
-      MyDiameterEngine *commEngine = a_workingNode->getMyDiameterEngine();
       /////////////////////////////////////////////////////////////////////////////////////////////
-
-      // Assignments:
+      // Diameter communication engine:
+      std::string commEngineName = originHost->getValue() + "_DiameterCommEngine";
+      MyDiameterEngine *commEngine = new MyDiameterEngine(commEngineName.c_str(), bpd);
+      commEngine->setAutoBind(false);  // allow to create client-sessions without binding them, in order to set timeouts.
       commEngine->setMaxConnectionDelay(tcpConnectDelayMs);
       commEngine->setWatchdogPeriod(watchdogPeriodMs);
+      commEngine->setOriginHostName(originHost->getValue());
+      if (originRealm) commEngine->setOriginRealmName(originRealm->getValue());
+
+      // Origin host node:
+      a_workingNode = new anna::diameter::comm::OriginHost((anna::diameter::comm::Engine*)commEngine, applicationId);
       a_workingNode->setRequestRetransmissions(retransmissions);
+      /////////////////////////////////////////////////////////////////////////////////////////////
 
-      // Realm information:
-      commEngine->setOriginHost(originHost->getValue());
-      if (originRealm) commEngine->setOriginRealm(originRealm->getValue());
 
       // Diameter entity:
       if(entity) {
@@ -367,7 +383,7 @@ void Launcher::servicesFromXML(const anna::xml::Node* servicesNode, bool eventOp
 
           // Register one entity for this engine:
           a_workingNode->createEntity(entity->getValue(), ceaTimeoutMs, answersTimeoutMs);
-          a_workingNode->getEntity()->setSessionBasedModelsType(sessionBasedModelsType);
+          a_workingNode->getEntity()->setSessionBasedModelsType(sessionBasedModelsTypeEnum);
           a_workingNode->getEntity()->setBalance(balance ? (balance->getValue() == "yes") : false); // for sendings
           if (eventOperation) a_workingNode->getEntity()->bind();
         }
@@ -379,12 +395,12 @@ void Launcher::servicesFromXML(const anna::xml::Node* servicesNode, bool eventOp
         std::string ceaPathfile = cea ? cea->getValue() : "";
 
         int sessions = diameterServerSessions ? diameterServerSessions->getIntegerValue() : 1;
-        a_workingNode->startDiameterServer(diameterServer->getValue(), sessions, allowedInactivityTimeMs, answersTimeoutMs, ceaPathfile);
+        a_workingNode->createDiameterServer(diameterServer->getValue(), sessions, allowedInactivityTimeMs, answersTimeoutMs, ceaPathfile);
       }
 
       // Logs:
       if (!allLogsDisabled) {
-        std::string host = commEngine->getOriginHost();
+        std::string host = commEngine->getOriginHostName();
         std::string s_log = host + ".launcher.log"; if (log) s_log = log->getValue();
         bool b_splitLog = (splitLog ? (splitLog->getValue() == "yes") : false);
         bool b_detailedLog = (detailedLog ? (detailedLog->getValue() == "yes") : false);
@@ -505,20 +521,20 @@ bool Launcher::setWorkingNode(const std::string &name) throw() {
     );
   }
   else {
-    a_workingNode = const_cast<OriginHost*>(nodeIt->second);
+    a_workingNode = const_cast<anna::diameter::comm::OriginHost*>(nodeIt->second);
     result = true;
   }
 
   return result;
 }
 
-OriginHost *Launcher::getOriginHost(const std::string &oh) const throw(anna::RuntimeException) {
+anna::diameter::comm::OriginHost *Launcher::getOriginHost(const std::string &oh) const throw(anna::RuntimeException) {
   origin_hosts_it it = a_originHosts.find(oh);
   if (it != a_originHosts.end()) return it->second;
   throw anna::RuntimeException(anna::functions::asString("There is no origin host registered as '%s' (set Origin-Host avp correctly or force a specific host with 'node' operation)", oh.c_str()), ANNA_FILE_LOCATION);
 }
 
-OriginHost *Launcher::getOriginHost(const anna::diameter::codec::Message &message) const throw(anna::RuntimeException) {
+anna::diameter::comm::OriginHost *Launcher::getOriginHost(const anna::diameter::codec::Message &message) const throw(anna::RuntimeException) {
   std::string originHost = message.getAvp(anna::diameter::helpers::base::AVPID__Origin_Host)->getDiameterIdentity()->getValue();
   return (getOriginHost(originHost));
 }
@@ -528,7 +544,14 @@ void Launcher::updateOperatedOriginHostWithMessage(const anna::diameter::codec::
     a_operatedHost = getOriginHost(message);
 }
 
-OriginHost *Launcher::getOperatedHost() const throw(anna::RuntimeException) {
+anna::diameter::comm::OriginHost *Launcher::getWorkingNode() const throw(anna::RuntimeException) {
+  if(!a_workingNode)
+    throw anna::RuntimeException("Working node not identified (try to load services)", ANNA_FILE_LOCATION);
+
+  return a_workingNode;
+}
+
+anna::diameter::comm::OriginHost *Launcher::getOperatedHost() const throw(anna::RuntimeException) {
   if(!a_operatedHost)
     throw anna::RuntimeException("Node not identified (try to force a specific Origin-Host with 'node' operation)", ANNA_FILE_LOCATION);
 
@@ -536,21 +559,21 @@ OriginHost *Launcher::getOperatedHost() const throw(anna::RuntimeException) {
 }
 
 MyDiameterEntity *Launcher::getOperatedEntity() const throw(anna::RuntimeException) {
-  MyDiameterEntity *result = getOperatedHost()->getEntity();
+  MyDiameterEntity *result = (MyDiameterEntity *)(getOperatedHost()->getEntity());
   if (!result)
     throw anna::RuntimeException("No entity configured for the operated node", ANNA_FILE_LOCATION);
   return result;
 }
 
 MyLocalServer *Launcher::getOperatedServer() const throw(anna::RuntimeException) {
-  MyLocalServer *result = getOperatedHost()->getDiameterServer();
+  MyLocalServer *result = (MyLocalServer *)(getOperatedHost()->getDiameterServer());
   if (!result)
     throw anna::RuntimeException("No local server configured for the operated node", ANNA_FILE_LOCATION);
   return result;
 }
 
 MyDiameterEngine *Launcher::getOperatedEngine() const throw(anna::RuntimeException) {
-  return getOperatedHost()->getMyDiameterEngine(); // never will be NULL
+  return (MyDiameterEngine *)getOperatedHost()->getCommEngine(); // never will be NULL
 }
 
 void Launcher::initialize()
@@ -560,7 +583,7 @@ throw(anna::RuntimeException) {
   anna::comm::Communicator::WorkMode::_v workMode(anna::comm::Communicator::WorkMode::Single);
   a_communicator = new MyCommunicator(workMode);
   a_timeEngine = new anna::timex::Engine((anna::Millisecond)600000, a_admlMinResolution);
-  TestManager::instantiate().setTimerController(a_timeEngine);
+  anna::testing::TestManager::instantiate().setTimerController(a_timeEngine);
 
   // Counters record procedure:
   const char *varname = "cntRecordPeriod";
@@ -575,7 +598,7 @@ throw(anna::RuntimeException) {
   // Testing framework:
   std::string tmDir = ".";
   if(cl.exists("tmDir")) tmDir = cl.getValue("tmDir");
-  TestManager::instantiate().setReportsDirectory(tmDir);
+  anna::testing::TestManager::instantiate().setReportsDirectory(tmDir);
 
   // Tracing:
   if(cl.exists("trace"))
@@ -755,7 +778,7 @@ throw(anna::RuntimeException) {
   // Start client connections //////////////////////////////////////////////////////////////////////////////////
   MyDiameterEntity *entity;
   for (origin_hosts_it it = a_originHosts.begin(); it != a_originHosts.end(); it++) {
-    entity = it->second->getEntity();
+    entity = (MyDiameterEntity *)(it->second->getEntity());
     if (entity) entity->bind();
   }
 
@@ -796,11 +819,11 @@ bool Launcher::getDataBlockFromHexFile(const std::string &pathfile, anna::DataBl
 
 void Launcher::resetStatistics() throw() {
   if (a_workingNode) {
-    a_workingNode->getMyDiameterEngine()->resetStatistics();
+    a_workingNode->getCommEngine()->resetStatistics();
   }
   else {
     for (origin_hosts_it it = a_originHosts.begin(); it != a_originHosts.end(); it++) {
-      it->second->getMyDiameterEngine()->resetStatistics();
+      it->second->getCommEngine()->resetStatistics();
     }
   }
 }
@@ -1525,8 +1548,7 @@ void Launcher::eventOperation(const std::string &operation, std::string &respons
   anna::diameter::codec::Message codecMsg; // auxiliary codec message
 
   // Singletons:
-  CommandLine& cl(anna::CommandLine::instantiate());
-  TestManager &testManager = TestManager::instantiate();
+  anna::testing::TestManager &testManager = anna::testing::TestManager::instantiate();
 
 
   ///////////////////////////////////////////////////////////////////
@@ -1539,7 +1561,14 @@ void Launcher::eventOperation(const std::string &operation, std::string &respons
     std::string args = ((operation.find("dynamic|") == 0) && (op_size > 8)) ? operation.substr(8) : "";
     if (args == "" && op_size != 7)
       throw anna::RuntimeException("Wrong body content format on HTTP Request. Use 'help' management command to see more information.", ANNA_FILE_LOCATION);
-    p.execute(args, response_content);
+    try {
+      p.execute(args, response_content, getWorkingNode());
+    }
+    catch(anna::RuntimeException &ex) {
+      ex.trace();
+      response_content = ex.asString();
+      return;
+    }
     return;
   }
 
@@ -1789,7 +1818,7 @@ void Launcher::eventOperation(const std::string &operation, std::string &respons
       try { if(getOperatedHost()->logEnabled()) codecMsg.decode(db_aux); } catch(anna::RuntimeException &ex) { ex.trace(); }
     }
 
-    bool success = getOperatedEntity()->send(msg, cl.exists("balance"));
+    bool success = getOperatedEntity()->send(msg);
     getOperatedHost()->releaseCommMessage(msg);
 
     // Detailed log:
@@ -2033,7 +2062,7 @@ void Launcher::eventOperation(const std::string &operation, std::string &respons
         throw anna::RuntimeException("Wrong body content format on HTTP Request. Use 'help' management command to see more information.", ANNA_FILE_LOCATION);
 
       int id = ((param2 != "") ? atoi(param2.c_str()) : -1);
-      TestCase *testCase = testManager.findTestCase(id);
+      anna::testing::TestCase *testCase = testManager.findTestCase(id);
 
       if (testCase) {
         response_content = testCase->asXMLString();
@@ -2059,7 +2088,7 @@ void Launcher::eventOperation(const std::string &operation, std::string &respons
         throw anna::RuntimeException("Interactive amount must be -1 (to disable interactive mode) or a positive number.", ANNA_FILE_LOCATION);
 
       int id = atoi(param3.c_str());
-      TestCase *testCase = testManager.findTestCase(id);
+      anna::testing::TestCase *testCase = testManager.findTestCase(id);
       if (testCase) {
         if (amount == -1) {
           testCase->makeInteractive(false);
@@ -2090,7 +2119,7 @@ void Launcher::eventOperation(const std::string &operation, std::string &respons
         throw anna::RuntimeException("Wrong body content format on HTTP Request. Use 'help' management command to see more information.", ANNA_FILE_LOCATION);
 
       int id = ((param3 != "") ? atoi(param3.c_str()) : -1);
-      TestCase *testCase = ((id != -1) ? testManager.findTestCase(id) : NULL);
+      anna::testing::TestCase *testCase = ((id != -1) ? testManager.findTestCase(id) : NULL);
 
       if (testCase) {
         bool done = testCase->reset((param2 == "hard") ? true:false);
@@ -2259,7 +2288,7 @@ void Launcher::eventOperation(const std::string &operation, std::string &respons
     } else if (param1 == "clear") {
       getOperatedServer()->getReactingAnswers()->clear();
     } else if (param1 == "dump") {
-      getOperatedServer()->getReactingAnswers()->dump();
+      getOperatedServer()->getReactingAnswers()->dump("programmed_answer");
     } else {
       codecMsg.loadXML(param1);
       updateOperatedOriginHostWithMessage(codecMsg);
@@ -2285,7 +2314,7 @@ void Launcher::eventOperation(const std::string &operation, std::string &respons
     } else if (param1 == "clear") {
       getOperatedEntity()->getReactingAnswers()->clear();
     } else if (param1 == "dump") {
-      getOperatedEntity()->getReactingAnswers()->dump();
+      getOperatedEntity()->getReactingAnswers()->dump("programmed_answer");
     } else {
       codecMsg.loadXML(param1);
       updateOperatedOriginHostWithMessage(codecMsg);
@@ -2333,7 +2362,7 @@ throw() {
   statsAsXML(result);
 
   // Testing: could be heavy if test case reports are enabled
-  TestManager::instantiate().asXML(result);
+  anna::testing::TestManager::instantiate().asXML(result);
 
   return result;
 }