IBR-DTNSuite
0.8
|
00001 /* 00002 * AgeBlock.cpp 00003 * 00004 * Created on: 18.11.2010 00005 * Author: morgenro 00006 */ 00007 00008 #include "ibrdtn/data/AgeBlock.h" 00009 00010 namespace dtn 00011 { 00012 namespace data 00013 { 00014 dtn::data::Block* AgeBlock::Factory::create() 00015 { 00016 return new AgeBlock(); 00017 } 00018 00019 AgeBlock::AgeBlock() 00020 : dtn::data::Block(AgeBlock::BLOCK_TYPE) 00021 { 00022 _time.start(); 00023 00024 // set the replicate in every fragment bit 00025 set(REPLICATE_IN_EVERY_FRAGMENT, true); 00026 } 00027 00028 AgeBlock::~AgeBlock() 00029 { 00030 } 00031 00032 size_t AgeBlock::getMicroseconds() const 00033 { 00034 ibrcommon::TimeMeasurement time = this->_time; 00035 time.stop(); 00036 00037 return _age.getValue() + time.getMicroseconds(); 00038 } 00039 00040 size_t AgeBlock::getSeconds() const 00041 { 00042 ibrcommon::TimeMeasurement time = this->_time; 00043 time.stop(); 00044 00045 return (_age.getValue() + time.getMicroseconds()) / 1000000; 00046 } 00047 00048 void AgeBlock::addSeconds(size_t value) 00049 { 00050 _age += (value * 1000000); 00051 } 00052 00053 void AgeBlock::setSeconds(size_t value) 00054 { 00055 _age = value * 1000000; 00056 } 00057 00058 size_t AgeBlock::getLength() const 00059 { 00060 return SDNV(getMicroseconds()).getLength(); 00061 } 00062 00063 std::ostream& AgeBlock::serialize(std::ostream &stream, size_t &length) const 00064 { 00065 SDNV value(getMicroseconds()); 00066 stream << value; 00067 length += value.getLength(); 00068 return stream; 00069 } 00070 00071 std::istream& AgeBlock::deserialize(std::istream &stream, const size_t length) 00072 { 00073 stream >> _age; 00074 _time.start(); 00075 return stream; 00076 } 00077 00078 size_t AgeBlock::getLength_strict() const 00079 { 00080 return 1; 00081 } 00082 00083 std::ostream& AgeBlock::serialize_strict(std::ostream &stream, size_t &length) const 00084 { 00085 // we have to ignore the age field, because this is very dynamic data 00086 stream << (char)0; 00087 length += 1; 00088 return stream; 00089 } 00090 } 00091 }