artdaq_mfextensions  v1_06_02
ma_cell.cpp
1 
2 #include "ErrorHandler/MessageAnalyzer/ma_cell.h"
3 #include "ErrorHandler/MessageAnalyzer/ma_condition.h"
4 
5 #include <time.h>
6 
7 using namespace novadaq::errorhandler;
8 
9 ma_cell::ma_cell()
10  : msgs()
11  , on(false)
12  , what_()
13  , t_event(0)
14 {
15 }
16 
17 ma_cell::~ma_cell()
18 {
19 }
20 
21 bool ma_cell::hit(qt_mf_msg const& msg, boost::smatch const& w, ma_condition& cond, size_t s_idx, size_t t_idx)
22 {
23  // regex groups
24  what_ = w;
25 
26  // push new message
27  time_t latest = msg.time().tv_sec;
28  msgs.push_back(msg);
29 
30  if (on && cond.persistent())
31  {
32  msgs.pop_front();
33  return false;
34  }
35 
36  // pop expired messages ( >timespan )
37  while (latest - msgs.front().time().tv_sec > cond.timespan())
38  msgs.pop_front();
39 
40  // pop excessive messages ( >count )
41  while (msgs.size() > (size_t)cond.trigger_count())
42  msgs.pop_front();
43 
44  // determin the new state (on or off)
45  bool new_state = false;
46 
47  if (msgs.size() == (size_t)cond.trigger_count())
48  {
49  new_state = cond.at_least() ? true : false;
50 
51  // lock
52  boost::mutex::scoped_lock lock(cond.timing_events().lock);
53 
54  // schedule event
55  // t0 = events.front();
56  // schedule(t0 + ts + 1);
57 
58  time_t t0 = msgs.front().time().tv_sec;
59  t_event = t0 + cond.timespan() + 1;
60  cond.timing_events().event_queue().push(ma_timing_event(t_event, cond, s_idx, t_idx));
61  }
62  else if (cond.at_most())
63  {
64  // lock
65  boost::mutex::scoped_lock lock(cond.timing_events().lock);
66 
67  // not reached the critical size
68  // for occur_at_least, do nothing.
69  // for occur_at_most, schedule an event to turn on the cell
70  // t0 = events.front();
71  // schedule(t0 + ts + 1);
72 
73  time_t t0 = msgs.front().time().tv_sec;
74  t_event = t0 + cond.timespan() + 1;
75  cond.timing_events().event_queue().push(ma_timing_event(t_event, cond, s_idx, t_idx));
76  }
77 
78  // no change in status
79  if (new_state == on)
80  return false;
81 
82  // changed
83  on = new_state;
84  return true;
85 }
86 
87 bool ma_cell::event(time_t t, ma_condition& cond)
88 {
89  // not reached the event time, no flip
90  if (t != t_event)
91  return false;
92 
93  bool new_status = cond.at_most() ? true : (cond.persistent() ? true : false);
94 
95  // not flipped
96  if (new_status == on)
97  return false;
98 
99  // flipped
100  on = new_status;
101  return true;
102 }
103 
104 void ma_cell::reset()
105 {
106  on = false;
107  t_event = 0;
108  msgs.clear();
109 }
timeval time() const
Get the message timestamp
Definition: qt_mf_msg.hh:96
Qt wrapper around MessageFacility message
Definition: qt_mf_msg.hh:37