ASSA::TimerQueue Class Reference

#include <TimerQueue.h>

Collaboration diagram for ASSA::TimerQueue:

Collaboration graph
[legend]
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.
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

ASSA::TimerQueue::TimerQueue  )  [inline]
 

Constructor.

Definition at line 103 of file TimerQueue.h.

References trace.

00104 {
00105     trace("TimerQueue::TimerQueue");
00106 }

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.

00021 { 
00022     trace_with_mask("TimerQueue::~TimerQueue",REACTTRACE);
00023 
00024     while (m_queue.size ()) {
00025         delete m_queue.pop ();
00026     }
00027 }


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::registerTimerHandler(), and ASSA::Reactor::removeTimerHandler().

00153 {
00154     trace("TimerQueue::dump");
00155 
00156     if (m_queue.size() == 0) {
00157         DL((REACT,"Queue is empty\n"));
00158     }
00159     else {
00160         for (size_t i = 0; i < m_queue.size (); ) {
00161             m_queue[i++]->dump();
00162         }
00163     }
00164 }

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

Definition at line 89 of file TimerQueue.cpp.

References DL, ASSA::Timer::dump(), ASSA::Timer::getExpirationTime(), m_queue, ASSA::REACT, ASSA::REACTTRACE, ASSA::PriorityQueue< T, Compare >::size(), ASSA::PriorityQueue< T, Compare >::top(), and trace_with_mask.

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

00090 { 
00091     trace_with_mask("TimerQueue::expire",REACTTRACE);
00092 
00093     register Timer* tp = (Timer*) NULL;
00094     register int cnt = 0;
00095 
00096     while (m_queue.size () && (tp = m_queue.top ()) != (Timer*) NULL) {
00097         if (tp->getExpirationTime () > tv_) {
00098             DL((REACT,"Top timer:\n"));
00099             tp->dump ();
00100             break;
00101         }
00102         /* First, pop item from the queue. Then call an appropriate
00103            EventHandler. If done in reverse, EventHandler might
00104            remove item first and then pop () will fail
00105            (This needs more investigation!).
00106         */
00107         m_queue.pop ();
00108 
00109         DL((REACT,"Expired %s [t=%s] timer!\n",
00110             tp->get_id ().c_str (),
00111             tp->getExpirationTime ().fmtString ().c_str ()));
00112 
00113         int ret = tp->getHandler ()->handle_timeout ((TimerId) tp);
00114 
00118         if (ret == 1) {         
00119             tp->rescheduleExpirationTime ();
00120             m_queue.insert (tp);
00121         }
00122         else {
00123             delete tp;
00124             tp = (Timer*)NULL;
00125         }
00126         cnt++;
00127     }
00128 
00129     if (cnt) {
00130         DL((TRACE,"Expired total of %d timer(s).\n",cnt));
00131     }
00132 
00133     return cnt;
00134 }

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().

00142 { 
00143     trace("TimerQueue::insert");
00144 
00145     Timer* t = new Timer (eh_, tv_, delta_, name_);
00146     m_queue.insert (t);
00147     return (TimerId) t;
00148 }

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().

00111 { 
00112     return m_queue.size () == 0;
00113 }

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.

00069 { 
00070     trace_with_mask("TimerQueue::remove(tid)",REACTTRACE);
00071     register size_t i;
00072 
00073     DL((REACTTRACE,"Queue size before remove: %d\n", m_queue.size()));
00074 
00075     for (i = 0; i < m_queue.size (); i++) {
00076         if (m_queue[i] == (Timer*) tid_) {
00077             Timer* tmr = m_queue[i];
00078             int ret = m_queue.remove (tmr);
00079             delete tmr;
00080             DL((REACTTRACE,"Queue size after remove: %d\n", m_queue.size()));
00081             return ret;
00082         }
00083     }
00084     return false;
00085 }

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().

00032 { 
00033     // Like STL iterators, after deletion of an element,
00034     // queue structure and indexing might drastically change
00035     // and there is no guarantee that elements we haven't seen
00036     // yet will not be moved past the iterator. Therefore,
00037     // we must start scanning from the beginning after each deletion :-(
00038 
00039     trace_with_mask("TimerQueue::remove(eh_)",REACTTRACE);
00040 
00041     register size_t i;
00042     int cnt = 0;
00043     bool f = true;      // changed flag
00044     register Timer* tmr;
00045 
00046     DL((REACT,"Searching for Timer: 0x%x\n", dynamic_cast<void*> (eh_)));
00047 
00048     while (f) {
00049         f = false;
00050         DL((REACT,"Queue size: %d\n", m_queue.size()));
00051         for (i = 0; i < m_queue.size (); i++) {
00052             if (m_queue[i]->getHandler() == eh_) {
00053                 DL((REACT,"Found Timer: 0x%x in slot: %d\n", 
00054                     dynamic_cast<void*>(eh_), i));
00055                 tmr = m_queue[i];
00056                 m_queue.remove (tmr);
00057                 delete tmr;
00058                 cnt++;
00059                 f = true;
00060             }
00061         }
00062     }
00063     return cnt;
00064 }

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().

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


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:
Generated on Sun Aug 13 15:08:22 2006 for libassa by  doxygen 1.4.6