Remove global variables. Factory for comm/codec messages in application class (Launcher)
[anna.git] / example / diameter / launcher / Launcher.cpp
index 7e16c90..e4ea4ca 100644 (file)
 #define SIGUSR2_TASKS_OUTPUT_FILENAME "./sigusr2.tasks.output"
 
 
-// Auxiliary message for sendings
-anna::diameter::comm::Message G_commMsgSent2c, G_commMsgSent2e, G_commMsgFwd2c, G_commMsgFwd2e;
-anna::diameter::codec::Message G_codecMsg, G_codecAnsMsg;
-anna::Recycler<anna::diameter::comm::Message> G_commMessages; // create on requests forwards without programmed answer / release in answers forward
-
-
 Launcher::Launcher() : anna::comm::Application("launcher", "DiameterLauncher", "1.1"), a_communicator(NULL) {
   a_myDiameterEngine = new MyDiameterEngine();
   a_myDiameterEngine->setRealm("ADL.ericsson.com");
@@ -55,6 +49,23 @@ Launcher::Launcher() : anna::comm::Application("launcher", "DiameterLauncher", "
   a_burstPopCounter = 0;
 }
 
+anna::diameter::comm::Message *Launcher::createCommMessage() throw(anna::RuntimeException) {
+  return a_commMessages.create();
+}
+
+void Launcher::releaseCommMessage(anna::diameter::comm::Message *msg) throw() {
+  a_commMessages.release(msg);
+}
+
+anna::diameter::codec::Message *Launcher::createCodecMessage() throw(anna::RuntimeException) {
+  return a_codecMessages.create();
+}
+
+void Launcher::releaseCodecMessage(anna::diameter::codec::Message *msg) throw() {
+  a_codecMessages.release(msg);
+}
+
+
 void Launcher::baseProtocolSetupAsClient(void) throw(anna::RuntimeException) {
   // Build CER
   //   <CER> ::= < Diameter Header: 257, REQ >
@@ -187,11 +198,10 @@ void Launcher::baseProtocolSetupAsClient(void) throw(anna::RuntimeException) {
 
 void Launcher::writeLogFile(const anna::DataBlock & db, const std::string &logExtension, const std::string &detail) const throw() {
 //   if (!logEnabled()) return;
+  anna::diameter::codec::Message codecMsg;
+  try { codecMsg.decode(db); } catch(anna::RuntimeException &ex) { ex.trace(); }
+  writeLogFile(codecMsg, logExtension, detail);
 
-  // Decode
-  try { G_codecMsg.decode(db); } catch(anna::RuntimeException &ex) { ex.trace(); }
-
-  writeLogFile(G_codecMsg, logExtension, detail);
 }
 
 // Si ya lo tengo decodificado:
@@ -690,7 +700,7 @@ int Launcher::clearBurst() throw() {
     std::map<int, anna::diameter::comm::Message*>::const_iterator it_min(a_burstMessages.begin());
     std::map<int, anna::diameter::comm::Message*>::const_iterator it_max(a_burstMessages.end());
 
-    for(it = it_min; it != it_max; it++) G_commMessages.release((*it).second);
+    for(it = it_min; it != it_max; it++) releaseCommMessage((*it).second);
 
     a_burstMessages.clear();
   } else {
@@ -706,7 +716,7 @@ int Launcher::clearBurst() throw() {
 }
 
 int Launcher::loadBurstMessage(const anna::DataBlock & db) throw(anna::RuntimeException) {
-  anna::diameter::comm::Message *msg = G_commMessages.create();
+  anna::diameter::comm::Message *msg = createCommMessage();
   msg->setBody(db);
   a_burstMessages[a_burstLoadIndx++] = msg;
   return (a_burstLoadIndx - 1);
@@ -878,9 +888,9 @@ std::string Launcher::lookBurst(int order) const throw() {
 
   if(it != a_burstMessages.end()) {
     // Decode
-    try { G_codecMsg.decode((*it).second->getBody()); } catch(anna::RuntimeException &ex) { ex.trace(); }
-
-    result = G_codecMsg.asXMLString();
+    anna::diameter::codec::Message codecMsg;
+    try { codecMsg.decode((*it).second->getBody()); } catch(anna::RuntimeException &ex) { ex.trace(); }
+    result = codecMsg.asXMLString();
   }
 
   return result;
@@ -1191,6 +1201,7 @@ void Launcher::eventOperation(const std::string &operation, std::string &respons
   response_content = "Operation processed with exception. See traces\n"; // supposed
   std::string result = "";
   anna::DataBlock db_aux(true);
+  anna::diameter::codec::Message codecMsg;
 
   ///////////////////////////////////////////////////////////////////
   // Simple operations without arguments:
@@ -1262,8 +1273,8 @@ void Launcher::eventOperation(const std::string &operation, std::string &respons
 
   // Operations:
   if(opType == "code") {
-    G_codecMsg.loadXML(param1);
-    std::string hexString = anna::functions::asHexString(G_codecMsg.code());
+    codecMsg.loadXML(param1);
+    std::string hexString = anna::functions::asHexString(codecMsg.code());
     // write to outfile
     std::ofstream outfile(param2.c_str(), std::ifstream::out);
     outfile.write(hexString.c_str(), hexString.size());
@@ -1274,9 +1285,9 @@ void Launcher::eventOperation(const std::string &operation, std::string &respons
       throw anna::RuntimeException("Error reading hex file provided", ANNA_FILE_LOCATION);
 
     // Decode
-    try { G_codecMsg.decode(db_aux); } catch(anna::RuntimeException &ex) { ex.trace(); }
+    try { codecMsg.decode(db_aux); } catch(anna::RuntimeException &ex) { ex.trace(); }
 
-    std::string xmlString = G_codecMsg.asXMLString();
+    std::string xmlString = codecMsg.asXMLString();
     // write to outfile
     std::ofstream outfile(param2.c_str(), std::ifstream::out);
     outfile.write(xmlString.c_str(), xmlString.size());
@@ -1325,29 +1336,31 @@ void Launcher::eventOperation(const std::string &operation, std::string &respons
     MyDiameterEntity *entity = getEntity();
 
     if(!entity) throw anna::RuntimeException("No entity configured to send the message", ANNA_FILE_LOCATION);
+    anna::diameter::comm::Message *msg = createCommMessage();
 
     if((opType == "sendxml") || (opType == "sendxml2e")) {
-      G_codecMsg.loadXML(param1);
-      G_commMsgSent2e.clearBody();
-      try { G_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)
+      codecMsg.loadXML(param1);
+      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)
 
-      G_commMsgSent2e.setBody(G_codecMsg.code());
+      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);
 
-      G_commMsgSent2e.setBody(db_aux);
+      msg->setBody(db_aux);
     }
 
-    bool success = entity->send(G_commMsgSent2e, cl.exists("balance"));
+    bool success = entity->send(msg, cl.exists("balance"));
+    releaseCommMessage(msg);
 
     // Detailed log:
     if(logEnabled()) {
       anna::diameter::comm::Server *usedServer = entity->getLastUsedResource();
       anna::diameter::comm::ClientSession *usedClientSession = usedServer ? usedServer->getLastUsedResource() : NULL;
       std::string detail = usedClientSession ? usedClientSession->asString() : "<null client session>"; // esto no deberia ocurrir
-      writeLogFile(G_codecMsg, (success ? "sent2e" : "send2eError"), detail);
+      writeLogFile(codecMsg, (success ? "sent2e" : "send2eError"), detail);
     }
   } else if((opType == "burst")) {
     anna::diameter::comm::Entity *entity = getEntity();
@@ -1374,12 +1387,12 @@ void Launcher::eventOperation(const std::string &operation, std::string &respons
     } else if(param1 == "load") {
       if(param2 == "") throw anna::RuntimeException("Missing xml path file for burst load operation", ANNA_FILE_LOCATION);
 
-      G_codecMsg.loadXML(param2);
+      codecMsg.loadXML(param2);
 
-      if(G_codecMsg.isAnswer()) throw anna::RuntimeException("Cannot load diameter answers for burst feature", ANNA_FILE_LOCATION);
-      try { G_codecMsg.valid(); } catch(anna::RuntimeException &ex) { ex.trace(); }  // at least we need to see validation errors although it will continue loading (see validation mode configured in launcher)
+      if(codecMsg.isAnswer()) throw anna::RuntimeException("Cannot load diameter answers for burst feature", ANNA_FILE_LOCATION);
+      try { codecMsg.valid(); } catch(anna::RuntimeException &ex) { ex.trace(); }  // at least we need to see validation errors although it will continue loading (see validation mode configured in launcher)
 
-      int position = loadBurstMessage(G_codecMsg.code());
+      int position = loadBurstMessage(codecMsg.code());
       result = "Loaded '";
       result += param2;
       result += "' file into burst list position ";
@@ -1457,32 +1470,34 @@ void Launcher::eventOperation(const std::string &operation, std::string &respons
     MyLocalServer *localServer = getDiameterLocalServer();
 
     if(!localServer) throw anna::RuntimeException("No local server configured to send the message", ANNA_FILE_LOCATION);
+    anna::diameter::comm::Message *msg = createCommMessage();
 
     if(opType == "sendxml2c") {
-      G_codecMsg.loadXML(param1);
-      G_commMsgSent2c.clearBody();
-      try { G_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)
+      codecMsg.loadXML(param1);
+      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)
 
-      G_commMsgSent2c.setBody(G_codecMsg.code());
+      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);
 
-      G_commMsgSent2c.setBody(db_aux);
+      msg->setBody(db_aux);
     }
 
-    bool success = localServer->send(G_commMsgSent2c);
+    bool success = localServer->send(msg);
+    releaseCommMessage(msg);
 
     // Detailed log:
     if(logEnabled()) {
       anna::diameter::comm::ServerSession *usedServerSession = localServer->getLastUsedResource();
       std::string detail = usedServerSession ? usedServerSession->asString() : "<null server session>"; // esto no deberia ocurrir
-      writeLogFile(G_codecMsg, (success ? "sent2c" : "send2cError"), detail);
+      writeLogFile(codecMsg, (success ? "sent2c" : "send2cError"), detail);
     }
   } else if(opType == "loadxml") {
-    G_codecMsg.loadXML(param1);
-    std::string xmlString = G_codecMsg.asXMLString();
+    codecMsg.loadXML(param1);
+    std::string xmlString = codecMsg.asXMLString();
     std::cout << xmlString << std::endl;
   } else if(opType == "diameterServerSessions") {
     int diameterServerSessions = atoi(param1.c_str());