|
IBR-DTNSuite 0.6
|
00001 /* 00002 * PrimaryBlock.cpp 00003 * 00004 * Created on: 26.05.2010 00005 * Author: morgenro 00006 */ 00007 00008 #include "ibrdtn/data/PrimaryBlock.h" 00009 #include "ibrdtn/data/Exceptions.h" 00010 #include "ibrdtn/utils/Clock.h" 00011 #include <ibrcommon/thread/MutexLock.h> 00012 00013 namespace dtn 00014 { 00015 namespace data 00016 { 00017 size_t PrimaryBlock::__sequencenumber = 0; 00018 ibrcommon::Mutex PrimaryBlock::__sequence_lock; 00019 00020 PrimaryBlock::PrimaryBlock() 00021 : _procflags(0), _timestamp(0), _sequencenumber(0), _lifetime(3600), _fragmentoffset(0), _appdatalength(0) 00022 { 00023 relabel(); 00024 } 00025 00026 PrimaryBlock::~PrimaryBlock() 00027 { 00028 } 00029 00030 void PrimaryBlock::set(FLAGS flag, bool value) 00031 { 00032 if (value) 00033 { 00034 _procflags |= flag; 00035 } 00036 else 00037 { 00038 _procflags &= ~(flag); 00039 } 00040 } 00041 00042 bool PrimaryBlock::get(FLAGS flag) const 00043 { 00044 return (_procflags & flag); 00045 } 00046 00047 bool PrimaryBlock::operator!=(const PrimaryBlock& other) const 00048 { 00049 return !((*this) == other); 00050 } 00051 00052 bool PrimaryBlock::operator==(const PrimaryBlock& other) const 00053 { 00054 if (other._timestamp != _timestamp) return false; 00055 if (other._sequencenumber != _sequencenumber) return false; 00056 if (other._source != _source) return false; 00057 00058 if (other.get(PrimaryBlock::FRAGMENT)) 00059 { 00060 if (!get(PrimaryBlock::FRAGMENT)) return false; 00061 00062 if (other._fragmentoffset != _fragmentoffset) return false; 00063 if (other._appdatalength != _appdatalength) return false; 00064 } 00065 00066 return true; 00067 } 00068 00069 bool PrimaryBlock::operator<(const PrimaryBlock& other) const 00070 { 00071 if (_source < other._source) return true; 00072 if (_source != other._source) return false; 00073 00074 if (_timestamp < other._timestamp) return true; 00075 if (_timestamp != other._timestamp) return false; 00076 00077 if (_sequencenumber < other._sequencenumber) return true; 00078 if (_sequencenumber != other._sequencenumber) return false; 00079 00080 if (other.get(PrimaryBlock::FRAGMENT) && (_fragmentoffset < other._fragmentoffset)) return true; 00081 00082 return false; 00083 } 00084 00085 bool PrimaryBlock::operator>(const PrimaryBlock& other) const 00086 { 00087 return !(((*this) < other) || ((*this) == other)); 00088 } 00089 00090 bool PrimaryBlock::isExpired() const 00091 { 00092 return dtn::utils::Clock::isExpired(_lifetime + _timestamp, _lifetime); 00093 } 00094 00095 std::string PrimaryBlock::toString() const 00096 { 00097 stringstream ss; 00098 ss << "[" << _timestamp << "." << _sequencenumber; 00099 00100 if (get(FRAGMENT)) 00101 { 00102 ss << "." << _fragmentoffset; 00103 } 00104 00105 ss << "] " << _source.getString() << " -> " << _destination.getString(); 00106 00107 return ss.str(); 00108 } 00109 00110 void PrimaryBlock::relabel() 00111 { 00112 if (dtn::utils::Clock::badclock) 00113 { 00114 _timestamp = 0; 00115 } 00116 else 00117 { 00118 _timestamp = dtn::utils::Clock::getTime(); 00119 } 00120 00121 ibrcommon::MutexLock l(__sequence_lock); 00122 _sequencenumber = __sequencenumber; 00123 __sequencenumber++; 00124 } 00125 } 00126 }