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