IBR-DTNSuite  0.10
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>
31 #include <ibrcommon/Exceptions.h>
33 #include <algorithm>
34 #include <map>
35 
36 namespace dtn
37 {
38  namespace routing
39  {
46  {
47  public:
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 
60  {
61  public:
62  NoRouteKnownException() : ibrcommon::Exception("No route known.") { };
63  virtual ~NoRouteKnownException() throw () { };
64  };
65 
67  {
68  public:
69  NoMoreTransfersAvailable() : ibrcommon::Exception("No more transfers allowed.") { };
70  virtual ~NoMoreTransfersAvailable() throw () { };
71  };
72 
74  {
75  public:
76  AlreadyInTransitException() : ibrcommon::Exception("This bundle is already in transit.") { };
77  virtual ~AlreadyInTransitException() throw () { };
78  };
79 
81  {
82  public:
83  NeighborNotAvailableException() : ibrcommon::Exception("Entry for this neighbor not found.") { };
84  virtual ~NeighborNotAvailableException() throw () { };
85  };
86 
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 
153  const dtn::data::Timestamp& getLastUpdate() const;
154 
158  void touch();
159 
163  template <class T>
164  const T& getDataset() const throw (DatasetNotAvailableException)
165  {
166  NeighborDataset item(T::identifier);
167  data_set::const_iterator iter = _datasets.find(item);
168 
169  if (iter == _datasets.end()) throw DatasetNotAvailableException();
170 
171  try {
172  return dynamic_cast<const T&>(**iter);
173  } catch (const std::bad_cast&) {
174  throw DatasetNotAvailableException();
175  }
176  }
177 
182  void putDataset(NeighborDataset &dset);
183 
187  template <class T>
189  {
190  NeighborDataset item(T::identifier);
191  data_set::iterator it = _datasets.find(item);
192 
193  if (it == _datasets.end()) return;
194 
195  _datasets.erase(it);
196  }
197 
198  private:
199  // stores bundle currently in transit
200  ibrcommon::Mutex _transit_lock;
201  std::set<dtn::data::BundleID> _transit_bundles;
202 
203  // bloomfilter used as summary vector
204  ibrcommon::BloomFilter _filter;
205  dtn::data::BundleSet _summary;
206  dtn::data::Timestamp _filter_expire;
207 
208  // extended neighbor data
209  typedef std::set<NeighborDataset> data_set;
210  data_set _datasets;
211 
212  enum FILTER_REQUEST_STATE
213  {
214  FILTER_AWAITING = 0,
215  FILTER_AVAILABLE = 1,
216  FILTER_EXPIRED = 2,
217  FILTER_FINAL = 3
218  };
219 
221 
222  dtn::data::Timestamp _last_update;
223  };
224 
226  virtual ~NeighborDatabase();
227 
234  NeighborDatabase::NeighborEntry& get(const dtn::data::EID &eid) throw (NeighborNotAvailableException);
235 
242  NeighborDatabase::NeighborEntry& create(const dtn::data::EID &eid) throw ();
243 
248  void remove(const dtn::data::EID &eid);
249 
254  void expire(const dtn::data::Timestamp &timestamp);
255 
256  private:
257  typedef std::map<dtn::data::EID, NeighborDatabase::NeighborEntry* > neighbor_map;
258  neighbor_map _entries;
259  };
260  }
261 }
262 
263 #endif /* NEIGHBORDATABASE_H_ */