• Main Page
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

daemon/src/core/SimpleBundleStorage.h

Go to the documentation of this file.
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/Queue.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, private dtn::data::BundleList
00031                 {
00032                         class Task
00033                         {
00034                         public:
00035                                 virtual ~Task() {};
00036                                 virtual void run(SimpleBundleStorage &storage) = 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 
00076                         virtual const dtn::data::MetaBundle getByDestination(const dtn::data::EID &eid, bool exact = false);
00077 
00083                         virtual const dtn::data::MetaBundle getByFilter(const ibrcommon::BloomFilter &filter);
00084 
00089                         const std::list<dtn::data::BundleID> getList();
00090 
00096                         void remove(const dtn::data::BundleID &id);
00097 
00103                         dtn::data::MetaBundle remove(const ibrcommon::BloomFilter &filter);
00104 
00108                         void clear();
00109 
00113                         bool empty();
00114 
00118                         unsigned int count();
00119 
00123                         size_t size() const;
00124 
00128                         void releaseCustody(dtn::data::BundleID &bundle);
00129 
00134                         void raiseEvent(const Event *evt);
00135 
00139                         virtual const std::string getName() const;
00140 
00145                         void invokeExpiration(const size_t timestamp);
00146 
00147                 protected:
00148                         virtual void componentUp();
00149                         virtual void componentRun();
00150                         virtual void componentDown();
00151 
00152                         bool __cancellation();
00153 
00154                         virtual void eventBundleExpired(const ExpiringBundle &b);
00155 
00156                 private:
00157                         enum RunMode
00158                         {
00159                                 MODE_NONPERSISTENT = 0,
00160                                 MODE_PERSISTENT = 1
00161                         };
00162 
00163                         class BundleContainer : public dtn::data::MetaBundle
00164                         {
00165                         public:
00166                                 BundleContainer(const dtn::data::Bundle &b);
00167                                 BundleContainer(const ibrcommon::File &file);
00168                                 BundleContainer(const dtn::data::Bundle &b, const ibrcommon::File &workdir, const size_t size);
00169                                 ~BundleContainer();
00170 
00171                                 bool operator<(const BundleContainer& other) const;
00172 
00173                                 size_t size() const;
00174 
00175                                 dtn::data::Bundle get() const;
00176 
00177                                 BundleContainer& operator= (const BundleContainer &right);
00178                                 BundleContainer(const BundleContainer& right);
00179 
00180                                 void invokeStore();
00181 
00182                                 void remove();
00183 
00184                         protected:
00185                                 class Holder
00186                                 {
00187                                 public:
00188                                         Holder( const dtn::data::Bundle &b );
00189                                         Holder( const ibrcommon::File &file );
00190                                         Holder( const dtn::data::Bundle &b, const ibrcommon::File &workdir, const size_t size );
00191                                         ~Holder();
00192 
00193                                         size_t size() const;
00194 
00195                                         void invokeStore();
00196 
00197                                         dtn::data::Bundle getBundle();
00198 
00199                                         void remove();
00200 
00201                                         unsigned _count;
00202 
00203                                         ibrcommon::Mutex lock;
00204 
00205                                 private:
00206                                         enum STATE
00207                                         {
00208                                                 HOLDER_MEMORY = 0,
00209                                                 HOLDER_PENDING = 1,
00210                                                 HOLDER_STORED = 2,
00211                                                 HOLDER_DELETED = 3
00212                                         };
00213 
00214                                         ibrcommon::Mutex _state_lock;
00215                                         STATE _state;
00216 
00217                                         dtn::data::Bundle _bundle;
00218                                         ibrcommon::File _container;
00219 
00220                                         size_t _size;
00221                                 };
00222 
00223                         private:
00224                                 void down();
00225                                 Holder *_holder;
00226                         };
00227 
00228                         class TaskStoreBundle : public Task
00229                         {
00230                         public:
00231                                 TaskStoreBundle(const SimpleBundleStorage::BundleContainer&);
00232                                 ~TaskStoreBundle();
00233                                 virtual void run(SimpleBundleStorage &storage);
00234 
00235                         private:
00236                                 SimpleBundleStorage::BundleContainer _container;
00237                         };
00238 
00239                         class TaskRemoveBundle : public Task
00240                         {
00241                         public:
00242                                 TaskRemoveBundle(const SimpleBundleStorage::BundleContainer&);
00243                                 ~TaskRemoveBundle();
00244                                 virtual void run(SimpleBundleStorage &storage);
00245 
00246                         private:
00247                                 SimpleBundleStorage::BundleContainer _container;
00248                         };
00249 
00250                         class TaskExpireBundles : public Task
00251                         {
00252                         public:
00253                                 TaskExpireBundles(const size_t &timestamp);
00254                                 ~TaskExpireBundles();
00255                                 virtual void run(SimpleBundleStorage &storage);
00256 
00257                         private:
00258                                 const size_t _timestamp;
00259                         };
00260 
00261                         void load(const ibrcommon::File &file);
00262 
00263                         bool _running;
00264 
00265                         ibrcommon::Queue<Task*> _tasks;
00266 
00267                         std::set<BundleContainer> _bundles;
00268                         ibrcommon::Mutex _bundleslock;
00269 
00270                         ibrcommon::File _workdir;
00271                         RunMode _mode;
00272 
00273                         size_t _maxsize;
00274                         size_t _currentsize;
00275                 };
00276         }
00277 }
00278 
00279 #endif /*SIMPLEBUNDLESTORAGE_H_*/

Generated on Thu Nov 11 2010 09:49:46 for IBR-DTNSuite by  doxygen 1.7.1