Updated license
[anna.git] / include / anna / diameter.comm / ServerSession.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_ServerSession_hpp
38 #define anna_diameter_comm_ServerSession_hpp
39
40
41 // STL
42 #include <string>
43
44 #include <anna/core/util/Millisecond.hpp>
45 #include <anna/core/RuntimeException.hpp>
46
47 #include <anna/diameter.comm/Session.hpp>
48 #include <anna/diameter/defines.hpp>
49 #include <anna/diameter.comm/Message.hpp>
50 #include <anna/diameter.comm/ServerSessionReceiver.hpp>
51 #include <anna/diameter.comm/ReceiverFactoryImpl.hpp>
52
53
54 namespace anna {
55 class DataBlock;
56 namespace timex {
57 class Engine;
58 }
59
60 namespace comm {
61 class ClientSocket;
62 }
63 }
64
65
66 namespace anna {
67
68 namespace diameter {
69
70 namespace comm {
71
72
73 class LocalServer;
74
75
76 /**
77    Modela la conexion realizada contra un servidor diameter local.
78 */
79 class ServerSession : public Session {
80 public:
81
82   ServerSession();
83
84
85   /* virtual */void initialize() throw();
86
87   /**
88    * Default max inactivity period for the diameter server-session health.
89    */
90   static const anna::Millisecond DefaultAllowedInactivityTime;
91
92   /**
93      Sets the maximum allowed inactivity time on server session
94
95      @param allowedInactivityTime Inactivity time allowed
96   */
97   void setAllowedInactivityTime(const anna::Millisecond & allowedInactivityTime) throw();
98
99   /**
100      Diameter listening address (ip or hostname).
101      \return Diameter listening address.
102   */
103   /* virtual */const std::string& getAddress() const throw();
104
105   /**
106      Diameter listen port.
107      \return Diameter listen port.
108   */
109   /* virtual */int getPort() const throw();
110
111   /**
112      Server session key. Same as socket id
113   */
114   int getKey() const throw() { return getSocketId(); }
115
116   /**
117      Local server parent
118      \return Local server parent
119   */
120   LocalServer *getParent() throw() { return a_parent; }
121
122
123   /**
124      Sets the diameter client socket and assign the receiver factory to it
125      \param clientSocket Diameter client socket
126   */
127   void setClientSocket(anna::comm::ClientSocket *clientSocket) throw();
128
129   /**
130      Diameter client socket
131      \return Diameter client socket
132   */
133   anna::comm::ClientSocket *getClientSocket() throw() { return a_clientSocket; }
134
135 //   /**
136 //      Sets deprecated state to this server session
137 //   */
138 //   void setDeprecated(bool deprecated = true) throw() { a_deprecated = deprecated; }
139
140 //   /**
141 //    * Sets CEA and DWA diameter messages to be used over created server-sessions
142 //    *
143 //    * @param cea Capabilities-Exchange-Answer message (encoded) for the server-sessions bind.
144 //    * @param dwa Device-Watchdog-Answer message (encoded) for the server-sessions keep-alive.
145 //    */
146 //   void setCEAandDWA(const anna::DataBlock & cea, const anna::DataBlock & dwa) throw(anna::RuntimeException);
147
148   /* virtual */const Response* send(const Message* message) throw(anna::RuntimeException);
149   /* virtual */bool unbind(bool forceDisconnect /* se usa en timer, para el actionTimer del tipo SessionUnbind, etc. */ = false) throw(anna::RuntimeException); // returns true if done at call time (no pendings or ignore pendings, except Disconnecting state by mean DPR/DPA)
150
151
152   /**
153      Class string representation
154      \return String with relevant information for this instance.
155   */
156   /* virtual */std::string asString() const throw();
157
158
159   /**
160      Class xml representation
161      \param parent Parent XML node on which hold this instance information.
162      \return XML document with relevant information for this instance.
163   */
164   /* virtual */anna::xml::Node* asXML(anna::xml::Node* parent) const throw();
165
166 protected:
167
168   // Deprecated state
169   bool a_deprecated;
170
171 private:
172
173   // Receiver factory
174   ReceiverFactoryImpl<ServerSession, ServerSessionReceiver> a_receiverFactory;
175
176   // Parent information
177   LocalServer *a_parent;
178
179   // Client Socket
180   anna::comm::ClientSocket *a_clientSocket;
181
182   // Auxiliary messages:
183   Message a_cer, a_dwr;
184
185   /* virtual */void expire(anna::timex::Engine *timeController) throw(anna::RuntimeException);
186
187   // Activity:
188   /* virtual */void updateIncomingActivityTime() throw();
189   /* virtual */void updateOutgoingActivityTime() throw();
190   void countSendings(const diameter::CommandId & cid, bool ok) throw();
191
192   // Handlers:
193   /**
194      Handler about event break connection from diameter client over this server-session.
195
196      When notified, ANNA.diameter.comm generates an diameter::comm::ServerSession::eventResponse for every request with pending answers.
197   */
198   void eventPeerShutdown() throw();
199
200   /**
201      Handler for diameter client responses
202
203      \param response Answer container object for corresponding diameter request
204   */
205   void eventResponse(const Response& response) throw(anna::RuntimeException);
206
207   /**
208      Handler for diameter client requests
209
210      \param request Request data block object for corresponding diameter reception
211   */
212   void eventRequest(const anna::DataBlock& request) throw(anna::RuntimeException);
213   //void eventRequest(const Message& request) throw(anna::RuntimeException);
214
215   /**
216      Handler for diameter client responses out of context
217
218      \param response Answer data block object without context match
219   */
220   void eventUnknownResponse(const anna::DataBlock& response) throw(anna::RuntimeException);
221
222
223   /**
224   * Handlers for receptions
225   */
226   /* virtual */void receive(const anna::comm::Message& message) throw(anna::RuntimeException);
227   /* virtual */void finalize() throw();
228
229   /* virtual */void expireResponse(Response*) throw();
230   void sendCEA() throw(anna::RuntimeException);
231   void sendDWA() throw(anna::RuntimeException);
232
233
234   friend class anna::diameter::comm::Timer;
235   friend class LocalServer;
236   friend class Engine;
237   friend class ServerSessionReceiver;
238 };
239
240 }
241 }
242 }
243
244 #endif
245