00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 #include "Timer.h"
00040
00041 namespace ariba {
00042 namespace utility {
00043
00044 use_logging_cpp(Timer);
00045 SystemEventType TimerEventType("Timer");
00046
00047 Timer::Timer() {
00048 running = false;
00049
00050 #ifndef UNDERLAY_OMNET
00051 timerThread = NULL;
00052 #endif // UNDERLAY_OMNET
00053 }
00054
00055 Timer::~Timer() {
00056 #ifndef UNDERLAY_OMNET
00057 if( running ){
00058 running = false;
00059
00060 timerThread->join();
00061 delete timerThread;
00062 timerThread = NULL;
00063 }
00064 #endif // UNDERLAY_OMNET
00065 }
00066
00067 void Timer::setInterval(unsigned int millis, bool oneshot) {
00068 this->millis = millis;
00069 this->oneshot = oneshot;
00070 }
00071
00072 void Timer::start() {
00073 if( running ) return;
00074
00075 #ifndef UNDERLAY_OMNET
00076
00077 if( timerThread == NULL )
00078 timerThread = new boost::thread(boost::bind(&Timer::threadFunc, this) );
00079 #else
00080 running = true;
00081 SystemQueue::instance().scheduleEvent(
00082 SystemEvent( this, TimerEventType, NULL), millis );
00083 #endif
00084 }
00085
00086 void Timer::stop() {
00087 running = false;
00088 }
00089
00090 void Timer::eventFunction() {
00091
00092 }
00093
00094 #ifndef UNDERLAY_OMNET
00095 void Timer::threadFunc( Timer* obj ) {
00096 obj->running = true;
00097
00098 while( obj->running ) {
00099 boost::this_thread::sleep( boost::posix_time::milliseconds(obj->millis) );
00100 if (obj->running) SystemQueue::instance().scheduleEvent( SystemEvent( obj, TimerEventType, NULL), 0 );
00101 if( obj->oneshot ) break;
00102 }
00103
00104 if(! obj->oneshot )
00105 obj->running = false;
00106 }
00107 #endif // UNDERLAY_OMNET
00108
00109 void Timer::handleSystemEvent( const SystemEvent& event ) {
00110 if( running ){
00111 if( oneshot ) running = false;
00112 eventFunction();
00113 }
00114
00115 #ifdef UNDERLAY_OMNET
00116 if( ! oneshot && running )
00117 SystemQueue::instance().scheduleEvent(
00118 SystemEvent( this, TimerEventType, NULL), millis );
00119 #endif
00120 }
00121
00122 }}