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::BundleID
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& operator*();
00146 const dtn::data::Bundle& operator*() const;
00147
00148 BundleContainer& operator= (const BundleContainer &right);
00149 BundleContainer& operator= (BundleContainer &right);
00150 BundleContainer(const BundleContainer& right);
00151
00152 void invokeStore();
00153
00154 void remove();
00155
00156 protected:
00157 class Holder
00158 {
00159 public:
00160 Holder( const dtn::data::Bundle &b );
00161 Holder( const ibrcommon::File &file );
00162 Holder( const dtn::data::Bundle &b, const ibrcommon::File &workdir, const size_t size );
00163 ~Holder();
00164
00165 size_t size() const;
00166
00167 void invokeStore();
00168
00169 dtn::data::Bundle _bundle;
00170 ibrcommon::File _container;
00171 RunMode _mode;
00172 unsigned _count;
00173
00174 bool deletion;
00175
00176 size_t _size;
00177 };
00178
00179 private:
00180 Holder *_holder;
00181 };
00182
00183 class TaskStoreBundle : public Task
00184 {
00185 public:
00186 TaskStoreBundle(const SimpleBundleStorage::BundleContainer&);
00187 ~TaskStoreBundle();
00188 virtual void run();
00189
00190 private:
00191 SimpleBundleStorage::BundleContainer _container;
00192 };
00193
00194 class BundleStore : private dtn::data::BundleList
00195 {
00196 public:
00197 BundleStore(size_t maxsize = 0);
00198 BundleStore(ibrcommon::File workdir, size_t maxsize = 0);
00199 ~BundleStore();
00200
00201 void load(const ibrcommon::File &file);
00202 void store(const dtn::data::Bundle &bundle, SimpleBundleStorage &storage);
00203 void remove(const dtn::data::BundleID &id);
00204 void clear();
00205
00206 unsigned int count();
00207 size_t size() const;
00208
00209 dtn::data::Bundle get(const ibrcommon::BloomFilter &filter);
00210 dtn::data::Bundle get(const dtn::data::EID &eid);
00211 dtn::data::Bundle get(const dtn::data::BundleID &id);
00212
00213 const std::list<dtn::data::BundleID> getList();
00214
00215 void expire(const size_t timestamp);
00216
00217 std::set<BundleContainer> bundles;
00218 ibrcommon::Mutex bundleslock;
00219
00220 protected:
00221 virtual void eventBundleExpired(const ExpiringBundle &b);
00222
00223 private:
00224 ibrcommon::File _workdir;
00225 RunMode _mode;
00226
00227 size_t _maxsize;
00228 size_t _currentsize;
00229 };
00230
00231 BundleStore _store;
00232 bool _running;
00233
00234 ibrcommon::ThreadSafeQueue<Task*> _tasks;
00235 };
00236 }
00237 }
00238
00239 #endif