Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   Related Pages  

DelayedEvent.h

00001 /*
00002  * Copyright 2003 Michael A. Marsh, Cornell University. All rights reserved.
00003  * This software is released under the modified BSD license.
00004  * See the file LICENSE in the top-level directory for details.
00005  */
00006 //
00007 // $Id: DelayedEvent.h,v 1.3 2004/05/19 15:56:50 mmarsh Exp $
00008 //
00009 // $Log: DelayedEvent.h,v $
00010 // Revision 1.3  2004/05/19 15:56:50  mmarsh
00011 // *** empty log message ***
00012 //
00013 // Revision 1.2  2003/11/04 22:10:41  mmarsh
00014 // Added documentation for class DelayedEvent.
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                // There's no event, so don't keep clutter around.
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 /* __CODEX_EVENTS_DELAYEDEVENT_H__ */

Generated on Wed Jun 2 16:32:54 2004 for COrnell Data EXchange (CODEX) by doxygen1.2.18