BasicUsageEnvironment/include/DelayQueue.hh

Go to the documentation of this file.
00001 /**********
00002 This library is free software; you can redistribute it and/or modify it under
00003 the terms of the GNU Lesser General Public License as published by the
00004 Free Software Foundation; either version 2.1 of the License, or (at your
00005 option) any later version. (See <http://www.gnu.org/copyleft/lesser.html>.)
00006 
00007 This library is distributed in the hope that it will be useful, but WITHOUT
00008 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00009 FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
00010 more details.
00011 
00012 You should have received a copy of the GNU Lesser General Public License
00013 along with this library; if not, write to the Free Software Foundation, Inc.,
00014 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
00015 **********/
00016  // Copyright (c) 1996-2008, Live Networks, Inc.  All rights reserved
00017 // Delay queue
00018 // C++ header
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   // returns ZERO iff arg2 >= arg1
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 // returns ZERO iff arg2 >= arg1
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     // We use the Unix standard epoch: January 1, 1970
00125     : Timeval(secondsSinceEpoch, usecondsSinceEpoch) {}
00126 };
00127 
00128 EventTime TimeNow();
00129 
00130 DelayInterval TimeRemainingUntil(EventTime const& futureEvent);
00131 // Returns DELAY_ZERO if "futureEvent" has already occurred.
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: // abstract base class
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); // returns a token for the entry
00169   void updateEntry(DelayQueueEntry* entry, DelayInterval newDelay);
00170   void updateEntry(long tokenToFind, DelayInterval newDelay);
00171   void removeEntry(DelayQueueEntry* entry); // but doesn't delete it
00172   DelayQueueEntry* removeEntry(long tokenToFind); // but doesn't delete it
00173 
00174   DelayInterval const& timeToNextAlarm();
00175   void handleAlarm();
00176 
00177 private:
00178   DelayQueueEntry* head() { return fNext; }
00179   DelayQueueEntry* findEntryByToken(long token);
00180   void synchronize(); // bring the 'time remaining' fields up-to-date
00181 
00182   EventTime fLastSyncTime;
00183 };
00184 
00185 #endif

Generated on Tue Oct 7 15:38:08 2008 for live by  doxygen 1.5.2