00001 // 00002 // timers.hh : Timer Management Include File 00003 // authors : John Heidemann, Fabio Silva and Alefiya Hussain 00004 // 00005 // Copyright (C) 2000-2002 by the University of Southern California 00006 // $Id: timers.hh,v 1.2 2002/11/26 22:45:40 haldar Exp $ 00007 // 00008 // This program is free software; you can redistribute it and/or 00009 // modify it under the terms of the GNU General Public License, 00010 // version 2, as published by the Free Software Foundation. 00011 // 00012 // This program is distributed in the hope that it will be useful, 00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 // GNU General Public License for more details. 00016 // 00017 // You should have received a copy of the GNU General Public License along 00018 // with this program; if not, write to the Free Software Foundation, Inc., 00019 // 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. 00020 // 00021 // 00022 00023 #ifndef _TIMERS_H_ 00024 #define _TIMERS_H_ 00025 00026 #ifdef HAVE_CONFIG_H 00027 #include "config.h" 00028 #endif // HAVE_CONFIG_H 00029 00030 #ifdef USE_THREADS 00031 #include <pthread.h> 00032 #endif // USE_THREADS 00033 00034 #include <string.h> 00035 00036 #include "events.hh" 00037 00038 #ifdef NS_DIFFUSION 00039 class TimerManager; 00040 class DiffEvent; 00041 class DiffEventHandler; 00042 #include "difftimer.h" 00043 #endif // NS_DIFFUSION 00044 00045 // 00046 // To make a new timer, subclass TimerCallback and override the 00047 // expire() method. 00048 // 00049 // If you need to pass parameters to your timer, pass them in the 00050 // constructor of your subclass. 00051 // 00052 // If you allocate data in your callback, free it in the destructor. 00053 // 00054 // When the timer fires, expire() will be called. You can do anything 00055 // you want there. When you're done, return a value that indicates 00056 // what happens to the timer: 00057 // 00058 // return = 0 re-add timer again to queue with same timeout as last time 00059 // > 0 re-add timer to queue with new timeout given by return value 00060 // < 0 discard timer (do not re-add it to the queue) 00061 // 00062 00063 class TimerCallback { 00064 public: 00065 TimerCallback() {}; 00066 virtual ~TimerCallback() {}; 00067 virtual int expire() = 0; 00068 }; 00069 00070 // This class is used to define a timer in the event queue. The 00071 // timeout provided should be in milliseconds. cb specifies the 00072 // function that will be called. 00073 00074 class TimerEntry { 00075 public: 00076 handle hdl_; 00077 int timeout_; 00078 TimerCallback *cb_; 00079 00080 TimerEntry(handle hdl, int timeout,TimerCallback *cb) : 00081 hdl_(hdl), timeout_(timeout), cb_(cb) {}; 00082 ~TimerEntry() {}; 00083 }; 00084 00085 // Creates the Timer Management class. Creates the eventqueue 00086 // The eventqueue is used by the Timer class only. 00087 // Use the NextTimerTime and ExecuteNextTimer functions to access 00088 // the event queue 00089 00090 class TimerManager { 00091 public: 00092 TimerManager(); 00093 ~TimerManager() {}; 00094 00095 // Timer API functions: 00096 00097 // add a timer to the queue, returning the handle that can be used 00098 // to cancel it with removeTimer timeout value deifne in ms. When 00099 // the timer fires, expire() will be called. You can do anything 00100 // you want there. When you're done, return a value that indicates 00101 // what happens to the timer: 00102 // 00103 // return = 0 re-add timer again to queue with same timeout as last time 00104 // > 0 re-add timer to queue with new timeout given by return value 00105 // < 0 discard timer (do not re-add it to the queue) 00106 00107 handle addTimer(int timeout, TimerCallback *cb); 00108 00109 // remove a timer that's scheduled, returns if it was there. 00110 bool removeTimer(handle hdl); 00111 00112 // returns the timer value at head of the queue 00113 void nextTimerTime(struct timeval *tv); 00114 00115 // Executes the timer on the head of the queue 00116 #ifdef NS_DIFFUSION 00117 void diffTimeout(DiffEvent *e); 00118 #else 00119 void executeNextTimer(); 00120 void executeAllExpiredTimers(); 00121 #endif // NS_DIFFUSION 00122 00123 protected: 00124 int next_handle_; // counter of handle ids 00125 EventQueue *eq_; // internal list of timers 00126 #ifdef USE_THREADS 00127 pthread_mutex_t *queue_mtx_; 00128 #endif // USE_THREADS 00129 }; 00130 00131 #endif // _TIMERS_H_ 00132
1.3.3