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

#include <SporadicEvent.h>

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

Public Member Functions

 SporadicEvent (EventDispatcher_rch dispatcher, EventBase_rch event)
 
void schedule (const TimeDuration &duration)
 
void cancel ()
 
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_
 
MonotonicTimePoint expiration_
 
long timer_id_
 

Additional Inherited Members

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

Detailed Description

SporadicEvent is an event which can be scheduled multiple times but will only dispatch once

SporadicEvent allows repeated calls to schedule (when the application is unsure if the event has already been scheduled), but cumulative calls to schedule will only result in a single dispatch. After the base event dispatches, additional calls to schedule will result in another dispatch. Additional calls to schedule may, however, shorten the scheduled time until the base event dispatches. The SporadicEvent 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 30 of file SporadicEvent.h.

Constructor & Destructor Documentation

◆ SporadicEvent()

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

Creates a SporadicEvent 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 17 of file SporadicEvent.cpp.

18  : dispatcher_(dispatcher)
19  , event_(event)
20  , timer_id_(0)
21 {
22 }
RcHandle< EventBase > event_
Definition: SporadicEvent.h:71
WeakRcHandle< EventDispatcher > dispatcher_
Definition: SporadicEvent.h:70

Member Function Documentation

◆ cancel()

void OpenDDS::DCPS::SporadicEvent::cancel ( void  )

Cancel the SporadicEvent, canceling the scheduled base event if scheduled.

Definition at line 54 of file SporadicEvent.cpp.

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

55 {
57  if (timer_id_ > 0) {
58  EventDispatcher_rch dispatcher = dispatcher_.lock();
59  if (dispatcher) {
60  if (dispatcher->cancel(timer_id_)) {
61  timer_id_ = 0;
62  }
63  }
64  }
65 }
WeakRcHandle< EventDispatcher > dispatcher_
Definition: SporadicEvent.h:70
RcHandle< EventDispatcher > EventDispatcher_rch

◆ handle_cancel()

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

For use by EventDispatcher

Reimplemented from OpenDDS::DCPS::EventBase.

Definition at line 84 of file SporadicEvent.cpp.

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

85 {
87  if (event_) {
88  RcHandle<EventBase> event_copy(event_);
89  guard.release();
90  event_copy->handle_cancel();
91  }
92 }
ACE_Thread_Mutex event_mutex_
Definition: SporadicEvent.h:69
RcHandle< EventBase > event_
Definition: SporadicEvent.h:71

◆ handle_event()

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

For use by EventDispatcher

Implements OpenDDS::DCPS::EventBase.

Definition at line 73 of file SporadicEvent.cpp.

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

74 {
77  if (event_) {
78  RcHandle<EventBase> event_copy(event_);
79  guard.release();
80  event_copy->handle_event();
81  }
82 }
ACE_Thread_Mutex event_mutex_
Definition: SporadicEvent.h:69
RcHandle< EventBase > event_
Definition: SporadicEvent.h:71

◆ handle_event_scheduling()

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

Definition at line 67 of file SporadicEvent.cpp.

References mutex_, and timer_id_.

Referenced by handle_event().

◆ schedule()

void OpenDDS::DCPS::SporadicEvent::schedule ( const TimeDuration duration)

Schedule the SporadicEvent to dispatch the base event after the specified duration. If the SporadicEvent is already scheduled, the duration is compared with the existing scheduled time and the shorter (sooner) of the two durations is used.

Parameters
durationthe time period used to schedule / reschedule the base event

Definition at line 24 of file SporadicEvent.cpp.

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

25 {
28  if (timer_id_ < 1) {
29  EventDispatcher_rch dispatcher = dispatcher_.lock();
30  if (dispatcher) {
31  const MonotonicTimePoint expiration = now + duration;
32  long id = dispatcher->schedule(rchandle_from(this), expiration);
33  if (id > 0) {
34  expiration_ = expiration;
35  timer_id_ = id;
36  }
37  }
38  } else if (now + duration < expiration_) {
39  EventDispatcher_rch dispatcher = dispatcher_.lock();
40  if (dispatcher) {
41  if (dispatcher->cancel(timer_id_)) {
42  timer_id_ = 0;
43  const MonotonicTimePoint expiration = now + duration;
44  long id = dispatcher->schedule(rchandle_from(this), expiration);
45  if (id > 0) {
46  expiration_ = expiration;
47  timer_id_ = id;
48  }
49  }
50  }
51  }
52 }
static TimePoint_T< MonotonicClock > now()
Definition: TimePoint_T.inl:41
WeakRcHandle< EventDispatcher > dispatcher_
Definition: SporadicEvent.h:70
MonotonicTimePoint expiration_
Definition: SporadicEvent.h:72
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::SporadicEvent::dispatcher_
private

Definition at line 70 of file SporadicEvent.h.

Referenced by cancel(), and schedule().

◆ event_

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

Definition at line 71 of file SporadicEvent.h.

Referenced by handle_cancel(), and handle_event().

◆ event_mutex_

ACE_Thread_Mutex OpenDDS::DCPS::SporadicEvent::event_mutex_
mutableprivate

Definition at line 69 of file SporadicEvent.h.

Referenced by handle_cancel(), and handle_event().

◆ expiration_

MonotonicTimePoint OpenDDS::DCPS::SporadicEvent::expiration_
private

Definition at line 72 of file SporadicEvent.h.

Referenced by schedule().

◆ mutex_

ACE_Thread_Mutex OpenDDS::DCPS::SporadicEvent::mutex_
mutableprivate

Definition at line 68 of file SporadicEvent.h.

Referenced by cancel(), handle_event_scheduling(), and schedule().

◆ timer_id_

long OpenDDS::DCPS::SporadicEvent::timer_id_
private

Definition at line 73 of file SporadicEvent.h.

Referenced by cancel(), handle_event_scheduling(), and schedule().


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