Updated license
[anna.git] / include / anna / diameter.comm / Response.hpp
1 // ANNA - Anna is Not Nothingness Anymore
2 //
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
4 //
5 // https://bitbucket.org/testillano/anna
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
9 // are met:
10 //
11 //     * Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 //     * Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following disclaimer
15 // in the documentation and/or other materials provided with the
16 // distribution.
17 //     * Neither the name of Google Inc. nor the names of its
18 // contributors may be used to endorse or promote products derived from
19 // this software without specific prior written permission.
20 //
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 //
33 // Authors: eduardo.ramos.testillano@gmail.com
34 //          cisco.tierra@gmail.com
35
36
37 #ifndef anna_diameter_comm_Response_hpp
38 #define anna_diameter_comm_Response_hpp
39
40
41 #include <anna/core/util/Recycler.hpp>
42 #include <anna/core/Allocator.hpp>
43 #include <anna/core/RuntimeException.hpp>
44
45 // Local
46 #include <anna/diameter/defines.hpp>
47 #include <anna/diameter.comm/ClassCode.hpp>
48
49
50 namespace anna {
51 class DataBlock;
52 }
53
54
55 namespace anna {
56
57 namespace diameter {
58
59 namespace comm {
60
61 class Session;
62 class Timer;
63 class Message;
64
65 /**
66    Answers that we could receive from diameter servers.
67
68    La respuesta correspondiente a una peticion se genera automaticamente al invocar a diameter::comm::Session::send.
69    ANNA::diameter::comm notifies el resultado de la operacion solicitada mediante la invocacion
70    al metodo-menejador diameter::comm::Session::eventResponse de nuestra sesion.
71 */
72 class Response {
73 public:
74
75   struct ResultCode {
76     enum _v {
77       Undefined,
78       Success,
79       Timeout, /**< answer timeout from peer, or transport failure with OTA request notification of timeout event deferred by configurtion */
80       DiameterUnavailable /**< Diameter server was unavailable and notification is not deferred to timeout event for OTA requests */
81     };
82   };
83
84   /**
85      Devuelve el tipo de la clase de esta respuesta.
86      \return El tipo de la clase de esta respuesta.
87   */
88   const ClassCode::_v & getClassCode() const throw() { return a_classCode; }
89
90   /**
91      Devuelve la identificacion del mensaje diameter.
92      Esta identificacion sera generada automaticamente al enviar la peticion.
93      \return Identificacion del mensaje asociado a esta peticion/respuesta.
94      \see diameter::comm::Session::send
95   */
96   HopByHop getHopByHop() const throw() { return a_hopByHop; }
97
98   /**
99      Devuelve la sesion que genera esta respuesta.
100      \return La instancia de la sesion que genera esta respuesta.
101   */
102   const Session* getSession() const throw() { return a_session; }
103
104   /**
105      Devuelve la sesion que origino la creacion de esta respuesta.
106      \return La sesion que origino la creacion de esta respuesta.
107   */
108   Session* getSession() throw() { return a_session; }
109
110
111   /**
112      Devuelve el resultado de la peticion diameter solicitada.
113      \return El resultado de la peticion diameter solicitada.
114   */
115   const ResultCode::_v & getResultCode() const throw() { return a_resultCode; }
116
117
118   /**
119      Returns original request for the response received from the network.
120      \see diameter::comm::Session::send
121   */
122   const Message* getRequest() const throw() { return a_request; }
123
124   /**
125      Returns message received from the network.
126      \see diameter::comm::Session::send
127   */
128   const anna::DataBlock *getMessage() const throw() { return a_message; }
129
130   // helpers
131
132 //   /**
133 //      Reponse regarding diameter keepalive
134 //   */
135 //   bool isKeepAlive() const throw();
136
137   /**
138      Devuelve una cadena con la informacion relevante sobre esta instancia.
139      \return Una cadena con la informacion relevante sobre esta instancia.
140   */
141   std::string asString() const throw();
142
143 private:
144   ClassCode::_v a_classCode;
145   HopByHop a_hopByHop;
146   Session* a_session;
147   ResultCode::_v a_resultCode;
148   Timer* a_timer;
149   const Message* a_request; // associated original request
150   const anna::DataBlock *a_message; // received datablock from server
151
152   typedef anna::Recycler<Response> response_pool;
153   static response_pool st_responses;
154
155   Response();
156
157   static Response* instance(const ClassCode::_v &, const HopByHop) throw(anna::RuntimeException);
158   static void release(Response* response) throw();
159
160   void clear() throw();
161
162   void setSession(Session* session) throw() { a_session = session; }
163   void setRequest(const Message* request) throw() { a_request = request; }
164   void setMessage(const anna::DataBlock *message) throw() { a_message = message; }
165   void activateTimer() throw(anna::RuntimeException);
166   void cancelTimer() throw();
167   void setResultCode(const ResultCode::_v & resultCode) throw() { a_resultCode = resultCode; }
168
169   static const char* asText(const ResultCode::_v) throw();
170
171   friend class Session;
172   friend class ClientSession;
173   friend class ServerSession;
174   friend class Timer;
175   friend class anna::Allocator<Response>;
176 };
177
178 }
179 }
180 }
181
182 #endif
183