Changed LICENSE. Now referenced to web site and file on project root directory
[anna.git] / include / anna / diameter.comm / Message.hpp
1 // ANNA - Anna is Not Nothingness Anymore                                                         //
2 //                                                                                                //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo                         //
4 //                                                                                                //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite                           //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
7
8
9 #ifndef anna_diameter_comm_Message_hpp
10 #define anna_diameter_comm_Message_hpp
11
12 // Local
13 #include <anna/diameter/defines.hpp>
14 #include <anna/diameter.comm/ClassCode.hpp>
15
16 #include <anna/core/RuntimeException.hpp>
17 #include <anna/comm/Message.hpp>
18 #include <anna/core/util/Millisecond.hpp>
19 #include <anna/core/functions.hpp>
20
21
22
23 namespace anna {
24 namespace xml {
25 class Node;
26 }
27 }
28
29
30
31 namespace anna {
32
33 namespace diameter {
34
35 namespace comm {
36
37
38 class ClientSession;
39 class ServerSession;
40
41 /**
42    Messages launched to diameter servers
43 */
44 class Message : public anna::comm::Message {
45 public:
46
47   /**
48    * Define las acciones a realizar en caso de que el temporizador de la petición expire.
49    */
50   struct OnExpiry { enum _v { Abandon, Ignore }; };
51
52   /**
53      Constructor.
54      \param onExpiry Indica la acción a realizar si el temporizador de esta transación expira.
55   */
56   Message(const OnExpiry::_v onExpiry = OnExpiry::Ignore) : anna::comm::Message(StatusCodeBuffer::Reserve),
57     a_classCode(ClassCode::ApplicationMessage),
58     a_onExpiry(onExpiry) { initialize(); }
59
60   /**
61      Devuelve el tipo de la clase de esta peticion indicada en el contructor.
62      \return El tipo de la clase de esta peticion indicada en el contructor.
63   */
64   const ClassCode::_v & getClassCode() const throw() { return a_classCode; }
65
66   /**
67    * Devuelve la acción a realizar en caso de que el temporizador asociado a esta petición expire.
68    * \return la acción a realizar en caso de que el temporizador asociado a esta petición expire.
69    */
70   OnExpiry::_v getOnExpiry() const throw() { return a_onExpiry; }
71
72   /**
73    * Establece la acción a realizar en caso de que el temporizador asociado a esta petición expire.
74    * \param onExpiry Indica la acción a realizar en caso de que el temporizador asociado a esta petición expire.
75    *
76    * \warning Establecer el valor OnExpiry::Ignore podría causar pérdida de memoria y uso innecesario de recursos.
77    */
78   void setOnExpiry(const OnExpiry::_v onExpiry) throw() { a_onExpiry = onExpiry; }
79
80   // Internal use (CER message)
81   void setClassCode(const ClassCode::_v & classCode) throw() { a_classCode = classCode; }
82
83
84   /**
85      Class string representation
86      \return String with relevant information for this instance.
87   */
88   virtual std::string asString() const throw();
89
90   /**
91      Class xml representation
92      \param parent Parent XML node on which hold this instance information.
93      \return XML document with relevant information for this instance.
94   */
95   virtual anna::xml::Node* asXML(anna::xml::Node* parent) const throw();
96
97
98   // Helpers
99   HopByHop getHopByHop() const throw();
100   EndToEnd getEndToEnd() const throw();
101   HopByHop getRequestHopByHop() const throw() { return a_requestHopByHop; }
102   EndToEnd getRequestEndToEnd() const throw() { return a_requestEndToEnd; }
103   void setRequestHopByHop(HopByHop hbh) throw() { a_requestHopByHop = hbh; }
104   void setRequestEndToEnd(EndToEnd ete) throw() { a_requestEndToEnd = ete; }
105   CommandId getCommandId(bool &isRequest) const throw();
106   CommandId getCommandId() const throw() { bool dummy; return getCommandId(dummy); }
107
108   bool fixRequestSequence(HopByHop hbh, EndToEnd ete, bool freezeEndToEnd) throw();
109
110   // Statistics
111   void updateRequestTimestampMs(void) throw() { a_request_timestamp_ms = anna::functions::millisecond(); }
112   const anna::Millisecond & getRequestTimestampMs() const throw() { return (a_request_timestamp_ms); }
113
114   int getRetries() const throw() { return a_retries; }
115   void setRetries(int value) throw() { a_retries = value; }
116
117
118   int getRequestServerSessionKey() const throw() { return a_requestServerSessionKey; }
119
120   /** Application specific socket id to keep origin track for request which came from a specific client, at asyncronous contexts (process with both diameter interfaces: client & entities) */
121   void setRequestServerSessionKey(int value) throw() { a_requestServerSessionKey = value; }
122
123   const std::string & getRequestClientSessionKey() const throw() { return a_requestClientSessionKey; }
124
125   /** Application specific socket id to keep origin track for request which came from a specific server (entity), at asyncronous contexts (process with both diameter interfaces: client & entities) */
126   void setRequestClientSessionKey(const std::string & value) throw() { a_requestClientSessionKey = value; }
127
128   /** Initializes class information */
129   void initialize() throw() {
130     a_retries = 0;
131     a_requestServerSessionKey = -1; // means unknown/unset
132     a_requestClientSessionKey = ""; // means unknown/unset
133     a_requestHopByHop = 0;
134     a_requestEndToEnd = 0;
135   }
136
137
138 protected:
139   /**
140      Constructor.
141      \param classCode Tipo de clase de esta peticion.
142      \param onExpiry Indica la acción a realizar si el temporizador de esta transación expira.
143   */
144   Message(const ClassCode::_v & classCode, const OnExpiry::_v onExpiry = OnExpiry::Ignore) : anna::comm::Message(StatusCodeBuffer::Reserve),
145     a_classCode(classCode),
146     a_onExpiry(onExpiry) { initialize(); }
147
148
149 private:
150   ClassCode::_v a_classCode;
151   OnExpiry::_v a_onExpiry;
152   anna::Millisecond a_request_timestamp_ms;    // Lapsed timestamp milliseconds at request event. Used for statistic purposes.
153   int a_retries;
154   int a_requestServerSessionKey;            // useful to resolve origin of messages when diameter sessions are involved in the other side (if another interface, inherit from this Message class and put/carry context information)
155   std::string a_requestClientSessionKey;    // idem for request which was received from servers
156   HopByHop a_requestHopByHop; // application backup for hop-by-hop in order to restore on answer receive
157   EndToEnd a_requestEndToEnd; // application backup for end-to-end in order to restore on answer receive
158
159   void send(ClientSession&) const throw(anna::RuntimeException);
160   void send(ServerSession&) const throw(anna::RuntimeException);
161   void restoreSequencesAfterFix() throw();
162
163   static const char* asText(const OnExpiry::_v) throw();
164
165   friend class Session;
166   friend class ClientSession;
167   friend class ServerSession;
168 };
169
170 }
171 }
172 }
173
174 #endif
175