Go to the documentation of this file.00001
00002
00003
00004
00005
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
00038 try {
00039 const dtn::data::AgeBlock &agebl = b.getBlock<const dtn::data::AgeBlock>();
00040 size_t seconds_left = b._lifetime - agebl.getAge();
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
00061 if (Clock::quality == 0) return timestamp + lifetime;
00062
00063
00064 size_t sigma = lifetime * (1 - Clock::quality);
00065
00066
00067 return timestamp + lifetime + sigma;
00068 }
00069
00070 bool Clock::isExpired(const dtn::data::Bundle &b)
00071 {
00072 if ((b._timestamp == 0) || dtn::utils::Clock::badclock)
00073 {
00074
00075 try {
00076 const dtn::data::AgeBlock &agebl = b.getBlock<const dtn::data::AgeBlock>();
00077 return (b._lifetime < agebl.getAge());
00078 } catch (const dtn::data::Bundle::NoSuchBlockFoundException&) { };
00079 }
00080
00081 return __isExpired(b._timestamp, b._lifetime);
00082 }
00083
00084 bool Clock::isExpired(size_t timestamp, size_t lifetime)
00085 {
00086 return __isExpired(timestamp, lifetime);
00087 }
00088
00089 bool Clock::__isExpired(size_t timestamp, size_t lifetime)
00090 {
00091
00092 if (Clock::quality == 0) return false;
00093
00094
00095 size_t sigma = lifetime * (1 - Clock::quality);
00096
00097
00098 if ( Clock::getTime() > (timestamp + lifetime + sigma)) return true;
00099
00100 return false;
00101 }
00102
00103 size_t Clock::getTime()
00104 {
00105 struct timeval now;
00106 ::gettimeofday(&now, 0);
00107
00108
00109 int offset = Clock::timezone * 3600;
00110
00111
00112 if ((u_int)now.tv_sec < TIMEVAL_CONVERSION)
00113 {
00114 return 0;
00115 }
00116
00117 return (now.tv_sec - TIMEVAL_CONVERSION) + offset;
00118 }
00119 }
00120 }