| 1 | // [License]
|
---|
| 2 | // The Ariba-Underlay Copyright
|
---|
| 3 | //
|
---|
| 4 | // Copyright (c) 2008-2009, Institute of Telematics, UniversitÀt Karlsruhe (TH)
|
---|
| 5 | //
|
---|
| 6 | // Institute of Telematics
|
---|
| 7 | // UniversitÀt Karlsruhe (TH)
|
---|
| 8 | // Zirkel 2, 76128 Karlsruhe
|
---|
| 9 | // Germany
|
---|
| 10 | //
|
---|
| 11 | // Redistribution and use in source and binary forms, with or without
|
---|
| 12 | // modification, are permitted provided that the following conditions are
|
---|
| 13 | // met:
|
---|
| 14 | //
|
---|
| 15 | // 1. Redistributions of source code must retain the above copyright
|
---|
| 16 | // notice, this list of conditions and the following disclaimer.
|
---|
| 17 | // 2. Redistributions in binary form must reproduce the above copyright
|
---|
| 18 | // notice, this list of conditions and the following disclaimer in the
|
---|
| 19 | // documentation and/or other materials provided with the distribution.
|
---|
| 20 | //
|
---|
| 21 | // THIS SOFTWARE IS PROVIDED BY THE INSTITUTE OF TELEMATICS ``AS IS'' AND
|
---|
| 22 | // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
---|
| 23 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
---|
| 24 | // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE ARIBA PROJECT OR
|
---|
| 25 | // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
---|
| 26 | // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
---|
| 27 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
---|
| 28 | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
---|
| 29 | // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
---|
| 30 | // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
---|
| 31 | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 32 | //
|
---|
| 33 | // The views and conclusions contained in the software and documentation
|
---|
| 34 | // are those of the authors and should not be interpreted as representing
|
---|
| 35 | // official policies, either expressed or implied, of the Institute of
|
---|
| 36 | // Telematics.
|
---|
| 37 | // [License]
|
---|
| 38 |
|
---|
| 39 | #include "Timer.h"
|
---|
| 40 |
|
---|
| 41 | namespace ariba {
|
---|
| 42 | namespace utility {
|
---|
| 43 |
|
---|
| 44 | use_logging_cpp(Timer);
|
---|
| 45 | SystemEventType TimerEventType("Timer");
|
---|
| 46 |
|
---|
| 47 | Timer::Timer() {
|
---|
| 48 | running = false;
|
---|
| 49 |
|
---|
| 50 | #ifndef UNDERLAY_OMNET
|
---|
| 51 | timerThread = NULL;
|
---|
| 52 | #endif // UNDERLAY_OMNET
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | Timer::~Timer() {
|
---|
| 56 | #ifndef UNDERLAY_OMNET
|
---|
| 57 | stop();
|
---|
| 58 | delete timerThread;
|
---|
| 59 | timerThread = NULL;
|
---|
| 60 | #endif // UNDERLAY_OMNET
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | void Timer::setInterval(unsigned int millis, bool oneshot) {
|
---|
| 64 | this->millis = millis;
|
---|
| 65 | this->oneshot = oneshot;
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | void Timer::start() {
|
---|
| 69 | if( running ) return;
|
---|
| 70 |
|
---|
| 71 | #ifndef UNDERLAY_OMNET
|
---|
| 72 | // running var will be set in the thread function
|
---|
| 73 | if( timerThread == NULL )
|
---|
| 74 | timerThread = new boost::thread(boost::bind(&Timer::threadFunc, this) );
|
---|
| 75 | #else
|
---|
| 76 | running = true;
|
---|
| 77 | SystemQueue::instance().scheduleEvent(
|
---|
| 78 | SystemEvent( this, TimerEventType, NULL), millis );
|
---|
| 79 | #endif
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | void Timer::reset(){
|
---|
| 83 | #ifndef UNDERLAY_OMNET
|
---|
| 84 | if(timerThread == NULL) return;
|
---|
| 85 | timerThread->interrupt();
|
---|
| 86 | #else
|
---|
| 87 | #error timer interruption not implemented for omnet
|
---|
| 88 | #endif
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | bool Timer::isRunning(){
|
---|
| 92 | return running;
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | void Timer::stop() {
|
---|
| 96 | running = false;
|
---|
| 97 | reset(); // cause the sleep to abort
|
---|
| 98 | SystemQueue::instance().dropAll(this);
|
---|
| 99 | timerThread->join();
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | void Timer::eventFunction() {
|
---|
| 103 | //std::cout << "unimplemented eventFunction Timer(" << millis << ")" << std::endl;
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | #ifndef UNDERLAY_OMNET
|
---|
| 107 | void Timer::threadFunc( Timer* obj ) {
|
---|
| 108 | obj->running = true;
|
---|
| 109 |
|
---|
| 110 | while( obj->running ) {
|
---|
| 111 |
|
---|
| 112 | try{
|
---|
| 113 |
|
---|
| 114 | boost::this_thread::sleep( boost::posix_time::milliseconds(obj->millis) );
|
---|
| 115 | if (obj->running)
|
---|
| 116 | SystemQueue::instance().scheduleEvent( SystemEvent( obj, TimerEventType, NULL), 0 );
|
---|
| 117 |
|
---|
| 118 | }catch(boost::thread_interrupted e){
|
---|
| 119 | // exception called when Timer::reset is called
|
---|
| 120 | // don't need to handle the exception
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | if( obj->oneshot ) break;
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | if(! obj->oneshot )
|
---|
| 127 | obj->running = false;
|
---|
| 128 | }
|
---|
| 129 | #endif // UNDERLAY_OMNET
|
---|
| 130 |
|
---|
| 131 | void Timer::handleSystemEvent( const SystemEvent& event ) {
|
---|
| 132 | if( running ){
|
---|
| 133 | if( oneshot ) running = false;
|
---|
| 134 | eventFunction();
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | #ifdef UNDERLAY_OMNET
|
---|
| 138 | if( ! oneshot && running )
|
---|
| 139 | SystemQueue::instance().scheduleEvent(
|
---|
| 140 | SystemEvent( this, TimerEventType, NULL), millis );
|
---|
| 141 | #endif
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | }} // namespace ariba, common
|
---|