Add help url
[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    Could be proxied (end-to-end kept) or not (by default).
44 */
45 class Message : public anna::comm::Message {
46 public:
47
48   /**
49    * Define las acciones a realizar en caso de que el temporizador de la petici�n expire.
50    */
51   struct OnExpiry { enum _v { Abandon, Ignore }; };
52
53   /**
54      Constructor.
55      \param onExpiry Indica la acci�n a realizar si el temporizador de esta transaci�n expira.
56   */
57   Message(const OnExpiry::_v onExpiry = OnExpiry::Ignore) : anna::comm::Message(StatusCodeBuffer::Reserve),
58     a_classCode(ClassCode::ApplicationMessage),
59     a_onExpiry(onExpiry) { initialize(); }
60
61   /**
62      Devuelve el tipo de la clase de esta peticion indicada en el contructor.
63      \return El tipo de la clase de esta peticion indicada en el contructor.
64   */
65   const ClassCode::_v & getClassCode() const throw() { return a_classCode; }
66
67   /**
68    * Devuelve la acci�n a realizar en caso de que el temporizador asociado a esta petici�n expire.
69    * \return la acci�n a realizar en caso de que el temporizador asociado a esta petici�n expire.
70    */
71   OnExpiry::_v getOnExpiry() const throw() { return a_onExpiry; }
72
73   /**
74    * Establece la acci�n a realizar en caso de que el temporizador asociado a esta petici�n expire.
75    * \param onExpiry Indica la acci�n a realizar en caso de que el temporizador asociado a esta petici�n expire.
76    *
77    * \warning Establecer el valor OnExpiry::Ignore podr�a causar p�rdida de memoria y uso innecesario de recursos.
78    */
79   void setOnExpiry(const OnExpiry::_v onExpiry) throw() { a_onExpiry = onExpiry; }
80
81   // Internal use (CER message)
82   void setClassCode(const ClassCode::_v & classCode) throw() { a_classCode = classCode; }
83
84
85   /**
86      Class string representation
87      \return String with relevant information for this instance.
88   */
89   virtual std::string asString() const throw();
90
91   /**
92      Class xml representation
93      \param parent Parent XML node on which hold this instance information.
94      \return XML document with relevant information for this instance.
95   */
96   virtual anna::xml::Node* asXML(anna::xml::Node* parent) const throw();
97
98
99   // Helpers
100   HopByHop getHopByHop() const throw();
101   EndToEnd getEndToEnd() const throw();
102   HopByHop getRequestHopByHop() const throw() { return a_requestHopByHop; }
103   EndToEnd getRequestEndToEnd() const throw() { return a_requestEndToEnd; }
104   void setRequestHopByHop(HopByHop hbh) throw() { a_requestHopByHop = hbh; }
105   void setRequestEndToEnd(EndToEnd ete) throw() { a_requestEndToEnd = ete; }
106   CommandId getCommandId(bool &isRequest) const throw();
107   CommandId getCommandId() const throw() { bool dummy; return getCommandId(dummy); }
108
109   bool fixRequestSequence(HopByHop hbh, EndToEnd ete) throw();
110
111   // http://diameter-protocol.blogspot.com.es/2011/05/diameter-message-structure-and-message.html
112
113   /**
114    * In general, diameter agents CANNOT modify the end-to-end value during sending the message to the peer.
115    * That 'true' value stands for intermediate agents and also for retransmissions (must keep end-to-end
116    * during 4 minutes even upon reboots). False is used for new request created from end points (originators)
117    * as diameter clients.
118    */
119   bool updateEndToEnd() const throw() { return a_updateEndToEnd; }
120
121   /**
122    * In general, diameter agents CANNOT modify the end-to-end value during sending the message to the peer.
123    * The appropiate behaviour must be configured before sending the message. By default, the diameter::comm
124    * message will sequence the end-to-end increasing the initial value created during session establishment.
125    */
126   void updateEndToEnd(bool update) throw() { a_updateEndToEnd = update; }
127
128
129   // Statistics
130   void updateRequestTimestampMs(void) throw() { a_request_timestamp_ms = anna::functions::millisecond(); }
131   const anna::Millisecond & getRequestTimestampMs() const throw() { return (a_request_timestamp_ms); }
132
133   int getRetries() const throw() { return a_retries; }
134   void setRetries(int value) throw() { a_retries = value; }
135
136
137   int getRequestServerSessionKey() const throw() { return a_requestServerSessionKey; }
138
139   /** 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) */
140   void setRequestServerSessionKey(int value) throw() { a_requestServerSessionKey = value; }
141
142   const std::string & getRequestClientSessionKey() const throw() { return a_requestClientSessionKey; }
143
144   /** 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) */
145   void setRequestClientSessionKey(const std::string & value) throw() { a_requestClientSessionKey = value; }
146
147   /** Initializes class information */
148   void initialize() throw() {
149     a_retries = 0;
150     a_requestServerSessionKey = -1; // means unknown/unset
151     a_requestClientSessionKey = ""; // means unknown/unset
152     a_requestHopByHop = 0;
153     a_requestEndToEnd = 0;
154     a_updateEndToEnd = true;
155   }
156
157
158 protected:
159   /**
160      Constructor.
161      \param classCode Tipo de clase de esta peticion.
162      \param onExpiry Indica la acci�n a realizar si el temporizador de esta transaci�n expira.
163   */
164   Message(const ClassCode::_v & classCode, const OnExpiry::_v onExpiry = OnExpiry::Ignore) : anna::comm::Message(StatusCodeBuffer::Reserve),
165     a_classCode(classCode),
166     a_onExpiry(onExpiry) { initialize(); }
167
168
169 private:
170   ClassCode::_v a_classCode;
171   OnExpiry::_v a_onExpiry;
172   anna::Millisecond a_request_timestamp_ms;    // Lapsed timestamp milliseconds at request event. Used for statistic purposes.
173   int a_retries;
174   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)
175   std::string a_requestClientSessionKey;    // idem for request which was received from servers
176   HopByHop a_requestHopByHop; // application backup for hop-by-hop in order to restore on answer receive
177   EndToEnd a_requestEndToEnd; // application backup for end-to-end in order to restore on answer receive
178   bool a_updateEndToEnd; // end-to-end will be updated
179
180   void send(ClientSession&) const throw(anna::RuntimeException);
181   void send(ServerSession&) const throw(anna::RuntimeException);
182   void restoreSequencesAfterFix() throw();
183
184   static const char* asText(const OnExpiry::_v) throw();
185
186   friend class Session;
187   friend class ClientSession;
188   friend class ServerSession;
189 };
190
191 }
192 }
193 }
194
195 #endif
196