ANNA Suite  2020b
Multipurpose development suite for Telco applications
CongestionController.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_CongestionController_hpp
10 #define anna_comm_CongestionController_hpp
11 
12 #include <utility>
13 
14 #include <anna/config/defines.hpp>
15 #include <anna/core/Singleton.hpp>
16 #include <anna/core/mt/NRMutex.hpp>
18 
19 namespace anna {
20 
21 namespace xml {
22 class Node;
23 }
24 
25 namespace comm {
26 
27 class ClientSocket;
28 
29 namespace handler {
30 class LocalConnection;
31 }
32 
63 class CongestionController : public Singleton <CongestionController> {
64 public:
68  static const int MaxLevel = 4;
69 
73  struct Advice {
74  enum _v {
75  None,
77  Discard
78  };
79  };
80 
84  struct Mode {
85  enum _v {
86  Auto,
91  Global
92  };
93  };
94 
95  typedef std::pair <int, int> Workload;
96 
97  static const int DefaultLimit = 60;
98 
102  static const int MaxPendingBytes;
103 
108  Mode::_v getMode() const { return a_mode; }
109 
114  int getMessageCounter() const { return a_messageCounter; }
115 
120  int getSuccessCounter() const { return a_messageCounter - a_discardedCounter; }
121 
126  void setLimit(const int limit) ;
127 
132  void setMode(const Mode::_v mode) {
133  if((a_mode = mode) == Mode::Auto) {
134  WHEN_MULTITHREAD(a_effectiveMode = Mode::Local);
135  WHEN_SINGLETHREAD(a_effectiveMode = Mode::Global);
136  } else
137  a_effectiveMode = mode;
138  }
139 
144  bool isEmpty() const { return a_avgWorkload.isEmpty(); }
145 
154  void setMaxPendingBytes(const int maxPendingBytes) noexcept(false);
155 
164  Advice::_v getAdvice(const ClientSocket& clientSocket) ;
165 
174  Advice::_v getAdvice(const ClientSocket* clientSocket) {
175  return (clientSocket == NULL) ? Advice::Process : getAdvice(*clientSocket);
176  }
177 
190  Workload getAccumulatedWorkload() const ;
191 
206  Workload getCurrentWorkload(const ClientSocket& clientSocket) const ;
207 
212  xml::Node* asXML(xml::Node* parent) const ;
213 
218  static int getLoad(const Workload& workload) { return workload.second; }
219 
224  static int getLevel(const Workload& workload) { return workload.first; }
225 
226 private:
227  static const Millisecond DelayTrace;
228  static const int UnusedPendingBytes = -1;
229  static const Millisecond HeartBeat;
230 
231  //----------------------------------------------------------------------------------
232  // a_limit: % de ocupacion maxima al que vamos a limitar los canales.
233  // a_discardLevel: Valor maximo del % de cada nivel de descarte. 0 = 25, 1 = 50,
234  // 2 = 85, 3 = 99
235  //----------------------------------------------------------------------------------
236  int a_limit;
237  int a_discardLevel [MaxLevel];
238  int a_percentage [MaxLevel];
239  NRMutex a_mutex;
240  Mode::_v a_mode;
241  Mode::_v a_effectiveMode;
242  Average <unsigned int> a_avgWorkload;
243  unsigned int a_messageCounter;
244  unsigned int a_discardedCounter;
245  mutable Millisecond a_timeTrace;
246  int a_maxPendingBytes;
247  int a_incomingSocketCounter;
248  Millisecond a_tickTime;
249 
250  void incrementIncomingSocket() noexcept(false);
251  void decrementIncomingSocket() noexcept(false);
252 
255 
256  int calculeWorkload(const ClientSocket&) const ;
257 
259 
260  friend class ClientSocket;
261  // getUpdatePeriod
262 
264  // incrementIncomingSocket, decrementIncomingSocket
265 };
266 
267 }
268 }
269 
270 #endif
271 
_v
Definition: CongestionController.hpp:74
Definition: CongestionController.hpp:84
Definition: Millisecond.hpp:24
#define WHEN_MULTITHREAD(a)
Definition: defines.hpp:96
Definition: NRMutex.hpp:39
std::pair< int, int > Workload
Definition: CongestionController.hpp:95
Definition: Node.hpp:56
Definition: LocalConnection.hpp:22
Definition: Singleton.hpp:76
Definition: CongestionController.hpp:75
_v
Definition: CongestionController.hpp:85
Definition: ClientSocket.hpp:38
Definition: CongestionController.hpp:63
static int getLevel(const Workload &workload)
Definition: CongestionController.hpp:224
#define WHEN_SINGLETHREAD(a)
Definition: defines.hpp:97
static int getLoad(const Workload &workload)
Definition: CongestionController.hpp:218
static const int MaxPendingBytes
Definition: CongestionController.hpp:102
Advice::_v getAdvice(const ClientSocket *clientSocket)
Definition: CongestionController.hpp:174
int getMessageCounter() const
Definition: CongestionController.hpp:114
Definition: CongestionController.hpp:73
Definition: CongestionController.hpp:90
xml::Node Node
Definition: Node.hpp:21
Definition: CongestionController.hpp:76
Definition: app.hpp:12
bool isEmpty() const
Definition: CongestionController.hpp:144
void setMode(const Mode::_v mode)
Definition: CongestionController.hpp:132
Mode::_v getMode() const
Definition: CongestionController.hpp:108
int getSuccessCounter() const
Definition: CongestionController.hpp:120