00001 /* 00002 * Copyright 2007 Baylor University 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #ifndef _PROPHET_TIMER_H_ 00018 #define _PROPHET_TIMER_H_ 00019 00020 #include <oasys/thread/Timer.h> 00021 #include <oasys/thread/Lock.h> 00022 #include "prophet/Alarm.h" 00023 #include "prophet/Encounter.h" 00024 00025 namespace dtn 00026 { 00027 00028 class ProphetTimer : public oasys::Timer, 00029 public prophet::Alarm 00030 { 00031 public: 00032 ProphetTimer(prophet::ExpirationHandler* eh, 00033 oasys::SpinLock* lock) 00034 : oasys::Timer(), 00035 prophet::Alarm(eh), 00036 lock_(lock) 00037 { 00038 ASSERT( eh != NULL ); 00039 ASSERT( lock != NULL ); 00040 } 00041 00042 virtual ~ProphetTimer() {} 00043 00047 void timeout(const struct timeval& now) 00048 { 00049 (void)now; 00050 oasys::ScopeLock l(lock_,"ProphetTimer::timeout"); 00051 prophet::Alarm::timeout(); 00052 } 00053 00055 void schedule(u_int milliseconds) 00056 { 00057 oasys::Timer::schedule_in(milliseconds); 00058 } 00059 u_int time_remaining() const 00060 { 00061 struct timeval now; 00062 ::gettimeofday(&now,0); 00063 struct timeval when = const_cast<ProphetTimer*>(this)->when(); 00064 int diff = TIMEVAL_DIFF_MSEC(when, now); 00065 return (diff < 0) ? 0 : diff; 00066 } 00067 void cancel() { oasys::Timer::cancel(); } 00068 bool pending() const { return oasys::Timer::pending_; } 00069 bool cancelled() const { return oasys::Timer::cancelled_; } 00071 00072 protected: 00073 oasys::SpinLock* lock_; 00074 00075 }; // class ProphetTimer 00076 00077 }; // namespace dtn 00078 00079 #endif // _PROPHET_TIMER_H_