IBR-DTNSuite  0.8
daemon/src/routing/BaseRouter.h
Go to the documentation of this file.
00001 /*
00002  * BaseRouter.h
00003  *
00004  *  Created on: 15.02.2010
00005  *      Author: morgenro
00006  */
00007 
00008 #ifndef BASEROUTER_H_
00009 #define BASEROUTER_H_
00010 
00011 #include "Component.h"
00012 #include "routing/NeighborDatabase.h"
00013 #include "routing/BundleSummary.h"
00014 #include "routing/NodeHandshake.h"
00015 #include "core/EventReceiver.h"
00016 #include "storage/BundleStorage.h"
00017 
00018 #include <ibrdtn/data/DTNTime.h>
00019 #include <ibrdtn/data/BundleID.h>
00020 #include <ibrdtn/data/MetaBundle.h>
00021 
00022 #include <ibrcommon/thread/Thread.h>
00023 #include <ibrcommon/thread/Conditional.h>
00024 
00025 
00026 namespace dtn
00027 {
00028         namespace routing
00029         {
00030                 class NodeHandshakeExtension;
00031 
00032                 class BaseRouter : public dtn::core::EventReceiver, public dtn::daemon::IntegratedComponent
00033                 {
00034                 public:
00035                         class RoutingException : public ibrcommon::Exception
00036                         {
00037                                 public:
00038                                         RoutingException(string what = "An error occured during routing.") throw() : Exception(what)
00039                                         {
00040                                         };
00041                         };
00042 
00046                         class NoNeighbourFoundException : public RoutingException
00047                         {
00048                                 public:
00049                                         NoNeighbourFoundException(string what = "No neighbour was found.") throw() : RoutingException(what)
00050                                         {
00051                                         };
00052                         };
00053 
00057                         class NoRouteFoundException : public RoutingException
00058                         {
00059                                 public:
00060                                         NoRouteFoundException(string what = "No route found.") throw() : RoutingException(what)
00061                                         {
00062                                         };
00063                         };
00064 
00065                         class Extension
00066                         {
00067                         public:
00068                                 Extension();
00069                                 virtual ~Extension() = 0;
00070 
00071                                 virtual void notify(const dtn::core::Event *evt) = 0;
00072 
00073                                 enum CALLBACK_ACTION
00074                                 {
00075                                         ROUTE_CALLBACK_FORWARDED = 0,
00076                                         ROUTE_CALLBACK_ABORTED = 1,
00077                                         ROUTE_CALLBACK_REJECTED = 2,
00078                                         ROUTE_CALLBACK_DELETED = 3
00079                                 };
00080 
00087                                 virtual void requestHandshake(const dtn::data::EID&, NodeHandshake&) const { };
00088 
00096                                 virtual void responseHandshake(const dtn::data::EID&, const NodeHandshake&, NodeHandshake&) { };
00097 
00103                                 virtual void processHandshake(const dtn::data::EID&, NodeHandshake&) { };
00104 
00111                                 void transferTo(const dtn::data::EID &destination, const dtn::data::BundleID &id);
00112                                 void transferTo(NeighborDatabase::NeighborEntry &entry, const dtn::data::BundleID &id);
00113 
00119                                 static bool isRouting(const dtn::data::EID &eid);
00120 
00121                         protected:
00122                                 BaseRouter& operator*();
00123 
00124                         private:
00125                                 friend class BaseRouter;
00126                                 static BaseRouter *_router;
00127                         };
00128 
00129                         class ThreadedExtension : public Extension, public ibrcommon::JoinableThread
00130                         {
00131                         public:
00132                                 ThreadedExtension();
00133                                 virtual ~ThreadedExtension() = 0;
00134 
00135                                 virtual void notify(const dtn::core::Event *evt) = 0;
00136                         };
00137 
00138                         class Endpoint
00139                         {
00140                         public:
00141                                 Endpoint();
00142                                 virtual ~Endpoint() = 0;
00143                         };
00144 
00145                 private:
00146                         class VirtualEndpoint
00147                         {
00148                         public:
00149                                 VirtualEndpoint(dtn::data::EID name);
00150                                 virtual ~VirtualEndpoint();
00151 
00152                                 ibrcommon::Mutex _clientlock;
00153                                 Endpoint *_client;
00154 
00155                                 dtn::data::EID _name;
00156                                 dtn::data::DTNTime _lastseen;
00157                                 dtn::data::DTNTime _expire;
00158                         };
00159 
00160                 public:
00161                         BaseRouter(dtn::storage::BundleStorage &storage);
00162                         ~BaseRouter();
00163 
00168                         void addExtension(BaseRouter::Extension *extension);
00169 
00174                         const std::list<BaseRouter::Extension*>& getExtensions() const;
00175 
00179                         void raiseEvent(const dtn::core::Event *evt);
00180 
00186                         dtn::data::Bundle getBundle(const dtn::data::BundleID &id);
00187 
00188                         dtn::storage::BundleStorage &getStorage();
00189 
00194                         void doHandshake(const dtn::data::EID &eid);
00195 
00201                         bool isKnown(const dtn::data::BundleID &id);
00202 
00209                         bool filterKnown(const dtn::data::MetaBundle &meta);
00210 
00215                         void setKnown(const dtn::data::MetaBundle &meta);
00216 
00221                         const SummaryVector getSummaryVector();
00222 
00227                         const SummaryVector getPurgedBundles();
00228 
00233                         void addPurgedBundle(const dtn::data::MetaBundle &meta);
00234 
00238                         virtual const std::string getName() const;
00239 
00244                         NeighborDatabase& getNeighborDB();
00245 
00246                 protected:
00247                         virtual void componentUp();
00248                         virtual void componentDown();
00249 
00250                 private:
00251                         ibrcommon::Mutex _known_bundles_lock;
00252                         dtn::routing::BundleSummary _known_bundles;
00253 
00254                         ibrcommon::Mutex _purged_bundles_lock;
00255                         dtn::routing::BundleSummary _purged_bundles;
00256 
00257                         dtn::storage::BundleStorage &_storage;
00258                         std::list<BaseRouter::Extension*> _extensions;
00259 
00260                         NeighborDatabase _neighbor_database;
00261                         NodeHandshakeExtension *_nh_extension;
00262                 };
00263         }
00264 }
00265 
00266 
00267 #endif /* BASEROUTER_H_ */