ANNA Suite  2020b
Multipurpose development suite for Telco applications
Handler.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_comm_Handler_hpp
10 #define anna_comm_Handler_hpp
11 
12 #include <netinet/in.h>
13 
17 
18 namespace anna {
19 
20 namespace xml {
21 class Node;
22 }
23 
24 namespace comm {
25 
26 class Communicator;
27 class Transport;
28 class ClientSocket;
29 
33 class Handler : public Runnable {
34 public:
36  using Runnable::run;
38 
42  struct Support { enum _v { None = 0, CongestionControl = 1 }; };
43 
47  struct Type {
48  enum _v {
56  };
57  };
58 
63  Type::_v getType() const { return a_type; }
64 
69  int getfd() const { return a_fd; }
70 
77  bool supportCongestionControl() const { return (a_support & Support::CongestionControl) != 0; }
78 
85  bool supportTimeout() const { return a_timeout > 0; }
86 
92  bool operator == (const int fd) const { return a_fd == fd; }
93 
98  virtual void apply() noexcept(false) = 0;
99 
105  virtual ClientSocket* getClientSocket() { return NULL; }
106 
111  virtual std::string asString() const ;
112 
117  virtual xml::Node* asXML(xml::Node* parent) const noexcept(false);
118 
123  void asAttribute(xml::Node* node) const noexcept(false);
124 
125 protected:
130 
137  Handler(Communicator* communicator, const Type::_v type, const int support = Support::CongestionControl) :
138  a_communicator(communicator),
139  a_type(type),
140  a_support(support),
141  a_fd(-1),
142  a_timeout(0),
143  a_maxTime(0),
144  a_loop(0)
145  {;}
146 
152  Handler(const Type::_v type, const int support = Support::CongestionControl) :
153  a_communicator(NULL),
154  a_type(type),
155  a_support(support),
156  a_fd(-1),
157  a_timeout(0),
158  a_maxTime(0),
159  a_loop(0)
160  {;}
161 
168  void setfd(const int fd) { setId(anna::functions::asText("Handler", a_fd = fd)); }
169 
175  void setTimeout(const Millisecond &timeout) {
176  a_timeout = timeout;
177  a_maxTime = functions::hardwareClock() + a_timeout;
178  }
179 
184  bool canContinue() const { return hasRequestedStop() == false; }
185 
192  virtual void breakAddress(const in_addr_t& address) {;}
193 
200  virtual void recoverAddress(const in_addr_t& address) {;}
201 
208  virtual bool testClose() noexcept(false) { return false;}
209 
216  virtual void finalize() {;}
217 
223  virtual void clone() noexcept(false) {;}
224 
225 private:
226  const Type::_v a_type;
227  const int a_support;
228  int a_fd;
229  Microsecond a_timeout;
230  Microsecond a_maxTime;
231  int a_loop;
232 
233  Handler(const Handler&);
234  void do_action() noexcept(false);
235  void beat(const Microsecond& now) { a_maxTime = now + a_timeout; }
236  bool isTimeout(const Microsecond& now) { return a_maxTime > 0 && a_maxTime <= now; }
237 
238  friend class Communicator;
239 };
240 
241 }
242 }
243 
244 #endif
245 
Definition: Communicator.hpp:79
virtual void initialize() noexcept(false)
Definition: Runnable.hpp:103
Definition: Millisecond.hpp:24
Type::_v getType() const
Definition: Handler.hpp:63
Communicator * a_communicator
Definition: Handler.hpp:129
virtual bool testClose() noexcept(false)
Definition: Handler.hpp:208
static std::string asText(const char *comment, const int number)
Definition: functions.hpp:153
void setTimeout(const Millisecond &timeout)
Definition: Handler.hpp:175
Definition: Node.hpp:56
void setfd(const int fd)
Definition: Handler.hpp:168
int getfd() const
Definition: Handler.hpp:69
Handler(Communicator *communicator, const Type::_v type, const int support=Support::CongestionControl)
Definition: Handler.hpp:137
virtual void finalize()
Definition: Handler.hpp:216
bool supportTimeout() const
Definition: Handler.hpp:85
Definition: ClientSocket.hpp:38
virtual void recoverAddress(const in_addr_t &address)
Definition: Handler.hpp:200
virtual void clone() noexcept(false)
Definition: Handler.hpp:223
void setIsRunning(const bool isRunning)
Definition: Runnable.hpp:97
virtual void run() noexcept(false)
Definition: Handler.hpp:42
virtual void breakAddress(const in_addr_t &address)
Definition: Handler.hpp:192
virtual ClientSocket * getClientSocket()
Definition: Handler.hpp:105
Definition: Runnable.hpp:27
xml::Node Node
Definition: Node.hpp:21
_v
Definition: Handler.hpp:48
Handler(const Type::_v type, const int support=Support::CongestionControl)
Definition: Handler.hpp:152
Definition: app.hpp:12
Definition: Handler.hpp:49
bool supportCongestionControl() const
Definition: Handler.hpp:77
Definition: Handler.hpp:33
Definition: Handler.hpp:47
Definition: Handler.hpp:53
bool canContinue() const
Definition: Handler.hpp:184
Definition: Handler.hpp:54
static Microsecond hardwareClock()
Definition: functions.hpp:390
_v
Definition: Handler.hpp:42
Definition: Microsecond.hpp:22