IBR-DTNSuite  0.12
SimpleBundleStorage.h
Go to the documentation of this file.
1 /*
2  * SimpleBundleStorage.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 SIMPLEBUNDLESTORAGE_H_
23 #define SIMPLEBUNDLESTORAGE_H_
24 
25 #include "Component.h"
26 #include "core/BundleCore.h"
27 #include "storage/BundleStorage.h"
28 #include "core/Node.h"
29 #include "core/EventReceiver.h"
30 
31 #include "storage/DataStorage.h"
32 #include "storage/MetaStorage.h"
33 
37 
38 #include <ibrcommon/data/File.h>
39 #include <ibrdtn/data/Bundle.h>
40 #include <ibrdtn/data/BundleList.h>
41 #include <ibrcommon/thread/Queue.h>
42 
43 #include <set>
44 #include <map>
45 
46 namespace dtn
47 {
48  namespace storage
49  {
54  {
55  static const std::string TAG;
56 
57  public:
61  SimpleBundleStorage(const ibrcommon::File &workdir, const dtn::data::Length maxsize = 0, const unsigned int buffer_limit = 0);
62 
66  virtual ~SimpleBundleStorage();
67 
72  virtual void store(const dtn::data::Bundle &bundle);
73 
78  virtual bool contains(const dtn::data::BundleID &id);
79 
84 
91  virtual dtn::data::Bundle get(const dtn::data::BundleID &id);
92 
96  virtual void get(const BundleSelector &cb, BundleResult &result) throw (NoBundleFoundException, BundleSelectorException);
97 
101  virtual const eid_set getDistinctDestinations();
102 
108  void remove(const dtn::data::BundleID &id);
109 
113  void clear();
114 
118  bool empty();
119 
124 
128  void releaseCustody(const dtn::data::EID &custodian, const dtn::data::BundleID &id);
129 
134  void raiseEvent(const dtn::core::Event *evt) throw ();
135 
139  virtual const std::string getName() const;
140 
146 
147  /*** BEGIN: methods for unit-testing ***/
148 
152  virtual void wait();
153 
158  virtual void setFaulty(bool mode);
159 
160  /*** END: methods for unit-testing ***/
161 
162  protected:
163  virtual void componentUp() throw ();
164  virtual void componentDown() throw ();
165  virtual void eventBundleExpired(const dtn::data::MetaBundle &b) throw ();
166 
167  private:
168  class BundleContainer : public DataStorage::Container
169  {
170  public:
171  BundleContainer(const dtn::data::Bundle &b);
172  virtual ~BundleContainer();
173 
177  static std::string createId(const dtn::data::BundleID &id);
178 
182  std::string getId() const;
183 
187  std::ostream& serialize(std::ostream &stream);
188 
189  private:
190  const dtn::data::Bundle _bundle;
191  };
192 
193  void __remove(const dtn::data::MetaBundle &meta);
194  void __store(const dtn::data::Bundle &bundle, const dtn::data::Length &bundle_size);
195 
196  typedef std::map<DataStorage::Hash, dtn::data::Bundle> pending_map;
197  ibrcommon::RWMutex _pending_lock;
198  pending_map _pending_bundles;
199 
200  // This object manages data stored on disk
201  DataStorage _datastore;
202 
203  // stores all the meta data in memory
204  ibrcommon::RWMutex _meta_lock;
205  MetaStorage _metastore;
206  };
207  }
208 }
209 
210 #endif /*SIMPLEBUNDLESTORAGE_H_*/