IBR-DTN  1.0.0
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 #include "core/TimeEvent.h"
31 
32 #include "storage/DataStorage.h"
33 #include "storage/MetaStorage.h"
34 
35 #include <ibrcommon/thread/Conditional.h>
36 #include <ibrcommon/thread/AtomicCounter.h>
37 #include <ibrcommon/thread/RWMutex.h>
38 
39 #include <ibrcommon/data/File.h>
40 #include <ibrdtn/data/Bundle.h>
41 #include <ibrdtn/data/BundleList.h>
42 #include <ibrcommon/thread/Queue.h>
43 
44 #include <set>
45 #include <map>
46 
47 namespace dtn
48 {
49  namespace storage
50  {
55  {
56  static const std::string TAG;
57 
58  public:
62  SimpleBundleStorage(const ibrcommon::File &workdir, const dtn::data::Length maxsize = 0, const unsigned int buffer_limit = 0);
63 
67  virtual ~SimpleBundleStorage();
68 
73  virtual void store(const dtn::data::Bundle &bundle);
74 
79  virtual bool contains(const dtn::data::BundleID &id);
80 
85 
92  virtual dtn::data::Bundle get(const dtn::data::BundleID &id);
93 
97  virtual void get(const BundleSelector &cb, BundleResult &result) throw (NoBundleFoundException, BundleSelectorException);
98 
102  virtual const eid_set getDistinctDestinations();
103 
109  void remove(const dtn::data::BundleID &id);
110 
114  void clear();
115 
119  bool empty();
120 
125 
129  void releaseCustody(const dtn::data::EID &custodian, const dtn::data::BundleID &id);
130 
135  void raiseEvent(const dtn::core::TimeEvent &evt) throw ();
136 
140  virtual const std::string getName() const;
141 
143  virtual void eventDataStorageStoreFailed(const dtn::storage::DataStorage::Hash &hash, const ibrcommon::Exception&);
145  virtual void eventDataStorageRemoveFailed(const dtn::storage::DataStorage::Hash &hash, const ibrcommon::Exception&);
147 
148  /*** BEGIN: methods for unit-testing ***/
149 
153  virtual void wait();
154 
159  virtual void setFaulty(bool mode);
160 
161  /*** END: methods for unit-testing ***/
162 
163  protected:
164  virtual void componentUp() throw ();
165  virtual void componentDown() throw ();
166  virtual void eventBundleExpired(const dtn::data::MetaBundle &b) throw ();
167 
168  private:
169  class BundleContainer : public DataStorage::Container
170  {
171  public:
172  BundleContainer(const dtn::data::Bundle &b);
173  virtual ~BundleContainer();
174 
178  static std::string createId(const dtn::data::BundleID &id);
179 
183  std::string getId() const;
184 
188  std::ostream& serialize(std::ostream &stream);
189 
190  private:
191  const dtn::data::Bundle _bundle;
192  };
193 
194  void __remove(const dtn::data::MetaBundle &meta);
195  void __store(const dtn::data::Bundle &bundle, const dtn::data::Length &bundle_size);
196 
197  typedef std::map<DataStorage::Hash, dtn::data::Bundle> pending_map;
198  ibrcommon::RWMutex _pending_lock;
199  pending_map _pending_bundles;
200 
201  // This object manages data stored on disk
202  DataStorage _datastore;
203 
204  // stores all the meta data in memory
205  ibrcommon::RWMutex _meta_lock;
206  MetaStorage _metastore;
207  };
208  }
209 }
210 
211 #endif /*SIMPLEBUNDLESTORAGE_H_*/
virtual void eventDataStorageStoreFailed(const dtn::storage::DataStorage::Hash &hash, const ibrcommon::Exception &)
virtual dtn::data::MetaBundle info(const dtn::data::BundleID &id)
virtual void eventDataStorageStored(const dtn::storage::DataStorage::Hash &hash)
std::set< dtn::data::EID > eid_set
Definition: BundleSeeker.h:39
size_t Length
Definition: Number.h:33
void releaseCustody(const dtn::data::EID &custodian, const dtn::data::BundleID &id)
virtual void eventDataStorageRemoved(const dtn::storage::DataStorage::Hash &hash)
void raiseEvent(const dtn::core::TimeEvent &evt)
virtual const eid_set getDistinctDestinations()
virtual bool contains(const dtn::data::BundleID &id)
virtual void eventBundleExpired(const dtn::data::MetaBundle &b)
SimpleBundleStorage(const ibrcommon::File &workdir, const dtn::data::Length maxsize=0, const unsigned int buffer_limit=0)
virtual const std::string getName() const
virtual void eventDataStorageRemoveFailed(const dtn::storage::DataStorage::Hash &hash, const ibrcommon::Exception &)
size_t Size
Definition: Number.h:34
virtual void iterateDataStorage(const dtn::storage::DataStorage::Hash &hash, dtn::storage::DataStorage::istream &stream)
virtual void store(const dtn::data::Bundle &bundle)