Add ct automation
[anna.git] / example / diameter / launcher / Launcher.cpp
index d0151cd..507bea3 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;
 }
@@ -574,17 +575,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 +1171,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 +1187,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(); }
@@ -1192,6 +1203,87 @@ bool Launcher::eventOperation(const std::string &operation, std::string &respons
       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())
@@ -1784,34 +1876,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 +1884,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 != "") {