IBR-DTNSuite  0.8
daemon/src/storage/SQLiteBundleStorage.h
Go to the documentation of this file.
00001 /*
00002  * SQLiteBundleStorage.h
00003  *
00004  *  Created on: 09.01.2010
00005  *      Author: Myrtus
00006  */
00007 
00008 
00009 #ifndef SQLITEBUNDLESTORAGE_H_
00010 #define SQLITEBUNDLESTORAGE_H_
00011 
00012 #include "storage/BundleStorage.h"
00013 #include "storage/SQLiteDatabase.h"
00014 
00015 #include "Component.h"
00016 #include "core/EventReceiver.h"
00017 #include "core/EventReceiver.h"
00018 #include <ibrdtn/data/MetaBundle.h>
00019 
00020 #include <ibrcommon/thread/Thread.h>
00021 #include <ibrcommon/thread/RWMutex.h>
00022 #include <ibrcommon/thread/Conditional.h>
00023 #include <ibrcommon/data/File.h>
00024 #include <ibrcommon/thread/Queue.h>
00025 
00026 #include <string>
00027 #include <list>
00028 #include <set>
00029 
00030 namespace dtn
00031 {
00032         namespace storage
00033         {
00034                 class SQLiteBundleStorage: public BundleStorage, public dtn::core::EventReceiver, public dtn::daemon::IndependentComponent, public ibrcommon::BLOB::Provider
00035                 {
00036                 public:
00041                         ibrcommon::BLOB::Reference create();
00042 
00049                         SQLiteBundleStorage(const ibrcommon::File &path, const size_t &size);
00050 
00054                         virtual ~SQLiteBundleStorage();
00055 
00060                         void store(const dtn::data::Bundle &bundle);
00061 
00068                         dtn::data::Bundle get(const dtn::data::BundleID &id);
00069 
00073                         const std::list<dtn::data::MetaBundle> get(BundleFilterCallback &cb);
00074 
00078                         virtual const std::set<dtn::data::EID> getDistinctDestinations();
00079 
00085                         void remove(const dtn::data::BundleID &id);
00086 
00090                         void clear();
00091 
00095                         void clearAll();
00096 
00100                         bool empty();
00101 
00105                         unsigned int count();
00106 
00110                         void releaseCustody(const dtn::data::EID &custodian, const dtn::data::BundleID &id);
00111 
00116                         void raiseEvent(const dtn::core::Event *evt);
00117 
00118                 protected:
00119                         virtual void componentRun();
00120                         virtual void componentUp();
00121                         virtual void componentDown();
00122                         void __cancellation();
00123 
00124                 private:
00125 //                      enum Position
00126 //                      {
00127 //                              FIRST_FRAGMENT  = 0,
00128 //                              LAST_FRAGMENT   = 1,
00129 //                              BOTH_FRAGMENTS  = 2
00130 //                      };
00131 
00132                         class Task
00133                         {
00134                         public:
00135                                 virtual ~Task() {};
00136                                 virtual void run(SQLiteBundleStorage &storage) = 0;
00137                         };
00138 
00139                         class BlockingTask : public Task
00140                         {
00141                         public:
00142                                 BlockingTask() : _done(false), _abort(false) {};
00143                                 virtual ~BlockingTask() {};
00144                                 virtual void run(SQLiteBundleStorage &storage) = 0;
00145 
00149                                 void wait()
00150                                 {
00151                                         ibrcommon::MutexLock l(_cond);
00152                                         while (!_done && !_abort) _cond.wait();
00153 
00154                                         if (_abort) throw ibrcommon::Exception("Task aborted");
00155                                 }
00156 
00157                                 void abort()
00158                                 {
00159                                         ibrcommon::MutexLock l(_cond);
00160                                         _abort = true;
00161                                         _cond.signal(true);
00162                                 }
00163 
00164                                 void done()
00165                                 {
00166                                         ibrcommon::MutexLock l(_cond);
00167                                         _done = true;
00168                                         _cond.signal(true);
00169                                 }
00170 
00171                         private:
00172                                 ibrcommon::Conditional _cond;
00173                                 bool _done;
00174                                 bool _abort;
00175                         };
00176 
00177                         class TaskRemove : public Task
00178                         {
00179                         public:
00180                                 TaskRemove(const dtn::data::BundleID &id)
00181                                  : _id(id) { };
00182 
00183                                 virtual ~TaskRemove() {};
00184                                 virtual void run(SQLiteBundleStorage &storage);
00185 
00186                         private:
00187                                 const dtn::data::BundleID _id;
00188                         };
00189 
00190                         class TaskIdle : public Task
00191                         {
00192                         public:
00193                                 TaskIdle() { };
00194 
00195                                 virtual ~TaskIdle() {};
00196                                 virtual void run(SQLiteBundleStorage &storage);
00197 
00198                                 static ibrcommon::Mutex _mutex;
00199                                 static bool _idle;
00200                         };
00201 
00202                         class TaskExpire : public Task
00203                         {
00204                         public:
00205                                 TaskExpire(size_t timestamp)
00206                                 : _timestamp(timestamp) { };
00207 
00208                                 virtual ~TaskExpire() {};
00209                                 virtual void run(SQLiteBundleStorage &storage);
00210 
00211                         private:
00212                                 size_t _timestamp;
00213                         };
00214 
00219                         class SQLiteBLOB : public ibrcommon::BLOB
00220                         {
00221                                 friend class SQLiteBundleStorage;
00222                         public:
00223                                 virtual ~SQLiteBLOB();
00224 
00225                                 virtual void clear();
00226 
00227                                 virtual void open();
00228                                 virtual void close();
00229 
00230                         protected:
00231                                 std::iostream &__get_stream()
00232                                 {
00233                                         return _filestream;
00234                                 }
00235 
00236                                 size_t __get_size();
00237 
00238                         private:
00239                                 SQLiteBLOB(const ibrcommon::File &path);
00240                                 std::fstream _filestream;
00241                                 ibrcommon::File _file;
00242                                 ibrcommon::File _blobPath;
00243                         };
00244 
00245 
00246 //                      /**
00247 //                       *  This Funktion gets e list and a bundle. Every block of the bundle except the PrimaryBlock is saved in a File.
00248 //                       *  The filenames of the blocks are stored in the List. The order of the filenames matches the order of the blocks.
00249 //                       *  @param An empty Stringlist
00250 //                       *  @param A Bundle which should be prepared to be Stored.
00251 //                       *  @return A number bigges than zero is returned indicating an error. Zero is returned if no error occurred.
00252 //                       */
00253 //                      int prepareBundle(list<std::string> &filenames, dtn::data::Bundle &bundle);
00254 
00255 
00259                         virtual const std::string getName() const;
00260 
00261                         SQLiteDatabase _database;
00262 
00263                         ibrcommon::File _blobPath;
00264                         ibrcommon::File _blockPath;
00265 
00266                         // contains all jobs to do
00267                         ibrcommon::Queue<Task*> _tasks;
00268 
00269                         ibrcommon::RWMutex _global_lock;
00270 
00271 //                      ibrcommon::AccessLockContext _al_context;
00272                 };
00273         }
00274 }
00275 
00276 #endif /* SQLITEBUNDLESTORAGE_H_ */