IBR-DTNSuite  0.8
ibrdtn/ibrdtn/data/BundleID.cpp
Go to the documentation of this file.
00001 /*
00002  * BundleID.cpp
00003  *
00004  *  Created on: 01.09.2009
00005  *      Author: morgenro
00006  */
00007 
00008 #include "ibrdtn/config.h"
00009 #include "ibrdtn/data/BundleID.h"
00010 #include "ibrdtn/data/SDNV.h"
00011 #include "ibrdtn/data/BundleString.h"
00012 
00013 namespace dtn
00014 {
00015         namespace data
00016         {
00017                 BundleID::BundleID(const dtn::data::EID s, const size_t t, const size_t sq, const bool f, const size_t o)
00018                 : source(s), timestamp(t), sequencenumber(sq), fragment(f), offset(o)
00019                 {
00020                 }
00021 
00022                 BundleID::BundleID(const dtn::data::Bundle &b)
00023                 : source(b._source), timestamp(b._timestamp), sequencenumber(b._sequencenumber),
00024                 fragment(b.get(dtn::data::Bundle::FRAGMENT)), offset(b._fragmentoffset)
00025                 {
00026                 }
00027 
00028                 BundleID::~BundleID()
00029                 {
00030                 }
00031 
00032                 bool BundleID::operator<(const BundleID& other) const
00033                 {
00034                         if (source < other.source) return true;
00035                         if (source != other.source) return false;
00036 
00037                         if (timestamp < other.timestamp) return true;
00038                         if (timestamp != other.timestamp) return false;
00039 
00040                         if (sequencenumber < other.sequencenumber) return true;
00041                         if (sequencenumber != other.sequencenumber) return false;
00042 
00043                         if (other.fragment)
00044                         {
00045                                 if (!fragment) return true;
00046                                 return (offset < other.offset);
00047                         }
00048 
00049                         return false;
00050                 }
00051 
00052                 bool BundleID::operator>(const BundleID& other) const
00053                 {
00054                         return !(((*this) < other) || ((*this) == other));
00055                 }
00056 
00057                 bool BundleID::operator!=(const BundleID& other) const
00058                 {
00059                         return !((*this) == other);
00060                 }
00061 
00062                 bool BundleID::operator==(const BundleID& other) const
00063                 {
00064                         if (other.timestamp != timestamp) return false;
00065                         if (other.sequencenumber != sequencenumber) return false;
00066                         if (other.source != source) return false;
00067                         if (other.fragment != fragment) return false;
00068 
00069                         if (fragment)
00070                         {
00071                                 if (other.offset != offset) return false;
00072                         }
00073 
00074                         return true;
00075                 }
00076 
00077                 size_t BundleID::getTimestamp() const
00078                 {
00079                         return timestamp;
00080                 }
00081 
00082                 string BundleID::toString() const
00083                 {
00084                         stringstream ss;
00085                         ss << "[" << timestamp << "." << sequencenumber;
00086 
00087                         if (fragment)
00088                         {
00089                                 ss << "." << offset;
00090                         }
00091 
00092                         ss << "] " << source.getString();
00093 
00094                         return ss.str();
00095                 }
00096 
00097                 std::ostream &operator<<(std::ostream &stream, const BundleID &obj)
00098                 {
00099                         dtn::data::SDNV timestamp(obj.timestamp);
00100                         dtn::data::SDNV sequencenumber(obj.sequencenumber);
00101                         dtn::data::SDNV offset(obj.offset);
00102                         dtn::data::BundleString source(obj.source.getString());
00103 
00104                         stream << timestamp << sequencenumber << offset << source;
00105 
00106                         return stream;
00107                 }
00108 
00109                 std::istream &operator>>(std::istream &stream, BundleID &obj)
00110                 {
00111                         dtn::data::SDNV timestamp;
00112                         dtn::data::SDNV sequencenumber;
00113                         dtn::data::SDNV offset;
00114                         dtn::data::BundleString source;
00115 
00116                         stream >> timestamp >> sequencenumber >> offset >> source;
00117 
00118                         obj.timestamp = timestamp.getValue();
00119                         obj.sequencenumber = sequencenumber.getValue();
00120                         obj.offset = offset.getValue();
00121                         obj.source = dtn::data::EID(source);
00122 
00123                         return stream;
00124                 }
00125         }
00126 }