IBR-DTNSuite  0.12
Registration.h
Go to the documentation of this file.
1 /*
2  * Registration.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 REGISTRATION_H_
23 #define REGISTRATION_H_
24 
25 #include "storage/BundleStorage.h"
26 #include "storage/BundleResult.h"
27 #include <ibrdtn/data/BundleID.h>
28 #include <ibrdtn/data/BundleSet.h>
29 #include <ibrcommon/thread/Queue.h>
30 #include <ibrcommon/thread/Mutex.h>
31 #include <ibrcommon/thread/Timer.h>
32 #include <string>
33 #include <set>
34 
35 namespace dtn
36 {
37  namespace api
38  {
40  {
41  static const std::string TAG;
42 
43  public:
45  {
50  };
51 
53  {
54  public:
55  RegistrationException(string what = "") throw() : Exception(what)
56  {
57  }
58  };
59 
61  {
62  public:
64  {
65  }
66  };
67 
69  {
70  public:
72  {
73  }
74  };
75 
77  {
78  public:
80  {
81  }
82  };
83 
87  Registration();
88 
93  Registration(const std::string &handle);
94 
98  virtual ~Registration();
99 
103  void notify(const NOTIFY_CALL);
104 
109  NOTIFY_CALL wait();
110 
114  void wait_for_bundle(size_t timeout = 0);
115 
119  void subscribe(const dtn::data::EID &endpoint);
120 
124  void unsubscribe(const dtn::data::EID &endpoint);
125 
131  bool hasSubscribed(const dtn::data::EID &endpoint);
132 
136  const std::set<dtn::data::EID> getSubscriptions();
137 
141  bool operator==(const std::string&) const;
142 
146  bool operator==(const Registration&) const;
147 
151  bool operator<(const Registration&) const;
152 
159  dtn::data::Bundle receive() throw (dtn::storage::NoBundleFoundException);
160 
161  dtn::data::MetaBundle receiveMetaBundle() throw (dtn::storage::NoBundleFoundException);
162 
167  void delivered(const dtn::data::MetaBundle &m) const;
168 
173  const dtn::data::EID& getDefaultEID() const;
174 
179  const std::string& getHandle() const;
180 
184  void abort();
185 
190  void reset();
191 
196  void setPersistent(ibrcommon::Timer::time_t lifetime);
197 
201  void unsetPersistent();
202 
208  bool isPersistent();
209 
214  bool isPersistent() const;
215 
219  void setFilterFragments(bool val);
220 
227  ibrcommon::Timer::time_t getExpireTime() const;
228 
233  void attach();
234 
238  void detach();
239 
243  static void processIncomingBundle(const dtn::data::EID &source, dtn::data::Bundle &bundle);
244 
245  protected:
246  void underflow();
247 
248  private:
249  class RegistrationQueue : public dtn::storage::BundleResult {
250  public:
254  RegistrationQueue();
255 
259  virtual ~RegistrationQueue();
260 
266  virtual void put(const dtn::data::MetaBundle &bundle) throw ();
267 
273  dtn::data::MetaBundle pop() throw (const ibrcommon::QueueUnblockedException);
274 
278  void expire(const dtn::data::Timestamp &timestamp) throw ();
279 
283  void abort() throw ();
284 
288  void reset() throw ();
289 
293  bool has(const dtn::data::BundleID &bundle) const throw ();
294 
295  private:
296  // protect variables against concurrent altering
297  ibrcommon::Mutex _lock;
298 
299  // all bundles have to remain in this set to avoid duplicate delivery
300  dtn::data::BundleSet _recv_bundles;
301 
302  // queue where the currently queued bundles are stored
303  ibrcommon::Queue<dtn::data::MetaBundle> _queue;
304  };
305 
306  const std::string _handle;
307  dtn::data::EID _default_eid;
308 
309  ibrcommon::Mutex _endpoints_lock;
310  std::set<dtn::data::EID> _endpoints;
311  RegistrationQueue _queue;
312 
313  ibrcommon::Mutex _receive_lock;
314  ibrcommon::Conditional _wait_for_cond;
315  bool _no_more_bundles;
316 
317  ibrcommon::Queue<NOTIFY_CALL> _notify_queue;
318 
319  static const std::string gen_handle();
320  static const std::string& alloc_handle();
321  static const std::string& alloc_handle(const std::string &handle);
322  static void free_handle(const std::string &handle);
323 
324  static ibrcommon::Mutex _handle_lock;
325  static std::set<std::string> _handles;
326 
327  bool _persistent;
328  bool _detached;
329  ibrcommon::Mutex _attach_lock;
330  ibrcommon::Timer::time_t _expiry;
331 
332  bool _filter_fragments;
333  };
334  }
335 }
336 
337 #endif /* REGISTRATION_H_ */