ANNA Suite  2020b
Multipurpose development suite for Telco applications
LocalServer.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_LocalServer_hpp
10 #define anna_diameter_comm_LocalServer_hpp
11 
12 
16 
17 // STL
18 #include <string>
19 #include <vector>
20 #include <map>
21 
22 // Project
23 #include <anna/config/defines.hpp>
27 
28 
29 namespace anna {
30 namespace xml {
31 class Node;
32 }
33 namespace comm {
34 class ClientSocket;
35 //class LocalConnection;
36 }
37 class DataBlock;
38 }
39 
40 namespace anna {
41 
42 namespace diameter {
43 
44 namespace comm {
45 
46 class Response;
47 class ServerSocket;
48 class Message;
49 class Engine;
50 
51 
55 class LocalServer {
56 
57  // main
58  socket_t a_key;
59  std::string a_description;
60  int a_maxConnections;
61  int a_currentConnections; // deberia coincidir en todo momento con el numero de local connections del server socket
62  anna::Millisecond a_allowedInactivityTime;
63  ServerSocket *a_serverSocket;
64  int a_category;
65  bool a_lock;
66 
67  // Engine
68  Engine *a_engine; // only for refresh availability
69 
70  // Statistics
71  MessageStatistics a_messageStatistics;
72  void initializeStatisticResources() ;
73  void resetStatistics() ;
74 
75 // void eraseServerSession(const anna::comm::ClientSocket& clientSocket) ;
76 // void eraseServerSession(const serverSession_iterator &it) ;
77  void lostConnection() ;
78  void newConnection() noexcept(false);
79 
80  // Activity
81  anna::Millisecond a_lastIncomingActivityTime; // last unix timestamp (in milliseconds) when message reception was managed over this entity
82  anna::Millisecond a_lastOutgoingActivityTime; // last unix timestamp (in milliseconds) when message sending was managed over this entity
83  void updateIncomingActivityTime() ;
84  void updateOutgoingActivityTime() ;
85 
86  // Availability
87  bool a_available; // any of the server-sessions must be bound
88  void availabilityLost() ;
89  void availabilityRecovered() ;
90  bool refreshAvailability() ; // return true if change
91 
92  void attach() ; // attach server socket to the communicator
93  void attachPlanning() ; // used when attach fails (socket already in use, etc.)
94 
95  typedef int serverSession_key;
96  serverSession_key getServerSessionKey(const anna::comm::ClientSocket&) const ; // hash for Client Socket INetAddress serialization
97  typedef std::map <serverSession_key, ServerSession*> serverSession_container;
98  typedef serverSession_container::value_type serverSession_value_type;
99  typedef serverSession_container::iterator serverSession_iterator;
100  typedef serverSession_container::const_iterator const_serverSession_iterator;
101  serverSession_container a_serverSessions;
102  anna::Recycler<ServerSession> a_serverSessionsRecycler;
103  serverSession_iterator serverSession_find(const serverSession_key&) ;
104  serverSession_iterator serverSession_begin() { return a_serverSessions.begin(); }
105  serverSession_iterator serverSession_end() { return a_serverSessions.end(); }
106  static ServerSession* serverSession(serverSession_iterator ii) { return ii->second; }
107  const_serverSession_iterator serverSession_begin() const { return a_serverSessions.begin(); }
108  const_serverSession_iterator serverSession_end() const { return a_serverSessions.end(); }
109  static const ServerSession* serverSession(const_serverSession_iterator ii) { return ii->second; }
110 
111  // INTERNAL CREATORS AND CLOSE METHODS
112  ServerSession *createServerSession(const anna::comm::ClientSocket&) noexcept(false);
113  void closeServerSession(ServerSession*) noexcept(false);
114 
115  // INTERNAL ALLOCATORS
116  ServerSession* allocateServerSession() ;
117  void releaseServerSession(ServerSession*) ;
118 
119  // Auxiliary
120  serverSession_iterator a_deliveryIterator;
121  ServerSession *a_lastUsedResource;
122 
123 public:
124 
126  LocalServer();
127 
129  ~LocalServer() { close(); }
130 
131 
132  // setters
133 
138  void setKey(const socket_t &key) { a_key = key; }
139 
145  void setDescription(const std::string description) { a_description = description; }
146 
154  void setClassCodeTimeout(const ClassCode::_v v, const anna::Millisecond & millisecond) ;
155 
161  void setCategory(int category) { a_category = category; }
162 
171  void setMaxConnections(int maxConnections) noexcept(false);
172 
179  void setAllowedInactivityTime(const anna::Millisecond & allowedInactivityTime) { a_allowedInactivityTime = allowedInactivityTime; }
180 
185  void setEngine(Engine *e) { a_engine = e; }
186 
187  // getters
188 
193  const socket_t & getKey() const { return a_key; }
194 
198  int getMaxConnections() const { return a_maxConnections; }
199 
203  int getCurrentConnections() const { return a_currentConnections; }
204 
210  const anna::Millisecond & getAllowedInactivityTime() const { return a_allowedInactivityTime; }
211 
215  bool isAvailable() const { return a_available; }
216 
217  // helpers
218 
226  void disable(bool lock = false) noexcept(false);
227 
232  void enable(bool unlock = false) noexcept(false);
233 
239  int getOTARequests() const ;
240 
246  bool idle() const { return (getOTARequests() == 0); }
247 
252  void close() noexcept(false);
253 
257  void resetConnectionsMargin() noexcept(false) { setMaxConnections(a_currentConnections); }
258 
259 
270  ServerSession* findServerSession(const anna::comm::ClientSocket &clientSocket, anna::Exception::Mode::_v emode = anna::Exception::Mode::Throw) noexcept(false);
271 
282  ServerSession* findServerSession(int socketId, anna::Exception::Mode::_v emode = anna::Exception::Mode::Throw) noexcept(false);
283 
284 
299  bool send(const Message*, int socketId = -1 /* default uses readSocketId() */) noexcept(false);
300  bool send(const Message& message, int socketId = -1 /* default uses readSocketId() */) noexcept(false) { return send(&message, socketId); }
301 
306  ServerSession *getLastUsedResource() const { return (a_lastUsedResource); }
307 
320  virtual int readSocketId(const Message *message) const { return -1; }
321 
331  bool broadcast(const Message *message) noexcept(false);
332  bool broadcast(const Message &message) noexcept(false) { return broadcast(&message); }
333 
334 
339  std::string asString() const ;
340 
346  anna::xml::Node* asXML(anna::xml::Node* parent) const ;
347 
348  // Statistics
349  void updateProcessingTimeStatisticConcept(const double &value, const anna::diameter::CommandId &cid) ;
350  void updateReceivedMessageSizeStatisticConcept(const double &value, const anna::diameter::CommandId &cid) ;
351 // int getProcessingTimeStatisticConcept() const { return a_processing_time__StatisticConceptId; }
352 // int getReceivedMessageSizeStatisticConcept() const { return a_received_message_size__StatisticConceptId; }
353 
354 protected:
355 
356  // Handlers:
363  virtual void eventPeerShutdown(const ServerSession* serverSession) ;
364 
371  virtual void eventRequestRetransmission(const ServerSession* serverSession, Message *request) ;
372 
379  virtual void eventResponse(const Response& response, const anna::diameter::comm::OriginHost *myNode) noexcept(false) = 0;
380 
388  virtual void eventRequest(ServerSession* serverSession, const anna::DataBlock& request, const anna::diameter::comm::OriginHost *myNode) noexcept(false) = 0;
389  //void eventRequest(ServerSession* serverSession, const Message& request) noexcept(false);
390 
398  virtual void eventUnknownResponse(ServerSession* serverSession, const anna::DataBlock& response, const anna::diameter::comm::OriginHost *myNode) noexcept(false) = 0;
399 
407  virtual void eventDPA(ServerSession* serverSession, const anna::DataBlock& response, const anna::diameter::comm::OriginHost *myNode) noexcept(false) = 0;
408 
409 
411  friend class Engine;
412  friend class ServerSocket;
413  friend class ServerSession;
414  friend class ServerSessionReceiver;
415 };
416 
417 }
418 }
419 }
420 
421 #endif
Definition: Exception.hpp:26
Definition: Millisecond.hpp:24
bool send(const Message &message, int socketId=-1) noexcept(false)
Definition: LocalServer.hpp:300
_v
Definition: Exception.hpp:26
Definition: Engine.hpp:102
ServerSession * getLastUsedResource() const
Definition: LocalServer.hpp:306
Definition: Node.hpp:56
Definition: Timer.hpp:30
void setKey(const socket_t &key)
Definition: LocalServer.hpp:138
bool idle() const
Definition: LocalServer.hpp:246
Definition: ClientSocket.hpp:38
bool isAvailable() const
Definition: LocalServer.hpp:215
Definition: OriginHost.hpp:41
const socket_t & getKey() const
Definition: LocalServer.hpp:193
void setDescription(const std::string description)
Definition: LocalServer.hpp:145
void setAllowedInactivityTime(const anna::Millisecond &allowedInactivityTime)
Definition: LocalServer.hpp:179
_v
Definition: ClassCode.hpp:28
void setEngine(Engine *e)
Definition: LocalServer.hpp:185
int getCurrentConnections() const
Definition: LocalServer.hpp:203
Definition: MessageStatistics.hpp:33
Definition: Response.hpp:44
Definition: Message.hpp:45
bool broadcast(const Message &message) noexcept(false)
Definition: LocalServer.hpp:332
Definition: ServerSession.hpp:53
std::pair< std::string, int > socket_t
Definition: defines.hpp:104
Definition: Recycler.hpp:30
Definition: LocalServer.hpp:55
xml::Node Node
Definition: Node.hpp:21
virtual int readSocketId(const Message *message) const
Definition: LocalServer.hpp:320
const anna::Millisecond & getAllowedInactivityTime() const
Definition: LocalServer.hpp:210
Definition: app.hpp:12
Definition: ServerSessionReceiver.hpp:35
void resetConnectionsMargin() noexcept(false)
Definition: LocalServer.hpp:257
int getMaxConnections() const
Definition: LocalServer.hpp:198
std::pair< U24, bool > CommandId
Definition: defines.hpp:32
void setCategory(int category)
Definition: LocalServer.hpp:161
~LocalServer()
Definition: LocalServer.hpp:129
Definition: DataBlock.hpp:24
Definition: ServerSocket.hpp:44