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