Usage of stdint.h types
[anna.git] / example / diameter / launcher / main.cpp
index ab5ea0a..dde2f7c 100644 (file)
@@ -1,37 +1,9 @@
-// ANNA - Anna is Not Nothingness Anymore
-//
-// (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
-//
-// http://redmine.teslayout.com/projects/anna-suite
-//
-// Redistribution and use in source and binary forms, with or without
-// modification, are permitted provided that the following conditions
-// are met:
-//
-//     * Redistributions of source code must retain the above copyright
-// notice, this list of conditions and the following disclaimer.
-//     * Redistributions in binary form must reproduce the above
-// copyright notice, this list of conditions and the following disclaimer
-// in the documentation and/or other materials provided with the
-// distribution.
-//     *  Neither the name of the copyright holder nor the names of its
-// contributors may be used to endorse or promote products derived from
-// this software without specific prior written permission.
-//
-// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
-// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
-// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
-// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
-// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
-// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
-// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-//
-// Authors: eduardo.ramos.testillano@gmail.com
-//          cisco.tierra@gmail.com
+// ANNA - Anna is Not Nothingness Anymore                                                         //
+//                                                                                                //
+// (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo                         //
+//                                                                                                //
+// See project site at http://redmine.teslayout.com/projects/anna-suite                           //
+// See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
 
 
 #include <fstream>
@@ -119,7 +91,7 @@ typedef std::map < int /* message code */, codec_messages_deque* >::const_iterat
     ProgrammedAnswers() { a_rotate = false; }
     ~ProgrammedAnswers() { clear(); }
 
-    bool rotate() const const throw() { return a_rotate; }
+    bool rotate() const throw() { return a_rotate; }
     void rotate(bool r) throw() { a_rotate = r; }
 
     void clear () throw() {
@@ -397,7 +369,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;
@@ -436,6 +408,7 @@ public:
   void writeBurstLogFile(const std::string &buffer) throw();
   bool burstLogEnabled() const throw() { return (((a_burstLogFile == "") || (a_burstLogFile == "null")) ? false : true); }
   void startDiameterServer(int) throw(anna::RuntimeException);
+  void forceCountersRecord() throw(anna::RuntimeException) { if (a_counterRecorderClock) a_counterRecorderClock->tick(); }
 
   anna::xml::Node* asXML(anna::xml::Node* parent) const throw();
   void resetStatistics() throw() { a_myDiameterEngine->resetStatistics(); }
@@ -846,6 +819,7 @@ std::string Launcher::help() const throw() {
   result += "\ncollect                              Reset statistics and counters to start a new test stage of";
   result += "\n                                      performance measurement. Context data is written at";
   result += "\n                                      '/var/tmp/anna.context.<pid>' by mean 'kill -10 <pid>'.";
+  result += "\nforceCountersRecord                  Forces dump to file the current counters of the process.";
   result += "\n";
   result += "\n<visibility action>|[<address>:<port>]|[socket id]";
   result += "\n";
@@ -866,10 +840,10 @@ 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.<message code>.<sequence>',";
@@ -1071,6 +1045,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>.<type of event>.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");
@@ -1098,7 +1073,8 @@ int main(int argc, const char** argv) {
     commandLine.add("originHost", anna::CommandLine::Argument::Optional, "Diameter application host name (system name). If missing, process sets o.s. hostname");
     commandLine.add("originRealm", anna::CommandLine::Argument::Optional, "Diameter application node realm name. If missing, process sets domain name");
     commandLine.add("integrationAndDebugging", anna::CommandLine::Argument::Optional, "Sets validation mode to 'Always' (default validates only after decoding), and validation depth to 'Complete' (default validates until 'FirstError')", false);
-//      commandLine.add("clone", anna::CommandLine::Argument::Optional, "Enables fork mode for request processing", false);
+    commandLine.add("fixMode", anna::CommandLine::Argument::Optional, "Sets message fix mode (unreconized values will assume default 'BeforeEncoding'). Allowed: 'BeforeEncoding', 'AfterDecoding', 'Always', 'Never'");
+
     commandLine.initialize(argv, argc);
     commandLine.verify();
     std::cout << commandLine.asString() << std::endl;
@@ -1119,6 +1095,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;
@@ -1295,6 +1272,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;
@@ -1302,7 +1281,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;
@@ -1310,10 +1289,25 @@ 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.":"answer.");
+    name += logExtension;
+    name += ".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();
@@ -1609,6 +1603,18 @@ throw(anna::RuntimeException) {
     codecEngine->setValidationDepth(anna::diameter::codec::Engine::ValidationDepth::Complete);
   }
 
+  // Fix mode
+  if(cl.exists("fixMode")) { // BeforeEncoding(default), AfterDecoding, Always, Never
+    std::string fixMode = cl.getValue("fixMode");
+    anna::diameter::codec::Engine::FixMode::_v fm;
+    if (fixMode == "BeforeEncoding") fm = anna::diameter::codec::Engine::FixMode::BeforeEncoding;
+    else if (fixMode == "AfterDecoding") fm = anna::diameter::codec::Engine::FixMode::AfterDecoding;
+    else if (fixMode == "Always") fm = anna::diameter::codec::Engine::FixMode::Always;
+    else if (fixMode == "Never") fm = anna::diameter::codec::Engine::FixMode::Never;
+    else LOGINFORMATION(anna::Logger::information("Unreconized command-line fix mode. Assumed default 'BeforeEncoding'", ANNA_FILE_LOCATION));
+    codecEngine->setFixMode(fm);
+  }
+
   codecEngine->ignoreFlagsOnValidation(cl.exists("ignoreFlags"));
 
   // Diameter Server:
@@ -1677,6 +1683,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
@@ -1801,6 +1809,13 @@ void Launcher::eventOperation(const std::string &operation, std::string &respons
     return;
   }
 
+  // Counters dump on demand:
+  if(operation == "forceCountersRecord") {
+    forceCountersRecord();
+    response_content = "Current counters have been dump to disk\n";
+    return;
+  }
+
   ///////////////////////////////////////////////////////////////////
   // Tokenize operation
   Tokenizer params;
@@ -2194,14 +2209,14 @@ void Launcher::eventOperation(const std::string &operation, std::string &respons
     response_content += "' loaded.";
     response_content += "\n";
   } else if((opType == "answerxml") || (opType == "answerxml2c")) {
-    response_content += "Answer to client '";
+    response_content += "'";
     response_content += param1;
-    response_content += "' programmed.";
+    response_content += "' applied on server FIFO queue";
     response_content += "\n";
   } else if(opType == "answerxml2e") {
-    response_content += "Answer to entity '";
+    response_content += "'";
     response_content += param1;
-    response_content += "' programmed.";
+    response_content += "' applied on client FIFO queue";
     response_content += "\n";
   } else if(opType == "diameterServerSessions") {
     response_content += "Maximum server socket connections updated to '";
@@ -2315,6 +2330,7 @@ throw(anna::RuntimeException) {
   if(localServer && (cid != anna::diameter::helpers::base::COMMANDID__Capabilities_Exchange_Request) /* don't forward CER */) {
     try {
       anna::diameter::comm::Message *msg = G_commMessages.create();
+      msg->updateEndToEnd(false); // end-to-end will be kept
       msg->setBody(message);
       msg->setRequestClientSessionKey(clientSession->getKey());
       bool success = localServer->send(msg);
@@ -2397,6 +2413,7 @@ throw(anna::RuntimeException) {
 
     if(localServer && (request_cid != anna::diameter::helpers::base::COMMANDID__Capabilities_Exchange_Request) /* don't forward CEA */) {
       try {
+        G_commMsgFwd2c.updateEndToEnd(false); // end-to-end will be kept
         G_commMsgFwd2c.setBody(*message);
         bool success = localServer->send(&G_commMsgFwd2c, request->getRequestServerSessionKey());
         G_commMessages.release(request);
@@ -2495,6 +2512,7 @@ throw(anna::RuntimeException) {
   anna::diameter::comm::Entity *entity = my_app.getEntity();
   if(!programmed && entity) {  // forward condition (no programmed answer + entity available)
     anna::diameter::comm::Message *msg = G_commMessages.create();
+    msg->updateEndToEnd(false); // end-to-end will be kept
     msg->setBody(message);
     msg->setRequestServerSessionKey(serverSession->getKey());
     bool success = entity->send(msg, cl.exists("balance"));
@@ -2622,6 +2640,7 @@ throw(anna::RuntimeException) {
       if(my_app.logEnabled()) detail = usedClientSession ? usedClientSession->asString() : "<null client session>";  // esto no deberia ocurrir
 
       try {
+        G_commMsgFwd2e.updateEndToEnd(false); // end-to-end will be kept
         G_commMsgFwd2e.setBody(*message);
 
         // Metodo 1: