IBR-DTNSuite  0.8
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:
00031                 virtual size_t timeout(Timer *timer) = 0;
00032         };
00033 
00034         class Timer : public JoinableThread
00035         {
00036         public:
00037                 typedef size_t time_t;
00038                 static time_t get_current_time();
00039 
00044                 Timer(TimerCallback &callback, size_t timeout = 0);
00045 
00046                 virtual ~Timer();
00047 
00048                 void set(size_t timeout);
00049 
00053                 void reset();
00054 
00059                 void pause();
00060 
00064                 size_t getTimeout() const;
00065 
00066         protected:
00067                 void run();
00068                 void __cancellation();
00069 
00070         private:
00071                 enum TIMER_STATE
00072                 {
00073                         TIMER_UNSET = 0,
00074                         TIMER_RUNNING = 1,
00075                         TIMER_RESET = 2,
00076                         TIMER_STOPPED = 3,
00077                         TIMER_CANCELLED = 4
00078                 };
00079 
00080                 StatefulConditional<Timer::TIMER_STATE, TIMER_STOPPED> _state;
00081                 TimerCallback &_callback;
00082                 size_t _timeout;
00083         public:
00084                 class StopTimerException : public ibrcommon::Exception
00085                 {
00086                 public:
00087                         StopTimerException(const std::string& what = "") : Exception(what)
00088                         {
00089                         }
00090                 };
00091         };
00092 }
00093 
00094 #endif /* TIMER_H_ */