libassa 3.5.1
Loading...
Searching...
No Matches
Public Member Functions | Private Attributes | List of all members
ASSA::TimerQueue Class Reference

#include <TimerQueue.h>

Public Member Functions

 TimerQueue ()
 Constructor.
 
 ~TimerQueue ()
 Destructor.
 
bool isEmpty ()
 Is queue empty?
 
TimerId insert (EventHandler *eh_, const TimeVal &tv_, const TimeVal &delta_, const std::string &name_)
 Add timer (EventHandler object) to the queue to be dispatch at the time specified.
 
int remove (EventHandler *eh_)
 Cancel all timers for the EventHandler eh_.
 
bool remove (TimerId tid_)
 Cancel timer.
 
int expire (const TimeVal &tv_)
 Traverse the queue, triggering all timers that are past argument timeval.
 
TimeValtop (void)
 Return expiration time of the top element in the queue.
 
void dump (void)
 Dump Queue information to the log file.
 

Private Attributes

PriorityQueue< Timer *, TimerComparem_queue
 Timer queue itself.
 

Detailed Description

Definition at line 35 of file TimerQueue.h.

Constructor & Destructor Documentation

◆ TimerQueue()

ASSA::TimerQueue::TimerQueue ( )
inline

Constructor.

Definition at line 102 of file TimerQueue.h.

104{
105 trace("TimerQueue::TimerQueue");
106}
#define trace(s)
trace() is used to trace function call chain in C++ program.
Definition Logger.h:429

References trace.

◆ ~TimerQueue()

TimerQueue::~TimerQueue ( )

Destructor.

Definition at line 19 of file TimerQueue.cpp.

21{
22 trace_with_mask("TimerQueue::~TimerQueue",REACTTRACE);
23
24 while (m_queue.size ()) {
25 delete m_queue.pop ();
26 }
27}
#define trace_with_mask(s, m)
trace_with_mask() is used to trace function call chain in C++ program.
Definition Logger.h:437
PriorityQueue< Timer *, TimerCompare > m_queue
Timer queue itself.
Definition TimerQueue.h:94
@ REACTTRACE
Extended Reactor/PrioriyQueue messages
Definition LogMask.h:40

References m_queue, ASSA::REACTTRACE, and trace_with_mask.

Member Function Documentation

◆ dump()

void TimerQueue::dump ( void  )

Dump Queue information to the log file.

Definition at line 151 of file TimerQueue.cpp.

153{
154 trace("TimerQueue::dump");
155
156 if (m_queue.size() == 0) {
157 DL((REACT,"Queue is empty\n"));
158 }
159 else {
160 for (size_t i = 0; i < m_queue.size (); ) {
161 m_queue[i++]->dump();
162 }
163 }
164}
#define DL(X)
A macro for writing debug message to the Logger.
Definition Logger.h:273
A wrapper class to provide AutoPtr with reference semantics.
Definition AutoPtr.h:32
@ REACT
Class Reactor/PrioriyQueue messages
Definition LogMask.h:39

References DL, m_queue, ASSA::REACT, and trace.

Referenced by ASSA::Reactor::calculateTimeout(), ASSA::Reactor::registerTimerHandler(), and ASSA::Reactor::removeTimerHandler().

◆ expire()

int TimerQueue::expire ( const TimeVal tv_)

Traverse the queue, triggering all timers that are past argument timeval.

Timer(s) are then removed from the queue.

Parameters
tv_Expiration time
Returns
Number of callbacks dispatched

Reschedule without deleting the Timer object so that application-level code can still hold to the valid TimerId.

Definition at line 88 of file TimerQueue.cpp.

90{
91 trace_with_mask("TimerQueue::expire",REACTTRACE);
92
93 register Timer* tp = (Timer*) NULL;
94 register int cnt = 0;
95
96 while (m_queue.size () && (tp = m_queue.top ()) != (Timer*) NULL) {
97 if (tp->getExpirationTime () > tv_) {
98 DL((REACT,"Top timer:\n"));
99 tp->dump ();
100 break;
101 }
102 /* First, pop item from the queue. Then call an appropriate
103 EventHandler. If done in reverse, EventHandler might
104 remove item first and then pop () will fail
105 (This needs more investigation!).
106 */
107 m_queue.pop ();
108
109 DL((REACT,"Expired %s [t=%s] timer!\n",
110 tp->get_id ().c_str (),
111 tp->getExpirationTime ().fmtString ().c_str ()));
112
113 int ret = tp->getHandler ()->handle_timeout ((TimerId) tp);
114
118 if (ret == 1) {
119 tp->rescheduleExpirationTime ();
120 m_queue.insert (tp);
121 }
122 else {
123 delete tp;
124 tp = (Timer*)NULL;
125 }
126 cnt++;
127 }
128
129 if (cnt) {
130 DL((TRACE,"Expired total of %d timer(s).\n",cnt));
131 }
132
133 return cnt;
134}
unsigned long TimerId
Timer Id is used in handle_timeout() calls.
@ TRACE
Function call trace
Definition LogMask.h:26

References DL, m_queue, ASSA::REACT, ASSA::REACTTRACE, ASSA::TRACE, and trace_with_mask.

Referenced by ASSA::Reactor::dispatch(), and ASSA::Reactor::waitForEvents().

◆ insert()

TimerId TimerQueue::insert ( EventHandler eh_,
const TimeVal tv_,
const TimeVal delta_,
const std::string &  name_ 
)

Add timer (EventHandler object) to the queue to be dispatch at the time specified.

Parameters
eh_Pointer to Event Handler that will be called when timer expires.
tv_Absolute expiration time.
delta_Relative timeout value.
name_Name of the timer (for easy identification).
Returns
TimerId that uniquely identifies the timer. It can be used to cancel timer.

Definition at line 137 of file TimerQueue.cpp.

142{
143 trace("TimerQueue::insert");
144
145 Timer* t = new Timer (eh_, tv_, delta_, name_);
146 m_queue.insert (t);
147 return (TimerId) t;
148}

References m_queue, and trace.

Referenced by ASSA::Reactor::registerTimerHandler().

◆ isEmpty()

bool ASSA::TimerQueue::isEmpty ( )
inline

Is queue empty?

Returns
true if queue empty; false if not.

Definition at line 109 of file TimerQueue.h.

111{
112 return m_queue.size () == 0;
113}

References m_queue.

Referenced by ASSA::Reactor::calculateTimeout().

◆ remove() [1/2]

int TimerQueue::remove ( EventHandler eh_)

Cancel all timers for the EventHandler eh_.

Parameters
eh_Pointer to Event Handler.
Returns
Number timers removed.

Definition at line 30 of file TimerQueue.cpp.

32{
33 // Like STL iterators, after deletion of an element,
34 // queue structure and indexing might drastically change
35 // and there is no guarantee that elements we haven't seen
36 // yet will not be moved past the iterator. Therefore,
37 // we must start scanning from the beginning after each deletion :-(
38
39 trace_with_mask("TimerQueue::remove(eh_)",REACTTRACE);
40
41 register size_t i;
42 int cnt = 0;
43 bool f = true; // changed flag
44 register Timer* tmr;
45
46 DL((REACT,"Searching for Timer: 0x%x\n", dynamic_cast<void*> (eh_)));
47
48 while (f) {
49 f = false;
50 DL((REACT,"Queue size: %d\n", m_queue.size()));
51 for (i = 0; i < m_queue.size (); i++) {
52 if (m_queue[i]->getHandler() == eh_) {
53 DL((REACT,"Found Timer: 0x%x in slot: %d\n",
54 dynamic_cast<void*>(eh_), i));
55 tmr = m_queue[i];
56 m_queue.remove (tmr);
57 delete tmr;
58 cnt++;
59 f = true;
60 }
61 }
62 }
63 return cnt;
64}

References DL, m_queue, ASSA::REACT, ASSA::REACTTRACE, and trace_with_mask.

Referenced by ASSA::Reactor::removeHandler(), and ASSA::Reactor::removeTimerHandler().

◆ remove() [2/2]

bool TimerQueue::remove ( TimerId  tid_)

Cancel timer.

Parameters
tid_Timer id.
Returns
true if timer was found in the queue; false otherwise.

Definition at line 67 of file TimerQueue.cpp.

69{
70 trace_with_mask("TimerQueue::remove(tid)",REACTTRACE);
71 register size_t i;
72
73 DL((REACTTRACE,"Queue size before remove: %d\n", m_queue.size()));
74
75 for (i = 0; i < m_queue.size (); i++) {
76 if (m_queue[i] == (Timer*) tid_) {
77 Timer* tmr = m_queue[i];
78 int ret = m_queue.remove (tmr);
79 delete tmr;
80 DL((REACTTRACE,"Queue size after remove: %d\n", m_queue.size()));
81 return ret;
82 }
83 }
84 return false;
85}

References DL, m_queue, ASSA::REACTTRACE, and trace_with_mask.

◆ top()

TimeVal & ASSA::TimerQueue::top ( void  )
inline

Return expiration time of the top element in the queue.

Definition at line 116 of file TimerQueue.h.

118{
119 return (TimeVal&) m_queue.top ()->getExpirationTime ();
120}

References m_queue.

Referenced by ASSA::Reactor::calculateTimeout().

Member Data Documentation

◆ m_queue

PriorityQueue<Timer*, TimerCompare> ASSA::TimerQueue::m_queue
private

Timer queue itself.

Definition at line 94 of file TimerQueue.h.

Referenced by dump(), expire(), insert(), isEmpty(), remove(), remove(), top(), and ~TimerQueue().


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