|
IBR-DTNSuite 0.6
|
00001 /* 00002 * Clock.cpp 00003 * 00004 * Created on: 24.06.2010 00005 * Author: morgenro 00006 */ 00007 00008 #include "ibrdtn/utils/Clock.h" 00009 #include "ibrdtn/data/AgeBlock.h" 00010 #include <sys/time.h> 00011 00012 namespace dtn 00013 { 00014 namespace utils 00015 { 00016 int Clock::timezone = 0; 00017 float Clock::quality = 0; 00018 bool Clock::badclock = false; 00019 00023 u_int32_t Clock::TIMEVAL_CONVERSION = 946684800; 00024 00025 Clock::Clock() 00026 { 00027 } 00028 00029 Clock::~Clock() 00030 { 00031 } 00032 00033 size_t Clock::getExpireTime(const dtn::data::Bundle &b) 00034 { 00035 if ((b._timestamp == 0) || dtn::utils::Clock::badclock) 00036 { 00037 // use the AgeBlock to verify the age 00038 try { 00039 const dtn::data::AgeBlock &agebl = b.getBlock<const dtn::data::AgeBlock>(); 00040 size_t seconds_left = b._lifetime - agebl.getSeconds(); 00041 return getTime() + seconds_left; 00042 } catch (const dtn::data::Bundle::NoSuchBlockFoundException&) { }; 00043 } 00044 00045 return __getExpireTime(b._timestamp, b._lifetime); 00046 } 00047 00048 size_t Clock::getExpireTime(size_t timestamp, size_t lifetime) 00049 { 00050 return __getExpireTime(timestamp, lifetime); 00051 } 00052 00053 size_t Clock::getExpireTime(size_t lifetime) 00054 { 00055 return __getExpireTime(getTime(), lifetime); 00056 } 00057 00058 size_t Clock::__getExpireTime(size_t timestamp, size_t lifetime) 00059 { 00060 // if the quality of time is zero, return standard expire time 00061 if (Clock::quality == 0) return timestamp + lifetime; 00062 00063 // calculate sigma based on the quality of time and the original lifetime 00064 size_t sigma = lifetime * (1 - Clock::quality); 00065 00066 // expiration adjusted by quality of time 00067 return timestamp + lifetime + sigma; 00068 } 00069 00070 bool Clock::isExpired(const dtn::data::Bundle &b) 00071 { 00072 // use the AgeBlock to verify the age 00073 try { 00074 const dtn::data::AgeBlock &agebl = b.getBlock<const dtn::data::AgeBlock>(); 00075 return (b._lifetime < agebl.getSeconds()); 00076 } catch (const dtn::data::Bundle::NoSuchBlockFoundException&) { }; 00077 00078 return __isExpired(b._timestamp, b._lifetime); 00079 } 00080 00081 bool Clock::isExpired(size_t timestamp, size_t lifetime) 00082 { 00083 return __isExpired(timestamp, lifetime); 00084 } 00085 00086 bool Clock::__isExpired(size_t timestamp, size_t lifetime) 00087 { 00088 // if the quality of time is zero, then never expire a bundle 00089 if (Clock::quality == 0) return false; 00090 00091 // calculate sigma based on the quality of time and the original lifetime 00092 size_t sigma = lifetime * (1 - Clock::quality); 00093 00094 // expiration adjusted by quality of time 00095 if ( Clock::getTime() > (timestamp + lifetime + sigma)) return true; 00096 00097 return false; 00098 } 00099 00100 size_t Clock::getTime() 00101 { 00102 struct timeval now; 00103 ::gettimeofday(&now, 0); 00104 00105 // timezone 00106 int offset = Clock::timezone * 3600; 00107 00108 // do we believe we are before the year 2000? 00109 if ((u_int)now.tv_sec < TIMEVAL_CONVERSION) 00110 { 00111 return 0; 00112 } 00113 00114 return (now.tv_sec - TIMEVAL_CONVERSION) + offset; 00115 } 00116 } 00117 }