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