1 // ANNA - Anna is Not Nothingness Anymore //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo //
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 //
9 #ifndef anna_diameter_comm_ClientSession_hpp
10 #define anna_diameter_comm_ClientSession_hpp
16 #include <anna/core/util/SortedVector.hpp>
17 #include <anna/core/util/Millisecond.hpp>
18 #include <anna/core/RuntimeException.hpp>
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/ClientSessionReceiver.hpp>
24 #include <anna/diameter.comm/ReceiverFactoryImpl.hpp>
49 Modela la conexion realizada contra un servidor diameter.
51 class ClientSession : public Session {
54 static std::string getKey(const std::string & addr, int port, int socketId) {
55 return (anna::functions::asString("%s:%d|%d", addr.c_str(), port, socketId));
58 bool a_hidden; // hide resource for restricted delivery over servers/entities
66 /* virtual */void initialize() ;
69 * Default watchdog period for the diameter client-session health.
71 static const anna::Millisecond DefaultWatchdogPeriod;
74 Client session key: <address ip or hostname>:<remote port>|<socket id: 0..N-1>
76 std::string getKey() const { return ClientSession::getKey(getAddress(), getPort(), getSocketId()); }
79 Diameter server address (IPv4 or hostname)
80 \return Diameter server address
82 /* virtual */const std::string& getAddress() const ;
85 Diameter server listen port
86 \return Diameter server listen port
88 /* virtual */int getPort() const ;
91 Diameter parent server.
92 \return Diameter parent server.
94 const Server *getParent() const { return a_parent; }
97 Diameter server created at diameter::comm::Engine::createClientSession.
98 \return Diameter server
100 anna::comm::Server * getServer() { return a_server; }
103 Disables server resource (avoid the use of the server)
105 void disable() { a_server->disable(); }
108 Sets auto recovery indicator. When a connection is lost, by default it will be recovered automatically.
109 \param autoRecovery Auto recovery indicator. TRue by default.
111 void setAutoRecovery(bool autoRecovery = true) { a_autoRecovery = autoRecovery; a_server->setAutoRecovery(autoRecovery); }
114 Gets the auto recovery indicator for the client connection (client-session).
116 @return Auto recovery indicator.
118 bool getAutoRecovery() const { return a_autoRecovery; }
121 Sets the milliseconds wait to achieve a client connection to server by mean connect primitive.
122 This allow to perform specific configurations (some servers could be slower than others).
123 Changes will be taken into account on the next connect operation.
125 \param maxConnectionDelay Milliseconds wait to get connection
127 void setMaxConnectionDelay(const anna::Millisecond & maxConnectionDelay) { a_server->setMaxConnectionDelay(maxConnectionDelay); }
130 Gets the milliseconds wait to achieve a client connection to server by mean connect primitive.
132 \return Milliseconds wait to get connection
134 const anna::Millisecond & getMaxConnectionDelay() { return a_server->getMaxConnectionDelay(); }
137 void bind() noexcept(false);
139 /* virtual */const Response* send(const Message* message) noexcept(false);
140 /* 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)
143 Deny resource for delivery restriction
145 void hide() { a_hidden = true; }
148 Allow resource for delivery permission
150 void show() { a_hidden = false; }
153 Returns true when client session resource is hidden for application messages delivery
155 bool hidden() const { return a_hidden; }
158 Returns true when client session resource is shown for application messages delivery
160 bool shown() const { return !a_hidden; }
164 Class string representation
165 \return String with relevant information for this instance.
167 /* virtual */std::string asString() const ;
171 Class xml representation
172 \param parent Parent XML node on which hold this instance information.
173 \return XML document with relevant information for this instance.
175 /* virtual */anna::xml::Node* asXML(anna::xml::Node* parent) const ;
181 ReceiverFactoryImpl<ClientSession, ClientSessionReceiver> a_receiverFactory;
183 // Parent information
186 // ClientSession messages:
187 Engine *a_engine; // it is unique for a client session (not in server session, which have one per origin host)
195 anna::comm::Server *a_server;
198 struct WatchdogState {
200 TimerStopped, // Until CEA (bound state), timer is stopped
201 WaitingTimerExpiration, // DWA has been received and we wait for next expiration to send DWR
202 WaitingDWA // DWR has been sent, but DWA hasn't been received yet
205 WatchdogState::_v a_watchdogState;
206 void setWatchdogState(WatchdogState::_v wState) ;
208 /* virtual */void expire(anna::timex::Engine *timeController) noexcept(false);
209 void setWatchdogPeriod(const anna::Millisecond & watchdogPeriod) ;
211 /*virtual*/ void timerStopped() ;
212 /*virtual*/ void timerStarted() ;
216 /* virtual */void updateIncomingActivityTime() ;
217 /* virtual */void updateOutgoingActivityTime() ;
218 void countSendings(const diameter::CommandId & cid, unsigned int aid, bool ok) ;
222 Handler about event break connection from diameter server over this client-session.
224 When notified, ANNA.diameter.comm generates an diameter::comm::ClientSession::eventResponse for every request with pending answers.
226 void eventPeerShutdown() ;
229 Handler about a request retransmission over the session.
231 \param request Message retransmitted
233 void eventRequestRetransmission(Message *request) ;
236 Handler for diameter server (client-session) responses
238 \param response Answer container object for corresponding diameter request
239 \param myNode Own origin host
241 void eventResponse(const Response& response, const anna::diameter::comm::OriginHost *myNode) noexcept(false);
244 Handler for diameter server (client-session) requests
246 \param request Request data block object for corresponding diameter reception
247 \param myNode Own origin host
249 void eventRequest(const anna::DataBlock& request, const anna::diameter::comm::OriginHost *myNode) noexcept(false);
250 //void eventRequest(const Message& request) noexcept(false);
253 Handler for diameter server (client-session) responses out of context
255 \param response Answer data block object without context match
256 \param myNode Own origin host
258 void eventUnknownResponse(const anna::DataBlock& response, const anna::diameter::comm::OriginHost *myNode) noexcept(false);
261 Handler for diameter server (client-session) Disconnect-Peer-Answer messages
263 \param response Answer data block object without context match
264 \param myNode Own origin host
266 void eventDPA(const anna::DataBlock& response, const anna::diameter::comm::OriginHost *myNode) noexcept(false);
271 * Handlers for receptions
273 /* virtual */void receive(const anna::comm::Message& message) noexcept(false);
274 /* virtual */void finalize() ;
277 /* virtual */void expireResponse(Response*) ;
278 /* virtual */void setState(State::_v state) ;
280 void sendDWAToServer(const anna::DataBlock& dwrDB) noexcept(false); // non-usual behaviour, but DWR could be received from server
283 static const char* asText(const WatchdogState::_v) ;
288 friend class ClientSessionReceiver;