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_LocalServer_hpp
10 #define anna_diameter_comm_LocalServer_hpp
13 #include <anna/core/RuntimeException.hpp>
14 #include <anna/core/util/Millisecond.hpp>
15 #include <anna/core/util/Recycler.hpp>
21 #include <anna/config/defines.hpp>
22 #include <anna/statistics/Accumulator.hpp>
23 #include <anna/diameter.comm/ServerSession.hpp>
32 //class LocalConnection;
50 Diameter server socket
56 std::string a_description;
58 int a_currentConnections; // deberia coincidir en todo momento con el numero de local connections del server socket
59 anna::Millisecond a_allowedInactivityTime;
60 ServerSocket *a_serverSocket;
68 int a_processing_time__StatisticConceptId; // request from local server (dpr's, etc.)
69 int a_received_message_size__StatisticConceptId;
70 anna::statistics::Accumulator *a_statisticsAccumulator;
71 void initializeStatisticResources() throw();
72 void resetStatistics() throw();
74 // void eraseServerSession(const anna::comm::ClientSocket& clientSocket) throw();
75 // void eraseServerSession(const serverSession_iterator &it) throw();
76 void lostConnection() throw();
77 void newConnection() throw(anna::RuntimeException);
80 anna::Millisecond a_lastIncomingActivityTime; // last unix timestamp (in milliseconds) when message reception was managed over this entity
81 anna::Millisecond a_lastOutgoingActivityTime; // last unix timestamp (in milliseconds) when message sending was managed over this entity
82 void updateIncomingActivityTime() throw();
83 void updateOutgoingActivityTime() throw();
86 bool a_available; // any of the server-sessions must be bound
87 void availabilityLost() throw();
88 void availabilityRecovered() throw();
89 bool refreshAvailability() throw(); // return true if change
91 void attach() throw(); // attach server socket to the communicator
92 void attachPlanning() throw(); // used when attach fails (socket already in use, etc.)
94 typedef int serverSession_key;
95 serverSession_key getServerSessionKey(const anna::comm::ClientSocket&) const throw(); // hash for Client Socket INetAddress serialization
96 typedef std::map <serverSession_key, ServerSession*> serverSession_container;
97 typedef serverSession_container::value_type serverSession_value_type;
98 typedef serverSession_container::iterator serverSession_iterator;
99 typedef serverSession_container::const_iterator const_serverSession_iterator;
100 serverSession_container a_serverSessions;
101 anna::Recycler<ServerSession> a_serverSessionsRecycler;
102 serverSession_iterator serverSession_find(const serverSession_key&) throw();
103 serverSession_iterator serverSession_begin() throw() { return a_serverSessions.begin(); }
104 serverSession_iterator serverSession_end() throw() { return a_serverSessions.end(); }
105 static ServerSession* serverSession(serverSession_iterator ii) throw() { return ii->second; }
106 const_serverSession_iterator serverSession_begin() const throw() { return a_serverSessions.begin(); }
107 const_serverSession_iterator serverSession_end() const throw() { return a_serverSessions.end(); }
108 static const ServerSession* serverSession(const_serverSession_iterator ii) throw() { return ii->second; }
110 // INTERNAL CREATORS AND CLOSE METHODS
111 ServerSession *createServerSession(const anna::comm::ClientSocket&) throw(anna::RuntimeException);
112 void closeServerSession(ServerSession*) throw(anna::RuntimeException);
114 // INTERNAL ALLOCATORS
115 ServerSession* allocateServerSession() throw();
116 void releaseServerSession(ServerSession*) throw();
119 serverSession_iterator a_deliveryIterator;
120 ServerSession *a_lastUsedResource;
128 ~LocalServer() { close(); }
134 * Sets the local server key
135 * @param LocalServer key
137 void setKey(const socket_t &key) throw() { a_key = key; }
140 Sets the server socket optional description
142 @param description Server socket description
144 void setDescription(const std::string description) throw() { a_description = description; }
148 Sets the server socket optional category
150 @param description Server socket category
152 void setCategory(int category) throw() { a_category = category; }
155 Sets the maximum supported connections.
156 If provided value is negative or lesser than the number of current connections, an exception will be launched.
157 If all the connections was established, a new maximum will open the listen port.
158 when margin is zeroed (maximum configured is equal to current connections), listen port will be closed.
160 @param maxConnections Number of maximum connections allowed
162 void setMaxConnections(int maxConnections) throw(anna::RuntimeException);
165 Sets the maximum allowed inactivity time on server sessions born over the local server before being reset.
166 Communication engine assign a default value of 90000 msecs.
168 @param allowedInactivityTime Inactivity time allowed
170 void setAllowedInactivityTime(const anna::Millisecond & allowedInactivityTime) throw() { a_allowedInactivityTime = allowedInactivityTime; }
173 * Sets the diameter::comm::Engine
174 * @param e Diameter::comm::Engine
176 void setEngine(Engine *e) throw() { a_engine = e; }
181 * Gets the diameter::comm::Engine
182 * @return Diameter::comm::Engine
184 Engine *getEngine() const throw() { return a_engine; }
187 * Gets the local server key
188 * @return LocalServer key
190 const socket_t & getKey() const throw() { return a_key; }
193 Gets the number of maximum accepted connections that server socket is configured to handle
195 int getMaxConnections() const throw() { return a_maxConnections; }
198 Gets the number of current connections being established through server socket
200 int getCurrentConnections() const throw() { return a_currentConnections; }
203 Gets the maximum allowed inactivity time on server sessions born over the local server before being reset
205 @return Inactivity time allowed
207 const anna::Millisecond & getAllowedInactivityTime() const throw() { return a_allowedInactivityTime; }
210 Returns true when any of the server-sessions is Bound. False when all not-bound.
212 bool isAvailable() const throw() { return a_available; }
217 Disables local server socket (listener) keeping current server sessions alive.
218 Note that applications should not close the listen port directly to keep coherence (see #resetConnectionsMargin)
220 @param lock Locks disabled state (make it permanent even if new connections margin is reached).
221 Used during diameter agent isolation (lost of service, maintenance, etc.)
223 void disable(bool lock = false) throw(anna::RuntimeException);
225 /** Enables local server socket (listener)
227 @param unlock Unlocks permanent disabled states
229 void enable(bool unlock = false) throw(anna::RuntimeException);
232 Gets the number of requests messages over-the-air.
234 @return OTA messages.
236 int getOTARequests() const throw();
239 Returns idle state (no pending answers).
243 bool idle() const throw() { return (getOTARequests() == 0); }
246 Close the local server means two things: close the server socket and close all the server sessions born
247 from this local server freeing such server sessions resources.
249 void close() throw(anna::RuntimeException);
252 Performs coherent server socket close procedure zeroing margin between current established connections and maximum allowed.
254 void resetConnectionsMargin() throw(anna::RuntimeException) { setMaxConnections(a_currentConnections); }
258 * Returns server-session instance identified by client socket provided.
260 * \param clientSocket Client socket associated to the server session
261 * \param emode Action when no client-session is found with provided parameters (Throw/Ignore).
263 * \return The server-session instance identified by client socket provided.
265 * \warning If no server-session found, an exception is launched by default.
267 ServerSession* findServerSession(const anna::comm::ClientSocket &clientSocket, anna::Exception::Mode::_v emode = anna::Exception::Mode::Throw) throw(anna::RuntimeException);
270 * Returns server-session instance identified by socket id provided (hash over serialized client socket information).
272 * \param socketId Socket id which is key for the server session
273 * \param emode Action when no client-session is found with provided parameters (Throw/Ignore).
275 * \return The server-session instance identified by client socket provided.
277 * \warning If no server-session found, an exception is launched by default.
279 ServerSession* findServerSession(int socketId, anna::Exception::Mode::_v emode = anna::Exception::Mode::Throw) throw(anna::RuntimeException);
283 Sent a message to the client using a certain server-session provided or defined by #readSocketId if not.
284 When the message is a request, a timer will be set automatically to control the response time.
285 If expires, the ResultCode Timeout will be finally notified on #LocalServer::eventResponse. This
286 timeout value will be configured at #setClassCodeTimeout.
288 \param message Message sent.
289 \param socketId Server session socket id INetAddress serialization. By default, #readSocketId is invoked to get the socket id used (which uses round-robin if not re-implemented)
291 @return Boolean about success in send operation. True when any of the server sessions could send the message.
292 False, when neither of the server sessions was available or fail to send the message. Broadcast try to send all over
293 the resources in spite of any fail. If a specific socket id is provided, only this socket is used without trying any other
294 and returning false if fails.
296 bool send(const Message*, int socketId = -1 /* default uses readSocketId() */) throw(anna::RuntimeException);
297 bool send(const Message& message, int socketId = -1 /* default uses readSocketId() */) throw(anna::RuntimeException) { return send(&message, socketId); }
300 Gets the last used resource (server session) during sending.
301 Broadcast doesn't updates this information.
303 ServerSession *getLastUsedResource() const throw() { return (a_lastUsedResource); }
306 Before sending a message over each local server, socketId could be specified to select
307 which session within such server will manage the message.
309 Default implementation performs round-robin (value '-1' for socketId) but any other kind of
310 application could re-implement this method and change the behaviour.
312 \param message Message which is being sent.
314 @return Socket-id (hash over serialized client socket information). Value '-1' if round-robin is desired.
315 If socket-id is unkonwn, send procedure will throw an exception.
317 virtual int readSocketId(const Message *message) const throw() { return -1; }
320 Sent a message to all the server sessions.
321 It is used, i.e., in Disconnect-Peer-Request procedure over a certain entity.
323 \param message Message which is being sent.
325 @return Returns true (success) only when broadcast is success over all the entity servers. If any server fails,
326 then false is returned.
328 bool broadcast(const Message *message) throw(anna::RuntimeException);
329 bool broadcast(const Message &message) throw(anna::RuntimeException) { return broadcast(&message); }
333 Class string representation
334 \return String with relevant information for this instance.
336 std::string asString() const throw();
339 Class xml representation
340 \param parent Parent XML node on which hold this instance information.
341 \return XML document with relevant information for this instance.
343 anna::xml::Node* asXML(anna::xml::Node* parent) const throw();
346 void updateProcessingTimeStatisticConcept(const double &value) throw();
347 void updateReceivedMessageSizeStatisticConcept(const double &value) throw();
348 int getProcessingTimeStatisticConcept() const throw() { return a_processing_time__StatisticConceptId; }
349 int getReceivedMessageSizeStatisticConcept() const throw() { return a_received_message_size__StatisticConceptId; }
355 Handler about event break connection from diameter client over this server-session.
356 When notified, ANNA.diameter.comm generates an diameter::comm::ServerSession::eventResponse for every request with pending answers.
357 Default implementation traces warning event
358 \param serverSession ServerSession from which shutdown has been received
360 virtual void eventPeerShutdown(const ServerSession* serverSession) throw();
363 Handler about a request retransmission over the server-session.
364 Default implementation traces warning event
365 \param serverSession ServerSession from which retransmission happened
366 \param request Retransmitted request message
368 virtual void eventRequestRetransmission(const ServerSession* serverSession, Message *request) throw();
371 Handler for diameter client responses
373 \param response Answer container object for corresponding diameter request
375 virtual void eventResponse(const Response& response) throw(anna::RuntimeException) = 0;
378 Handler for diameter client requests
380 \param serverSession ServerSession from which request has been received
381 \param request Request data block object for corresponding diameter reception
383 virtual void eventRequest(ServerSession* serverSession, const anna::DataBlock& request) throw(anna::RuntimeException) = 0;
384 //void eventRequest(ServerSession* serverSession, const Message& request) throw(anna::RuntimeException);
387 Handler for diameter client responses out of context
389 \param serverSession ServerSession from which request has been received
390 \param response Answer data block object without context match
392 virtual void eventUnknownResponse(ServerSession* serverSession, const anna::DataBlock& response) throw(anna::RuntimeException) = 0;
395 Handler for diameter client Disconnect-Peer-Answer messages
397 \param serverSession ServerSession from which request has been received
398 \param response Answer data block object without context match
400 virtual void eventDPA(ServerSession* serverSession, const anna::DataBlock& response) throw(anna::RuntimeException) = 0;
403 friend class anna::diameter::comm::Timer;
405 friend class ServerSocket;
406 friend class ServerSession;
407 friend class ServerSessionReceiver;