• Main Page
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

ibrdtn/ibrdtn/data/PrimaryBlock.cpp

Go to the documentation of this file.
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 << "] " << _source.getString() << " -> " << _destination.getString();
00099                         return ss.str();
00100                 }
00101 
00102                 void PrimaryBlock::relabel()
00103                 {
00104                         if (dtn::utils::Clock::badclock)
00105                         {
00106                                 _timestamp = 0;
00107                         }
00108                         else
00109                         {
00110                                 _timestamp = dtn::utils::Clock::getTime();
00111                         }
00112 
00113                         ibrcommon::MutexLock l(__sequence_lock);
00114                         _sequencenumber = __sequencenumber;
00115                         __sequencenumber++;
00116                 }
00117         }
00118 }

Generated on Wed Mar 30 2011 11:11:49 for IBR-DTNSuite by  doxygen 1.7.1