00001 #ifndef SIMPLEBUNDLESTORAGE_H_
00002 #define SIMPLEBUNDLESTORAGE_H_
00003
00004 #include "Component.h"
00005 #include "core/BundleCore.h"
00006 #include "core/BundleStorage.h"
00007 #include "core/Node.h"
00008 #include "core/EventReceiver.h"
00009
00010 #include <ibrcommon/thread/Conditional.h>
00011 #include <ibrcommon/thread/AtomicCounter.h>
00012 #include <ibrcommon/thread/Mutex.h>
00013
00014 #include <ibrcommon/data/File.h>
00015 #include <ibrdtn/data/Bundle.h>
00016 #include <ibrdtn/data/BundleList.h>
00017 #include <ibrcommon/thread/ThreadSafeQueue.h>
00018
00019 #include <set>
00020
00021 using namespace dtn::data;
00022
00023 namespace dtn
00024 {
00025 namespace core
00026 {
00030 class SimpleBundleStorage : public BundleStorage, public EventReceiver, public dtn::daemon::IndependentComponent
00031 {
00032 class Task
00033 {
00034 public:
00035 virtual ~Task() {};
00036 virtual void run() = 0;
00037 };
00038
00039 public:
00043 SimpleBundleStorage(size_t maxsize = 0);
00044
00048 SimpleBundleStorage(const ibrcommon::File &workdir, size_t maxsize = 0);
00049
00053 virtual ~SimpleBundleStorage();
00054
00059 virtual void store(const dtn::data::Bundle &bundle);
00060
00067 virtual dtn::data::Bundle get(const dtn::data::BundleID &id);
00068 virtual dtn::data::Bundle get(const dtn::data::EID &eid);
00069
00075 virtual dtn::data::Bundle get(const ibrcommon::BloomFilter &filter);
00076
00077
00078 const std::list<dtn::data::BundleID> getList();
00079
00085 void remove(const dtn::data::BundleID &id);
00086
00090 void clear();
00091
00095 bool empty();
00096
00100 unsigned int count();
00101
00105 size_t size() const;
00106
00110 void releaseCustody(dtn::data::BundleID &bundle);
00111
00116 void raiseEvent(const Event *evt);
00117
00118 protected:
00119 virtual void componentUp();
00120 virtual void componentRun();
00121 virtual void componentDown();
00122
00123 private:
00124 enum RunMode
00125 {
00126 MODE_NONPERSISTENT = 0,
00127 MODE_PERSISTENT = 1
00128 };
00129
00130 class BundleContainer : public dtn::data::MetaBundle
00131 {
00132 public:
00133 BundleContainer(const dtn::data::Bundle &b);
00134 BundleContainer(const ibrcommon::File &file);
00135 BundleContainer(const dtn::data::Bundle &b, const ibrcommon::File &workdir, const size_t size);
00136 ~BundleContainer();
00137
00138 bool operator!=(const BundleContainer& other) const;
00139 bool operator==(const BundleContainer& other) const;
00140 bool operator<(const BundleContainer& other) const;
00141 bool operator>(const BundleContainer& other) const;
00142
00143 size_t size() const;
00144
00145 dtn::data::Bundle get() const;
00146
00147 BundleContainer& operator= (const BundleContainer &right);
00148 BundleContainer& operator= (BundleContainer &right);
00149 BundleContainer(const BundleContainer& right);
00150
00151 void invokeStore();
00152
00153 void remove();
00154
00155 protected:
00156 class Holder
00157 {
00158 public:
00159 Holder( const dtn::data::Bundle &b );
00160 Holder( const ibrcommon::File &file );
00161 Holder( const dtn::data::Bundle &b, const ibrcommon::File &workdir, const size_t size );
00162 ~Holder();
00163
00164 size_t size() const;
00165
00166 void invokeStore();
00167
00168 dtn::data::Bundle getBundle();
00169
00170 void remove();
00171
00172 unsigned _count;
00173
00174 ibrcommon::Mutex lock;
00175
00176 private:
00177 enum STATE
00178 {
00179 HOLDER_MEMORY = 0,
00180 HOLDER_PENDING = 1,
00181 HOLDER_STORED = 2,
00182 HOLDER_DELETED = 3
00183 };
00184
00185 ibrcommon::Mutex _state_lock;
00186 STATE _state;
00187
00188 dtn::data::Bundle _bundle;
00189 ibrcommon::File _container;
00190
00191 size_t _size;
00192 };
00193
00194 private:
00195 void down();
00196 Holder *_holder;
00197 };
00198
00199 class TaskStoreBundle : public Task
00200 {
00201 public:
00202 TaskStoreBundle(const SimpleBundleStorage::BundleContainer&);
00203 ~TaskStoreBundle();
00204 virtual void run();
00205
00206 private:
00207 SimpleBundleStorage::BundleContainer _container;
00208 };
00209
00210 class BundleStore : private dtn::data::BundleList
00211 {
00212 public:
00213 BundleStore(size_t maxsize = 0);
00214 BundleStore(ibrcommon::File workdir, size_t maxsize = 0);
00215 ~BundleStore();
00216
00217 void load(const ibrcommon::File &file);
00218 void store(const dtn::data::Bundle &bundle, SimpleBundleStorage &storage);
00219 void remove(const dtn::data::BundleID &id);
00220 void clear();
00221
00222 unsigned int count();
00223 size_t size() const;
00224
00225 dtn::data::Bundle get(const ibrcommon::BloomFilter &filter);
00226 dtn::data::Bundle get(const dtn::data::EID &eid);
00227 dtn::data::Bundle get(const dtn::data::BundleID &id);
00228
00229 const std::list<dtn::data::BundleID> getList();
00230
00231 void expire(const size_t timestamp);
00232
00233 std::set<BundleContainer> bundles;
00234 ibrcommon::Mutex bundleslock;
00235
00236 protected:
00237 virtual void eventBundleExpired(const ExpiringBundle &b);
00238
00239 private:
00240 ibrcommon::File _workdir;
00241 RunMode _mode;
00242
00243 size_t _maxsize;
00244 size_t _currentsize;
00245 };
00246
00247 BundleStore _store;
00248 bool _running;
00249
00250 ibrcommon::ThreadSafeQueue<Task*> _tasks;
00251 };
00252 }
00253 }
00254
00255 #endif