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