OpenDDS  Snapshot(2023/04/28-20:55)
PeriodicEvent.cpp
Go to the documentation of this file.
1 /*
2  *
3  *
4  * Distributed under the OpenDDS License.
5  * See: http://www.opendds.org/license.html
6  */
7 
8 #include "DCPS/DdsDcps_pch.h" //Only the _pch include should start with DCPS/
9 
10 #include "debug.h"
11 #include "PeriodicEvent.h"
12 
14 
15 namespace OpenDDS {
16 namespace DCPS {
17 
19  : dispatcher_(dispatcher)
20  , event_(event)
21  , strict_timing_(true)
22  , timer_id_(0)
23 {
24 }
25 
26 void PeriodicEvent::enable(const TimeDuration& period, bool immediate_dispatch, bool strict_timing)
27 {
30  if (timer_id_ < 1) {
31  EventDispatcher_rch dispatcher = dispatcher_.lock();
32  if (dispatcher) {
33  const MonotonicTimePoint expiration = now + period;
34  long id = dispatcher->schedule(rchandle_from(this), expiration);
35  if (id > 0) {
36  period_ = period;
37  expiration_ = expiration;
38  timer_id_ = id;
39  strict_timing_ = strict_timing;
40  if (immediate_dispatch) {
41  dispatcher->dispatch(rchandle_from(this));
42  }
43  } else if (log_level >= LogLevel::Warning) {
44  ACE_ERROR((LM_WARNING, "(%P|%t) PeriodicEvent::enable: failed to schedule\n"));
45  }
46  }
47  }
48 }
49 
51 {
53  if (timer_id_ > 0) {
54  EventDispatcher_rch dispatcher = dispatcher_.lock();
55  if (dispatcher) {
56  if (dispatcher->cancel(timer_id_)) {
57  timer_id_ = 0;
58  }
59  }
60  }
61 }
62 
64 {
66  return timer_id_ > 0;
67 }
68 
70 {
72  timer_id_ = 0;
73  EventDispatcher_rch dispatcher = dispatcher_.lock();
74  if (dispatcher) {
76  long id = dispatcher->schedule(rchandle_from(this), expiration);
77  if (id > 0) {
78  expiration_ = expiration;
79  timer_id_ = id;
80  }
81  }
82 }
83 
85 {
88  if (event_) {
89  RcHandle<EventBase> event_copy(event_);
90  guard.release();
91  event_copy->handle_event();
92  }
93 }
94 
96 {
98  if (event_) {
99  RcHandle<EventBase> event_copy(event_);
100  guard.release();
101  event_copy->handle_cancel();
102  }
103 }
104 
105 } // DCPS
106 } // OpenDDS
107 
ACE_Thread_Mutex event_mutex_
Definition: PeriodicEvent.h:76
RcHandle< T > rchandle_from(T *pointer)
Definition: RcHandle_T.h:310
#define ACE_ERROR(X)
void enable(const TimeDuration &period, bool immediate_dispatch=true, bool strict_timing=true)
RcHandle< EventBase > event_
Definition: PeriodicEvent.h:78
PeriodicEvent(EventDispatcher_rch dispatcher, EventBase_rch event)
virtual size_t cancel(long id)=0
int release(void)
static TimePoint_T< MonotonicClock > now()
Definition: TimePoint_T.inl:41
virtual long schedule(EventBase_rch event, const MonotonicTimePoint &expiration=MonotonicTimePoint::now())=0
LM_WARNING
OpenDDS_Dcps_Export LogLevel log_level
WeakRcHandle< EventDispatcher > dispatcher_
Definition: PeriodicEvent.h:77
#define OPENDDS_END_VERSIONED_NAMESPACE_DECL
MonotonicTimePoint expiration_
Definition: PeriodicEvent.h:81
virtual bool dispatch(EventBase_rch event)=0
The Internal API and Implementation of OpenDDS.
Definition: AddressCache.h:28