libassa  3.5.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Public Member Functions | Private Attributes
ASSA::TimerQueue Class Reference

#include <TimerQueue.h>

List of all members.

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

ASSA::TimerQueue::TimerQueue ( )
inline

Constructor.

Definition at line 103 of file TimerQueue.h.

References trace.

{
trace("TimerQueue::TimerQueue");
}
TimerQueue::~TimerQueue ( )

Destructor.

Definition at line 20 of file TimerQueue.cpp.

References m_queue, ASSA::PriorityQueue< T, Compare >::pop(), ASSA::REACTTRACE, ASSA::PriorityQueue< T, Compare >::size(), and trace_with_mask.

{
trace_with_mask("TimerQueue::~TimerQueue",REACTTRACE);
while (m_queue.size ()) {
delete m_queue.pop ();
}
}

Member Function Documentation

void TimerQueue::dump ( void  )

Dump Queue information to the log file.

Definition at line 152 of file TimerQueue.cpp.

References DL, m_queue, ASSA::REACT, ASSA::PriorityQueue< T, Compare >::size(), and trace.

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

{
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();
}
}
}
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.

   @param tv_ Expiration time
   @return 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.

References DL, ASSA::Timer::dump(), ASSA::TimeVal::fmtString(), ASSA::Timer::get_id(), ASSA::Timer::getExpirationTime(), ASSA::Timer::getHandler(), ASSA::EventHandler::handle_timeout(), ASSA::PriorityQueue< T, Compare >::insert(), m_queue, ASSA::PriorityQueue< T, Compare >::pop(), ASSA::REACT, ASSA::REACTTRACE, ASSA::PriorityQueue< T, Compare >::size(), ASSA::PriorityQueue< T, Compare >::top(), ASSA::TRACE, and trace_with_mask.

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

{
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!).
*/
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 ();
}
else {
delete tp;
tp = (Timer*)NULL;
}
cnt++;
}
if (cnt) {
DL((TRACE,"Expired total of %d timer(s).\n",cnt));
}
return cnt;
}
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.

References ASSA::PriorityQueue< T, Compare >::insert(), m_queue, and trace.

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

{
trace("TimerQueue::insert");
Timer* t = new Timer (eh_, tv_, delta_, name_);
return (TimerId) t;
}
bool ASSA::TimerQueue::isEmpty ( )
inline

Is queue empty?

Returns:
true if queue empty; false if not.

Definition at line 110 of file TimerQueue.h.

References m_queue, and ASSA::PriorityQueue< T, Compare >::size().

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

{
return m_queue.size () == 0;
}
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.

References DL, m_queue, ASSA::REACT, ASSA::REACTTRACE, ASSA::PriorityQueue< T, Compare >::remove(), ASSA::PriorityQueue< T, Compare >::size(), and trace_with_mask.

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

{
// 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;
}
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.

References DL, m_queue, ASSA::REACTTRACE, ASSA::PriorityQueue< T, Compare >::remove(), ASSA::PriorityQueue< T, Compare >::size(), and trace_with_mask.

{
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;
}
TimeVal & ASSA::TimerQueue::top ( void  )
inline

Return expiration time of the top element in the queue.

Definition at line 117 of file TimerQueue.h.

References ASSA::Timer::getExpirationTime(), m_queue, and ASSA::PriorityQueue< T, Compare >::top().

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

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

Member Data Documentation

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: