ELmap.cc
Go to the documentation of this file.
2 
3 #include <ctime>
4 
5 namespace mf {
6 
7  // ----------------------------------------------------------------------
8  // LimitAndTimespan:
9  // ----------------------------------------------------------------------
10 
11  LimitAndTimespan::LimitAndTimespan(int const lim, int const ts, int const ivl)
12  : limit{lim}, timespan{ts}, interval{ivl}
13  {}
14 
15  // ----------------------------------------------------------------------
16  // CountAndLimit:
17  // ----------------------------------------------------------------------
18 
19  CountAndLimit::CountAndLimit(int const lim, int const ts, int const ivl)
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  {}
26 
27  bool
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()
79 
80  // ----------------------------------------------------------------------
81  // StatsCount:
82  // ----------------------------------------------------------------------
83 
84  void
85  StatsCount::add(std::string const& context, bool const reactedTo)
86  {
87  ++n;
88  ++aggregateN;
89 
90  ((1 == n) ? context1 : (2 == n) ? context2 : contextLast) =
91  std::string(context, 0, 16);
92 
93  if (!reactedTo)
94  ignoredFlag = true;
95  }
96 
97 } // end of namespace mf */
time_t lastTime
Definition: ELmap.h:55
LimitAndTimespan(int lim=-1, int ts=-1, int ivl=-1)
Definition: ELmap.cc:11
std::string string
Definition: nybbler.cc:12
CountAndLimit(int lim=-1, int ts=-1, int ivl=-1)
Definition: ELmap.cc:19
int aggregateN
Definition: ELmap.h:54
void add(std::string const &context, bool reactedTo)
Definition: ELmap.cc:85
bool add()
Definition: ELmap.cc:28