IBR-DTNSuite  0.10
BundleStorage.h
Go to the documentation of this file.
1 /*
2  * BundleStorage.h
3  *
4  * Copyright (C) 2011 IBR, TU Braunschweig
5  *
6  * Written-by: Johannes Morgenroth <morgenroth@ibr.cs.tu-bs.de>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21 
22 #ifndef BUNDLESTORAGE_H_
23 #define BUNDLESTORAGE_H_
24 
25 #include <storage/BundleSeeker.h>
26 #include <storage/BundleResult.h>
27 #include <storage/BundleIndex.h>
28 #include <ibrdtn/data/Bundle.h>
29 #include <ibrdtn/data/BundleID.h>
30 #include <ibrdtn/data/MetaBundle.h>
33 #include <ibrcommon/thread/Mutex.h>
34 
35 #include <stdexcept>
36 #include <iterator>
37 #include <set>
38 
39 namespace dtn
40 {
41  namespace storage
42  {
43  class BundleStorage : public BundleSeeker
44  {
45  public:
47  {
48  public:
49  BundleLoadException(string what = "Error while loading bundle data.") throw() : NoBundleFoundException(what)
50  {
51  };
52  };
53 
55  {
56  public:
57  StorageSizeExeededException(string what = "No space left in the storage.") throw() : ibrcommon::Exception(what)
58  {
59  };
60  };
61 
65  virtual ~BundleStorage() = 0;
66 
71  virtual void store(const dtn::data::Bundle &bundle) = 0;
72 
78  virtual dtn::data::Bundle get(const dtn::data::BundleID &id) = 0;
79 
83  virtual void get(const BundleSelector &cb, BundleResult &result) throw (NoBundleFoundException, BundleSelectorException) = 0;
84 
88  virtual const eid_set getDistinctDestinations() = 0;
89 
95  virtual void remove(const dtn::data::BundleID &id) = 0;
96 
102  void remove(const dtn::data::Bundle &b);
103 
109  virtual dtn::data::MetaBundle remove(const ibrcommon::BloomFilter &filter);
110 
114  virtual void clear() {};
115 
119  virtual bool empty() { return true; };
120 
124  virtual dtn::data::Size count() { return 0; };
125 
129  dtn::data::Length size() const;
130 
136  virtual void releaseCustody(const dtn::data::EID &custodian, const dtn::data::BundleID &id) = 0;
137 
145 
152 
156  void attach(dtn::storage::BundleIndex *index);
157 
161  void detach(dtn::storage::BundleIndex *index);
162 
163  /*** BEGIN: methods for unit-testing ***/
164 
168  virtual void wait() { };
169 
174  virtual void setFaulty(bool mode) { _faulty = mode; };
175 
176  /*** END: methods for unit-testing ***/
177 
178  protected:
182  BundleStorage(const dtn::data::Length &maxsize);
183 
184  void allocSpace(const dtn::data::Length &size) throw (StorageSizeExeededException);
185  void freeSpace(const dtn::data::Length &size) throw ();
186  void clearSpace() throw ();
187 
188  void eventBundleAdded(const dtn::data::MetaBundle &b) throw ();
189  void eventBundleRemoved(const dtn::data::BundleID &id) throw ();
190 
191  bool _faulty;
192 
193  private:
194  ibrcommon::Mutex _sizelock;
195  const dtn::data::Length _maxsize;
196  dtn::data::Length _currentsize;
197 
198  ibrcommon::Mutex _index_lock;
199  typedef std::set<dtn::storage::BundleIndex*> index_list;
200  index_list _indexes;
201  };
202  }
203 }
204 
205 #endif /* BUNDLESTORAGE_H_ */