IBR-DTNSuite  0.10
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"
29 
30 #include "Component.h"
31 #include "core/EventReceiver.h"
32 #include "core/EventReceiver.h"
33 #include <ibrdtn/data/MetaBundle.h>
34 
38 #include <ibrcommon/data/File.h>
39 #include <ibrcommon/thread/Queue.h>
40 
41 #include <string>
42 #include <list>
43 #include <set>
44 
45 namespace dtn
46 {
47  namespace storage
48  {
50  {
51  static const std::string TAG;
52 
53  public:
59 
66  SQLiteBundleStorage(const ibrcommon::File &path, const dtn::data::Length &maxsize);
67 
71  virtual ~SQLiteBundleStorage();
72 
77  void store(const dtn::data::Bundle &bundle);
78 
86 
90  virtual void get(const BundleSelector &cb, BundleResult &result) throw (NoBundleFoundException, BundleSelectorException);
91 
95  virtual const eid_set getDistinctDestinations();
96 
102  void remove(const dtn::data::BundleID &id);
103 
107  void clear();
108 
112  void clearAll();
113 
117  bool empty();
118 
123 
127  void releaseCustody(const dtn::data::EID &custodian, const dtn::data::BundleID &id);
128 
133  void raiseEvent(const dtn::core::Event *evt) throw ();
134 
138  void eventBundleExpired(const dtn::data::BundleID &id) throw ();
139  void iterateDatabase(const dtn::data::MetaBundle &bundle);
140 
141  /*** BEGIN: methods for unit-testing ***/
142 
146  virtual void wait();
147 
152  virtual void setFaulty(bool mode);
153 
154  /*** END: methods for unit-testing ***/
155 
156  protected:
157  virtual void componentRun() throw ();
158  virtual void componentUp() throw ();
159  virtual void componentDown() throw ();
160  void __cancellation() throw ();
161 
162  private:
163 // enum Position
164 // {
165 // FIRST_FRAGMENT = 0,
166 // LAST_FRAGMENT = 1,
167 // BOTH_FRAGMENTS = 2
168 // };
169 
170  class Task
171  {
172  public:
173  virtual ~Task() {};
174  virtual void run(SQLiteBundleStorage &storage) = 0;
175  };
176 
177  class BlockingTask : public Task
178  {
179  public:
180  BlockingTask() : _done(false), _abort(false) {};
181  virtual ~BlockingTask() {};
182  virtual void run(SQLiteBundleStorage &storage) = 0;
183 
187  void wait()
188  {
189  ibrcommon::MutexLock l(_cond);
190  while (!_done && !_abort) _cond.wait();
191 
192  if (_abort) throw ibrcommon::Exception("Task aborted");
193  }
194 
195  void abort()
196  {
197  ibrcommon::MutexLock l(_cond);
198  _abort = true;
199  _cond.signal(true);
200  }
201 
202  void done()
203  {
204  ibrcommon::MutexLock l(_cond);
205  _done = true;
206  _cond.signal(true);
207  }
208 
209  private:
211  bool _done;
212  bool _abort;
213  };
214 
215  class TaskIdle : public Task
216  {
217  public:
218  TaskIdle() { };
219 
220  virtual ~TaskIdle() {};
221  virtual void run(SQLiteBundleStorage &storage);
222 
223  static ibrcommon::Mutex _mutex;
224  static bool _idle;
225  };
226 
227  class TaskExpire : public Task
228  {
229  public:
230  TaskExpire(const dtn::data::Timestamp &timestamp)
231  : _timestamp(timestamp) { };
232 
233  virtual ~TaskExpire() {};
234  virtual void run(SQLiteBundleStorage &storage);
235 
236  private:
237  const dtn::data::Timestamp _timestamp;
238  };
239 
244  class SQLiteBLOB : public ibrcommon::BLOB
245  {
246  friend class SQLiteBundleStorage;
247  public:
248  virtual ~SQLiteBLOB();
249 
250  virtual void clear();
251 
252  virtual void open();
253  virtual void close();
254 
255  protected:
256  std::iostream &__get_stream()
257  {
258  return _filestream;
259  }
260 
261  std::streamsize __get_size();
262 
263  private:
264  SQLiteBLOB(const ibrcommon::File &path);
265  std::fstream _filestream;
267  };
268 
269 
270 // /**
271 // * This Funktion gets e list and a bundle. Every block of the bundle except the PrimaryBlock is saved in a File.
272 // * The filenames of the blocks are stored in the List. The order of the filenames matches the order of the blocks.
273 // * @param An empty Stringlist
274 // * @param A Bundle which should be prepared to be Stored.
275 // * @return A number bigges than zero is returned indicating an error. Zero is returned if no error occurred.
276 // */
277 // int prepareBundle(list<std::string> &filenames, dtn::data::Bundle &bundle);
278 
284  void purge(const dtn::data::BundleID &id) throw ();
285 
289  virtual const std::string getName() const;
290 
291  SQLiteDatabase _database;
292 
293  ibrcommon::File _blobPath;
294  ibrcommon::File _blockPath;
295 
296  // contains all jobs to do
298 
299  ibrcommon::RWMutex _global_lock;
300 
301 // ibrcommon::AccessLockContext _al_context;
302  };
303  }
304 }
305 
306 #endif /* SQLITEBUNDLESTORAGE_H_ */