IBR-DTN  1.0.0
NeighborDatabase.h
Go to the documentation of this file.
1 /*
2  * NeighborDatabase.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 NEIGHBORDATABASE_H_
23 #define NEIGHBORDATABASE_H_
24 
26 #include <ibrdtn/data/BundleSet.h>
27 #include <ibrdtn/data/EID.h>
28 #include <ibrdtn/data/BundleID.h>
29 #include <ibrdtn/data/Number.h>
30 #include <ibrcommon/data/BloomFilter.h>
31 #include <ibrcommon/Exceptions.h>
32 #include <ibrcommon/thread/ThreadsafeState.h>
33 #include <algorithm>
34 #include <map>
35 
36 namespace dtn
37 {
38  namespace routing
39  {
45  class NeighborDatabase : public ibrcommon::Mutex
46  {
47  public:
48  class BloomfilterNotAvailableException : public ibrcommon::Exception
49  {
50  public:
52  : ibrcommon::Exception("Bloom filter is not available for this node."), eid(host) { };
53 
54  virtual ~BloomfilterNotAvailableException() throw () { };
55 
56  const dtn::data::EID eid;
57  };
58 
59  class NoRouteKnownException : public ibrcommon::Exception
60  {
61  public:
62  NoRouteKnownException() : ibrcommon::Exception("No route known.") { };
63  virtual ~NoRouteKnownException() throw () { };
64  };
65 
66  class NoMoreTransfersAvailable : public ibrcommon::Exception
67  {
68  public:
69  NoMoreTransfersAvailable() : ibrcommon::Exception("No more transfers allowed.") { };
70  virtual ~NoMoreTransfersAvailable() throw () { };
71  };
72 
73  class AlreadyInTransitException : public ibrcommon::Exception
74  {
75  public:
76  AlreadyInTransitException() : ibrcommon::Exception("This bundle is already in transit.") { };
77  virtual ~AlreadyInTransitException() throw () { };
78  };
79 
80  class EntryNotFoundException : public ibrcommon::Exception
81  {
82  public:
83  EntryNotFoundException() : ibrcommon::Exception("Entry for this neighbor not found.") { };
84  virtual ~EntryNotFoundException() throw () { };
85  };
86 
87  class DatasetNotAvailableException : public ibrcommon::Exception
88  {
89  public:
90  DatasetNotAvailableException() : ibrcommon::Exception("Dataset not found.") { };
91  virtual ~DatasetNotAvailableException() throw () { };
92  };
93 
95  {
96  public:
97  NeighborEntry();
99  virtual ~NeighborEntry();
100 
106  void update(const ibrcommon::BloomFilter &bf, const dtn::data::Number &lifetime = 0);
107 
108  void reset();
109 
110  void add(const dtn::data::MetaBundle&);
111 
112  bool has(const dtn::data::BundleID&, const bool require_bloomfilter = false) const;
113 
119 
124 
128  bool isTransferThresholdReached() const;
129 
134  void releaseTransfer(const dtn::data::BundleID &id);
135 
136  // the EID of the corresponding node
138 
143  void expire(const dtn::data::Timestamp &timestamp);
144 
148  bool isExpired(const dtn::data::Timestamp &timestamp) const;
149 
154  bool isFilterValid() const;
155 
159  const dtn::data::Timestamp& getLastUpdate() const;
160 
164  void touch();
165 
169  template <class T>
170  const T& getDataset() const throw (DatasetNotAvailableException)
171  {
172  NeighborDataset item(T::identifier);
173  data_set::const_iterator iter = _datasets.find(item);
174 
175  if (iter == _datasets.end()) throw DatasetNotAvailableException();
176 
177  try {
178  return dynamic_cast<const T&>(**iter);
179  } catch (const std::bad_cast&) {
180  throw DatasetNotAvailableException();
181  }
182  }
183 
188  void putDataset(NeighborDataset &dset);
189 
193  template <class T>
195  {
196  NeighborDataset item(T::identifier);
197  data_set::iterator it = _datasets.find(item);
198 
199  if (it == _datasets.end()) return;
200 
201  _datasets.erase(it);
202  }
203 
204  private:
205  // stores bundle currently in transit
206  std::set<dtn::data::BundleID> _transit_bundles;
207 
208  // bloomfilter used as summary vector
209  ibrcommon::BloomFilter _filter;
210  dtn::data::BundleSet _summary;
211  dtn::data::Timestamp _filter_expire;
212 
213  // extended neighbor data
214  typedef std::set<NeighborDataset> data_set;
215  data_set _datasets;
216 
217  enum FILTER_REQUEST_STATE
218  {
219  FILTER_AWAITING = 0,
220  FILTER_AVAILABLE = 1,
221  FILTER_EXPIRED = 2,
222  FILTER_FINAL = 3
223  };
224 
225  ibrcommon::ThreadsafeState<FILTER_REQUEST_STATE> _filter_state;
226 
227  dtn::data::Timestamp _last_update;
228  };
229 
231  virtual ~NeighborDatabase();
232 
240  NeighborDatabase::NeighborEntry& get(const dtn::data::EID &eid, bool noCached = false) throw (EntryNotFoundException);
241 
248  NeighborDatabase::NeighborEntry& create(const dtn::data::EID &eid) throw ();
249 
254  void remove(const dtn::data::EID &eid);
255 
260  void expire(const dtn::data::Timestamp &timestamp);
261 
262  private:
263  typedef std::map<dtn::data::EID, NeighborDatabase::NeighborEntry* > neighbor_map;
264  neighbor_map _entries;
265  };
266  }
267 }
268 
269 #endif /* NEIGHBORDATABASE_H_ */
bool isExpired(const dtn::data::Timestamp &timestamp) const
void add(const dtn::data::MetaBundle &)
const dtn::data::Timestamp & getLastUpdate() const
void expire(const dtn::data::Timestamp &timestamp)
void update(const ibrcommon::BloomFilter &bf, const dtn::data::Number &lifetime=0)
bool has(const dtn::data::BundleID &, const bool require_bloomfilter=false) const
void acquireTransfer(const dtn::data::BundleID &id)
size_t Size
Definition: Number.h:34
void expire(const dtn::data::Timestamp &timestamp)
NeighborDatabase::NeighborEntry & create(const dtn::data::EID &eid)
void releaseTransfer(const dtn::data::BundleID &id)