00001 /* -*- Mode:C++; c-basic-offset:8; tab-width:8; indent-tabs-mode:t -*- */ 00002 00003 /* 00004 * Copyright (C) 1997 by the University of Southern California 00005 * $Id: timer-handler.h,v 1.9 2005/08/25 18:58:02 johnh Exp $ 00006 * 00007 * This program is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU General Public License, 00009 * version 2, as published by the Free Software Foundation. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License along 00017 * with this program; if not, write to the Free Software Foundation, Inc., 00018 * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. 00019 * 00020 * 00021 * The copyright of this module includes the following 00022 * linking-with-specific-other-licenses addition: 00023 * 00024 * In addition, as a special exception, the copyright holders of 00025 * this module give you permission to combine (via static or 00026 * dynamic linking) this module with free software programs or 00027 * libraries that are released under the GNU LGPL and with code 00028 * included in the standard release of ns-2 under the Apache 2.0 00029 * license or under otherwise-compatible licenses with advertising 00030 * requirements (or modified versions of such code, with unchanged 00031 * license). You may copy and distribute such a system following the 00032 * terms of the GNU GPL for this module and the licenses of the 00033 * other code concerned, provided that you include the source code of 00034 * that other code when and as the GNU GPL requires distribution of 00035 * source code. 00036 * 00037 * Note that people who make modified versions of this module 00038 * are not obligated to grant this special exception for their 00039 * modified versions; it is their choice whether to do so. The GNU 00040 * General Public License gives permission to release a modified 00041 * version without this exception; this exception also makes it 00042 * possible to release a modified version which carries forward this 00043 * exception. 00044 * 00045 */ 00046 00047 /* 00048 * @(#) $Header: /cvsroot/nsnam/ns-2/common/timer-handler.h,v 1.9 2005/08/25 18:58:02 johnh Exp $ (USC/ISI) 00049 */ 00050 00051 #ifndef timer_handler_h 00052 #define timer_handler_h 00053 00054 #include "scheduler.h" 00055 00056 /* 00057 * Abstract base class to deal with timer-style handlers. 00058 * 00059 * 00060 * To define a new timer, subclass this function and define handle: 00061 * 00062 * class MyTimer : public TimerHandler { 00063 * public: 00064 * MyTimer(MyAgentClass *a) : AgentTimerHandler() { a_ = a; } 00065 * virtual double expire(Event *e); 00066 * protected: 00067 * MyAgentClass *a_; 00068 * }; 00069 * 00070 * Then define expire. 00071 * 00072 * Often MyTimer will be a friend of MyAgentClass, 00073 * or expire() will only call a function of MyAgentClass. 00074 * 00075 * See tcp-rbp.{cc,h} for a real example. 00076 */ 00077 #define TIMER_HANDLED -1.0 // xxx: should be const double in class? 00078 00079 class TimerHandler : public Handler { 00080 public: 00081 TimerHandler() : status_(TIMER_IDLE) { } 00082 00083 void sched(double delay); // cannot be pending 00084 void resched(double delay); // may or may not be pending 00085 // if you don't know the pending status 00086 // call resched() 00087 void cancel(); // must be pending 00088 inline void force_cancel() { // cancel! 00089 if (status_ == TIMER_PENDING) { 00090 _cancel(); 00091 status_ = TIMER_IDLE; 00092 } 00093 } 00094 enum TimerStatus { TIMER_IDLE, TIMER_PENDING, TIMER_HANDLING }; 00095 int status() { return status_; }; 00096 00097 protected: 00098 virtual void expire(Event *) = 0; // must be filled in by client 00099 // Should call resched() if it wants to reschedule the interface. 00100 00101 virtual void handle(Event *); 00102 int status_; 00103 Event event_; 00104 00105 private: 00106 inline void _sched(double delay) { 00107 (void)Scheduler::instance().schedule(this, &event_, delay); 00108 } 00109 inline void _cancel() { 00110 (void)Scheduler::instance().cancel(&event_); 00111 // no need to free event_ since it's statically allocated 00112 } 00113 }; 00114 00115 // Local Variables: 00116 // mode:c++ 00117 // End: 00118 00119 #endif /* timer_handler_h */