IBR-DTNSuite 0.6

ibrcommon/ibrcommon/thread/Timer.h

Go to the documentation of this file.
00001 /*
00002  * Timer.h
00003  *
00004  *  Created on: 29.09.2009
00005  *      Author: morgenro
00006  */
00007 
00008 #ifndef IBRCOMMON_TIMER_H_
00009 #define IBRCOMMON_TIMER_H_
00010 
00011 #include "ibrcommon/thread/Thread.h"
00012 #include "ibrcommon/thread/Conditional.h"
00013 #include <map>
00014 #include <set>
00015 
00016 #include <string>
00017 #include <iostream>
00018 
00019 namespace ibrcommon
00020 {
00021         class Timer;
00022         class TimerCallback
00023         {
00024         public:
00030                 virtual bool timeout(Timer *timer) = 0;
00031         };
00032 
00033         class SimpleTimerCallback
00034         {
00035         public:
00041                 virtual size_t timeout(size_t identifier) = 0;
00042         };
00043 
00044         class Timer : public JoinableThread
00045         {
00046         public:
00047                 static size_t get_current_time();
00048 
00053                 Timer(TimerCallback &callback, size_t timeout);
00054 
00055                 virtual ~Timer();
00056 
00057                 void set(size_t timeout);
00058 
00062                 void reset();
00063 
00064         protected:
00065                 void run();
00066                 bool __cancellation();
00067 
00068         private:
00069                 enum TIMER_STATE
00070                 {
00071                         TIMER_UNSET = 0,
00072                         TIMER_RUNNING = 1,
00073                         TIMER_RESET = 2,
00074                         TIMER_STOPPED = 3
00075                 };
00076 
00077                 StatefulConditional<Timer::TIMER_STATE, TIMER_STOPPED> _state;
00078                 TimerCallback &_callback;
00079                 size_t _timeout;
00080         };
00081 
00082         class SimpleTimer : public JoinableThread
00083         {
00084         public:
00085                 SimpleTimer(SimpleTimerCallback &callback, size_t identifier);
00086                 virtual ~SimpleTimer();
00087 
00088                 void set(size_t timeout);
00089                 void remove();
00090 
00091         protected:
00092                 void run();
00093                 bool __cancellation();
00094 
00095         private:
00096                 SimpleTimerCallback &_callback;
00097                 size_t _identifier;
00098                 size_t _timeout;
00099                 ibrcommon::Conditional _timercond;
00100 
00101                 bool _running;
00102         };
00103 }
00104 
00105 #endif /* TIMER_H_ */