• Main Page
  • Classes
  • Files
  • File List

/Users/yzchen/ns/ns-allinone-2.33/ns-2.33/diffusion3/lib/main/timers.hh

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.4 2005/09/13 04:53:50 tomh 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 // Linking this file statically or dynamically with other modules is making
00022 // a combined work based on this file.  Thus, the terms and conditions of
00023 // the GNU General Public License cover the whole combination.
00024 //
00025 // In addition, as a special exception, the copyright holders of this file
00026 // give you permission to combine this file with free software programs or
00027 // libraries that are released under the GNU LGPL and with code included in
00028 // the standard release of ns-2 under the Apache 2.0 license or under
00029 // otherwise-compatible licenses with advertising requirements (or modified
00030 // versions of such code, with unchanged license).  You may copy and
00031 // distribute such a system following the terms of the GNU GPL for this
00032 // file and the licenses of the other code concerned, provided that you
00033 // include the source code of that other code when and as the GNU GPL
00034 // requires distribution of source code.
00035 //
00036 // Note that people who make modified versions of this file are not
00037 // obligated to grant this special exception for their modified versions;
00038 // it is their choice whether to do so.  The GNU General Public License
00039 // gives permission to release a modified version without this exception;
00040 // this exception also makes it possible to release a modified version
00041 // which carries forward this exception.
00042 
00043 #ifndef _TIMERS_H_
00044 #define _TIMERS_H_
00045 
00046 #ifdef HAVE_CONFIG_H
00047 #include "config.h"
00048 #endif // HAVE_CONFIG_H
00049 
00050 #ifdef USE_THREADS
00051 #include <pthread.h>
00052 #endif // USE_THREADS
00053 
00054 #include <string.h>
00055 
00056 #include "events.hh"
00057 
00058 #ifdef NS_DIFFUSION
00059 class TimerManager;
00060 class DiffEvent;
00061 class DiffEventHandler;
00062 #include "difftimer.h"
00063 #endif // NS_DIFFUSION
00064 
00065 //
00066 // To make a new timer, subclass TimerCallback and override the
00067 // expire() method.
00068 //
00069 // If you need to pass parameters to your timer, pass them in the
00070 // constructor of your subclass.
00071 //
00072 // If you allocate data in your callback, free it in the destructor.
00073 //
00074 // When the timer fires, expire() will be called.  You can do anything
00075 // you want there.  When you're done, return a value that indicates
00076 // what happens to the timer:
00077 //
00078 // return = 0   re-add timer again to queue with same timeout as last time
00079 //        > 0   re-add timer to queue with new timeout given by return value
00080 //        < 0   discard timer (do not re-add it to the queue)
00081 //
00082 
00083 class TimerCallback {
00084 public:
00085   TimerCallback() {};
00086   virtual ~TimerCallback() {};
00087   virtual int expire() = 0;
00088 };
00089 
00090 // This class is used to define a timer in the event queue. The
00091 // timeout provided should be in milliseconds. cb specifies the
00092 // function that will be called.
00093 
00094 class TimerEntry {
00095 public:
00096   handle hdl_;
00097   int timeout_;
00098   TimerCallback *cb_;
00099 
00100   TimerEntry(handle hdl, int timeout,TimerCallback *cb) : 
00101     hdl_(hdl), timeout_(timeout), cb_(cb) {};
00102   ~TimerEntry() {};
00103 };
00104 
00105 // Creates the Timer Management class. Creates the eventqueue
00106 // The eventqueue is used by the Timer class only. 
00107 // Use the NextTimerTime and ExecuteNextTimer functions to access
00108 // the event queue
00109 
00110 class TimerManager {
00111 public:
00112   TimerManager();
00113   ~TimerManager() {};
00114 
00115   // Timer API functions:
00116         
00117   // add a timer to the queue, returning the handle that can be used
00118   // to cancel it with removeTimer timeout value deifne in ms.  When
00119   // the timer fires, expire() will be called.  You can do anything
00120   // you want there.  When you're done, return a value that indicates
00121   // what happens to the timer:
00122   //
00123   // return = 0   re-add timer again to queue with same timeout as last time
00124   //        > 0   re-add timer to queue with new timeout given by return value
00125   //        < 0   discard timer (do not re-add it to the queue)
00126 
00127   handle addTimer(int timeout, TimerCallback *cb);
00128 
00129   // remove a timer that's scheduled, returns if it was there.
00130   bool removeTimer(handle hdl);
00131 
00132   // returns the timer value at head of the queue
00133   void nextTimerTime(struct timeval *tv);
00134 
00135   // Executes the timer on the head of the queue
00136 #ifdef NS_DIFFUSION
00137   void diffTimeout(DiffEvent *e);
00138 #else
00139   void executeNextTimer();
00140   void executeAllExpiredTimers();
00141 #endif // NS_DIFFUSION
00142 
00143 protected:
00144   int next_handle_; // counter of handle ids
00145   EventQueue *eq_;  // internal list of timers
00146 #ifdef USE_THREADS
00147   pthread_mutex_t *queue_mtx_;
00148 #endif // USE_THREADS
00149 };
00150 
00151 #endif // _TIMERS_H_
00152 

Generated on Tue Aug 10 2010 16:16:06 for ns-2.33 by  doxygen 1.7.1