Fixes and improvements
[anna.git] / example / diameter / launcher / Launcher.cpp
index 1633e5c..94d8f66 100644 (file)
 #include <anna/diameter.comm/ApplicationMessageOamModule.hpp>
 #include <anna/testing/defines.hpp>
 #include <anna/xml/xml.hpp>
+#include <anna/diameter.comm/OriginHost.hpp>
+#include <anna/diameter.comm/OriginHostManager.hpp>
 #include <Procedure.hpp>
 
 // Process
 #include <Launcher.hpp>
 #include <MyDiameterEngine.hpp>
-#include <anna/diameter.comm/OriginHost.hpp>
 #include <anna/testing/TestManager.hpp>
 #include <anna/testing/TestCase.hpp>
 
@@ -141,7 +142,6 @@ Launcher::Launcher() : anna::comm::Application("launcher", "DiameterLauncher", "
   //a_admlMinResolution = (anna::Millisecond)100;
   a_counterRecorderClock = NULL;
 
-  // a_originHosts.clear();
   a_workingNode = NULL;
 
   a_httpServerSocket = NULL;
@@ -302,8 +302,9 @@ void Launcher::servicesFromXML(const anna::xml::Node* servicesNode, bool eventOp
       burstLog = (*it)->getAttribute("burstLog", false /* no exception */); // (yes | no)
 
       // Basic checkings:
-      origin_hosts_it nodeIt = a_originHosts.find(originHost->getValue());
-      if (nodeIt != a_originHosts.end()) {
+      anna::diameter::comm::OriginHostManager &ohm = anna::diameter::comm::OriginHostManager::instantiate();
+      anna::diameter::comm::OriginHost *oh = ohm.getOriginHost(originHost->getValue());
+      if (oh) {
         std::string msg = "Already registered such Origin-Host: "; msg += originHost->getValue();
         throw anna::RuntimeException(msg, ANNA_FILE_LOCATION);
       }
@@ -416,7 +417,7 @@ void Launcher::servicesFromXML(const anna::xml::Node* servicesNode, bool eventOp
       if (eventOperation) commEngine->lazyInitialize();
 
       // Node and Codec Engine registration ///////////////////////////////////////////////////////
-      a_originHosts[originHost->getValue()] = a_workingNode;
+      ohm.registerOriginHost(originHost->getValue(), a_workingNode);
       /////////////////////////////////////////////////////////////////////////////////////////////
     }
   }
@@ -515,25 +516,25 @@ anna::Millisecond Launcher::checkTimeMeasure(const std::string &parameter, const
 bool Launcher::setWorkingNode(const std::string &name) throw() {
   bool result = false;
 
-  origin_hosts_it nodeIt = a_originHosts.find(name);
-  if (nodeIt == a_originHosts.end()) {
-    LOGWARNING(
-        std::string msg = "Unknown node with name '"; msg += name; msg += "'. Ignoring ...";
-    anna::Logger::warning(msg, ANNA_FILE_LOCATION);
-    );
-  }
-  else {
-    a_workingNode = const_cast<anna::diameter::comm::OriginHost*>(nodeIt->second);
+  anna::diameter::comm::OriginHostManager &ohm = anna::diameter::comm::OriginHostManager::instantiate();
+  anna::diameter::comm::OriginHost *oh = ohm.getOriginHost(name);
+
+  if (oh) {
+    a_workingNode = const_cast<anna::diameter::comm::OriginHost*>(oh);
     result = true;
   }
 
   return result;
 }
 
-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);
+anna::diameter::comm::OriginHost *Launcher::getOriginHost(const std::string &name) const throw(anna::RuntimeException) {
+  anna::diameter::comm::OriginHostManager &ohm = anna::diameter::comm::OriginHostManager::instantiate();
+  anna::diameter::comm::OriginHost *result = ohm.getOriginHost(name);
+
+  if (!result)
+  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)", name.c_str()), ANNA_FILE_LOCATION);
+
+  return result;
 }
 
 anna::diameter::comm::OriginHost *Launcher::getOriginHost(const anna::diameter::codec::Message &message) const throw(anna::RuntimeException) {
@@ -541,6 +542,12 @@ anna::diameter::comm::OriginHost *Launcher::getOriginHost(const anna::diameter::
   return (getOriginHost(originHost));
 }
 
+bool Launcher::uniqueOriginHost() const throw() {
+  anna::diameter::comm::OriginHostManager &ohm = anna::diameter::comm::OriginHostManager::instantiate();
+  return (ohm.size() == 1);
+}
+
+
 void Launcher::updateOperatedOriginHostWithMessage(const anna::diameter::codec::Message &message) throw(anna::RuntimeException) {
   if (!a_operatedHost) // priority for working node by mean 'node' operation
     a_operatedHost = getOriginHost(message);
@@ -589,7 +596,14 @@ throw(anna::RuntimeException) {
 
   // Counters record procedure:
   const char *varname = "cntRecordPeriod";
-  anna::Millisecond cntRecordPeriod = (cl.exists(varname)) ? checkTimeMeasure(varname, cl.getValue(varname)) : (anna::Millisecond)300000;
+  anna::Millisecond cntRecordPeriod;
+  try {
+    cntRecordPeriod = (cl.exists(varname)) ? checkTimeMeasure(varname, cl.getValue(varname)) : (anna::Millisecond)300000;
+  }
+  catch(anna::RuntimeException &ex) {
+    if (cntRecordPeriod != 0) throw ex;
+  }
+
   if(cntRecordPeriod != 0) {
     a_counterRecorderClock = new MyCounterRecorderClock("Counters record procedure clock", cntRecordPeriod); // clock
     std::string cntDir = ".";
@@ -614,7 +628,7 @@ void Launcher::run()
 throw(anna::RuntimeException) {
   LOGMETHOD(anna::TraceMethod tm("Launcher", "run", ANNA_FILE_LOCATION));
   CommandLine& cl(anna::CommandLine::instantiate());
-  anna::diameter::stack::Engine &stackEngine = anna::diameter::stack::Engine::instantiate();
+  anna::diameter::stack::Engine::instantiate();
 
   // Start time:
   a_start_time.setNow();
@@ -779,7 +793,8 @@ throw(anna::RuntimeException) {
 
   // Start client connections //////////////////////////////////////////////////////////////////////////////////
   MyDiameterEntity *entity;
-  for (origin_hosts_it it = a_originHosts.begin(); it != a_originHosts.end(); it++) {
+  anna::diameter::comm::OriginHostManager &ohm = anna::diameter::comm::OriginHostManager::instantiate();
+  for (diameter::comm::origin_hosts_it it = ohm.begin(); it != ohm.end(); it++) {
     entity = (MyDiameterEntity *)(it->second->getEntity());
     if (entity) entity->bind();
   }
@@ -839,7 +854,8 @@ void Launcher::resetStatistics() throw() {
     a_workingNode->getCommEngine()->resetStatistics();
   }
   else {
-    for (origin_hosts_it it = a_originHosts.begin(); it != a_originHosts.end(); it++) {
+    anna::diameter::comm::OriginHostManager &ohm = anna::diameter::comm::OriginHostManager::instantiate();
+    for (diameter::comm::origin_hosts_it it = ohm.begin(); it != ohm.end(); it++) {
       it->second->getCommEngine()->resetStatistics();
     }
   }
@@ -1272,7 +1288,8 @@ std::string Launcher::help() const throw() {
   result += "\n                                                          source file (xml representation). Fix mode must be enabled to avoid";
   result += "\n                                                          unexpected matching behaviour. If you need a strict matching you";
   result += "\n                                                          must add parameter 'strict', if not, regexp is built ignoring sequence";
-  result += "\n                                                          information (hop-by-hop-id=\"[0-9]+\" end-to-end-id=\"[0-9]+\").";
+  result += "\n                                                          information (hop-by-hop-id=\"[0-9]+\" end-to-end-id=\"[0-9]+\") and";
+  result += "\n                                                          Origin-State-Id value.";
   result += "\n                                                          All LF codes will be internally removed when comparison is executed";
   result += "\n                                                          in order to ease xml content configuration.";
   result += "\n";
@@ -1447,6 +1464,10 @@ std::string Launcher::help() const throw() {
   result += "\n                                 been done before. Test cases state & data will be reset (when achieved again), but general";
   result += "\n                                 statistics and counters will continue measuring until reset with 'collect' operation.";
   result += "\n";
+  result += "\n   test|auto-reset|<soft|hard>   When cycling, current test cases can be soft (default) or hard reset. If no timeout has";
+  result += "\n                                 been configured for the test case, hard reset could prevent stuck on the next cycle for";
+  result += "\n                                 those test cases still in progress.";
+  result += "\n";
   result += "\n   test|clear                    Clears all the programmed test cases and stop testing (if in progress).";
   result += "\n";
   result += "\n   test|summary                  Test manager general report (number of test cases, counts by state, global configuration,";
@@ -1592,7 +1613,7 @@ void Launcher::eventOperation(const std::string &operation, std::string &respons
     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);
     try {
-      p.execute(args, response_content, getWorkingNode());
+      p.execute(args, response_content);
     }
     catch(anna::RuntimeException &ex) {
       ex.trace();
@@ -1968,6 +1989,7 @@ void Launcher::eventOperation(const std::string &operation, std::string &respons
     // test|look[|id]                           Show programmed test case for id provided, current when missing ...
     // test|interact|amount|id                  Makes interactive a specific test case id. The amount is the margin of execution steps ...
     // test|reset|<[soft]/hard>[|id]            Reset the test case for id provided, all the tests when missing ...
+    // test|auto-reset|<soft|hard>              When cycling, current test cases can be soft (default) or hard reset ...
     // test|clear                               Clears all the programmed test cases.
     // test|summary                             Test manager general report (number of test cases, counts by state ...
 
@@ -2152,7 +2174,7 @@ void Launcher::eventOperation(const std::string &operation, std::string &respons
       anna::testing::TestCase *testCase = ((id != -1) ? testManager.findTestCase(id) : NULL);
 
       if (testCase) {
-        bool done = testCase->reset((param2 == "hard") ? true:false);
+        bool done = testCase->reset(param2 == "hard");
         opt_response_content = "test ";
         opt_response_content += param2;
         opt_response_content += " reset for id ";
@@ -2161,7 +2183,7 @@ void Launcher::eventOperation(const std::string &operation, std::string &respons
       }
       else {
         if (id == -1) {
-          bool anyReset = testManager.resetPool((param2 == "hard") ? true:false);
+          bool anyReset = testManager.resetPool(param2 == "hard");
           opt_response_content = param2; opt_response_content += " reset have been sent to all programmed tests: "; opt_response_content += anyReset ? "some/all have been reset" : "nothing was reset";
         }
         else {
@@ -2171,6 +2193,16 @@ void Launcher::eventOperation(const std::string &operation, std::string &respons
         }
       }
     }
+    else if(param1 == "auto-reset") {
+      if (numParams != 2)
+        throw anna::RuntimeException("Wrong body content format on HTTP Request. Use 'help' management command to see more information.", ANNA_FILE_LOCATION);
+
+      if (param2 != "soft" && param2 != "hard")
+        throw anna::RuntimeException("Wrong body content format on HTTP Request. Use 'help' management command to see more information.", ANNA_FILE_LOCATION);
+
+      testManager.setAutoResetHard(param2 == "hard");
+      opt_response_content += anna::functions::asString("Auto-reset configured to '%s'", param2.c_str());
+    }
     else if(param1 == "clear") {
       if (numParams > 1)
         throw anna::RuntimeException("Wrong body content format on HTTP Request. Use 'help' management command to see more information.", ANNA_FILE_LOCATION);
@@ -2289,13 +2321,17 @@ void Launcher::eventOperation(const std::string &operation, std::string &respons
 /*
           std::string s_from = "hop-by-hop-id=\"[0-9]+\" end-to-end-id=\"[0-9]+\"";
           std::string s_to = s_from;
+          std::string s_from2 = "avp name=\"Origin-State-Id\" data=\"[0-9]+\"";
+          std::string s_to2 = s_from2;
 
           try {
             regexp = std::regex_replace (regexp, std::regex(s_from), s_to);
+            regexp = std::regex_replace (regexp, std::regex(s_from2), s_to2);
           }
           catch (const std::regex_error& e) {
             throw anna::RuntimeException(e.what(), ANNA_FILE_LOCATION);
           }
+
 */
           std::string::size_type pos, pos_1, pos_2;
 
@@ -2313,6 +2349,14 @@ void Launcher::eventOperation(const std::string &operation, std::string &respons
           pos_2 = pos;
           regexp.replace(pos_1 + 1, pos_2 - pos_1 - 1, "[0-9]+");
 
+          pos = regexp.find("Origin-State-Id", 0u);
+          pos = regexp.find("\"", pos);
+          pos = regexp.find("\"", pos+1);
+          pos_1 = pos;
+          pos = regexp.find("\"", pos+1);
+          pos_2 = pos;
+          regexp.replace(pos_1 + 1, pos_2 - pos_1 - 1, "[0-9]+");
+
           //regexp.insert(0, "^");
           //regexp += "$";
         }
@@ -2442,7 +2486,8 @@ throw() {
   result->createAttribute("InitialWorkingDirectory", a_initialWorkingDirectory);
   result->createAttribute("SecondsLifeTime", anna::time::functions::lapsedMilliseconds() / 1000);
   // Diameter:
-  for (origin_hosts_it it = a_originHosts.begin(); it != a_originHosts.end(); it++) {
+  anna::diameter::comm::OriginHostManager &ohm = anna::diameter::comm::OriginHostManager::instantiate();
+  for (diameter::comm::origin_hosts_it it = ohm.begin(); it != ohm.end(); it++) {
     it->second->asXML(result);
   }