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