IBR-DTNSuite 0.6

ibrdtn/ibrdtn/data/BundleList.cpp

Go to the documentation of this file.
00001 /*
00002  * BundleList.cpp
00003  *
00004  *  Created on: 19.02.2010
00005  *      Author: morgenro
00006  */
00007 
00008 #include "ibrdtn/data/BundleList.h"
00009 #include "ibrdtn/utils/Clock.h"
00010 #include <algorithm>
00011 
00012 namespace dtn
00013 {
00014         namespace data
00015         {
00016                 BundleList::BundleList()
00017                 { }
00018 
00019                 BundleList::~BundleList()
00020                 { }
00021 
00022                 void BundleList::add(const dtn::data::MetaBundle &bundle)
00023                 {
00024                         // insert bundle id to the private list
00025                         _bundles.insert(bundle);
00026 
00027                         // insert the bundle to the public list
00028                         std::set<dtn::data::MetaBundle>::insert(bundle);
00029 
00030                         // increment the version
00031                         _version++;
00032                 }
00033 
00034                 void BundleList::remove(const dtn::data::MetaBundle &bundle)
00035                 {
00036                         // delete bundle id in the private list
00037                         _bundles.erase(bundle);
00038 
00039                         // delete bundle id in the public list
00040                         std::set<dtn::data::MetaBundle>::erase(bundle);
00041 
00042                         // increment the version
00043                         _version++;
00044                 }
00045 
00046                 void BundleList::clear()
00047                 {
00048                         _bundles.clear();
00049                         std::set<dtn::data::MetaBundle>::clear();
00050 
00051                         // increment the version
00052                         _version++;
00053                 }
00054 
00055                 bool BundleList::contains(const dtn::data::BundleID &bundle) const
00056                 {
00057                         if (::find(begin(), end(), bundle) == end())
00058                         {
00059                                 return false;
00060                         }
00061 
00062                         return true;
00063                 }
00064 
00065                 void BundleList::expire(const size_t timestamp)
00066                 {
00067                         bool commit = false;
00068 
00069                         // we can not expire bundles if we have no idea of time
00070                         if (dtn::utils::Clock::quality == 0) return;
00071 
00072                         std::set<ExpiringBundle>::iterator iter = _bundles.begin();
00073 
00074                         while (iter != _bundles.end())
00075                         {
00076                                 const ExpiringBundle &b = (*iter);
00077 
00078                                 if ( b.expiretime >= timestamp ) break;
00079 
00080                                 // raise expired event
00081                                 eventBundleExpired( b );
00082 
00083                                 // remove this item in public list
00084                                 std::set<dtn::data::MetaBundle>::erase( b.bundle );
00085 
00086                                 // remove this item in private list
00087                                 _bundles.erase( iter++ );
00088 
00089                                 commit = true;
00090                         }
00091 
00092                         if (commit)
00093                         {
00094                                 eventCommitExpired();
00095 
00096                                 // increment the version
00097                                 _version++;
00098                         }
00099                 }
00100 
00101                 bool BundleList::operator==(const size_t version) const
00102                 {
00103                         return (version == _version);
00104                 }
00105 
00106                 size_t BundleList::getVersion() const
00107                 {
00108                         return _version;
00109                 }
00110 
00111                 BundleList::ExpiringBundle::ExpiringBundle(const MetaBundle &b)
00112                  : bundle(b), expiretime(b.expiretime)
00113                 { }
00114 
00115                 BundleList::ExpiringBundle::~ExpiringBundle()
00116                 { }
00117 
00118                 bool BundleList::ExpiringBundle::operator!=(const ExpiringBundle& other) const
00119                 {
00120                         return !(other == *this);
00121                 }
00122 
00123                 bool BundleList::ExpiringBundle::operator==(const ExpiringBundle& other) const
00124                 {
00125                         return (other.bundle == this->bundle);
00126                 }
00127 
00128                 bool BundleList::ExpiringBundle::operator<(const ExpiringBundle& other) const
00129                 {
00130                         if (expiretime < other.expiretime) return true;
00131                         if (expiretime != other.expiretime) return false;
00132 
00133                         if (bundle < other.bundle) return true;
00134 
00135                         return false;
00136                 }
00137 
00138                 bool BundleList::ExpiringBundle::operator>(const ExpiringBundle& other) const
00139                 {
00140                         return !(((*this) < other) || ((*this) == other));
00141                 }
00142         }
00143 }