ANNA Suite  2020b
Multipurpose development suite for Telco applications
Entity.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_Entity_hpp
10 #define anna_diameter_comm_Entity_hpp
11 
12 
13 // STL
14 #include <string>
15 #include <vector>
16 
17 #include <anna/config/defines.hpp>
18 
21 
23 
24 
25 namespace anna {
26 class DataBlock;
27 namespace xml {
28 class Node;
29 }
30 }
31 
32 
33 
34 namespace anna {
35 
36 namespace diameter {
37 
38 namespace comm {
39 
40 class Engine;
41 class Server;
42 class Response;
43 class Message;
44 class ClientSession;
45 
46 
50 class Entity {
51 
52 public:
53 
57  struct SessionBasedModelsType { enum _v { RoundRobin, SessionIdOptionalPart, SessionIdHighPart, SessionIdLowPart /* default */ }; };
58 
59 private:
60 
61 
62  std::vector<Server*> a_servers;
63  int a_maxServers; // -1 means "no limit to add servers"
64  std::string a_description;
65  int a_category;
66  std::vector<Server*>::iterator a_deliveryIterator;
67  Server *a_lastUsedResource;
68 
69  // Engine
70  Engine *a_engine;
71 
72  // Balance and socket selection:
73  bool a_balance;
74  SessionBasedModelsType::_v a_sessionBasedModelsType;
75 
76  // Availability
77  bool a_available; // any of the servers must be bound
78  void availabilityLost() ;
79  void availabilityRecovered() ;
80  bool refreshAvailability() ; // return true if change
81  void assertReady() noexcept(false);
82  void initialize() ;
83  void childIdle() const ;
84 
85  // Selected for remove
86  bool a_deprecated; // entity has been selected as deprecated (will be removed when idle)
87 
88  // internal helpers
89  std::string a_socketListLiteral; // ADDRESS:PORT space-separated list
90  std::string a_primarySocketLiteral; // ADDRESS:PORT for primary (used on standard entities)
91  std::string a_secondarySocketLiteral; // ADDRESS:PORT for primary (used on standard entities)
92 
93 
94  // Activity
95  anna::Millisecond a_lastIncomingActivityTime; // last unix timestamp (in milliseconds) when message reception was managed over this entity
96  anna::Millisecond a_lastOutgoingActivityTime; // last unix timestamp (in milliseconds) when message sending was managed over this entity
97  void updateIncomingActivityTime() ;
98  void updateOutgoingActivityTime() ;
99 
100  // Private close/destroy method
101  void close(bool destroy) noexcept(false);
102 
103  // helpers
104  static const char* asText(const SessionBasedModelsType::_v) ;
105 
106 public:
107 
108 
114  Entity(int maxServers = 2) : a_maxServers(maxServers) { initialize(); }
115 
116 
122  void setMaxServers(int maxServers) { a_maxServers = maxServers; }
123 
129  int getMaxServers() const { return a_maxServers; }
130 
131 
137  void setDescription(const std::string &description) { a_description = description; }
138 
144  const std::string & getDescription() const { return a_description; }
145 
146 
151  void setEngine(Engine *e) { a_engine = e; }
152 
157  Engine *getEngine() const { return a_engine; }
158 
166  void addServer(const socket_t & serverId) noexcept(false);
167 
173  bool bind() noexcept(false);
174 
180  void raiseAutoRecovery(bool autoRecovery = true) noexcept(false);
181 
188  void setClassCodeTimeout(const ClassCode::_v v, const anna::Millisecond & millisecond) ;
189 
195  void setCategory(int category) { a_category = category; }
196 
202  int getCategory() const { return a_category; }
203 
208  Server *getLastUsedResource() const { return (a_lastUsedResource); }
209 
210 
217  void setBalance (bool balance) { a_balance = balance; }
218 
224  bool getBalance() const { return a_balance; }
225 
226 
227 // SIMPLE BALANCE or STANDARD documentation version
228 // /**
229 // Sent a message to the entity. First uses primary server, secondary if fails and so on to the
230 // last defined resource (server) within entity. Another sending algorithm (non standard) could
231 // be enabled (balance boolean parameter): it consist in round-robin server selection without
232 // trying any one if fails (standard behaviour tries all servers from FIRST defined).
233 // Anyway, last used delivery resource could be known through #getLastUsedResource().
234 //
235 // When the message is a request, a timer will be set automatically to control the response time.
236 // If expires, the ResultCode Timeout will be finally notified on #Entity::eventResponse. This
237 // timeout value will be configured at #setClassCodeTimeout.
238 //
239 // \param message Message sent.
240 // \param balance False by default (standard beaviour), but useful to balance over servers within entity.
241 //
242 // @return Boolean about success in send operation. Standard behaviour (no balance) implies true result when any
243 // of the entity servers could send the message, and false when neither of the servers was available or fail to
244 // send the message. Broadcast try to send all over the resources in spite of any fail, and balanced sendings
245 // fails when next selected resource fail to send the message (and no alarm or error counter is generated in
246 // this case because it can't be understood/ensured as entity-sending fail.
247 // */
248 
249 // OJO: en el caso estandard, no se prueban todas las sessiones de un servidor si tiene mas de una, luego la alarma
250 // generada en caso de error, presupone que las sessiones no usadas, tambi�n dar�an error, lo cual no tiene porque
251 // ser cierto. En condiciones normales, los servidores tienen una session, con lo que lo anterior es cierto y el
252 // la practica es lo mas normal.
253 
272  bool send(const Message *message) noexcept(false);
273  bool send(const Message &message) noexcept(false) { return send(&message); }
274 
279  void setSessionBasedModelsType(const SessionBasedModelsType::_v sessionBasedModelsType) { a_sessionBasedModelsType = sessionBasedModelsType; }
280 
285  SessionBasedModelsType::_v getSessionBasedModelsType() const { return a_sessionBasedModelsType; }
286 
287 
343  virtual int readSocketId(const Message *message, int maxClientSessions) const ;
344 
345 
355  bool broadcast(const Message *message) noexcept(false);
356  bool broadcast(const Message &message) noexcept(false) { return broadcast(&message); }
357 
358 
362  bool isAvailable() const { return a_available; }
363 
367  bool isDeprecated() const { return a_deprecated; }
368 
372  void setDeprecated(bool deprecated = true) { a_deprecated = deprecated; }
373 
374 
380  int getOTARequests() const ;
381 
387  bool idle() const { return (getOTARequests() == 0); }
388 
389 
390  std::vector<Server*>::iterator begin() { return a_servers.begin(); }
391  std::vector<Server*>::iterator end() { return a_servers.end(); }
392  std::vector<Server*>::const_iterator begin() const { return a_servers.begin(); }
393  std::vector<Server*>::const_iterator end() const { return a_servers.end(); }
394 
400  void close() noexcept(false) { close(false /* no destroy */); }
401 
402  // helpers
403 
407  int getNumberOfServers() const { return a_servers.size(); }
408 
412  int getMaxServerss() const { return a_maxServers; }
413 
417  socket_v getAddressPortList() const ;
418 
419 
424  void hide() ;
425 
430  void show() ;
431 
435  bool hidden() const ;
436 
440  bool shown() const ;
441 
442 
447  std::string asString() const ;
448 
454  anna::xml::Node* asXML(anna::xml::Node* parent) const ;
455 
456 protected:
457 
464  virtual void eventPeerShutdown(const ClientSession* clientSession) ;
465 
472  virtual void eventRequestRetransmission(const ClientSession* clientSession, Message *request) ;
473 
480  virtual void eventResponse(const Response& response, const anna::diameter::comm::OriginHost *myNode) noexcept(false) = 0;
481 
489  virtual void eventRequest(ClientSession* clientSession, const anna::DataBlock &request, const anna::diameter::comm::OriginHost *myNode) noexcept(false) = 0;
490  //virtual void eventRequest(ClientSession* clientSession, const Message& request) noexcept(false) = 0;
491 
499  virtual void eventUnknownResponse(ClientSession *clientSession, const anna::DataBlock& response, const anna::diameter::comm::OriginHost *myNode) noexcept(false) = 0;
500 
508  virtual void eventDPA(ClientSession *clientSession, const anna::DataBlock& response, const anna::diameter::comm::OriginHost *myNode) noexcept(false) = 0;
509 
510 
511 
512 
513  friend class Engine;
514  friend class Server;
515 };
516 
517 }
518 }
519 }
520 
521 #endif
522 
int getNumberOfServers() const
Definition: Entity.hpp:407
Entity(int maxServers=2)
Definition: Entity.hpp:114
void close() noexcept(false)
Definition: Entity.hpp:400
Definition: Millisecond.hpp:24
Engine * getEngine() const
Definition: Entity.hpp:157
std::vector< Server * >::const_iterator end() const
Definition: Entity.hpp:393
Definition: Engine.hpp:102
void setDescription(const std::string &description)
Definition: Entity.hpp:137
void setCategory(int category)
Definition: Entity.hpp:195
Definition: Node.hpp:56
void setEngine(Engine *e)
Definition: Entity.hpp:151
Definition: Server.hpp:54
std::vector< Server * >::const_iterator begin() const
Definition: Entity.hpp:392
void setBalance(bool balance)
Definition: Entity.hpp:217
bool broadcast(const Message &message) noexcept(false)
Definition: Entity.hpp:356
Definition: OriginHost.hpp:41
bool isAvailable() const
Definition: Entity.hpp:362
bool getBalance() const
Definition: Entity.hpp:224
const std::string & getDescription() const
Definition: Entity.hpp:144
_v
Definition: ClassCode.hpp:28
SessionBasedModelsType::_v getSessionBasedModelsType() const
Definition: Entity.hpp:285
Definition: Response.hpp:44
std::vector< Server * >::iterator end()
Definition: Entity.hpp:391
Definition: Message.hpp:45
void setMaxServers(int maxServers)
Definition: Entity.hpp:122
Definition: ClientSession.hpp:51
std::pair< std::string, int > socket_t
Definition: defines.hpp:104
bool isDeprecated() const
Definition: Entity.hpp:367
void setDeprecated(bool deprecated=true)
Definition: Entity.hpp:372
bool idle() const
Definition: Entity.hpp:387
xml::Node Node
Definition: Node.hpp:21
bool send(const Message &message) noexcept(false)
Definition: Entity.hpp:273
Definition: app.hpp:12
int getMaxServerss() const
Definition: Entity.hpp:412
std::vector< Server * >::iterator begin()
Definition: Entity.hpp:390
int getCategory() const
Definition: Entity.hpp:202
std::vector< socket_t > socket_v
Definition: defines.hpp:109
Definition: Entity.hpp:50
Definition: DataBlock.hpp:24
void setSessionBasedModelsType(const SessionBasedModelsType::_v sessionBasedModelsType)
Definition: Entity.hpp:279
int getMaxServers() const
Definition: Entity.hpp:129
Server * getLastUsedResource() const
Definition: Entity.hpp:208