IBR-DTNSuite  0.12
SQLiteBundleStorage.h
Go to the documentation of this file.
1 /*
2  * SQLiteBundleStorage.h
3  *
4  * Copyright (C) 2011 IBR, TU Braunschweig
5  *
6  * Written-by: Johannes Morgenroth <morgenroth@ibr.cs.tu-bs.de>
7  * Written-by: Matthias Myrtus
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  */
22 
23 
24 #ifndef SQLITEBUNDLESTORAGE_H_
25 #define SQLITEBUNDLESTORAGE_H_
26 
27 #include "storage/BundleStorage.h"
28 #include "storage/SQLiteDatabase.h"
30 
31 #include "Component.h"
32 #include "core/EventReceiver.h"
33 #include "core/EventReceiver.h"
34 #include <ibrdtn/data/MetaBundle.h>
35 
39 #include <ibrcommon/data/File.h>
40 #include <ibrcommon/thread/Queue.h>
41 
42 #include <string>
43 #include <list>
44 #include <set>
45 
46 namespace dtn
47 {
48  namespace storage
49  {
51  {
52  static const std::string TAG;
53 
54  public:
60 
68  SQLiteBundleStorage(const ibrcommon::File &path, const dtn::data::Length &maxsize, bool usePersistentBundleSets = false);
69 
73  virtual ~SQLiteBundleStorage();
74 
79  void store(const dtn::data::Bundle &bundle);
80 
85  bool contains(const dtn::data::BundleID &id);
86 
91 
99 
103  virtual void get(const BundleSelector &cb, BundleResult &result) throw (NoBundleFoundException, BundleSelectorException);
104 
108  virtual const eid_set getDistinctDestinations();
109 
115  void remove(const dtn::data::BundleID &id);
116 
120  void clear();
121 
125  bool empty();
126 
131 
135  void releaseCustody(const dtn::data::EID &custodian, const dtn::data::BundleID &id);
136 
141  void raiseEvent(const dtn::core::Event *evt) throw ();
142 
146  void eventBundleExpired(const dtn::data::BundleID &id, const dtn::data::Length size) throw ();
147  void iterateDatabase(const dtn::data::MetaBundle &bundle, const dtn::data::Length size);
148 
149 
150  /*** BEGIN: methods for unit-testing ***/
151 
155  virtual void wait();
156 
161  virtual void setFaulty(bool mode);
162 
163  /*** END: methods for unit-testing ***/
164 
165 
166  protected:
167  virtual void componentRun() throw ();
168  virtual void componentUp() throw ();
169  virtual void componentDown() throw ();
170  void __cancellation() throw ();
171 
172  private:
173 // enum Position
174 // {
175 // FIRST_FRAGMENT = 0,
176 // LAST_FRAGMENT = 1,
177 // BOTH_FRAGMENTS = 2
178 // };
179 
180  class Task
181  {
182  public:
183  virtual ~Task() {};
184  virtual void run(SQLiteBundleStorage &storage) = 0;
185  };
186 
187  class BlockingTask : public Task
188  {
189  public:
190  BlockingTask() : _done(false), _abort(false) {};
191  virtual ~BlockingTask() {};
192  virtual void run(SQLiteBundleStorage &storage) = 0;
193 
197  void wait()
198  {
199  ibrcommon::MutexLock l(_cond);
200  while (!_done && !_abort) _cond.wait();
201 
202  if (_abort) throw ibrcommon::Exception("Task aborted");
203  }
204 
205  void abort()
206  {
207  ibrcommon::MutexLock l(_cond);
208  _abort = true;
209  _cond.signal(true);
210  }
211 
212  void done()
213  {
214  ibrcommon::MutexLock l(_cond);
215  _done = true;
216  _cond.signal(true);
217  }
218 
219  private:
221  bool _done;
222  bool _abort;
223  };
224 
225  class TaskIdle : public Task
226  {
227  public:
228  TaskIdle() { };
229 
230  virtual ~TaskIdle() {};
231  virtual void run(SQLiteBundleStorage &storage);
232 
233  static ibrcommon::Mutex _mutex;
234  static bool _idle;
235  };
236 
237  class TaskExpire : public Task
238  {
239  public:
240  TaskExpire(const dtn::data::Timestamp &timestamp)
241  : _timestamp(timestamp) { };
242 
243  virtual ~TaskExpire() {};
244  virtual void run(SQLiteBundleStorage &storage);
245 
246  private:
247  const dtn::data::Timestamp _timestamp;
248  };
249 
254  class SQLiteBLOB : public ibrcommon::BLOB
255  {
256  friend class SQLiteBundleStorage;
257  public:
258  virtual ~SQLiteBLOB();
259 
260  virtual void clear();
261 
262  virtual void open();
263  virtual void close();
264 
265  protected:
266  std::iostream &__get_stream()
267  {
268  return _filestream;
269  }
270 
271  std::streamsize __get_size();
272 
273  private:
274  SQLiteBLOB(const ibrcommon::File &path);
275  std::fstream _filestream;
277  };
278 
279 
280 // /**
281 // * This Funktion gets e list and a bundle. Every block of the bundle except the PrimaryBlock is saved in a File.
282 // * The filenames of the blocks are stored in the List. The order of the filenames matches the order of the blocks.
283 // * @param An empty Stringlist
284 // * @param A Bundle which should be prepared to be Stored.
285 // * @return A number bigges than zero is returned indicating an error. Zero is returned if no error occurred.
286 // */
287 // int prepareBundle(list<std::string> &filenames, dtn::data::Bundle &bundle);
288 
292  virtual const std::string getName() const;
293 
294  SQLiteDatabase _database;
295 
296  ibrcommon::File _blobPath;
297  ibrcommon::File _blockPath;
298 
299  // contains all jobs to do
301 
302  ibrcommon::RWMutex _global_lock;
303  };
304  }
305 }
306 
307 #endif /* SQLITEBUNDLESTORAGE_H_ */