IBR-DTNSuite
0.8
|
00001 #ifndef SIMPLEBUNDLESTORAGE_H_ 00002 #define SIMPLEBUNDLESTORAGE_H_ 00003 00004 #include "Component.h" 00005 #include "core/BundleCore.h" 00006 #include "storage/BundleStorage.h" 00007 #include "core/Node.h" 00008 #include "core/EventReceiver.h" 00009 00010 #include "storage/DataStorage.h" 00011 00012 #include <ibrcommon/thread/Conditional.h> 00013 #include <ibrcommon/thread/AtomicCounter.h> 00014 #include <ibrcommon/thread/RWMutex.h> 00015 00016 #include <ibrcommon/data/File.h> 00017 #include <ibrdtn/data/Bundle.h> 00018 #include <ibrdtn/data/BundleList.h> 00019 #include <ibrcommon/thread/Queue.h> 00020 00021 #include <set> 00022 #include <map> 00023 00024 using namespace dtn::data; 00025 00026 namespace dtn 00027 { 00028 namespace storage 00029 { 00033 class SimpleBundleStorage : public DataStorage::Callback, public BundleStorage, public dtn::core::EventReceiver, public dtn::daemon::IntegratedComponent, private dtn::data::BundleList 00034 { 00035 public: 00039 SimpleBundleStorage(const ibrcommon::File &workdir, size_t maxsize = 0, size_t buffer_limit = 0); 00040 00044 virtual ~SimpleBundleStorage(); 00045 00050 virtual void store(const dtn::data::Bundle &bundle); 00051 00058 virtual dtn::data::Bundle get(const dtn::data::BundleID &id); 00059 00063 virtual const std::list<dtn::data::MetaBundle> get(BundleFilterCallback &cb); 00064 00068 virtual const std::set<dtn::data::EID> getDistinctDestinations(); 00069 00075 void remove(const dtn::data::BundleID &id); 00076 00082 dtn::data::MetaBundle remove(const ibrcommon::BloomFilter &filter); 00083 00087 void clear(); 00088 00092 bool empty(); 00093 00097 unsigned int count(); 00098 00102 size_t size() const; 00103 00107 void releaseCustody(const dtn::data::EID &custodian, const dtn::data::BundleID &id); 00108 00113 void raiseEvent(const dtn::core::Event *evt); 00114 00118 virtual const std::string getName() const; 00119 00120 virtual void eventDataStorageStored(const dtn::storage::DataStorage::Hash &hash); 00121 virtual void eventDataStorageStoreFailed(const dtn::storage::DataStorage::Hash &hash, const ibrcommon::Exception&); 00122 virtual void eventDataStorageRemoved(const dtn::storage::DataStorage::Hash &hash); 00123 virtual void eventDataStorageRemoveFailed(const dtn::storage::DataStorage::Hash &hash, const ibrcommon::Exception&); 00124 virtual void iterateDataStorage(const dtn::storage::DataStorage::Hash &hash, dtn::storage::DataStorage::istream &stream); 00125 00126 protected: 00127 virtual void componentUp(); 00128 virtual void componentDown(); 00129 virtual void eventBundleExpired(const ExpiringBundle &b); 00130 00131 private: 00132 class BundleContainer : public DataStorage::Container 00133 { 00134 public: 00135 BundleContainer(const dtn::data::Bundle &b); 00136 virtual ~BundleContainer(); 00137 00138 std::string getKey() const; 00139 std::ostream& serialize(std::ostream &stream); 00140 00141 private: 00142 const dtn::data::Bundle _bundle; 00143 }; 00144 00145 dtn::data::Bundle __get(const dtn::data::MetaBundle&); 00146 00147 // This object manage data stored on disk 00148 DataStorage _datastore; 00149 00150 ibrcommon::RWMutex _bundleslock; 00151 std::map<DataStorage::Hash, dtn::data::Bundle> _pending_bundles; 00152 std::map<dtn::data::MetaBundle, DataStorage::Hash> _stored_bundles; 00153 00154 std::map<dtn::data::MetaBundle, size_t> _bundle_size; 00155 00156 struct CMP_BUNDLE_PRIORITY 00157 { 00158 bool operator() (const dtn::data::MetaBundle& lhs, const dtn::data::MetaBundle& rhs) const 00159 { 00160 if (lhs.getPriority() == rhs.getPriority()) 00161 return rhs > lhs; 00162 00163 return (lhs.getPriority() > rhs.getPriority()); 00164 } 00165 }; 00166 00167 std::set<dtn::data::MetaBundle, CMP_BUNDLE_PRIORITY> _priority_index; 00168 00169 size_t _maxsize; 00170 size_t _currentsize; 00171 }; 00172 } 00173 } 00174 00175 #endif /*SIMPLEBUNDLESTORAGE_H_*/