Testing library separation: now not in launcher but isolated
[anna.git] / include / anna / diameter.comm / Entity.hpp
index e8e1b95..b372497 100644 (file)
@@ -1,37 +1,9 @@
-// ANNA - Anna is Not 'N' Anymore
-//
-// (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
-//
-// https://bitbucket.org/testillano/anna
-//
-// 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 Google Inc. 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 //
 
 
 #ifndef anna_diameter_comm_Entity_hpp
@@ -47,6 +19,8 @@
 #include <anna/core/RuntimeException.hpp>
 #include <anna/core/util/Millisecond.hpp>
 
+#include <anna/diameter.comm/ClassCode.hpp>
+
 
 namespace anna {
 class DataBlock;
@@ -67,6 +41,7 @@ class Engine;
 class Server;
 class Response;
 class Message;
+class ClientSession;
 
 
 /**
@@ -74,6 +49,16 @@ class Message;
 */
 class Entity {
 
+public:
+
+  /**
+   * Defines behaviour on validation procedure: complete analysis or stop at first validation error over the message (by default)
+   */
+  struct SessionBasedModelsType { enum _v { RoundRobin, SessionIdOptionalPart, SessionIdHighPart, SessionIdLowPart /* default */ }; };
+
+private:
+
+
   std::vector<Server*> a_servers;
   int a_maxServers; // -1 means "no limit to add servers"
   std::string a_description;
@@ -84,6 +69,10 @@ class Entity {
   // Engine
   Engine *a_engine;
 
+  // Balance and socket selection:
+  bool a_balance;
+  SessionBasedModelsType::_v a_sessionBasedModelsType;
+
   // Availability
   bool a_available; // any of the servers must be bound
   void availabilityLost() throw();
@@ -111,6 +100,8 @@ class Entity {
   // Private close/destroy method
   void close(bool destroy) throw(anna::RuntimeException);
 
+  // helpers
+  static const char* asText(const SessionBasedModelsType::_v) throw();
 
 public:
 
@@ -159,6 +150,11 @@ public:
   */
   void setEngine(Engine *e) throw() { a_engine = e; }
 
+  /**
+  * Gets the diameter::comm::Engine
+  * @return Diameter::comm::Engine
+  */
+  Engine *getEngine() const throw() { return a_engine; }
 
   /**
   * Add a server to the entity and create all the servers configured at #setSocketsPerDiameterServer within that server.
@@ -212,6 +208,21 @@ public:
   Server *getLastUsedResource() const throw() { return (a_lastUsedResource); }
 
 
+  /**
+   * Balance over entity servers or use standard behaviour (first primary, secondary if fails, etc.).
+   * New created entities have balance disabled.
+   *
+   * @param balance True or false to enable or disable.
+   */
+   void setBalance (bool balance) throw() { a_balance = balance; }
+
+  /**
+   * Gets the balance mode
+   *
+   * @return True if balance over entity servers is enabled, false if standard behaviour is configured (default).
+   */
+   bool getBalance() const throw() { return a_balance; }
+
 
 // SIMPLE BALANCE or STANDARD documentation version
 //   /**
@@ -236,15 +247,15 @@ public:
 //   */
 
 // OJO: en el caso estandard, no se prueban todas las sessiones de un servidor si tiene mas de una, luego la alarma
-//      generada en caso de error, presupone que las sessiones no usadas, también darían error, lo cual no tiene porque
+//      generada en caso de error, presupone que las sessiones no usadas, tambi�n dar�an error, lo cual no tiene porque
 //      ser cierto. En condiciones normales, los servidores tienen una session, con lo que lo anterior es cierto y el
 //      la practica es lo mas normal.
 
   /**
      Sent a message to the entity. First uses primary server, secondary if fails and so on to the
      last defined resource (server) within entity. Another sending algorithm (non standard) could
-     be enabled (balance boolean parameter): it consist in round-robin server selection to set the
-     first resource in a complete cycle (standard behaviour tries all servers from FIRST defined).
+     be enabled (@setBalance): it consist in round-robin server selection to set the first resource
+     in a complete cycle (standard behaviour tries all servers from FIRST defined).
      Anyway, last used delivery resource could be known through #getLastUsedResource().
 
      When the message is a request, a timer will be set automatically to control the response time.
@@ -252,15 +263,26 @@ public:
      timeout value will be configured at #setClassCodeTimeout.
 
      \param message Message sent.
-     \param balance False by default (standard beaviour), but useful to balance over servers within entity.
 
      @return Boolean about success in send operation. Implies true result when any of the entity servers could
      send the message, and false when neither of the servers was available or fail to send the message (an alarm
      and error counter will be generated in this case). Broadcast try to send all over the resources in spite of
      any fail.
   */
-  bool send(const Message *message, bool balance = false) throw(anna::RuntimeException);
-  bool send(const Message &message, bool balance = false) throw(anna::RuntimeException) { return send(&message, balance); }
+  bool send(const Message *message) throw(anna::RuntimeException);
+  bool send(const Message &message) throw(anna::RuntimeException) { return send(&message); }
+
+  /**
+   * Sets sessions based models type.
+   * \param sessionBasedModelsType Session based models type: RoundRobin, SessionIdLowPart (default), SessionIdHighPart or SessionIdOptionalPart.
+   */
+  void setSessionBasedModelsType(const SessionBasedModelsType::_v sessionBasedModelsType)  throw() { a_sessionBasedModelsType = sessionBasedModelsType; }
+
+  /**
+   * Returns sessions based models type.
+   * \return Session based models type: RoundRobin, SessionIdLowPart, SessionIdHighPart or SessionIdOptionalPart.
+   */
+  SessionBasedModelsType::_v getSessionBasedModelsType() const throw() { return a_sessionBasedModelsType; }
 
 
   /**
@@ -306,7 +328,7 @@ public:
      subscriber and service aware contextual load balancing strategies. By default, Session-Id avp
      is used to select the resource. Session-Id is split into 4 sections: diameter identity, high
      part, low part and optional part. Default implementation analizes 'low' part, returning its
-     value as reference for socket selection.
+     value as reference for socket selection. Use @setSessionBasedModelsType to change this behaviour.
      </pre>
 
      When server is configured as single client session (max client sessions equal to 1), entity will ignore
@@ -441,6 +463,14 @@ protected:
   */
   virtual void eventPeerShutdown(const ClientSession* clientSession) throw();
 
+  /**
+     Handler about a request retransmission over the session.
+     Default implementation traces warning event
+     \param clientSession ClientSession from which retransmission happened
+     \param request Retransmitted request message
+  */
+  virtual void eventRequestRetransmission(const ClientSession* clientSession, Message *request) throw();
+
   /**
      Handler for diameter server (server) responses
 
@@ -465,6 +495,16 @@ protected:
   */
   virtual void eventUnknownResponse(ClientSession *clientSession, const anna::DataBlock& response) throw(anna::RuntimeException) = 0;
 
+  /**
+     Handler for diameter session Disconnect-Peer-Answer messages
+
+     \param clientSession ClientSession from which request has been received
+     \param response Answer data block object without context match
+  */
+  virtual void eventDPA(ClientSession *clientSession, const anna::DataBlock& response) throw(anna::RuntimeException) = 0;
+
+
+
 
   friend class Engine;
   friend class Server;