Fix bug on regexp insertion for xml messages
[anna.git] / example / diameter / launcher / Launcher.cpp
index d0151cd..30637be 100644 (file)
@@ -144,6 +144,7 @@ Launcher::Launcher() : anna::comm::Application("launcher", "DiameterLauncher", "
   a_counterRecorderClock = NULL;
 
   a_workingNode = NULL;
+  a_operatedHost = NULL;
 
   a_httpServerSocket = NULL;
 }
@@ -188,7 +189,8 @@ void Launcher::servicesFromXML(const anna::xml::Node* servicesNode, bool bindRes
   // We will register a scope per stack id registered. The counters will be dynamically registered at count method.
   anna::diameter::comm::ApplicationMessageOamModule & appMsgOamModule = anna::diameter::comm::ApplicationMessageOamModule::instantiate();
   appMsgOamModule.enableCounters(); // this special module is disabled by default (the only)
-  static int scope_id = 3;
+  static int scope_id = 3 + appMsgOamModule.scopes();
+
   unsigned int id_value;
   std::string codecEngineName;
 
@@ -574,17 +576,26 @@ anna::diameter::comm::OriginHost *Launcher::getOriginHost(const anna::diameter::
   return (getOriginHost(originHost));
 }
 
+anna::diameter::comm::OriginHost *Launcher::getOriginHost(const anna::DataBlock &messageDataBlock) const throw(anna::RuntimeException) {
+  std::string originHost = anna::diameter::helpers::base::functions::getOriginHost(messageDataBlock);
+  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);
 }
 
+void Launcher::updateOperatedOriginHostWithMessage(const anna::DataBlock &messageDataBlock) throw(anna::RuntimeException) {
+  if (!a_operatedHost) // priority for working node by mean 'node' operation
+    a_operatedHost = getOriginHost(messageDataBlock);
+}
+
 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);
@@ -1161,7 +1172,7 @@ bool Launcher::eventOperation(const std::string &operation, std::string &respons
     outfile.write(xmlString.c_str(), xmlString.size());
     outfile.close();
   } else if((opType == "hide") || (opType == "show") || (opType == "hidden") || (opType == "shown")) {
-    return eop.visibility(opt_response, opType, param1, (param2 != "") ? atoi(param2.c_str()) : -1);
+    result = eop.visibility(opt_response, opType, param1, (param2 != "") ? atoi(param2.c_str()) : -1);
 
   } else if((opType == "sendxml2e") || (opType == "sendhex2e")) {
     anna::diameter::comm::Message *msg;
@@ -1177,6 +1188,7 @@ bool Launcher::eventOperation(const std::string &operation, std::string &respons
       // Get DataBlock from file with hex content:
       if(!getDataBlockFromHexFile(param1, db_aux))
         throw anna::RuntimeException("Error reading hex file provided", ANNA_FILE_LOCATION);
+      updateOperatedOriginHostWithMessage(db_aux);
       msg = getOperatedHost()->createCommMessage();
       msg->setBody(db_aux);
       try { if(getOperatedHost()->logEnabled()) codecMsg.decode(db_aux); } catch(anna::RuntimeException &ex) { ex.trace(); }
@@ -1189,9 +1201,90 @@ bool Launcher::eventOperation(const std::string &operation, std::string &respons
     if(getOperatedHost()->logEnabled()) {
       anna::diameter::comm::Server *usedServer = getOperatedEntity()->getLastUsedResource();
       anna::diameter::comm::ClientSession *usedClientSession = usedServer ? usedServer->getLastUsedResource() : NULL;
-      std::string detail = usedClientSession ? usedClientSession->asString() : "<null client session>"; // shouldn't happen
+      std::string detail = usedClientSession ? usedClientSession->asString() : "[null client session]"; // shouldn't happen
       getOperatedHost()->writeLogFile(codecMsg, (success ? "sent2e" : "send2eError"), detail);
     }
+  } else if((opType == "sendxml2c") || (opType == "sendhex2c")) {
+    anna::diameter::comm::Message *msg;
+
+    if(opType == "sendxml2c") {
+      codecMsg.loadXMLFile(param1);
+      updateOperatedOriginHostWithMessage(codecMsg);
+      msg = getOperatedHost()->createCommMessage();
+      msg->clearBody();
+      try { codecMsg.valid(); } catch(anna::RuntimeException &ex) { ex.trace(); }  // at least we need to see validation errors although it will continue sending (see validation mode configured in launcher)
+
+      msg->setBody(codecMsg.code());
+    } else {
+      // Get DataBlock from file with hex content:
+      if(!getDataBlockFromHexFile(param1, db_aux))
+        throw anna::RuntimeException("Error reading hex file provided", ANNA_FILE_LOCATION);
+      updateOperatedOriginHostWithMessage(db_aux);
+      msg = getOperatedHost()->createCommMessage();
+      msg->setBody(db_aux);
+    }
+
+    bool success = getOperatedServer()->send(msg);
+    getOperatedHost()->releaseCommMessage(msg);
+
+    // Detailed log:
+    if(getOperatedHost()->logEnabled()) {
+      anna::diameter::comm::ServerSession *usedServerSession = getOperatedServer()->getLastUsedResource();
+      std::string detail = usedServerSession ? usedServerSession->asString() : "[null server session]"; // shouldn't happen
+      getOperatedHost()->writeLogFile(codecMsg, (success ? "sent2c" : "send2cError"), detail);
+    }
+  } else if(opType == "answerxml2e") {
+
+    if(param1 == "") { // programmed answers FIFO's to stdout
+      response = getOperatedEntity()->getReactingAnswers()->asString("ANSWERS TO ENTITY");
+      return true; // OK
+    } else if (param1 == "rotate") {
+      getOperatedEntity()->getReactingAnswers()->rotate(true);
+    } else if (param1 == "exhaust") {
+      getOperatedEntity()->getReactingAnswers()->rotate(false);
+    } else if (param1 == "clear") {
+      getOperatedEntity()->getReactingAnswers()->clear();
+    } else if (param1 == "dump") {
+      getOperatedEntity()->getReactingAnswers()->dump("programmed_answer");
+    } else {
+      codecMsg.loadXMLFile(param1);
+      updateOperatedOriginHostWithMessage(codecMsg);
+      anna::diameter::codec::Message *message = getOperatedHost()->getCodecEngine()->createMessage(param1); // loads xml again, lesser of two evils
+      LOGDEBUG(anna::Logger::debug(message->asXMLString(), ANNA_FILE_LOCATION));
+
+      if(message->isRequest())
+        throw anna::RuntimeException("Cannot program diameter requests. Answer type must be provided", ANNA_FILE_LOCATION);
+
+      int code = message->getId().first;
+      LOGDEBUG(anna::Logger::debug("Adding a new programed 'answer to entity' to the FIFO queue corresponding to its message code ...", ANNA_FILE_LOCATION));
+      getOperatedEntity()->getReactingAnswers()->addMessage(code, message);
+    }
+  } else if(opType == "answerxml2c") {
+
+    if(param1 == "") { // programmed answers FIFO's to stdout
+      response = getOperatedServer()->getReactingAnswers()->asString("ANSWERS TO CLIENT");
+      return true; // OK
+    } else if (param1 == "rotate") {
+      getOperatedServer()->getReactingAnswers()->rotate(true);
+    } else if (param1 == "exhaust") {
+      getOperatedServer()->getReactingAnswers()->rotate(false);
+    } else if (param1 == "clear") {
+      getOperatedServer()->getReactingAnswers()->clear();
+    } else if (param1 == "dump") {
+      getOperatedServer()->getReactingAnswers()->dump("programmed_answer");
+    } else {
+      codecMsg.loadXMLFile(param1);
+      updateOperatedOriginHostWithMessage(codecMsg);
+      anna::diameter::codec::Message *message = getOperatedHost()->getCodecEngine()->createMessage(param1); // loads xml again, lesser of two evils
+      LOGDEBUG(anna::Logger::debug(message->asXMLString(), ANNA_FILE_LOCATION));
+
+      if(message->isRequest())
+        throw anna::RuntimeException("Cannot program diameter requests. Answer type must be provided", ANNA_FILE_LOCATION);
+
+      int code = message->getId().first;
+      LOGDEBUG(anna::Logger::debug("Adding a new programed 'answer to client' to the FIFO queue corresponding to its message code ...", ANNA_FILE_LOCATION));
+      getOperatedServer()->getReactingAnswers()->addMessage(code, message);
+    }
   } else if((opType == "burst")) {
 
     if (!uniqueOriginHost())
@@ -1348,12 +1441,12 @@ bool Launcher::eventOperation(const std::string &operation, std::string &respons
         limit = atoi(param2.c_str());
         testManager.setInProgressLimit(limit);
         opt_response = "new in-progress limit: ";
-        opt_response += (limit != UINT_MAX) ? anna::functions::asString(limit) : "<no limit>";
+        opt_response += (limit != UINT_MAX) ? anna::functions::asString(limit) : "[no limit]";
       }
       else {
         opt_response = "in-progress limit amount: ";
         limit = testManager.getInProgressLimit();
-        opt_response += (limit != UINT_MAX) ? anna::functions::asString(limit) : "<no limit>";
+        opt_response += (limit != UINT_MAX) ? anna::functions::asString(limit) : "[no limit]";
         opt_response += "; currently there are ";
         opt_response += anna::functions::asString(testManager.getInProgressCount());
         opt_response += " test cases running";
@@ -1583,12 +1676,7 @@ bool Launcher::eventOperation(const std::string &operation, std::string &respons
       if (numParams > 1)
         throw anna::RuntimeException("Wrong body content format on HTTP Request. Check 'HELP.md' for more information.", ANNA_FILE_LOCATION);
 
-      if (testManager.clearPool()) {
-        opt_response = "all the programmed test cases have been dropped";
-      }
-      else {
-        opt_response = "there are not programmed test cases to be removed";
-      }
+      result = testManager.clearPool(opt_response);
     }
     else if(param1 == "junit") {
       response = testManager.junitAsXMLString();
@@ -1732,18 +1820,22 @@ bool Launcher::eventOperation(const std::string &operation, std::string &respons
           std::string::size_type pos, pos_1, pos_2;
 
           pos = regexp.find("end-to-end-id=", 0u);
-          pos = regexp.find("\"", pos);
-          pos_1 = pos;
-          pos = regexp.find("\"", pos+1);
-          pos_2 = pos;
-          regexp.replace(pos_1 + 1, pos_2 - pos_1 - 1, "[0-9]+");
+          if (pos != std::string::npos) {
+            pos = regexp.find("\"", pos);
+            pos_1 = pos;
+            pos = regexp.find("\"", pos+1);
+            pos_2 = pos;
+            regexp.replace(pos_1 + 1, pos_2 - pos_1 - 1, "[0-9]+");
+          }
 
           pos = regexp.find("hop-by-hop-id=", 0u);
-          pos = regexp.find("\"", pos);
-          pos_1 = pos;
-          pos = regexp.find("\"", pos+1);
-          pos_2 = pos;
-          regexp.replace(pos_1 + 1, pos_2 - pos_1 - 1, "[0-9]+");
+          if (pos != std::string::npos) {
+            pos = regexp.find("\"", pos);
+            pos_1 = pos;
+            pos = regexp.find("\"", pos+1);
+            pos_2 = pos;
+            regexp.replace(pos_1 + 1, pos_2 - pos_1 - 1, "[0-9]+");
+          }
 
           // For this representation: <avp name="Origin-State-Id" data="1428633668"/>
           //pos = regexp.find("Origin-State-Id", 0u);
@@ -1755,13 +1847,15 @@ bool Launcher::eventOperation(const std::string &operation, std::string &respons
           //regexp.replace(pos_1 + 1, pos_2 - pos_1 - 1, "[0-9]+");
           // But we have this one: <avp data="1428633668" name="Origin-State-Id"/>
           pos = regexp.find("Origin-State-Id", 0u);
-          pos = regexp.rfind("\"", pos);
-          pos = regexp.rfind("\"", pos-1);
-          pos = regexp.rfind("\"", 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]+");
+          if (pos != std::string::npos) {
+            pos = regexp.rfind("\"", pos);
+            pos = regexp.rfind("\"", pos-1);
+            pos = regexp.rfind("\"", 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 += "$";
@@ -1784,34 +1878,6 @@ bool Launcher::eventOperation(const std::string &operation, std::string &respons
       }
     }
 
-  } else if((opType == "sendxml2c") || (opType == "sendhex2c")) {
-    anna::diameter::comm::Message *msg;
-
-    if(opType == "sendxml2c") {
-      codecMsg.loadXMLFile(param1);
-      updateOperatedOriginHostWithMessage(codecMsg);
-      msg = getOperatedHost()->createCommMessage();
-      msg->clearBody();
-      try { codecMsg.valid(); } catch(anna::RuntimeException &ex) { ex.trace(); }  // at least we need to see validation errors although it will continue sending (see validation mode configured in launcher)
-
-      msg->setBody(codecMsg.code());
-    } else {
-      // Get DataBlock from file with hex content:
-      if(!getDataBlockFromHexFile(param1, db_aux))
-        throw anna::RuntimeException("Error reading hex file provided", ANNA_FILE_LOCATION);
-      msg = getOperatedHost()->createCommMessage();
-      msg->setBody(db_aux);
-    }
-
-    bool success = getOperatedServer()->send(msg);
-    getOperatedHost()->releaseCommMessage(msg);
-
-    // Detailed log:
-    if(getOperatedHost()->logEnabled()) {
-      anna::diameter::comm::ServerSession *usedServerSession = getOperatedServer()->getLastUsedResource();
-      std::string detail = usedServerSession ? usedServerSession->asString() : "<null server session>"; // shouldn't happen
-      getOperatedHost()->writeLogFile(codecMsg, (success ? "sent2c" : "send2cError"), detail);
-    }
   } else if(opType == "loadxml") {
     codecMsg.loadXMLFile(param1);
     response = codecMsg.asXMLString();
@@ -1820,61 +1886,14 @@ bool Launcher::eventOperation(const std::string &operation, std::string &respons
     int diameterServerSessions = atoi(param1.c_str());
     getOperatedServer()->setMaxConnections(diameterServerSessions);
 
-  } else if(opType == "answerxml2c") {
-    if(param1 == "") { // programmed answers FIFO's to stdout
-      response = getOperatedServer()->getReactingAnswers()->asString("ANSWERS TO CLIENT");
-      return true; // OK
-    } else if (param1 == "rotate") {
-      getOperatedServer()->getReactingAnswers()->rotate(true);
-    } else if (param1 == "exhaust") {
-      getOperatedServer()->getReactingAnswers()->rotate(false);
-    } else if (param1 == "clear") {
-      getOperatedServer()->getReactingAnswers()->clear();
-    } else if (param1 == "dump") {
-      getOperatedServer()->getReactingAnswers()->dump("programmed_answer");
-    } else {
-      codecMsg.loadXMLFile(param1);
-      updateOperatedOriginHostWithMessage(codecMsg);
-      anna::diameter::codec::Message *message = getOperatedHost()->getCodecEngine()->createMessage(param1); // loads xml again, lesser of two evils
-      LOGDEBUG(anna::Logger::debug(message->asXMLString(), ANNA_FILE_LOCATION));
-
-      if(message->isRequest())
-        throw anna::RuntimeException("Cannot program diameter requests. Answer type must be provided", ANNA_FILE_LOCATION);
-
-      int code = message->getId().first;
-      LOGDEBUG(anna::Logger::debug("Adding a new programed 'answer to client' to the FIFO queue corresponding to its message code ...", ANNA_FILE_LOCATION));
-      getOperatedServer()->getReactingAnswers()->addMessage(code, message);
-    }
-  } else if(opType == "answerxml2e") {
-
-    if(param1 == "") { // programmed answers FIFO's to stdout
-      response = getOperatedEntity()->getReactingAnswers()->asString("ANSWERS TO ENTITY");
-      return true; // OK
-    } else if (param1 == "rotate") {
-      getOperatedEntity()->getReactingAnswers()->rotate(true);
-    } else if (param1 == "exhaust") {
-      getOperatedEntity()->getReactingAnswers()->rotate(false);
-    } else if (param1 == "clear") {
-      getOperatedEntity()->getReactingAnswers()->clear();
-    } else if (param1 == "dump") {
-      getOperatedEntity()->getReactingAnswers()->dump("programmed_answer");
-    } else {
-      codecMsg.loadXMLFile(param1);
-      updateOperatedOriginHostWithMessage(codecMsg);
-      anna::diameter::codec::Message *message = getOperatedHost()->getCodecEngine()->createMessage(param1); // loads xml again, lesser of two evils
-      LOGDEBUG(anna::Logger::debug(message->asXMLString(), ANNA_FILE_LOCATION));
 
-      if(message->isRequest())
-        throw anna::RuntimeException("Cannot program diameter requests. Answer type must be provided", ANNA_FILE_LOCATION);
 
-      int code = message->getId().first;
-      LOGDEBUG(anna::Logger::debug("Adding a new programed 'answer to entity' to the FIFO queue corresponding to its message code ...", ANNA_FILE_LOCATION));
-      getOperatedEntity()->getReactingAnswers()->addMessage(code, message);
-    }
   } else {
     throw anna::RuntimeException("Wrong body content format on HTTP Request. Check 'HELP.md' for more information.", ANNA_FILE_LOCATION);
   }
 
+
+
   // HTTP response
   response = "Operation correctly processed: "; response += operation;
   if (opt_response != "") {