Public Member Functions | Public Attributes | List of all members
mf::CountAndLimit Class Reference

#include <ELmap.h>

Public Member Functions

 CountAndLimit (int lim=-1, int ts=-1, int ivl=-1)
 
bool add ()
 

Public Attributes

int n {}
 
int aggregateN {}
 
time_t lastTime
 
int limit
 
int timespan
 
int interval
 
int skipped
 

Detailed Description

Definition at line 51 of file ELmap.h.

Constructor & Destructor Documentation

mf::CountAndLimit::CountAndLimit ( int  lim = -1,
int  ts = -1,
int  ivl = -1 
)

Definition at line 19 of file ELmap.cc.

20  : lastTime{time(0)}
21  , limit{lim}
22  , timespan{ts}
23  , interval{ivl}
24  , skipped{ivl - 1} // So that the FIRST of the prescaled messages emerges
25  {}
time_t lastTime
Definition: ELmap.h:55

Member Function Documentation

bool mf::CountAndLimit::add ( )

Definition at line 28 of file ELmap.cc.

29  {
30  time_t now = time(0);
31 
32  // Has it been so long that we should restart counting toward the limit?
33  if ((timespan >= 0) && (difftime(now, lastTime) >= timespan)) {
34  n = 0;
35  if (interval > 0) {
36  skipped = interval - 1; // So this message will be reacted to
37  } else {
38  skipped = 0;
39  }
40  }
41 
42  lastTime = now;
43 
44  ++n;
45  ++aggregateN;
46  ++skipped;
47 
48  if (skipped < interval)
49  return false;
50 
51  if (limit == 0)
52  return false; // Zero limit - never react to this
53  if ((limit < 0) || (n <= limit)) {
54  skipped = 0;
55  return true;
56  }
57 
58  // Now we are over the limit - have we exceeded limit by 2^N * limit?
59  long diff = n - limit;
60  long r = diff / limit;
61  if (r * limit != diff) { // Not a multiple of limit - don't react
62  return false;
63  }
64  if (r == 1) { // Exactly twice limit - react
65  skipped = 0;
66  return true;
67  }
68 
69  while (r > 1) {
70  if ((r & 1) != 0)
71  return false; // Not 2**n times limit - don't react
72  r >>= 1;
73  }
74  // If you never get an odd number till one, r is 2**n so react
75 
76  skipped = 0;
77  return true;
78  } // add()
time_t lastTime
Definition: ELmap.h:55
int aggregateN
Definition: ELmap.h:54

Member Data Documentation

int mf::CountAndLimit::aggregateN {}

Definition at line 54 of file ELmap.h.

int mf::CountAndLimit::interval

Definition at line 58 of file ELmap.h.

time_t mf::CountAndLimit::lastTime

Definition at line 55 of file ELmap.h.

int mf::CountAndLimit::limit

Definition at line 56 of file ELmap.h.

int mf::CountAndLimit::n {}

Definition at line 53 of file ELmap.h.

int mf::CountAndLimit::skipped

Definition at line 59 of file ELmap.h.

int mf::CountAndLimit::timespan

Definition at line 57 of file ELmap.h.


The documentation for this class was generated from the following files: