libassa  3.5.1
Public Member Functions | Private Attributes | List of all members
ASSA::TimerQueue Class Reference

#include <TimerQueue.h>

Public Member Functions

 TimerQueue ()
 Constructor. More...
 
 ~TimerQueue ()
 Destructor. More...
 
bool isEmpty ()
 Is queue empty? More...
 
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. More...
 
int remove (EventHandler *eh_)
 Cancel all timers for the EventHandler eh_. More...
 
bool remove (TimerId tid_)
 Cancel timer. More...
 
int expire (const TimeVal &tv_)
 Traverse the queue, triggering all timers that are past argument timeval. More...
 
TimeValtop (void)
 Return expiration time of the top element in the queue. More...
 
void dump (void)
 Dump Queue information to the log file. More...
 

Private Attributes

PriorityQueue< Timer *, TimerComparem_queue
 Timer queue itself. More...
 

Detailed Description

Definition at line 35 of file TimerQueue.h.

Constructor & Destructor Documentation

◆ TimerQueue()

ASSA::TimerQueue::TimerQueue ( )
inline

Constructor.

Definition at line 103 of file TimerQueue.h.

104 {
105  trace("TimerQueue::TimerQueue");
106 }

References trace.

◆ ~TimerQueue()

TimerQueue::~TimerQueue ( )

Destructor.

Definition at line 20 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 }

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 152 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 }

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 89 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 }

References DL, ASSA::Timer::dump(), ASSA::TimeVal::fmtString(), ASSA::Timer::get_id(), ASSA::Timer::getExpirationTime(), ASSA::Timer::getHandler(), ASSA::EventHandler::handle_timeout(), 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 138 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 110 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 31 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 68 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 117 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(), top(), and ~TimerQueue().


The documentation for this class was generated from the following files:
ASSA::TimerId
unsigned long TimerId
Definition: EventHandler.h:27
ASSA::Timer::getHandler
EventHandler * getHandler() const
Get EventHandler pointer.
Definition: Timer.h:68
ASSA::TRACE
@ TRACE
Function call trace
Definition: LogMask.h:26
ASSA::REACT
@ REACT
Class Reactor/PrioriyQueue messages
Definition: LogMask.h:39
ASSA::EventHandler::handle_timeout
virtual int handle_timeout(TimerId tid)
Timeout handler callback.
Definition: EventHandler.h:196
ASSA::TimeVal::fmtString
string fmtString(const char *fmt_=NULL) const
Format timeval structure into readable format.
Definition: TimeVal.cpp:146
trace
#define trace(s)
Definition: Logger.h:429
ASSA::TimerQueue::m_queue
PriorityQueue< Timer *, TimerCompare > m_queue
Timer queue itself.
Definition: TimerQueue.h:94
ASSA::REACTTRACE
@ REACTTRACE
Extended Reactor/PrioriyQueue messages
Definition: LogMask.h:40
ASSA::Timer::dump
void dump(void)
Dump contents to logfile.
Definition: Timer.h:177
DL
#define DL(X)
Definition: Logger.h:273
ASSA::Timer::getExpirationTime
const TimeVal & getExpirationTime() const
Get Expiration Time.
Definition: Timer.h:71
ASSA::Timer::get_id
std::string get_id() const
Retrieve Timer ID.
Definition: Timer.h:89
trace_with_mask
#define trace_with_mask(s, m)
Definition: Logger.h:437
ASSA::Timer
Definition: Timer.h:35