00001 // 00002 // events.hh : Implements a queue of timer-based events 00003 // Authors : Lewis Girod, John Heidemann and Fabio Silva 00004 // 00005 // Copyright (C) 2000-2002 by the University of Southern California 00006 // $Id: events.hh,v 1.8 2005/09/13 04:53:49 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 00044 // This file defines lower level events (which contain a type, a 00045 // payload and an expiration time) and an event queue, kept in sorted 00046 // order by expiration time. This is used by both the Diffusion 00047 // Routing Library API and the Diffusion Core module for keeping track 00048 // of events. Application (or Filter) developers should use the Timer 00049 // API instead. 00050 00051 #ifndef _EVENT_H_ 00052 #define _EVENT_H_ 00053 00054 #ifdef HAVE_CONFIG_H 00055 #include "config.h" 00056 #endif // HAVE_CONFIG_H 00057 00058 #include "tools.hh" 00059 00060 typedef long handle; 00061 #define MAXVALUE 0x7ffffff // No events in the queue 00062 00063 class QueueEvent { 00064 public: 00065 struct timeval tv_; 00066 handle handle_; 00067 void *payload_; 00068 00069 QueueEvent *next_; 00070 }; 00071 00072 class EventQueue { 00073 00074 // 00075 // Methods 00076 // 00077 // eqAdd inserts an event into the queue 00078 // eqAddAfter creates a new event and inserts it 00079 // eqPop extracts the first event and returns it 00080 // eqNextTimer returns the time of expiration for the next event 00081 // eqTopInPast returns true if the head of the timer queue is in the past 00082 // eqRemoveEvent removes an event from the queue 00083 // eqRemove removes the event with the given handle from the queue 00084 // 00085 00086 public: 00087 EventQueue(){ 00088 // Empty 00089 head_ = NULL; 00090 }; 00091 virtual ~EventQueue(){ 00092 // Empty 00093 }; 00094 00095 virtual void eqAddAfter(handle hdl, void *payload, int delay_msec); 00096 QueueEvent * eqPop(); 00097 QueueEvent * eqFindEvent(handle hdl); 00098 void eqNextTimer(struct timeval *tv); 00099 virtual bool eqRemove(handle hdl); 00100 00101 private: 00102 00103 int eqTopInPast(); 00104 void eqPrint(); 00105 bool eqRemoveEvent(QueueEvent *e); 00106 QueueEvent * eqFindNextEvent(handle hdl, QueueEvent *e); 00107 void eqAdd(QueueEvent *e); 00108 00109 // 00110 // QueueEvent methods 00111 // 00112 // setDelay sets the expiration time to a delay after present time 00113 // randDelay computes a delay given a vector {base delay, variance} 00114 // delay times are chosen uniformly within the variance. 00115 // eventCmp compares the expiration times from two events and returns 00116 // -1, 0 1 for <, == and > 00117 // 00118 00119 void setDelay(QueueEvent *e, int delay_msec); 00120 int randDelay(int timer[2]); 00121 int eventCmp(QueueEvent *x, QueueEvent *y); 00122 00123 QueueEvent *head_; 00124 00125 }; 00126 00127 // Extra utility functions for managing struct timeval 00128 00129 // Compares two timeval structures, returning -1, 0 or 1 for <, == or > 00130 int TimevalCmp(struct timeval *x, struct timeval *y); 00131 00132 // tv will be x - y (or 0,0 if x < y) 00133 void TimevalSub(struct timeval *x, struct timeval *y, struct timeval *tv); 00134 00135 // Add usecs to tv 00136 void TimevalAddusecs(struct timeval *tv, int usecs); 00137 00138 #ifdef NS_DIFFUSION 00139 #ifdef RAND_MAX 00140 #undef RAND_MAX 00141 #endif // RAND_MAX 00142 #define RAND_MAX 2147483647 00143 #else 00144 #ifndef RAND_MAX 00145 #define RAND_MAX 2147483647 00146 #endif // !RAND_MAX 00147 #endif // NS_DIFFUSION 00148 00149 #endif // !_EVENT_H_