00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef __CODEX_EVENTS_DELAYEDEVENT_H__
00019 #define __CODEX_EVENTS_DELAYEDEVENT_H__
00020
00021 #include "Event.h"
00022 #include <sys/time.h>
00023
00024 namespace CODEX_Events
00025 {
00035 class DelayedEvent : public EventBase
00036 {
00037 public :
00043 DelayedEvent( EventBase* evt ) :
00044 EventBase( 0 ),
00045 m_evt( evt ),
00046 m_aged( false )
00047 {
00048 gettimeofday( &m_enqueuedStart, 0 );
00049 }
00050
00052 virtual ~DelayedEvent()
00053 {
00054 if ( 0 != m_evt ) delete m_evt;
00055 }
00056
00071 bool handle()
00072 {
00073 if ( 0 == m_evt )
00074 {
00075
00076 return true;
00077 }
00078 if ( ! m_aged )
00079 {
00080 struct timeval tv;
00081 gettimeofday( &tv, 0 );
00082 tv.tv_usec -= m_enqueuedStart.tv_usec;
00083 tv.tv_sec -= m_enqueuedStart.tv_sec;
00084 if ( tv.tv_usec < 0 )
00085 {
00086 tv.tv_usec += 1000000;
00087 tv.tv_sec -= 1;
00088 }
00089 if ( tv.tv_sec < sDelay )
00090 {
00091 return false;
00092 }
00093 if ( ( tv.tv_sec == sDelay ) && ( tv.tv_usec < usDelay ) )
00094 {
00095 return false;
00096 }
00097 m_aged = true;
00098 }
00099 return m_evt->handle();
00100 }
00101
00102 static long int sDelay;
00103 static long int usDelay;
00104
00105 private :
00106 EventBase* m_evt;
00107 bool m_aged;
00108 struct timeval m_enqueuedStart;
00109 };
00110 }
00111
00112 #endif