OpenDDS  Snapshot(2023/04/07-19:43)
Public Member Functions | Private Member Functions | Private Attributes | List of all members
OpenDDS::DCPS::PeriodicEvent Class Reference

#include <PeriodicEvent.h>

Inheritance diagram for OpenDDS::DCPS::PeriodicEvent:
Inheritance graph
[legend]
Collaboration diagram for OpenDDS::DCPS::PeriodicEvent:
Collaboration graph
[legend]

Public Member Functions

 PeriodicEvent (EventDispatcher_rch dispatcher, EventBase_rch event)
 
void enable (const TimeDuration &period, bool immediate_dispatch=true, bool strict_timing=true)
 
void disable ()
 
bool enabled () const
 
void handle_event ()
 
void handle_cancel ()
 
- Public Member Functions inherited from OpenDDS::DCPS::EventBase
virtual ~EventBase ()
 
virtual void handle_error ()
 Only called when an exception is caught during handle_event. More...
 
void operator() ()
 
- Public Member Functions inherited from OpenDDS::DCPS::RcObject
virtual ~RcObject ()
 
virtual void _add_ref ()
 
virtual void _remove_ref ()
 
long ref_count () const
 
WeakObject_get_weak_object () const
 

Private Member Functions

void handle_event_scheduling ()
 

Private Attributes

ACE_Thread_Mutex mutex_
 
ACE_Thread_Mutex event_mutex_
 
WeakRcHandle< EventDispatcherdispatcher_
 
RcHandle< EventBaseevent_
 
TimeDuration period_
 
bool strict_timing_
 
MonotonicTimePoint expiration_
 
long timer_id_
 

Additional Inherited Members

- Protected Member Functions inherited from OpenDDS::DCPS::RcObject
 RcObject ()
 

Detailed Description

PeriodicEvent is an event that is dispatched with a regular, repeating period

PeriodicEvent serves as a helpful utility class for repeatedly dispatching another base event at a regular, fixed interval. The PeriodicEvent takes both an EventDispatcher and a base event (c.f. EventBase) and handles the logic of scheduling, rescheduling, and canceling the base event with the EventDispatcher.

Definition at line 27 of file PeriodicEvent.h.

Constructor & Destructor Documentation

◆ PeriodicEvent()

OpenDDS::DCPS::PeriodicEvent::PeriodicEvent ( EventDispatcher_rch  dispatcher,
EventBase_rch  event 
)

Creates a PeriodicEvent to handle scheduling a base event with an EventDispatcher

Parameters
dispatcherthe EventDispatcher to use for scheduling
eventthe base event (c.f EventBase) to schedule for dispatch

Definition at line 18 of file PeriodicEvent.cpp.

19  : dispatcher_(dispatcher)
20  , event_(event)
21  , strict_timing_(true)
22  , timer_id_(0)
23 {
24 }
RcHandle< EventBase > event_
Definition: PeriodicEvent.h:78
WeakRcHandle< EventDispatcher > dispatcher_
Definition: PeriodicEvent.h:77

Member Function Documentation

◆ disable()

void OpenDDS::DCPS::PeriodicEvent::disable ( )

Disable the PeriodicEvent, canceling the scheduled base event if enabled.

Definition at line 50 of file PeriodicEvent.cpp.

References OpenDDS::DCPS::EventDispatcher::cancel(), dispatcher_, mutex_, and timer_id_.

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 }
WeakRcHandle< EventDispatcher > dispatcher_
Definition: PeriodicEvent.h:77
RcHandle< EventDispatcher > EventDispatcher_rch

◆ enable()

void OpenDDS::DCPS::PeriodicEvent::enable ( const TimeDuration period,
bool  immediate_dispatch = true,
bool  strict_timing = true 
)

Enable the PeriodicEvent to begin scheduling / rescheduling the base event using the internal EventDispatcher and the provided time period. If strict scheduling is requested, the elapsed time since the previous dispatch will not impact the time point provided for the next scheduled dispatch. Note: strict timing may potentially lead to CPU starvation if the dispatch takes longer than the requested period.

Parameters
periodthe time period used to schedule / reschedule the base event
immediate_dispatchset to true to immediately dispatch base event
strict_timingset to true to strictly calculate scheduled dispatch times

Definition at line 26 of file PeriodicEvent.cpp.

References ACE_ERROR, OpenDDS::DCPS::EventDispatcher::dispatch(), dispatcher_, expiration_, LM_WARNING, OpenDDS::DCPS::log_level, mutex_, OpenDDS::DCPS::TimePoint_T< MonotonicClock >::now(), period_, OpenDDS::DCPS::rchandle_from(), OpenDDS::DCPS::EventDispatcher::schedule(), strict_timing_, timer_id_, and OpenDDS::DCPS::LogLevel::Warning.

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 }
OpenDDS_Dcps_Export LogLevel log_level
#define ACE_ERROR(X)
static TimePoint_T< MonotonicClock > now()
Definition: TimePoint_T.inl:41
WeakRcHandle< EventDispatcher > dispatcher_
Definition: PeriodicEvent.h:77
MonotonicTimePoint expiration_
Definition: PeriodicEvent.h:81
TimePoint_T< MonotonicClock > MonotonicTimePoint
Definition: TimeTypes.h:51
RcHandle< T > rchandle_from(T *pointer)
Definition: RcHandle_T.h:310
RcHandle< EventDispatcher > EventDispatcher_rch

◆ enabled()

bool OpenDDS::DCPS::PeriodicEvent::enabled ( ) const

Check to see if the PeriodicEvent is currently enabled

Returns
true if the PeriodicEvent is currently enabled

Definition at line 63 of file PeriodicEvent.cpp.

References mutex_, and timer_id_.

◆ handle_cancel()

void OpenDDS::DCPS::PeriodicEvent::handle_cancel ( )
virtual

For use by EventDispatcher

Reimplemented from OpenDDS::DCPS::EventBase.

Definition at line 95 of file PeriodicEvent.cpp.

References event_, event_mutex_, OPENDDS_END_VERSIONED_NAMESPACE_DECL, and ACE_Guard< ACE_LOCK >::release().

96 {
98  if (event_) {
99  RcHandle<EventBase> event_copy(event_);
100  guard.release();
101  event_copy->handle_cancel();
102  }
103 }
RcHandle< EventBase > event_
Definition: PeriodicEvent.h:78
ACE_Thread_Mutex event_mutex_
Definition: PeriodicEvent.h:76

◆ handle_event()

void OpenDDS::DCPS::PeriodicEvent::handle_event ( )
virtual

For use by EventDispatcher

Implements OpenDDS::DCPS::EventBase.

Definition at line 84 of file PeriodicEvent.cpp.

References event_, event_mutex_, handle_event_scheduling(), and ACE_Guard< ACE_LOCK >::release().

85 {
88  if (event_) {
89  RcHandle<EventBase> event_copy(event_);
90  guard.release();
91  event_copy->handle_event();
92  }
93 }
RcHandle< EventBase > event_
Definition: PeriodicEvent.h:78
ACE_Thread_Mutex event_mutex_
Definition: PeriodicEvent.h:76

◆ handle_event_scheduling()

void OpenDDS::DCPS::PeriodicEvent::handle_event_scheduling ( )
private

Definition at line 69 of file PeriodicEvent.cpp.

References dispatcher_, expiration_, mutex_, OpenDDS::DCPS::TimePoint_T< MonotonicClock >::now(), period_, OpenDDS::DCPS::rchandle_from(), OpenDDS::DCPS::EventDispatcher::schedule(), strict_timing_, and timer_id_.

Referenced by handle_event().

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 }
static TimePoint_T< MonotonicClock > now()
Definition: TimePoint_T.inl:41
WeakRcHandle< EventDispatcher > dispatcher_
Definition: PeriodicEvent.h:77
MonotonicTimePoint expiration_
Definition: PeriodicEvent.h:81
TimePoint_T< MonotonicClock > MonotonicTimePoint
Definition: TimeTypes.h:51
RcHandle< T > rchandle_from(T *pointer)
Definition: RcHandle_T.h:310
RcHandle< EventDispatcher > EventDispatcher_rch

Member Data Documentation

◆ dispatcher_

WeakRcHandle<EventDispatcher> OpenDDS::DCPS::PeriodicEvent::dispatcher_
private

Definition at line 77 of file PeriodicEvent.h.

Referenced by disable(), enable(), and handle_event_scheduling().

◆ event_

RcHandle<EventBase> OpenDDS::DCPS::PeriodicEvent::event_
private

Definition at line 78 of file PeriodicEvent.h.

Referenced by handle_cancel(), and handle_event().

◆ event_mutex_

ACE_Thread_Mutex OpenDDS::DCPS::PeriodicEvent::event_mutex_
mutableprivate

Definition at line 76 of file PeriodicEvent.h.

Referenced by handle_cancel(), and handle_event().

◆ expiration_

MonotonicTimePoint OpenDDS::DCPS::PeriodicEvent::expiration_
private

Definition at line 81 of file PeriodicEvent.h.

Referenced by enable(), and handle_event_scheduling().

◆ mutex_

ACE_Thread_Mutex OpenDDS::DCPS::PeriodicEvent::mutex_
mutableprivate

Definition at line 75 of file PeriodicEvent.h.

Referenced by disable(), enable(), enabled(), and handle_event_scheduling().

◆ period_

TimeDuration OpenDDS::DCPS::PeriodicEvent::period_
private

Definition at line 79 of file PeriodicEvent.h.

Referenced by enable(), and handle_event_scheduling().

◆ strict_timing_

bool OpenDDS::DCPS::PeriodicEvent::strict_timing_
private

Definition at line 80 of file PeriodicEvent.h.

Referenced by enable(), and handle_event_scheduling().

◆ timer_id_

long OpenDDS::DCPS::PeriodicEvent::timer_id_
private

Definition at line 82 of file PeriodicEvent.h.

Referenced by disable(), enable(), enabled(), and handle_event_scheduling().


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