Remove warnings
[anna.git] / source / diameter.comm / ServerSession.cpp
index ba40dff..e47170a 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 <anna/core/functions.hpp>
@@ -50,6 +22,7 @@
 #include <anna/diameter.comm/Response.hpp>
 #include <anna/diameter.comm/Message.hpp>
 #include <anna/diameter.comm/OamModule.hpp>
+#include <anna/diameter.comm/ApplicationMessageOamModule.hpp>
 #include <anna/diameter.comm/TimerManager.hpp>
 #include <anna/diameter.comm/Timer.hpp>
 #include <anna/diameter.comm/LocalServer.hpp>
@@ -82,7 +55,7 @@ const anna::Millisecond ServerSession::DefaultAllowedInactivityTime(90000); // I
 ServerSession::ServerSession() : Session("diameter::comm::ServerSession", "Diameter Inactivity Detection Timer"),
   a_receiverFactory(this),
   a_cer(ClassCode::Bind),
-  a_dwr(ClassCode::ApplicationMessage) // realmente no es necesario, los Message son por defecto de aplicacion
+  a_dwr(ClassCode::ApplicationMessage) // not actually needed; Message is application type by default
 { initialize(); }
 
 void ServerSession::initialize() throw() {
@@ -108,7 +81,6 @@ int ServerSession::getPort() const throw() {
   return a_parent->getKey().second;
 }
 
-
 const Response* ServerSession::send(const Message* message) throw(anna::RuntimeException) {
   LOGMETHOD(anna::TraceMethod traceMethod(a_className, "send", ANNA_FILE_LOCATION));
 
@@ -118,6 +90,8 @@ const Response* ServerSession::send(const Message* message) throw(anna::RuntimeE
   // Command id:
   bool isRequest;
   diameter::CommandId cid = message->getCommandId(isRequest);
+  diameter::ApplicationId aid = message->getApplicationId();
+
   LOGDEBUG(
     std::string msg = "Sending diameter message: ";
     msg += anna::diameter::functions::commandIdAsPairString(cid);
@@ -213,7 +187,7 @@ const Response* ServerSession::send(const Message* message) throw(anna::RuntimeE
 
   if(isRequest) {
     // Fixing indicator:
-    fixed = message_nc->fixRequestSequence(a_nextHopByHop, a_nextEndToEnd, a_engine->getFreezeEndToEndOnSending());
+    fixed = message_nc->fixRequestSequence(a_nextHopByHop, a_nextEndToEnd);
     message_nc->updateRequestTimestampMs(); // statistics purposes (processing time for request type)
   }
 
@@ -236,15 +210,22 @@ const Response* ServerSession::send(const Message* message) throw(anna::RuntimeE
     //
     updateOutgoingActivityTime();
     // OAM
-    countSendings(cid, true /* send ok */);
+    countSendings(cid, aid, true /* send ok */);
     // Trace non-application messages:
     LOGDEBUG(
 
-      if((cid == helpers::base::COMMANDID__Device_Watchdog_Request) ||
-    (cid == helpers::base::COMMANDID__Disconnect_Peer_Request)) {
-    anna::Logger::debug("Sent DataBlock to XML representation:", ANNA_FILE_LOCATION);
-      try { anna::diameter::codec::Message msg; msg.decode(message->getBody()); /* decode to be traced */ } catch(anna::RuntimeException&) {;}
-    }
+      if( (cid == helpers::base::COMMANDID__Device_Watchdog_Request) ||
+          (cid == helpers::base::COMMANDID__Disconnect_Peer_Request)) {
+        anna::Logger::debug("Sent DataBlock to XML representation:", ANNA_FILE_LOCATION);
+        try {
+          anna::diameter::codec::Message msg(a_engine->getBaseProtocolCodecEngine()); msg.decode(message->getBody()); /* decode to be traced */
+        }
+        catch(anna::RuntimeException &ex) {
+          std::string msg = ex.getText();
+          msg += " | Use diameter::comm::Engine::setBaseProtocolCodecEngine() to allow internal base protocol messages full tracing";
+          anna::Logger::debug(msg, ANNA_FILE_LOCATION);
+        }
+      }
     );
 
     // Restore sequences:
@@ -253,7 +234,7 @@ const Response* ServerSession::send(const Message* message) throw(anna::RuntimeE
     if(fixed) message_nc->restoreSequencesAfterFix();  // restore to application sequences after fix
 
     // OAM
-    countSendings(cid, false /* send no ok */);
+    countSendings(cid, aid, false /* send no ok */);
     throw;
   }
 
@@ -331,6 +312,17 @@ void ServerSession::eventPeerShutdown() throw() {
   a_parent->eventPeerShutdown(this);
 }
 
+void ServerSession::eventRequestRetransmission(Message *request) throw() {
+
+  // OAM
+  OamModule &oamModule = OamModule::instantiate();
+  oamModule.count(OamModule::Counter::RequestRetransmitted);
+  oamModule.count(OamModule::Counter::RequestRetransmittedOnServerSession);
+
+  // Inform father server:
+  a_parent->eventRequestRetransmission(this, request);
+}
+
 void ServerSession::eventResponse(const Response& response) throw(anna::RuntimeException) {
   // Inform father server:
   a_parent->eventResponse(response);
@@ -346,6 +338,10 @@ void ServerSession::eventUnknownResponse(const anna::DataBlock& response) throw(
   a_parent->eventUnknownResponse(this, response);
 }
 
+void ServerSession::eventDPA(const anna::DataBlock& response) throw(anna::RuntimeException) {
+  // Inform father server:
+  a_parent->eventDPA(this, response);
+}
 
 //------------------------------------------------------------------------------------------
 // Se invoca desde el diameter::comm::Receiver
@@ -365,15 +361,24 @@ throw(anna::RuntimeException) {
     msg += anna::diameter::functions::commandIdAsPairString(cid);
     anna::Logger::debug(msg, ANNA_FILE_LOCATION);
 
-    if((cid == helpers::base::COMMANDID__Capabilities_Exchange_Request) || (cid.first == helpers::base::COMMANDID__Device_Watchdog_Request.first))
-  try { anna::diameter::codec::Message dmsg; dmsg.decode(db); /* decode to be traced */ } catch(anna::RuntimeException&) {;}
-);
+    if( (cid == helpers::base::COMMANDID__Capabilities_Exchange_Request) ||
+        (cid.first == helpers::base::COMMANDID__Device_Watchdog_Request.first)) {
+      try {
+        anna::diameter::codec::Message dmsg(a_engine->getBaseProtocolCodecEngine()); dmsg.decode(db); /* decode to be traced */
+      }
+      catch(anna::RuntimeException &ex) {
+        std::string msg = ex.getText();
+        msg += " | Use diameter::comm::Engine::setBaseProtocolCodecEngine() to allow internal base protocol messages full tracing";
+        anna::Logger::debug(msg, ANNA_FILE_LOCATION);
+      }
+    }
+  );
   // Main counters:
   OamModule &oamModule = OamModule::instantiate();
   oamModule.count(isRequest ? OamModule::Counter::RequestReceived : OamModule::Counter::AnswerReceived);
   oamModule.count(isRequest ? OamModule::Counter::RequestReceivedOnServerSession : OamModule::Counter::AnswerReceivedOnServerSession);
   // Statistic (size)
-  a_parent->updateReceivedMessageSizeStatisticConcept(message.getSize()); // only on reception (application could manage sent sizes)
+  a_parent->updateReceivedMessageSizeStatisticConcept(message.getSize(), cid); // only on reception (application could manage sent sizes)
 
   if(isRequest) {
     // Si recibo un request, el message solo tiene fiable el DataBlock. Como por defecto se construye como ApplicationMessage,
@@ -392,6 +397,9 @@ throw(anna::RuntimeException) {
       }
 
       a_cer.setBody(db);
+      // Basic DRA:
+      getParent()->getEngine()->manageDrDhServerSession(this, true /* register */);
+
       sendCEA();
       //activateTimer(); // Ya se invoca al inicio de este metodo ::receive
       //bool changes = a_parent->refreshAvailability();
@@ -419,6 +427,9 @@ throw(anna::RuntimeException) {
     }
 
     try {
+      // application message counters
+      ApplicationMessageOamModule::instantiate().count(cid.first, anna::diameter::codec::functions::getApplicationId(db), ApplicationMessageOamModule::Counter::Request_Received_AsServer);
+
       eventRequest(db);
     } catch(anna::RuntimeException& ex) {
       ex.trace();
@@ -452,6 +463,9 @@ throw(anna::RuntimeException) {
         doUnbind = true;
       }
     }
+
+    eventDPA(db);
+
   } else if(cid == helpers::base::COMMANDID__Device_Watchdog_Answer) {  // non usual (server should not send DWR's)
     oamModule.count(OamModule::Counter::DWAReceived);
   }
@@ -465,7 +479,12 @@ throw(anna::RuntimeException) {
     oamModule.count(OamModule::Counter::AnswerReceivedUnknown);
     oamModule.count(OamModule::Counter::AnswerReceivedOnServerSessionUnknown);
     oamModule.activateAlarm(OamModule::Alarm::AnswerReceivedOnServerSessionUnknown);
+
+    // application message counters
+    ApplicationMessageOamModule::instantiate().count(cid.first, anna::diameter::codec::functions::getApplicationId(db), ApplicationMessageOamModule::Counter::Answer_UnknownReceived_AsServer);
+
     eventUnknownResponse(db);
+
     string msg(asString());
     msg += anna::functions::asString(" | Response received from client, for non registered context (HopByHop: %u)", hopByHop);
     throw anna::RuntimeException(msg, ANNA_FILE_LOCATION);
@@ -484,14 +503,14 @@ throw(anna::RuntimeException) {
   anna::Millisecond current = (anna::Millisecond)anna::functions::millisecond();
   anna::Millisecond request = response->getRequest()->getRequestTimestampMs();
   anna::Millisecond timeToAnswerMs = current - request;
-  a_parent->updateProcessingTimeStatisticConcept(timeToAnswerMs);
-  LOGDEBUG
-  (
-    std::string msg = "This diameter request context lasted ";
-    msg += anna::functions::asString(timeToAnswerMs);
-    msg += " milliseconds at diameter client (included network time)";
-    anna::Logger::debug(msg, ANNA_FILE_LOCATION);
-  );
+  a_parent->updateProcessingTimeStatisticConcept(timeToAnswerMs, cid);
+  //LOGDEBUG
+  //(
+  //  std::string msg = "This diameter request context lasted ";
+  //  msg += anna::functions::asString(timeToAnswerMs);
+  //  msg += " milliseconds at diameter client (included network time)";
+  //  anna::Logger::debug(msg, ANNA_FILE_LOCATION);
+  //);
   // Progress origin for tracking purposes on asyncronous boxes with both diameter interfaces (entities and clients)
   Message * requestMessage = const_cast<Message*>(response->getRequest());
   requestMessage->setRequestClientSessionKey(response->getRequest()->getRequestClientSessionKey()); // "" means unkown/unset
@@ -511,7 +530,12 @@ throw(anna::RuntimeException) {
       );
       diameter::codec::functions::setHopByHop((anna::DataBlock&)db, response->getRequest()->getRequestHopByHop());
       diameter::codec::functions::setEndToEnd((anna::DataBlock&)db, response->getRequest()->getRequestEndToEnd());
+
+      // application message counters
+      ApplicationMessageOamModule::instantiate().count(cid.first, anna::diameter::codec::functions::getApplicationId(db), ApplicationMessageOamModule::Counter::Answer_Received_AsServer);
+
       eventResponse(*response);
+
     } catch(anna::RuntimeException& ex) {
       ex.trace();
     }
@@ -543,7 +567,7 @@ void ServerSession::finalize() throw() {
   }
 
   // Inform father local server (availability changes):
-  bool changes = getParent()->refreshAvailability();
+  getParent()->refreshAvailability();
   // OAM
   bool multipleConnections = (getParent()->getMaxConnections() > 1);
   std::string socket = anna::functions::socketLiteralAsString(getAddress(), getPort());
@@ -576,7 +600,7 @@ throw(anna::RuntimeException) {
 
   if(cea.isEmpty()) {
     LOGDEBUG(anna::Logger::debug("Empty CEA message. Remote client never will bound this connection at application level", ANNA_FILE_LOCATION));
-    LOGWARNING(anna::Logger::warning("Discarding received CER without sending CEA (consider to send CEA with Result-Code DIAMETER_UNKNOWN_PEER)", ANNA_FILE_LOCATION));
+    LOGWARNING(anna::Logger::warning("Discarding received CER: cannot send empty CEA (consider to send CEA with Result-Code DIAMETER_UNKNOWN_PEER)", ANNA_FILE_LOCATION));
     return;
   }
 
@@ -717,20 +741,26 @@ void ServerSession::updateOutgoingActivityTime(void) throw() {
 //------------------------------------------------------------------------------
 //----------------------------------------------- ServerSession::countSendings()
 //------------------------------------------------------------------------------
-void ServerSession::countSendings(const diameter::CommandId & cid, bool ok)throw() {
+void ServerSession::countSendings(const diameter::CommandId & cid, unsigned int aid, bool ok)throw() {
   OamModule &oamModule = OamModule::instantiate();
+  ApplicationMessageOamModule &appMsgOamModule = ApplicationMessageOamModule::instantiate();
+
   bool isRequest = cid.second;
 
   if(ok) {
     // Main counters:
     oamModule.count(isRequest ? OamModule::Counter::RequestSentOK : OamModule::Counter::AnswerSentOK);
-    oamModule.count(isRequest ? OamModule::Counter::RequestSentOnServerSessionOK : OamModule::Counter::RequestSentOnServerSessionOK);
+    oamModule.count(isRequest ? OamModule::Counter::RequestSentOnServerSessionOK : OamModule::Counter::AnswerSentOnServerSessionOK);
 
     if(cid == helpers::base::COMMANDID__Capabilities_Exchange_Answer) oamModule.count(OamModule::Counter::CEASentOK);
     else if(cid == helpers::base::COMMANDID__Device_Watchdog_Answer) oamModule.count(OamModule::Counter::DWASentOK);
     else if(cid == helpers::base::COMMANDID__Device_Watchdog_Request) oamModule.count(OamModule::Counter::DWRSentOK);  // not usual
     else if(cid == helpers::base::COMMANDID__Disconnect_Peer_Answer) oamModule.count(OamModule::Counter::DPASentOK);
     else if(cid == helpers::base::COMMANDID__Disconnect_Peer_Request) oamModule.count(OamModule::Counter::DPRSentOK);
+    // Application messages:
+    else {
+      appMsgOamModule.count(cid.first, aid, isRequest ? ApplicationMessageOamModule::Counter::Request_SentOK_AsServer : ApplicationMessageOamModule::Counter::Answer_SentOK_AsServer);
+    }
   } else {
     // Main counters:
     oamModule.count(isRequest ? OamModule::Counter::RequestSentNOK : OamModule::Counter::AnswerSentNOK);
@@ -741,6 +771,10 @@ void ServerSession::countSendings(const diameter::CommandId & cid, bool ok)throw
     else if(cid == helpers::base::COMMANDID__Device_Watchdog_Request) oamModule.count(OamModule::Counter::DWRSentNOK);  // not usual
     else if(cid == helpers::base::COMMANDID__Disconnect_Peer_Answer) oamModule.count(OamModule::Counter::DPASentNOK);
     else if(cid == helpers::base::COMMANDID__Disconnect_Peer_Request) oamModule.count(OamModule::Counter::DPRSentNOK);
+    // Application messages:
+    else {
+      appMsgOamModule.count(cid.first, aid, isRequest ? ApplicationMessageOamModule::Counter::Request_SentNOK_AsServer : ApplicationMessageOamModule::Counter::Answer_SentNOK_AsServer);
+    }
   }
 }