Support positional arguments
[anna.git] / example / diameter / launcher / main.cpp
index 7baab28..e761b9f 100644 (file)
@@ -113,11 +113,15 @@ typedef std::map < int /* message code */, codec_messages_deque* >::iterator  re
 typedef std::map < int /* message code */, codec_messages_deque* >::const_iterator  reacting_answers_const_iterator;
 
   reacting_answers_container a_deques;
+  bool a_rotate;
 
   public:
-    ProgrammedAnswers() {};
+    ProgrammedAnswers() { a_rotate = false; }
     ~ProgrammedAnswers() { clear(); }
 
+    bool rotate() const throw() { return a_rotate; }
+    void rotate(bool r) throw() { a_rotate = r; }
+
     void clear () throw() {
       for (reacting_answers_const_iterator it = a_deques.begin(); it != a_deques.end(); it++) {
         anna::diameter::codec::Engine *engine = anna::functions::component <Engine> (ANNA_FILE_LOCATION);
@@ -172,25 +176,42 @@ typedef std::map < int /* message code */, codec_messages_deque* >::const_iterat
       if (it != a_deques.end()) {
         if (!it->second->empty()) {
           anna::diameter::codec::Engine *engine = anna::functions::component <Engine> (ANNA_FILE_LOCATION);
-          engine->releaseMessage(*(it->second->begin()));
+          if (a_rotate) {
+            addMessage(code, *(it->second->begin()));
+          }
+          else {
+            engine->releaseMessage(*(it->second->begin()));
+          }
           it->second->pop_front();
         }
       }
     }
 
-    std::string asString() const throw() {
-      std::string result = "No ocurrences found\n\n";
+    std::string asString(const char *queueName) const throw() {
+      std::string result = "";
+      std::string aux = "FIFO QUEUE '";
+      aux += queueName;
+      aux += "', Rotation ";
+      aux += a_rotate ? "enabled":"disabled";
+      result += anna::functions::highlightJustify(aux);
       if(a_deques.size() != 0) {
         for(reacting_answers_const_iterator it = a_deques.begin(); it != a_deques.end(); it++) {
-          result += "Answer code .............................................................. ";
-          result += anna::functions::asString(it->first); result += "\n";
-          for(codec_messages_deque_const_iterator itm = it->second->begin(); itm != it->second->end(); itm++) {
-            result += (*itm)->asXMLString();
+          if (it->second->size() != 0) {      
+            aux = "Answer code ";
+            aux += anna::functions::asString(it->first);
+            result += anna::functions::highlightJustify(aux, anna::functions::TextHighlightMode::OverAndUnderline,
+                                                             anna::functions::TextJustifyMode::Left, '-');
+            for(codec_messages_deque_const_iterator itm = it->second->begin(); itm != it->second->end(); itm++) {
+              result += (*itm)->asXMLString();
+              result += "\n";
+            }
             result += "\n";
           }
-          result += "\n";
         }
       }
+      else {
+        result = "No ocurrences found\n\n";
+      }
       return result;
     }
 };
@@ -296,6 +317,7 @@ class MyDiameterEntity : public anna::diameter::comm::Entity {
   void eventResponse(const anna::diameter::comm::Response&) throw(anna::RuntimeException);
   void eventRequest(anna::diameter::comm::ClientSession *, const anna::DataBlock&) throw(anna::RuntimeException);
   void eventUnknownResponse(anna::diameter::comm::ClientSession *, const anna::DataBlock&) throw(anna::RuntimeException);
+  void eventDPA(anna::diameter::comm::ClientSession *, const anna::DataBlock&) throw(anna::RuntimeException);
 
   // Reimplementation
   int readSocketId(const anna::diameter::comm::Message* message, int maxClientSessions) const throw();
@@ -306,6 +328,7 @@ class MyLocalServer : public anna::diameter::comm::LocalServer {
   void eventResponse(const anna::diameter::comm::Response&) throw(anna::RuntimeException);
   void eventRequest(anna::diameter::comm::ServerSession *, const anna::DataBlock&) throw(anna::RuntimeException);
   void eventUnknownResponse(anna::diameter::comm::ServerSession *, const anna::DataBlock&) throw(anna::RuntimeException);
+  void eventDPA(anna::diameter::comm::ServerSession *, const anna::DataBlock&) throw(anna::RuntimeException);
 };
 
 class MyDiameterEngine : public anna::diameter::comm::Engine {
@@ -374,7 +397,7 @@ class Launcher : public anna::comm::Application {
   anna::diameter::comm::Entity *a_entity;
   std::string a_logFile, a_burstLogFile;
   std::ofstream a_burstLogStream;
-  bool a_splitLog, a_detailedLog;
+  bool a_splitLog, a_detailedLog, a_dumpLog;
   anna::time::Date a_start_time;
   anna::timex::Engine* a_timeEngine;
   MyCounterRecorder *a_counterRecorder;
@@ -843,15 +866,18 @@ std::string Launcher::help() const throw() {
   result += "\nsendxml|<source_file>      Same as 'sendxml2e'.";
   result += "\nanswerxml2e|[source_file]  Answer xml source file (pathfile) for incoming request with same code from entity.";
   result += "\n                           The answer is stored in a FIFO queue for a specific message code, then there are";
-  result += "\n                           as many queues as different message codes have been received.";
+  result += "\n                           as many queues as different message codes have been programmed.";
   result += "\nanswerxml2c|[source_file]  Answer xml source file (pathfile) for incoming request with same code from client.";
   result += "\n                           The answer is stored in a FIFO queue for a specific message code, then there are";
-  result += "\n                           as many queues as different message codes have been received.";
+  result += "\n                           as many queues as different message codes have been programmed.";
   result += "\nanswerxml|[source_file]    Same as 'answerxml2c'.";
   result += "\nanswerxml(2e/2c)           List programmed answers (to entity/client) if no parameter provided.";
-  result += "\nanswerxml(2e/2c)|dump      Write programmed answers (to entity/client) to file 'programmed_answer.<code>.<sequence>',";
+  result += "\nanswerxml(2e/2c)|dump      Write programmed answers (to entity/client) to file 'programmed_answer.<message code>.<sequence>',";
   result += "\n                           where 'sequence' is the order of the answer in each FIFO code-queue of programmed answers.";
   result += "\nanswerxml(2e/2c)|clear     Clear programmed answers (to entity/client).";
+  result += "\nanswerxml(2e/2c)|exhaust   Disable the corresponding queue rotation, which is the default behaviour.";
+  result += "\nanswerxml(2e/2c)|rotate    Enable the corresponding queue rotation, useful in performance tests.";
+  result += "\n                           Rotation consists in add again to the queue, each element retrieved for answering.";
   result += "\n";
   result += "\nSend operations are available using hexadecimal content (hex formatted files) which also allow to test";
   result += "\nspecial scenarios (protocol errors):";
@@ -861,8 +887,8 @@ std::string Launcher::help() const throw() {
   result += "\nsendhex|<source_file>      Same as 'sendhex2e'.";
   result += "\n";
   result += "\nAnswer programming in hexadecimal is not really neccessary (you could use send primitives) and also";
-  result += "\nis intended to be used with decoded messages in order to replace things like hop by hop, end to end,";
-  result += "\nsubscriber id, session id, etc. Anyway you could use 'decode' operation and then program the xml created.";
+  result += "\n is intended to be used with decoded messages in order to replace things like hop by hop, end to end,";
+  result += "\n subscriber id, session id, etc. Anyway you could use 'decode' operation and then program the xml created.";
   result += "\n";
   result += "\nIf a request is received, answer map (built with 'answerxml<[2c] or 2e>' operations) will be";
   result += "\n checked to find a corresponding programmed answer to be replied(*). If no ocurrence is found,";
@@ -870,6 +896,11 @@ std::string Launcher::help() const throw() {
   result += "\n or nothing but trace when no peer at that side is configured. Answer to client have sense when";
   result += "\n diameter server socket is configured, answer to entity have sense when entity does.";
   result += "\n";
+  result += "\nIn the most complete situation (process with both client and server side) there are internally";
+  result += "\n two maps with N FIFO queues, one for each different message code within programmed answers.";
+  result += "\nOne map is for answers towards the client, and the other is to react entity requests. Then in";
+  result += "\n each one we could program different answers corresponding to different request codes received.";
+  result += "\n";
   result += "\n(*) sequence values (hop-by-hop and end-to-end), Session-Id and Subscription-Id avps, are mirrored";
   result += "\n    to the peer which sent the request. If user wants to test a specific answer without changing it,";
   result += "\n    use sendxml/sendhex operations better than programming.";
@@ -1040,6 +1071,7 @@ int main(int argc, const char** argv) {
     commandLine.add("log", anna::CommandLine::Argument::Optional, "Process log file (operations result, traffic log, etc.). By default 'launcher.log'. Empty string or \"null\" name, to disable. Warning: there is no rotation for log files (use logrotate or whatever)");
     commandLine.add("splitLog", anna::CommandLine::Argument::Optional, "Splits log file (appends to log filename, extensions with the type of event: see help on startup information-level traces). No log files for code/decode and load operations are created", false);
     commandLine.add("detailedLog", anna::CommandLine::Argument::Optional, "Insert detailed information at log files. Should be disabled on automatic tests. Useful on '-balance' mode to know messages flow along the sockets", false);
+    commandLine.add("dumpLog", anna::CommandLine::Argument::Optional, "Write to disk every incoming/outcoming message named as '<hop by hop>.<end to end>.<message code>.<request|answer>.xml'", false);
     commandLine.add("logStatisticSamples", anna::CommandLine::Argument::Optional, "Log statistics samples for the provided concept id list, over './sample.<concept id>.csv' files. For example: \"1,2\" will log concepts 1 and 2. Reserved word \"all\" activates all registered statistics concept identifiers. That ids are shown at context dump (see help to get it).");
     commandLine.add("burstLog", anna::CommandLine::Argument::Optional, "Burst operations log file. By default 'launcher.burst'. Empty string or \"null\" name, to disable. Warning: there is no rotation for log files (use logrotate or whatever). Output: dot (.) for each burst message sent/pushed, cross (x) for popped ones, and order number when multiple of 1% of burst list size, plus OTA requests when changed.");
     commandLine.add("cntDir", anna::CommandLine::Argument::Optional, "Counters directory. By default is the current execution directory. Warning: a counter file will be dump per record period; take care about the possible accumulation of files");
@@ -1088,6 +1120,7 @@ Launcher::Launcher() : anna::comm::Application("launcher", "DiameterLauncher", "
   a_burstLogFile = "launcher.burst";
   a_splitLog = false;
   a_detailedLog = false;
+  a_dumpLog = false;
   a_timeEngine = NULL;
   a_counterRecorder = NULL;
   a_counterRecorderClock = NULL;
@@ -1264,6 +1297,8 @@ void Launcher::writeLogFile(const anna::diameter::codec::Message & decodedMessag
   title += "]";
   // Build complete log:
   std::string log = "\n";
+  std::string xml = decodedMessage.asXMLString();
+
 
   if(a_detailedLog) {
     anna::time::Date now;
@@ -1271,7 +1306,7 @@ void Launcher::writeLogFile(const anna::diameter::codec::Message & decodedMessag
     title += " ";
     title += now.asString();
     log += anna::functions::highlight(title, anna::functions::TextHighlightMode::OverAndUnderline);
-    log += decodedMessage.asXMLString();
+    log += xml;
     log += "\n";
     log += anna::functions::highlight("Used resource");
     log += detail;
@@ -1279,10 +1314,23 @@ void Launcher::writeLogFile(const anna::diameter::codec::Message & decodedMessag
   } else {
     log += title;
     log += "\n";
-    log += decodedMessage.asXMLString();
+    log += xml;
     log += "\n";
   }
 
+  if(a_dumpLog) {
+    std::string name = anna::functions::asString(decodedMessage.getHopByHop());
+    name += ".";
+    name += anna::functions::asString(decodedMessage.getEndToEnd());
+    name += ".";
+    name += anna::functions::asString(decodedMessage.getId().first);
+    name += ".";
+    name += ((decodedMessage.getId().second) ? "request.xml":"answer.xml");
+    ofstream outMsg(name.c_str(), ifstream::out | ifstream::app);
+    outMsg.write(xml.c_str(), xml.size());
+    outMsg.close();
+  }
+
   // Write and close
   out.write(log.c_str(), log.size());
   out.close();
@@ -1646,6 +1694,8 @@ throw(anna::RuntimeException) {
 
   if(cl.exists("detailedLog")) a_detailedLog = true;
 
+  if(cl.exists("dumpLog")) a_dumpLog = true;
+
   if(cl.exists("burstLog")) a_burstLogFile = cl.getValue("burstLog");
 
   // Log statistics concepts
@@ -2049,11 +2099,13 @@ void Launcher::eventOperation(const std::string &operation, std::string &respons
       throw anna::RuntimeException("Operation not applicable (no own diameter server has been configured)", ANNA_FILE_LOCATION);
 
     if(param1 == "") { // programmed answers FIFO's to stdout
-      std::cout << std::endl << std::endl;
-      std::cout << "          ------------- CURRENT PROGRAMMED ANSWERS TO CLIENT -------------\n\n";
-      std::cout << G_reactingAnswers2C.asString() << std::endl;
+      std::cout << G_reactingAnswers2C.asString("ANSWERS TO CLIENT") << std::endl;
       response_content = "Programmed answers dumped on stdout\n";
       return;
+    } else if (param1 == "rotate") {
+      G_reactingAnswers2C.rotate(true);
+    } else if (param1 == "exhaust") {
+      G_reactingAnswers2C.rotate(false);
     } else if (param1 == "clear") {
       G_reactingAnswers2C.clear();
     } else if (param1 == "dump") {
@@ -2080,11 +2132,13 @@ void Launcher::eventOperation(const std::string &operation, std::string &respons
       throw anna::RuntimeException("Operation not applicable (no diameter entity has been configured)", ANNA_FILE_LOCATION);
 
     if(param1 == "") { // programmed answers FIFO's to stdout
-      std::cout << std::endl << std::endl;
-      std::cout << "          ------------- CURRENT PROGRAMMED ANSWERS TO ENTITY -------------\n\n";
-      std::cout << G_reactingAnswers2E.asString() << std::endl;
+      std::cout << G_reactingAnswers2E.asString("ANSWERS TO ENTITY") << std::endl;
       response_content = "Programmed answers dumped on stdout\n";
       return;
+    } else if (param1 == "rotate") {
+      G_reactingAnswers2C.rotate(true);
+    } else if (param1 == "exhaust") {
+      G_reactingAnswers2C.rotate(false);
     } else if (param1 == "clear") {
       G_reactingAnswers2E.clear();
     } else if (param1 == "dump") {
@@ -2408,8 +2462,27 @@ throw(anna::RuntimeException) {
   if(my_app.logEnabled()) my_app.writeLogFile(message, "recvfe-ans-unknown", clientSession->asString());
 }
 
+void MyDiameterEntity::eventDPA(anna::diameter::comm::ClientSession *clientSession, const anna::DataBlock &message)
+throw(anna::RuntimeException) {
+  LOGMETHOD(anna::TraceMethod tm("launcher::MyDiameterEntity", "eventDPA", ANNA_FILE_LOCATION));
+  // Performance stats:
+  Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
+  // CommandId:
+  anna::diameter::CommandId cid = anna::diameter::codec::functions::getCommandId(message);
+  LOGDEBUG
+  (
+    std::string msg = "Disconnect-Peer-Answer received from entity: ";
+    msg += anna::diameter::functions::commandIdAsPairString(cid);
+    msg += " | DiameterServer: ";
+    msg += anna::functions::socketLiteralAsString(clientSession->getAddress(), clientSession->getPort());
+    msg += " | EventTime: ";
+    msg += anna::time::functions::currentTimeAsString();
+    anna::Logger::debug(msg, ANNA_FILE_LOCATION);
+  );
 
-
+  // Write reception
+  if(my_app.logEnabled()) my_app.writeLogFile(message, "recvfe", clientSession->asString());
+}
 
 void MyLocalServer::eventRequest(anna::diameter::comm::ServerSession *serverSession, const anna::DataBlock &message)
 throw(anna::RuntimeException) {
@@ -2609,6 +2682,26 @@ throw(anna::RuntimeException) {
   if(my_app.logEnabled()) my_app.writeLogFile(message, "recvfc-ans-unknown", serverSession->asString());
 }
 
+void MyLocalServer::eventDPA(anna::diameter::comm::ServerSession *serverSession, const anna::DataBlock &message)
+throw(anna::RuntimeException) {
+  LOGMETHOD(anna::TraceMethod tm("launcher::MyLocalServer", "eventDPA", ANNA_FILE_LOCATION));
+  // Performance stats:
+  Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
+  // CommandId:
+  anna::diameter::CommandId cid = anna::diameter::codec::functions::getCommandId(message);
+  LOGDEBUG
+  (
+    std::string msg = "Disconnect-Peer-Answer response received from client: ";
+    msg += anna::diameter::functions::commandIdAsPairString(cid);
+    msg += " | DiameterServer: ";
+    msg += anna::functions::socketLiteralAsString(serverSession->getAddress(), serverSession->getPort());
+    msg += " | EventTime: ";
+    msg += anna::time::functions::currentTimeAsString();
+    anna::Logger::debug(msg, ANNA_FILE_LOCATION);
+  );
+
+  if(my_app.logEnabled()) my_app.writeLogFile(message, "recvfc", serverSession->asString());
+}
 
 anna::xml::Node* Launcher::asXML(anna::xml::Node* parent) const
 throw() {