IBR-DTNSuite
0.8
|
00001 /* 00002 * SummaryVector.cpp 00003 * 00004 * Created on: 02.03.2010 00005 * Author: morgenro 00006 */ 00007 00008 #include "routing/SummaryVector.h" 00009 #include <ibrcommon/Logger.h> 00010 #include <ibrcommon/TimeMeasurement.h> 00011 00012 namespace dtn 00013 { 00014 namespace routing 00015 { 00016 SummaryVector::SummaryVector(const std::set<dtn::data::MetaBundle> &list) 00017 : _bf(8192, 2) 00018 { 00019 add(list); 00020 } 00021 00022 SummaryVector::SummaryVector() 00023 : _bf(8192, 2) 00024 { 00025 } 00026 00027 SummaryVector::~SummaryVector() 00028 { 00029 } 00030 00031 void SummaryVector::commit() 00032 { 00033 IBRCOMMON_LOGGER_DEBUG(60) << "rebuilt of the bloomfilter needed" << IBRCOMMON_LOGGER_ENDL; 00034 ibrcommon::TimeMeasurement tm; 00035 tm.start(); 00036 00037 _bf.clear(); 00038 00039 for (std::set<dtn::data::BundleID>::const_iterator iter = _ids.begin(); iter != _ids.end(); iter++) 00040 { 00041 _bf.insert( (*iter).toString() ); 00042 } 00043 00044 tm.stop(); 00045 IBRCOMMON_LOGGER_DEBUG(60) << "rebuilt done in " << tm << IBRCOMMON_LOGGER_ENDL; 00046 } 00047 00048 void SummaryVector::add(const std::set<dtn::data::MetaBundle> &list) 00049 { 00050 for (std::set<dtn::data::MetaBundle>::const_iterator iter = list.begin(); iter != list.end(); iter++) 00051 { 00052 add( *iter ); 00053 } 00054 } 00055 00056 bool SummaryVector::contains(const dtn::data::BundleID &id) const 00057 { 00058 return _bf.contains(id.toString()); 00059 } 00060 00061 void SummaryVector::add(const dtn::data::BundleID &id) 00062 { 00063 _bf.insert(id.toString()); 00064 _ids.insert( id ); 00065 } 00066 00067 void SummaryVector::remove(const dtn::data::BundleID &id) 00068 { 00069 _ids.erase( id ); 00070 } 00071 00072 void SummaryVector::clear() 00073 { 00074 _bf.clear(); 00075 _ids.clear(); 00076 } 00077 00078 const ibrcommon::BloomFilter& SummaryVector::getBloomFilter() const 00079 { 00080 return _bf; 00081 } 00082 00083 std::set<dtn::data::BundleID> SummaryVector::getNotIn(ibrcommon::BloomFilter &filter) const 00084 { 00085 std::set<dtn::data::BundleID> ret; 00086 00087 // // if the lists are equal return an empty list 00088 // if (filter == _bf) return ret; 00089 00090 // iterate through all items to find the differences 00091 for (std::set<dtn::data::BundleID>::const_iterator iter = _ids.begin(); iter != _ids.end(); iter++) 00092 { 00093 if (!filter.contains( (*iter).toString() ) ) 00094 { 00095 ret.insert( (*iter) ); 00096 } 00097 } 00098 00099 return ret; 00100 } 00101 00102 size_t SummaryVector::getLength() const 00103 { 00104 return dtn::data::SDNV(_bf.size()).getLength() + _bf.size(); 00105 } 00106 00107 std::ostream &operator<<(std::ostream &stream, const SummaryVector &obj) 00108 { 00109 dtn::data::SDNV size(obj._bf.size()); 00110 stream << size; 00111 00112 const char *data = reinterpret_cast<const char*>(obj._bf.table()); 00113 stream.write(data, obj._bf.size()); 00114 00115 return stream; 00116 } 00117 00118 std::istream &operator>>(std::istream &stream, SummaryVector &obj) 00119 { 00120 dtn::data::SDNV count; 00121 stream >> count; 00122 00123 char buffer[count.getValue()]; 00124 00125 stream.read(buffer, count.getValue()); 00126 00127 obj.clear(); 00128 obj._bf.load((unsigned char*)buffer, count.getValue()); 00129 00130 return stream; 00131 } 00132 } 00133 }