IBR-DTNSuite
0.8
|
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 if (other.get(PrimaryBlock::FRAGMENT) != get(PrimaryBlock::FRAGMENT)) return false; 00058 00059 if (get(PrimaryBlock::FRAGMENT)) 00060 { 00061 if (other._fragmentoffset != _fragmentoffset) return false; 00062 if (other._appdatalength != _appdatalength) return false; 00063 } 00064 00065 return true; 00066 } 00067 00068 bool PrimaryBlock::operator<(const PrimaryBlock& other) const 00069 { 00070 if (_source < other._source) return true; 00071 if (_source != other._source) return false; 00072 00073 if (_timestamp < other._timestamp) return true; 00074 if (_timestamp != other._timestamp) return false; 00075 00076 if (_sequencenumber < other._sequencenumber) return true; 00077 if (_sequencenumber != other._sequencenumber) return false; 00078 00079 if (other.get(PrimaryBlock::FRAGMENT)) 00080 { 00081 if (!get(PrimaryBlock::FRAGMENT)) return true; 00082 return (_fragmentoffset < other._fragmentoffset); 00083 } 00084 00085 return false; 00086 } 00087 00088 bool PrimaryBlock::operator>(const PrimaryBlock& other) const 00089 { 00090 return !(((*this) < other) || ((*this) == other)); 00091 } 00092 00093 bool PrimaryBlock::isExpired() const 00094 { 00095 return dtn::utils::Clock::isExpired(_lifetime + _timestamp, _lifetime); 00096 } 00097 00098 std::string PrimaryBlock::toString() const 00099 { 00100 stringstream ss; 00101 ss << "[" << _timestamp << "." << _sequencenumber; 00102 00103 if (get(FRAGMENT)) 00104 { 00105 ss << "." << _fragmentoffset; 00106 } 00107 00108 ss << "] " << _source.getString() << " -> " << _destination.getString(); 00109 00110 return ss.str(); 00111 } 00112 00113 void PrimaryBlock::relabel() 00114 { 00115 if (dtn::utils::Clock::badclock) 00116 { 00117 _timestamp = 0; 00118 } 00119 else 00120 { 00121 _timestamp = dtn::utils::Clock::getTime(); 00122 } 00123 00124 ibrcommon::MutexLock l(__sequence_lock); 00125 _sequencenumber = __sequencenumber; 00126 __sequencenumber++; 00127 } 00128 } 00129 }