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_Server_hpp
10 #define anna_diameter_comm_Server_hpp
19 #include <anna/core/util/Millisecond.hpp>
20 #include <anna/core/RuntimeException.hpp>
21 #include <anna/diameter/defines.hpp>
22 #include <anna/config/defines.hpp>
23 #include <anna/diameter.comm/ClassCode.hpp>
24 #include <anna/diameter.comm/MessageStatistics.hpp>
51 Diameter server with 1..N connections.
58 // Main server attributes
62 std::vector<ClientSession*> a_clientSessions;
63 int a_maxClientSessions; // -1 means "no limit to add client-sessions" (sockets per server)
64 std::vector<ClientSession*>::iterator a_deliveryIterator;
65 ClientSession *a_lastUsedResource;
68 anna::Millisecond a_lastIncomingActivityTime; // last unix timestamp (in milliseconds) when message reception was managed over this server
69 anna::Millisecond a_lastOutgoingActivityTime; // last unix timestamp (in milliseconds) when message sending was managed over this server
70 void updateIncomingActivityTime() throw();
71 void updateOutgoingActivityTime() throw();
77 MessageStatistics a_messageStatistics;
78 void initializeStatisticResources() throw();
79 void resetStatistics() throw();
82 bool a_available; // any of the client-sessions must be bound
83 void availabilityLost() throw();
84 void availabilityRecovered() throw();
85 bool refreshAvailability() throw(); // return true if change
86 void assertReady() throw(anna::RuntimeException);
87 void initialize() throw();
88 void childIdle() const throw();
90 // Private close/destroy method
91 void close(bool destroy) throw(anna::RuntimeException);
98 * Default constructor.
99 * @param maxClientSessions Maximum number of client-sessions managed by the server. Default is 1 (monoconnection server).
100 * The value -1, means no limit to add client-sessions.
102 Server(int maxClientSessions = 1) : a_maxClientSessions(maxClientSessions) { initialize(); }
105 virtual ~Server() {;}
109 * Add a server to the entity and create all the client-sessions configured at #setSocketsPerDiameterServer within that server.
111 * \param socketId Diameter socket identifier within the server.
113 void addClientSession(int socketId) throw(anna::RuntimeException);
116 Set timeout to consider failed a request.
117 \param v Requests class code.
118 \param millisecond Milliseconds wait before considering the requests failed.
120 Timers are internally managed and automatically activated.
122 void setClassCodeTimeout(const ClassCode::_v v, const anna::Millisecond & millisecond) throw();
125 * Binds server client-sessions.
127 * @return Returns true if all client-session were successfully bound
129 bool bind() throw(anna::RuntimeException);
132 * Propagate auto recovery configuration to client-sessions within server
134 * @param autoRecovery Auto recovery indicator. True by default.
136 void raiseAutoRecovery(bool autoRecovery = true) throw(anna::RuntimeException);
139 // Sent a message to the server using a certain client-session by mean round-robin between socketId's for
140 // multiple client client-sessions functionality. If a specific socketId is provided, then uses such specific client-session.
141 // Last used delivery resource could be known through #getLastUsedResource().
142 bool send(const Message*, int socketId = -1 /* client-sessions round-robin */) throw(anna::RuntimeException);
143 bool send(const Message& message, int socketId = -1 /* client-sessions round-robin */) throw(anna::RuntimeException) { return send(&message, socketId); }
146 Gets the last used resource (client session) during sending.
147 Broadcast doesn't updates this information.
149 ClientSession *getLastUsedResource() const throw() { return (a_lastUsedResource); }
152 Sent a message to all the server client-sessions (socketId's) for multiple client client-sessions functionality.
153 It is used, i.e., in Disconnect-Peer-Request procedure over a certain server.
154 Returns true (success) only when broadcast is success over all the server client-sessions. If any client-session fails,
155 then false is returned. Broadcast try to send all over the resources in spite of any fail.
157 bool broadcast(const Message*) throw(anna::RuntimeException);
158 bool broadcast(const Message& message) throw(anna::RuntimeException) { return broadcast(&message); }
161 Close all the server client-sessions. Depending on client-session configuration ('OnDisconnect' behaviour),
162 pending answers will be wait (graceful) or ignored (immediate-abrupt close).
163 Resources are not destroyed.
165 void close() throw(anna::RuntimeException) { close(false /* no destroy */); }
169 Diameter parent entity.
170 \return Diameter parent entity.
172 const Entity *getParent() const throw() { return a_parent; }
175 Returns true when any of the server client-sessions is Bound. False when all not-bound.
177 bool isAvailable() const throw() { return a_available; }
180 std::vector<ClientSession*>::iterator begin() throw() { return a_clientSessions.begin(); }
181 std::vector<ClientSession*>::iterator end() throw() { return a_clientSessions.end(); }
182 std::vector<ClientSession*>::const_iterator begin() const throw() { return a_clientSessions.begin(); }
183 std::vector<ClientSession*>::const_iterator end() const throw() { return a_clientSessions.end(); }
185 int getNumberOfClientSessions() const throw() { return a_clientSessions.size(); }
186 int getMaxClientSessions() const throw() { return a_maxClientSessions; }
187 void setMaxClientSessions(int maxClientSessions) throw() { a_maxClientSessions = maxClientSessions; }
190 Diameter server address (IP or hostname)
191 \return Diameter server address.
193 const std::string& getAddress() const throw() { return a_socket.first; }
196 Diameter server port.
197 \return Diameter server port.
199 int getPort() const throw() { return a_socket.second; }
202 /** Server presentation as 'ADDRESS:PORT' */
203 std::string socketAsString() const throw();
207 Gets the timestamp for last incoming activity over the server.
209 @return Last incoming activity timestamp.
211 const anna::Millisecond & getLastIncomingActivityTime() const throw() { return a_lastIncomingActivityTime; }
214 Gets the timestamp for last outgoing activity over the server.
216 @return Last outgoing activity timestamp.
218 const anna::Millisecond & getLastOutgoingActivityTime() const throw() { return a_lastOutgoingActivityTime; }
222 Gets the number of requests messages over-the-air.
224 @return OTA messages.
226 int getOTARequests() const throw();
229 Returns idle state (no pending answers).
233 bool idle() const throw() { return (getOTARequests() == 0); }
237 Deny resources for delivery restriction.
238 Deny all its client sessions
243 Allow resource for delivery permission.
244 Allow all its client sessions
249 Returns true when all its client session resources are hidden for application messages delivery
251 bool hidden() const throw();
254 Returns true when all its client session resources are shown for application messages delivery
256 bool shown() const throw();
260 Class string representation
261 \return String with relevant information for this instance.
263 std::string asString() const throw();
266 Class xml representation
267 \param parent Parent XML node on which hold this instance information.
268 \return XML document with relevant information for this instance.
270 anna::xml::Node* asXML(anna::xml::Node* parent) const throw();
274 void updateProcessingTimeStatisticConcept(const double &value, const anna::diameter::CommandId &cid) throw();
275 void updateReceivedMessageSizeStatisticConcept(const double &value, const anna::diameter::CommandId &cid) throw();
276 // int getProcessingTimeStatisticConcept() const throw() { return a_processing_time__StatisticConceptId; }
277 // int getReceivedMessageSizeStatisticConcept() const throw() { return a_received_message_size__StatisticConceptId; }
282 Handler about event break connection from diameter server (client-session) over this entity.
284 When notified, ANNA.diameter.comm generates an diameter::comm::Entity::eventResponse for every request with pending answers.
286 virtual void eventPeerShutdown(const ClientSession*) throw();
289 Handler about a request retransmission over the session.
291 \param request Message retransmitted
293 virtual void eventRequestRetransmission(const ClientSession*, Message *request) throw();
296 Handler for diameter server (client-session) responses
298 \param response Answer container object for corresponding diameter request
300 virtual void eventResponse(const Response & response) throw(anna::RuntimeException);
303 Handler for diameter server (client-session) requests
305 \param clientSession ClientSession from which request has been received
306 \param request Diameter request message received
308 virtual void eventRequest(ClientSession *clientSession, const anna::DataBlock &request) throw(anna::RuntimeException);
309 //virtual void eventRequest(ClientSession *clientSession, const Message & request) throw(anna::RuntimeException);
312 Handler for diameter server (client-session) responses out of context
314 \param clientSession ClientSession from which request has been received
315 \param response Answer data block object without context match
317 virtual void eventUnknownResponse(ClientSession *clientSession, const anna::DataBlock& response) throw(anna::RuntimeException);
320 Handler for diameter server (client-session) Disconnect-Peer-Answer messages
322 \param clientSession ClientSession from which request has been received
323 \param response Answer data block object without context match
325 virtual void eventDPA(ClientSession *clientSession, const anna::DataBlock& response) throw(anna::RuntimeException);
330 friend class ClientSession;