00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef _DELAY_QUEUE_HH
00021 #define _DELAY_QUEUE_HH
00022
00023 #ifndef _NET_COMMON_H
00024 #include "NetCommon.h"
00025 #endif
00026
00027 #ifdef TIME_BASE
00028 typedef TIME_BASE time_base_seconds;
00029 #else
00030 typedef long time_base_seconds;
00031 #endif
00032
00034
00035 class Timeval {
00036 public:
00037 time_base_seconds seconds() const {
00038 return fTv.tv_sec;
00039 }
00040 time_base_seconds seconds() {
00041 return fTv.tv_sec;
00042 }
00043 time_base_seconds useconds() const {
00044 return fTv.tv_usec;
00045 }
00046 time_base_seconds useconds() {
00047 return fTv.tv_usec;
00048 }
00049
00050 int operator>=(Timeval const& arg2) const;
00051 int operator<=(Timeval const& arg2) const {
00052 return arg2 >= *this;
00053 }
00054 int operator<(Timeval const& arg2) const {
00055 return !(*this >= arg2);
00056 }
00057 int operator>(Timeval const& arg2) const {
00058 return arg2 < *this;
00059 }
00060 int operator==(Timeval const& arg2) const {
00061 return *this >= arg2 && arg2 >= *this;
00062 }
00063 int operator!=(Timeval const& arg2) const {
00064 return !(*this == arg2);
00065 }
00066
00067 void operator+=(class DelayInterval const& arg2);
00068 void operator-=(class DelayInterval const& arg2);
00069
00070
00071 protected:
00072 Timeval(time_base_seconds seconds, time_base_seconds useconds) {
00073 fTv.tv_sec = seconds; fTv.tv_usec = useconds;
00074 }
00075
00076 private:
00077 time_base_seconds& secs() {
00078 return (time_base_seconds&)fTv.tv_sec;
00079 }
00080 time_base_seconds& usecs() {
00081 return (time_base_seconds&)fTv.tv_usec;
00082 }
00083
00084 struct timeval fTv;
00085 };
00086
00087 #ifndef max
00088 inline Timeval max(Timeval const& arg1, Timeval const& arg2) {
00089 return arg1 >= arg2 ? arg1 : arg2;
00090 }
00091 #endif
00092 #ifndef min
00093 inline Timeval min(Timeval const& arg1, Timeval const& arg2) {
00094 return arg1 <= arg2 ? arg1 : arg2;
00095 }
00096 #endif
00097
00098 class DelayInterval operator-(Timeval const& arg1, Timeval const& arg2);
00099
00100
00101
00103
00104 class DelayInterval: public Timeval {
00105 public:
00106 DelayInterval(time_base_seconds seconds, time_base_seconds useconds)
00107 : Timeval(seconds, useconds) {}
00108 };
00109
00110 DelayInterval operator*(short arg1, DelayInterval const& arg2);
00111
00112 extern DelayInterval const DELAY_ZERO;
00113 extern DelayInterval const DELAY_SECOND;
00114 DelayInterval const DELAY_MINUTE = 60*DELAY_SECOND;
00115 DelayInterval const DELAY_HOUR = 60*DELAY_MINUTE;
00116 DelayInterval const DELAY_DAY = 24*DELAY_HOUR;
00117
00119
00120 class EventTime: public Timeval {
00121 public:
00122 EventTime(unsigned secondsSinceEpoch = 0,
00123 unsigned usecondsSinceEpoch = 0)
00124
00125 : Timeval(secondsSinceEpoch, usecondsSinceEpoch) {}
00126 };
00127
00128 EventTime TimeNow();
00129
00130 DelayInterval TimeRemainingUntil(EventTime const& futureEvent);
00131
00132
00133 extern EventTime const THE_END_OF_TIME;
00134
00135
00137
00138 class DelayQueueEntry {
00139 public:
00140 virtual ~DelayQueueEntry();
00141
00142 long token() {
00143 return fToken;
00144 }
00145
00146 protected:
00147 DelayQueueEntry(DelayInterval delay);
00148
00149 virtual void handleTimeout();
00150
00151 private:
00152 friend class DelayQueue;
00153 DelayQueueEntry* fNext;
00154 DelayQueueEntry* fPrev;
00155 DelayInterval fDeltaTimeRemaining;
00156
00157 long fToken;
00158 static long tokenCounter;
00159 };
00160
00162
00163 class DelayQueue: public DelayQueueEntry {
00164 public:
00165 DelayQueue();
00166 virtual ~DelayQueue();
00167
00168 void addEntry(DelayQueueEntry* newEntry);
00169 void updateEntry(DelayQueueEntry* entry, DelayInterval newDelay);
00170 void updateEntry(long tokenToFind, DelayInterval newDelay);
00171 void removeEntry(DelayQueueEntry* entry);
00172 DelayQueueEntry* removeEntry(long tokenToFind);
00173
00174 DelayInterval const& timeToNextAlarm();
00175 void handleAlarm();
00176
00177 private:
00178 DelayQueueEntry* head() { return fNext; }
00179 DelayQueueEntry* findEntryByToken(long token);
00180 void synchronize();
00181
00182 EventTime fLastSyncTime;
00183 };
00184
00185 #endif