IBR-DTNSuite  0.12
DataStorage.h
Go to the documentation of this file.
1 /*
2  * DataStorage.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 #include <iostream>
23 #include <fstream>
24 #include <string>
25 #include <ibrdtn/data/BundleID.h>
26 #include <ibrcommon/data/File.h>
27 #include <ibrcommon/thread/Mutex.h>
29 #include <ibrcommon/thread/Queue.h>
32 #include <memory>
33 
34 #ifndef DATASTORAGE_H_
35 #define DATASTORAGE_H_
36 
37 namespace dtn
38 {
39  namespace storage
40  {
42  {
43  public:
45  {
46  public:
47  DataNotAvailableException(string what = "Requested data is not available.") throw() : Exception(what)
48  { };
49  };
50 
51  class Container
52  {
53  public:
54  virtual ~Container() = 0;
55  virtual std::string getId() const = 0;
56  virtual std::ostream& serialize(std::ostream &stream) = 0;
57  };
58 
59  class Hash
60  {
61  public:
62  Hash();
63  Hash(const std::string &value);
64  Hash(const DataStorage::Container &container);
65  Hash(const ibrcommon::File &file);
66  virtual ~Hash();
67 
68  bool operator!=(const Hash &other) const;
69  bool operator==(const Hash &other) const;
70  bool operator<(const Hash &other) const;
71 
72  std::string value;
73  };
74 
75  class istream : public ibrcommon::File
76  {
77  public:
78  istream(ibrcommon::Mutex &mutex, const ibrcommon::File &file);
79  virtual ~istream();
80  std::istream& operator*();
81 
82  private:
83  std::ifstream *_stream;
84  ibrcommon::Mutex &_lock;
85  };
86 
87  class Callback
88  {
89  public:
90  virtual void eventDataStorageStored(const Hash &hash) = 0;
91  virtual void eventDataStorageStoreFailed(const Hash &hash, const ibrcommon::Exception&) = 0;
92  virtual void eventDataStorageRemoved(const Hash &hash) = 0;
93  virtual void eventDataStorageRemoveFailed(const Hash &hash, const ibrcommon::Exception&) = 0;
94  virtual void iterateDataStorage(const Hash &hash, DataStorage::istream &stream) = 0;
95  };
96 
97  DataStorage(Callback &callback, const ibrcommon::File &path, unsigned int write_buffer = 0, bool initialize = false);
98  virtual ~DataStorage();
99 
100  const Hash store(Container *data);
101  void store(const Hash &hash, Container *data);
102 
104  void remove(const Hash &hash);
105 
109  void wait();
110 
114  void iterateAll();
115 
119  void reset();
120 
121  /*** BEGIN: methods for unit-testing ***/
122 
127  void setFaulty(bool mode);
128 
129  /*** END: methods for unit-testing ***/
130 
131  protected:
132  void run() throw ();
133  void __cancellation() throw ();
134 
135  private:
136  class Task
137  {
138  public:
139  virtual ~Task() = 0;
140  };
141 
142  class StoreDataTask : public Task
143  {
144  public:
145  StoreDataTask(const Hash &h, Container *c);
146  virtual ~StoreDataTask();
147 
148  const Hash hash;
149  std::auto_ptr<Container> _container;
150  };
151 
152  class RemoveDataTask : public Task
153  {
154  public:
155  RemoveDataTask(const Hash &h);
156  virtual ~RemoveDataTask();
157 
158  const Hash hash;
159  };
160 
161  Callback &_callback;
162  ibrcommon::File _path;
164  ibrcommon::Semaphore _store_sem;
165  bool _store_limited;
166  bool _faulty;
167 
168  ibrcommon::Mutex _global_mutex;
169  };
170  }
171 }
172 
173 #endif /* DATASTORAGE_H_ */