IBR-DTNSuite  0.8
daemon/src/api/Registration.h
Go to the documentation of this file.
00001 /*
00002  * Registration.h
00003  *
00004  *  Created on: 15.06.2011
00005  *      Author: morgenro
00006  */
00007 
00008 #ifndef REGISTRATION_H_
00009 #define REGISTRATION_H_
00010 
00011 #include "storage/BundleStorage.h"
00012 #include <ibrdtn/data/BundleID.h>
00013 #include <ibrdtn/data/BundleList.h>
00014 #include <ibrcommon/thread/Queue.h>
00015 #include <ibrcommon/thread/Mutex.h>
00016 #include <ibrcommon/thread/Timer.h>
00017 #include <string>
00018 #include <set>
00019 
00020 namespace dtn
00021 {
00022         namespace api
00023         {
00024                 class Registration
00025                 {
00026                 public:
00027                         enum NOTIFY_CALL
00028                         {
00029                                 NOTIFY_BUNDLE_AVAILABLE = 0,
00030                                 NOTIFY_NEIGHBOR_AVAILABLE = 1,
00031                                 NOTIFY_NEIGHBOR_UNAVAILABLE = 2,
00032                                 NOTIFY_SHUTDOWN = 3
00033                         };
00034 
00035                         class RegistrationException : public ibrcommon::Exception
00036                         {
00037                         public:
00038                                 RegistrationException(string what = "") throw() : Exception(what)
00039                                 {
00040                                 }
00041                         };
00042 
00043                         class AlreadyAttachedException : public RegistrationException
00044                         {
00045                         public:
00046                                 AlreadyAttachedException(string what = "") throw() : RegistrationException(what)
00047                                 {
00048                                 }
00049                         };
00050 
00051                         class NotFoundException : public RegistrationException
00052                         {
00053                         public:
00054                                 NotFoundException(string what = "") throw() : RegistrationException(what)
00055                                 {
00056                                 }
00057                         };
00058 
00059                         class NotPersistentException : public RegistrationException
00060                         {
00061                         public:
00062                                 NotPersistentException(string what = "") throw() : RegistrationException(what)
00063                                 {
00064                                 }
00065                         };
00066 
00070                         Registration();
00071 
00075                         virtual ~Registration();
00076 
00080                         void notify(const NOTIFY_CALL);
00081 
00086                         NOTIFY_CALL wait();
00087 
00091                         void wait_for_bundle(size_t timeout = 0);
00092 
00096                         void subscribe(const dtn::data::EID &endpoint);
00097 
00101                         void unsubscribe(const dtn::data::EID &endpoint);
00102 
00108                         bool hasSubscribed(const dtn::data::EID &endpoint);
00109 
00113                         const std::set<dtn::data::EID> getSubscriptions();
00114 
00118                         bool operator==(const std::string&) const;
00119 
00123                         bool operator==(const Registration&) const;
00124 
00128                         bool operator<(const Registration&) const;
00129 
00136                         dtn::data::Bundle receive() throw (dtn::storage::BundleStorage::NoBundleFoundException);
00137 
00138                         dtn::data::MetaBundle receiveMetaBundle() throw (dtn::storage::BundleStorage::NoBundleFoundException);
00139 
00144                         void delivered(const dtn::data::MetaBundle &m);
00145 
00150                         const dtn::data::EID& getDefaultEID() const;
00151 
00156                         const std::string& getHandle() const;
00157 
00161                         void abort();
00162 
00167                         void reset();
00168 
00173                         void setPersistent(ibrcommon::Timer::time_t lifetime);
00174 
00178                         void unsetPersistent();
00179 
00185                         bool isPersistent();
00186 
00191                         bool isPersistent() const;
00192 
00199                         ibrcommon::Timer::time_t getExpireTime() const;
00200 
00205                         void attach();
00206 
00210                         void detach();
00211 
00212                 protected:
00213                         void underflow();
00214 
00215                 private:
00216                         ibrcommon::Queue<dtn::data::MetaBundle> _queue;
00217                         const std::string _handle;
00218                         const dtn::data::EID _default_eid;
00219 
00220                         ibrcommon::Mutex _endpoints_lock;
00221                         std::set<dtn::data::EID> _endpoints;
00222                         dtn::data::BundleList _received_bundles;
00223 
00224                         ibrcommon::Mutex _receive_lock;
00225                         ibrcommon::Conditional _wait_for_cond;
00226                         bool _no_more_bundles;
00227 
00228                         ibrcommon::Queue<NOTIFY_CALL> _notify_queue;
00229 
00230                         static const std::string alloc_handle();
00231                         static void free_handle(const std::string &handle);
00232 
00233                         static std::set<std::string> _handles;
00234 
00235                         bool _persistent;
00236                         bool _detached;
00237                         ibrcommon::Mutex _attach_lock;
00238                         ibrcommon::Timer::time_t _expiry;
00239                 };
00240         }
00241 }
00242 
00243 #endif /* REGISTRATION_H_ */