Loading...
Searching...
No Matches
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.
TimeVal & | top (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 *, TimerCompare > | m_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.

{
trace("TimerQueue::TimerQueue");
}
trace
#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.

{
trace_with_mask("TimerQueue::~TimerQueue",REACTTRACE);
while (m_queue.size ()) {
delete m_queue.pop ();
}
}
trace_with_mask
#define trace_with_mask(s, m)
trace_with_mask() is used to trace function call chain in C++ program.
Definition Logger.h:437
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

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.

{
trace("TimerQueue::dump");
if (m_queue.size() == 0) {
DL((REACT,"Queue is empty\n"));
}
else {
for (size_t i = 0; i < m_queue.size (); ) {
m_queue[i++]->dump();
}
}
}
DL
#define DL(X)
A macro for writing debug message to the Logger.
Definition Logger.h:273
ASSA::AutoPtrRef
A wrapper class to provide AutoPtr with reference semantics.
Definition AutoPtr.h:32
ASSA::REACT
@ 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.

{
trace_with_mask("TimerQueue::expire",REACTTRACE);
register Timer* tp = (Timer*) NULL;
register int cnt = 0;
while (m_queue.size () && (tp = m_queue.top ()) != (Timer*) NULL) {
if (tp->getExpirationTime () > tv_) {
DL((REACT,"Top timer:\n"));
tp->dump ();
break;
}
/* First, pop item from the queue. Then call an appropriate
EventHandler. If done in reverse, EventHandler might
remove item first and then pop () will fail
(This needs more investigation!).
*/
m_queue.pop ();
DL((REACT,"Expired %s [t=%s] timer!\n",
tp->get_id ().c_str (),
tp->getExpirationTime ().fmtString ().c_str ()));
int ret = tp->getHandler ()->handle_timeout ((TimerId) tp);
if (ret == 1) {
tp->rescheduleExpirationTime ();
m_queue.insert (tp);
}
else {
delete tp;
tp = (Timer*)NULL;
}
cnt++;
}
if (cnt) {
DL((TRACE,"Expired total of %d timer(s).\n",cnt));
}
return cnt;
}
ASSA::Timer
Definition Timer.h:36
ASSA::TimerId
unsigned long TimerId
Timer Id is used in handle_timeout() calls.
Definition EventHandler.h:27
ASSA::TRACE
@ 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.

{
trace("TimerQueue::insert");
Timer* t = new Timer (eh_, tv_, delta_, name_);
m_queue.insert (t);
return (TimerId) t;
}

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.

{
return m_queue.size () == 0;
}

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.

{
// Like STL iterators, after deletion of an element,
// queue structure and indexing might drastically change
// and there is no guarantee that elements we haven't seen
// yet will not be moved past the iterator. Therefore,
// we must start scanning from the beginning after each deletion :-(
trace_with_mask("TimerQueue::remove(eh_)",REACTTRACE);
register size_t i;
int cnt = 0;
bool f = true; // changed flag
register Timer* tmr;
DL((REACT,"Searching for Timer: 0x%x\n", dynamic_cast<void*> (eh_)));
while (f) {
f = false;
DL((REACT,"Queue size: %d\n", m_queue.size()));
for (i = 0; i < m_queue.size (); i++) {
if (m_queue[i]->getHandler() == eh_) {
DL((REACT,"Found Timer: 0x%x in slot: %d\n",
dynamic_cast<void*>(eh_), i));
tmr = m_queue[i];
m_queue.remove (tmr);
delete tmr;
cnt++;
f = true;
}
}
}
return cnt;
}

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.

{
trace_with_mask("TimerQueue::remove(tid)",REACTTRACE);
register size_t i;
DL((REACTTRACE,"Queue size before remove: %d\n", m_queue.size()));
for (i = 0; i < m_queue.size (); i++) {
if (m_queue[i] == (Timer*) tid_) {
Timer* tmr = m_queue[i];
int ret = m_queue.remove (tmr);
delete tmr;
DL((REACTTRACE,"Queue size after remove: %d\n", m_queue.size()));
return ret;
}
}
return false;
}

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.

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

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:

- TimerQueue.h
- TimerQueue.cpp