IBR-DTNSuite  0.8
ibrdtn/ibrdtn/data/CustodySignalBlock.cpp
Go to the documentation of this file.
00001 /*
00002  * CustodySignalBlock.cpp
00003  *
00004  *  Created on: 05.06.2009
00005  *      Author: morgenro
00006  */
00007 
00008 #include "ibrdtn/data/Bundle.h"
00009 #include "ibrdtn/data/CustodySignalBlock.h"
00010 #include "ibrdtn/data/BundleString.h"
00011 #include <stdlib.h>
00012 #include <sstream>
00013 
00014 namespace dtn
00015 {
00016         namespace data
00017         {
00018                 CustodySignalBlock::CustodySignalBlock()
00019                  : Block(dtn::data::PayloadBlock::BLOCK_TYPE), _admfield(32), _custody_accepted(false), _reason(NO_ADDITIONAL_INFORMATION), _fragment_offset(0),
00020                  _fragment_length(0), _timeofsignal(), _bundle_timestamp(0), _bundle_sequence(0)
00021                 {
00022                 }
00023 
00024                 CustodySignalBlock::~CustodySignalBlock()
00025                 {
00026                 }
00027 
00028                 size_t CustodySignalBlock::getLength() const
00029                 {
00030                         // determine the block size
00031                         size_t len = 0;
00032                         len += sizeof(_admfield);
00033                         len += sizeof(char); // status
00034 
00035                         if ( _admfield & 0x01 )
00036                         {
00037                                 len += _fragment_offset.getLength();
00038                                 len += _fragment_length.getLength();
00039                         }
00040 
00041                         len += _timeofsignal.getLength();
00042                         len += _bundle_timestamp.getLength();
00043                         len += _bundle_sequence.getLength();
00044 
00045                         BundleString sourceid(_source.getString());
00046                         len += sourceid.getLength();
00047 
00048                         return len;
00049                 }
00050 
00051                 std::istream& CustodySignalBlock::deserialize(std::istream &stream, const size_t length)
00052                 {
00053                         stream >> _admfield;
00054 
00055                         char status; stream >> status;
00056 
00057                         // decode custody acceptance
00058                         _custody_accepted = (status & 0x01);
00059 
00060                         // decode reason flag
00061                         _reason = REASON_CODE(status >> 1);
00062 
00063                         if ( _admfield & 0x01 )
00064                         {
00065                                 stream >> _fragment_offset;
00066                                 stream >> _fragment_length;
00067                         }
00068 
00069                         stream >> _timeofsignal;
00070                         stream >> _bundle_timestamp;
00071                         stream >> _bundle_sequence;
00072 
00073                         BundleString source;
00074                         stream >> source;
00075                         _source = EID(source);
00076 
00077                         // unset block not processed bit
00078                         set(dtn::data::Block::FORWARDED_WITHOUT_PROCESSED, false);
00079 
00080                         return stream;
00081                 }
00082 
00083                 std::ostream& CustodySignalBlock::serialize(std::ostream &stream, size_t &length) const
00084                 {
00085                         // write the content
00086                         stream << _admfield;
00087 
00088                         // encode reason flag
00089                         char status = (_reason << 1);
00090 
00091                         // encode custody acceptance
00092                         if (_custody_accepted) status |= 0x01;
00093 
00094                         // write the status byte
00095                         stream << status;
00096 
00097                         if ( _admfield & 0x01 )
00098                         {
00099                                 stream << _fragment_offset;
00100                                 stream << _fragment_length;
00101                         }
00102 
00103                         BundleString sourceid(_source.getString());
00104 
00105                         stream << _timeofsignal
00106                            << _bundle_timestamp
00107                            << _bundle_sequence
00108                            << sourceid;
00109 
00110                         return stream;
00111                 }
00112 
00113                 void CustodySignalBlock::setMatch(const dtn::data::MetaBundle& other)
00114                 {
00115                         // set bundle parameter
00116                         if (other.get(Bundle::FRAGMENT))
00117                         {
00118                                 _fragment_offset = other.offset;
00119                                 _fragment_length = other.appdatalength;
00120 
00121                                 if (!(_admfield & 1)) _admfield += 1;
00122                         }
00123 
00124                         _bundle_timestamp = other.timestamp;
00125                         _bundle_sequence = other.sequencenumber;
00126                         _source = other.source;
00127                 }
00128 
00129                 void CustodySignalBlock::setMatch(const dtn::data::Bundle& other)
00130                 {
00131                         // set bundle parameter
00132                         if (other.get(Bundle::FRAGMENT))
00133                         {
00134                                 _fragment_offset = other._fragmentoffset;
00135                                 _fragment_length = other._appdatalength;
00136 
00137                                 if (!(_admfield & 1)) _admfield += 1;
00138                         }
00139 
00140                         _bundle_timestamp = other._timestamp;
00141                         _bundle_sequence = other._sequencenumber;
00142                         _source = other._source;
00143                 }
00144 
00145                 bool CustodySignalBlock::match(const Bundle& other) const
00146                 {
00147                         if (_bundle_timestamp != other._timestamp) return false;
00148                         if (_bundle_sequence != other._sequencenumber) return false;
00149                         if (_source != other._source) return false;
00150 
00151                         // set bundle parameter
00152                         if (other.get(Bundle::FRAGMENT))
00153                         {
00154                                 if (!(_admfield & 1)) return false;
00155                                 if (_fragment_offset != other._fragmentoffset) return false;
00156                                 if (_fragment_length != other._appdatalength) return false;
00157                         }
00158 
00159                         return true;
00160                 }
00161 
00162         }
00163 }