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.6 2002/11/26 22:45:39 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 // This file defines lower level events (which contain a type, a 00024 // payload and an expiration time) and an event queue, kept in sorted 00025 // order by expiration time. This is used by both the Diffusion 00026 // Routing Library API and the Diffusion Core module for keeping track 00027 // of events. Application (or Filter) developers should use the Timer 00028 // API instead. 00029 00030 #ifndef _EVENT_H_ 00031 #define _EVENT_H_ 00032 00033 #ifdef HAVE_CONFIG_H 00034 #include "config.h" 00035 #endif // HAVE_CONFIG_H 00036 00037 #include "tools.hh" 00038 00039 typedef long handle; 00040 #define MAXVALUE 0x7ffffff // No events in the queue 00041 00042 class QueueEvent { 00043 public: 00044 struct timeval tv_; 00045 handle handle_; 00046 void *payload_; 00047 00048 QueueEvent *next_; 00049 }; 00050 00051 class EventQueue { 00052 00053 // 00054 // Methods 00055 // 00056 // eqAdd inserts an event into the queue 00057 // eqAddAfter creates a new event and inserts it 00058 // eqPop extracts the first event and returns it 00059 // eqNextTimer returns the time of expiration for the next event 00060 // eqTopInPast returns true if the head of the timer queue is in the past 00061 // eqRemoveEvent removes an event from the queue 00062 // eqRemove removes the event with the given handle from the queue 00063 // 00064 00065 public: 00066 EventQueue(){ 00067 // Empty 00068 head_ = NULL; 00069 }; 00070 virtual ~EventQueue(){ 00071 // Empty 00072 }; 00073 00074 virtual void eqAddAfter(handle hdl, void *payload, int delay_msec); 00075 QueueEvent * eqPop(); 00076 QueueEvent * eqFindEvent(handle hdl); 00077 void eqNextTimer(struct timeval *tv); 00078 virtual bool eqRemove(handle hdl); 00079 00080 private: 00081 00082 int eqTopInPast(); 00083 void eqPrint(); 00084 bool eqRemoveEvent(QueueEvent *e); 00085 QueueEvent * eqFindNextEvent(handle hdl, QueueEvent *e); 00086 void eqAdd(QueueEvent *e); 00087 00088 // 00089 // QueueEvent methods 00090 // 00091 // setDelay sets the expiration time to a delay after present time 00092 // randDelay computes a delay given a vector {base delay, variance} 00093 // delay times are chosen uniformly within the variance. 00094 // eventCmp compares the expiration times from two events and returns 00095 // -1, 0 1 for <, == and > 00096 // 00097 00098 void setDelay(QueueEvent *e, int delay_msec); 00099 int randDelay(int timer[2]); 00100 int eventCmp(QueueEvent *x, QueueEvent *y); 00101 00102 QueueEvent *head_; 00103 00104 }; 00105 00106 // Extra utility functions for managing struct timeval 00107 00108 // Compares two timeval structures, returning -1, 0 or 1 for <, == or > 00109 int TimevalCmp(struct timeval *x, struct timeval *y); 00110 00111 // tv will be x - y (or 0,0 if x < y) 00112 void TimevalSub(struct timeval *x, struct timeval *y, struct timeval *tv); 00113 00114 // Add usecs to tv 00115 void TimevalAddusecs(struct timeval *tv, int usecs); 00116 00117 #ifdef NS_DIFFUSION 00118 #ifdef RAND_MAX 00119 #undef RAND_MAX 00120 #endif // RAND_MAX 00121 #define RAND_MAX 2147483647 00122 #else 00123 #ifndef RAND_MAX 00124 #define RAND_MAX 2147483647 00125 #endif // !RAND_MAX 00126 #endif // NS_DIFFUSION 00127 00128 #endif // !_EVENT_H_
1.3.3