Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Compound Members | File Members

TimerManager Class Reference

#include <timers.hh>

Collaboration diagram for TimerManager:

Collaboration graph
[legend]
List of all members.

Public Member Functions

 TimerManager ()
 ~TimerManager ()
handle addTimer (int timeout, TimerCallback *cb)
bool removeTimer (handle hdl)
void nextTimerTime (struct timeval *tv)
void executeNextTimer ()
void executeAllExpiredTimers ()

Protected Attributes

int next_handle_
EventQueueeq_

Constructor & Destructor Documentation

TimerManager::TimerManager  ) 
 

Definition at line 32 of file timers.cc.

References eq_, GetTime(), next_handle_, and SetSeed().

00033 {
00034   struct timeval tv;
00035 
00036   // Initialize basic stuff
00037   next_handle_ = 1;
00038   GetTime(&tv);
00039   SetSeed(&tv);
00040 
00041   // Initialize event queue
00042 #ifdef NS_DIFFUSION
00043   eq_ = new DiffEventQueue(this);
00044 #else
00045   eq_ = new EventQueue;
00046 #endif // NS_DIFFUSION
00047 
00048 #ifdef USE_THREADS
00049   queue_mtx_ = new pthread_mutex_t;
00050   pthread_mutex_init(queue_mtx_, NULL);
00051 #endif // USE_THREADS
00052 }

Here is the call graph for this function:

TimerManager::~TimerManager  )  [inline]
 

Definition at line 93 of file timers.hh.

00093 {};


Member Function Documentation

handle TimerManager::addTimer int  timeout,
TimerCallback cb
 

Definition at line 58 of file timers.cc.

References eq_, EventQueue::eqAddAfter(), handle, TimerEntry::hdl_, and next_handle_.

Referenced by DiffusionRouting::addFilter(), DiffusionRouting::addTimer(), DiffusionCoreAgent::DiffusionCoreAgent(), and DiffusionRouting::subscribe().

00059 {
00060   TimerEntry *entry;
00061 
00062 #ifdef USE_THREADS
00063   pthread_mutex_lock(queue_mtx_);
00064 #endif // USE_THREADS
00065   entry = new TimerEntry(next_handle_, timeout, cb);
00066   eq_->eqAddAfter(next_handle_, entry, timeout);
00067   next_handle_++;
00068 
00069 #ifdef USE_THREADS
00070   pthread_mutex_unlock(queue_mtx_);
00071 #endif // USE_THREADS
00072   return entry->hdl_;
00073 }

Here is the call graph for this function:

void TimerManager::executeAllExpiredTimers  ) 
 

Definition at line 192 of file timers.cc.

References executeNextTimer(), and nextTimerTime().

Referenced by DiffusionRouting::run(), and DiffusionCoreAgent::run().

00193 {
00194   struct timeval tv;
00195 
00196   // Get next timer's timeout
00197   nextTimerTime(&tv);
00198 
00199   // Remove all expired timers from the top of the queue, calling
00200   // expire() for each one of them
00201   while (tv.tv_sec == 0 && tv.tv_usec == 0){
00202     // Timer at the head of the queue has expired
00203     executeNextTimer();
00204     nextTimerTime(&tv);
00205   }
00206 }

Here is the call graph for this function:

void TimerManager::executeNextTimer  ) 
 

Definition at line 159 of file timers.cc.

References TimerEntry::cb_, eq_, EventQueue::eqAddAfter(), EventQueue::eqPop(), TimerCallback::expire(), TimerEntry::hdl_, QueueEvent::payload_, and TimerEntry::timeout_.

Referenced by executeAllExpiredTimers().

00160 {
00161 #ifdef USE_THREADS
00162   pthread_mutex_lock(queue_mtx_);
00163 #endif // USE_THREADS
00164   QueueEvent *e = eq_->eqPop();
00165   TimerEntry *entry = (TimerEntry *) e->payload_;
00166 
00167 #ifdef USE_THREADS
00168   pthread_mutex_unlock(queue_mtx_);
00169 #endif // USE_THREADS
00170   // run it
00171   int new_timeout = entry->cb_->expire();
00172 
00173   if (new_timeout >= 0){
00174     if (new_timeout > 0){
00175       // Change the timer's timeout
00176       entry->timeout_ = new_timeout;
00177     }
00178 #ifdef USE_THREADS
00179     pthread_mutex_lock(queue_mtx_);
00180 #endif // USE_THREADS
00181     eq_->eqAddAfter(entry->hdl_, (TimerEntry *) entry, entry->timeout_);
00182 #ifdef USE_THREADS
00183     pthread_mutex_unlock(queue_mtx_);
00184 #endif // USE_THREADS
00185   }
00186   else{
00187     delete entry;
00188   }
00189   delete e;
00190 }

Here is the call graph for this function:

void TimerManager::nextTimerTime struct timeval *  tv  ) 
 

Definition at line 126 of file timers.cc.

References eq_, and EventQueue::eqNextTimer().

Referenced by executeAllExpiredTimers(), DiffusionRouting::run(), and DiffusionCoreAgent::run().

00127 {
00128 #ifdef USE_THREADS
00129   pthread_mutex_lock(queue_mtx_);
00130 #endif // USE_THREADS
00131   eq_->eqNextTimer(tv);
00132 #ifdef USE_THREADS
00133   pthread_mutex_unlock(queue_mtx_);
00134 #endif // USE_THREADS
00135 }

Here is the call graph for this function:

bool TimerManager::removeTimer handle  hdl  ) 
 

Definition at line 79 of file timers.cc.

References eq_, EventQueue::eqFindEvent(), EventQueue::eqRemove(), and QueueEvent::payload_.

Referenced by DiffusionRouting::removeTimer().

00080 {
00081 #ifdef NS_DIFFUSION
00082   if (eq_->eqRemove(hdl) == false){
00083     fprintf(stderr, "Error: Can't remove event from queue !\n");
00084     return false;
00085   }
00086   return true;
00087 #else
00088   QueueEvent *e = NULL;
00089   TimerEntry *entry;
00090 
00091 #ifdef USE_THREADS
00092   pthread_mutex_lock(queue_mtx_);
00093 #endif // USE_THREADS
00094 
00095   // Find the timer in the queue
00096   e = eq_->eqFindEvent(hdl);
00097 
00098   // If timer found, remove it from the queue
00099   if (e){
00100     entry = (TimerEntry *) e->payload_;
00101     if (eq_->eqRemove(hdl) == false){
00102       fprintf(stderr, "Error: Can't remove event from queue !\n");
00103       exit(-1);
00104     }
00105 
00106     // Call the application provided delete function
00107 
00108     delete entry;
00109     delete e;
00110   }
00111   else{
00112 #ifdef USE_THREADS
00113     pthread_mutex_unlock(queue_mtx_);
00114 #endif // USE_THREADS
00115     return false;
00116   }
00117 
00118 #ifdef USE_THREADS
00119   pthread_mutex_unlock(queue_mtx_);
00120 #endif // USE_THREADS
00121   return true;
00122 #endif // NS_DIFFUSION
00123 }

Here is the call graph for this function:


Member Data Documentation

EventQueue* TimerManager::eq_ [protected]
 

Definition at line 125 of file timers.hh.

Referenced by addTimer(), executeNextTimer(), nextTimerTime(), removeTimer(), and TimerManager().

int TimerManager::next_handle_ [protected]
 

Definition at line 124 of file timers.hh.

Referenced by addTimer(), and TimerManager().


The documentation for this class was generated from the following files:
Generated on Tue Apr 20 13:32:53 2004 for NS2.26SourcesOriginal by doxygen 1.3.3