ANNA Suite  2020b
Multipurpose development suite for Telco applications
Server.hpp
Go to the documentation of this file.
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_Server_hpp
10 #define anna_diameter_comm_Server_hpp
11 
12 
13 // STL
14 #include <string>
15 #include <vector>
16 #include <map>
17 
18 // Project
22 #include <anna/config/defines.hpp>
25 
26 
27 
28 
29 namespace anna {
30 class DataBlock;
31 namespace timex {
32 class Engine;
33 }
34 }
35 
36 
37 namespace anna {
38 
39 namespace diameter {
40 
41 namespace comm {
42 
43 class Engine;
44 class Entity;
45 class ClientSession;
46 class Response;
47 class Message;
48 class OriginHost;
49 
50 
54 class Server {
55 
56  // Parent information
57  Entity *a_parent;
58 
59  // Main server attributes
60  socket_t a_socket;
61 
62  // ClientSessions
63  std::vector<ClientSession*> a_clientSessions;
64  int a_maxClientSessions; // -1 means "no limit to add client-sessions" (sockets per server)
65  std::vector<ClientSession*>::iterator a_deliveryIterator;
66  ClientSession *a_lastUsedResource;
67 
68  // Activity
69  anna::Millisecond a_lastIncomingActivityTime; // last unix timestamp (in milliseconds) when message reception was managed over this server
70  anna::Millisecond a_lastOutgoingActivityTime; // last unix timestamp (in milliseconds) when message sending was managed over this server
71  void updateIncomingActivityTime() ;
72  void updateOutgoingActivityTime() ;
73 
74  // Engine
75  Engine *a_engine;
76 
77  // Statistics
78  MessageStatistics a_messageStatistics;
79  void initializeStatisticResources() ;
80  void resetStatistics() ;
81 
82  // Availability
83  bool a_available; // any of the client-sessions must be bound
84  void availabilityLost() ;
85  void availabilityRecovered() ;
86  bool refreshAvailability() ; // return true if change
87  void assertReady() noexcept(false);
88  void initialize() ;
89  void childIdle() const ;
90 
91  // Private close/destroy method
92  void close(bool destroy) noexcept(false);
93 
94 
95 public:
96 
97 
103  Server(int maxClientSessions = 1) : a_maxClientSessions(maxClientSessions) { initialize(); }
104 
106  virtual ~Server() {;}
107 
108 
114  void addClientSession(int socketId) noexcept(false);
115 
123  void setClassCodeTimeout(const ClassCode::_v v, const anna::Millisecond & millisecond) ;
124 
130  bool bind() noexcept(false);
131 
137  void raiseAutoRecovery(bool autoRecovery = true) noexcept(false);
138 
139 
140 // Sent a message to the server using a certain client-session by mean round-robin between socketId's for
141 // multiple client client-sessions functionality. If a specific socketId is provided, then uses such specific client-session.
142 // Last used delivery resource could be known through #getLastUsedResource().
143  bool send(const Message*, int socketId = -1 /* client-sessions round-robin */) noexcept(false);
144  bool send(const Message& message, int socketId = -1 /* client-sessions round-robin */) noexcept(false) { return send(&message, socketId); }
145 
150  ClientSession *getLastUsedResource() const { return (a_lastUsedResource); }
151 
158  bool broadcast(const Message*) noexcept(false);
159  bool broadcast(const Message& message) noexcept(false) { return broadcast(&message); }
160 
166  void close() noexcept(false) { close(false /* no destroy */); }
167 
168 
173  const Entity *getParent() const { return a_parent; }
174 
178  bool isAvailable() const { return a_available; }
179 
180 
181  std::vector<ClientSession*>::iterator begin() { return a_clientSessions.begin(); }
182  std::vector<ClientSession*>::iterator end() { return a_clientSessions.end(); }
183  std::vector<ClientSession*>::const_iterator begin() const { return a_clientSessions.begin(); }
184  std::vector<ClientSession*>::const_iterator end() const { return a_clientSessions.end(); }
185 
186  int getNumberOfClientSessions() const { return a_clientSessions.size(); }
187  int getMaxClientSessions() const { return a_maxClientSessions; }
188  void setMaxClientSessions(int maxClientSessions) { a_maxClientSessions = maxClientSessions; }
189 
194  const std::string& getAddress() const { return a_socket.first; }
195 
200  int getPort() const { return a_socket.second; }
201 
202 
204  std::string socketAsString() const ;
205 
206 
212  const anna::Millisecond & getLastIncomingActivityTime() const { return a_lastIncomingActivityTime; }
213 
219  const anna::Millisecond & getLastOutgoingActivityTime() const { return a_lastOutgoingActivityTime; }
220 
221 
227  int getOTARequests() const ;
228 
234  bool idle() const { return (getOTARequests() == 0); }
235 
236 
241  void hide() ;
242 
247  void show() ;
248 
252  bool hidden() const ;
253 
257  bool shown() const ;
258 
259 
264  std::string asString() const ;
265 
271  anna::xml::Node* asXML(anna::xml::Node* parent) const ;
272 
273 
274  // Statistics
275  void updateProcessingTimeStatisticConcept(const double &value, const anna::diameter::CommandId &cid) ;
276  void updateReceivedMessageSizeStatisticConcept(const double &value, const anna::diameter::CommandId &cid) ;
277 // int getProcessingTimeStatisticConcept() const { return a_processing_time__StatisticConceptId; }
278 // int getReceivedMessageSizeStatisticConcept() const { return a_received_message_size__StatisticConceptId; }
279 
280 protected:
281 
287  virtual void eventPeerShutdown(const ClientSession*) ;
288 
294  virtual void eventRequestRetransmission(const ClientSession*, Message *request) ;
295 
301  virtual void eventResponse(const Response & response, const anna::diameter::comm::OriginHost *myNode) noexcept(false);
302 
309  virtual void eventRequest(ClientSession *clientSession, const anna::DataBlock &request, const anna::diameter::comm::OriginHost *myNode) noexcept(false);
310 
317  virtual void eventUnknownResponse(ClientSession *clientSession, const anna::DataBlock& response, const anna::diameter::comm::OriginHost *myNode) noexcept(false);
318 
325  virtual void eventDPA(ClientSession *clientSession, const anna::DataBlock& response, const anna::diameter::comm::OriginHost *myNode) noexcept(false);
326 
327 
328 
329  friend class Engine;
330  friend class ClientSession;
331 };
332 
333 }
334 }
335 }
336 
337 #endif
Definition: Millisecond.hpp:24
const anna::Millisecond & getLastIncomingActivityTime() const
Definition: Server.hpp:212
std::vector< ClientSession * >::const_iterator begin() const
Definition: Server.hpp:183
virtual ~Server()
Definition: Server.hpp:106
Definition: Engine.hpp:102
Definition: Node.hpp:56
bool idle() const
Definition: Server.hpp:234
Definition: Server.hpp:54
bool broadcast(const Message &message) noexcept(false)
Definition: Server.hpp:159
Definition: OriginHost.hpp:41
ClientSession * getLastUsedResource() const
Definition: Server.hpp:150
int getMaxClientSessions() const
Definition: Server.hpp:187
_v
Definition: ClassCode.hpp:28
int getPort() const
Definition: Server.hpp:200
int getNumberOfClientSessions() const
Definition: Server.hpp:186
Definition: MessageStatistics.hpp:33
Definition: Response.hpp:44
Server(int maxClientSessions=1)
Definition: Server.hpp:103
Definition: Message.hpp:45
Definition: ClientSession.hpp:51
std::pair< std::string, int > socket_t
Definition: defines.hpp:104
const Entity * getParent() const
Definition: Server.hpp:173
bool send(const Message &message, int socketId=-1) noexcept(false)
Definition: Server.hpp:144
const std::string & getAddress() const
Definition: Server.hpp:194
std::vector< ClientSession * >::const_iterator end() const
Definition: Server.hpp:184
const anna::Millisecond & getLastOutgoingActivityTime() const
Definition: Server.hpp:219
Definition: app.hpp:12
std::vector< ClientSession * >::iterator begin()
Definition: Server.hpp:181
bool isAvailable() const
Definition: Server.hpp:178
void close() noexcept(false)
Definition: Server.hpp:166
std::pair< U24, bool > CommandId
Definition: defines.hpp:32
Definition: Entity.hpp:50
std::vector< ClientSession * >::iterator end()
Definition: Server.hpp:182
Definition: DataBlock.hpp:24
void setMaxClientSessions(int maxClientSessions)
Definition: Server.hpp:188