IBR-DTNSuite  0.8
ibrcommon/ibrcommon/net/LinkManager.h
Go to the documentation of this file.
00001 /*
00002  * LinkManager.h
00003  *
00004  *  Created on: 21.12.2010
00005  *      Author: morgenro
00006  */
00007 
00008 #ifndef LINKMANAGER_H_
00009 #define LINKMANAGER_H_
00010 
00011 #include "ibrcommon/net/vinterface.h"
00012 #include "ibrcommon/net/vaddress.h"
00013 #include "ibrcommon/thread/Mutex.h"
00014 #include <string>
00015 #include <map>
00016 #include <set>
00017 
00018 namespace ibrcommon
00019 {
00020         class LinkManagerEvent
00021         {
00022         public:
00023                 enum EventType
00024                 {
00025                         EVENT_UNKOWN,
00026                         EVENT_LINK_STATE,
00027                         EVENT_ADDRESS_REMOVED,
00028                         EVENT_ADDRESS_ADDED
00029                 };
00030 
00031                 virtual ~LinkManagerEvent() {};
00032 
00033                 virtual const ibrcommon::vinterface& getInterface() const = 0;
00034                 virtual const ibrcommon::vaddress& getAddress() const = 0;
00035                 virtual unsigned int getState() const = 0;
00036                 virtual EventType getType() const = 0;
00037         };
00038 
00039         class LinkManager
00040         {
00041         public:
00042                 class EventCallback
00043                 {
00044                 public:
00045                         virtual ~EventCallback() {};
00046                         virtual void eventNotify(const LinkManagerEvent&) = 0;
00047                 };
00048 
00049                 class ExternalLinkManagerEvent : public LinkManagerEvent
00050                 {
00051                 public:
00052                         ExternalLinkManagerEvent(LinkManagerEvent::EventType type, const ibrcommon::vinterface &iface, const ibrcommon::vaddress &addr, unsigned int state = 0);
00053                         virtual ~ExternalLinkManagerEvent();
00054 
00055                         virtual const ibrcommon::vinterface& getInterface() const;
00056                         virtual const ibrcommon::vaddress& getAddress() const;
00057                         virtual unsigned int getState() const;
00058                         virtual LinkManagerEvent::EventType getType() const;
00059 
00060                 private:
00061                         LinkManagerEvent::EventType _type;
00062                         const ibrcommon::vinterface _iface;
00063                         const ibrcommon::vaddress _addr;
00064                         unsigned int _state;
00065                 };
00066 
00067                 virtual ~LinkManager() {};
00068 
00069                 virtual const std::string getInterface(int index) const = 0;
00070                 virtual const std::list<vaddress> getAddressList(const vinterface &iface, const vaddress::Family f) = 0;
00071 
00072                 virtual void registerInterfaceEvent(const vinterface&, LinkManager::EventCallback*);
00073                 virtual void unregisterInterfaceEvent(const vinterface&, LinkManager::EventCallback*);
00074                 virtual void unregisterAllEvents(LinkManager::EventCallback*);
00075 
00076                 void addressAdded(const ibrcommon::vinterface &iface, const ibrcommon::vaddress &addr);
00077                 void addressRemoved(const ibrcommon::vinterface &iface, const ibrcommon::vaddress &addr);
00078 
00079                 static LinkManager& getInstance();
00080                 static void initialize();
00081 
00082         protected:
00083                 void raiseEvent(const LinkManagerEvent &lme);
00084 
00085                 ibrcommon::Mutex _listener_mutex;
00086                 std::map<ibrcommon::vinterface, std::set<LinkManager::EventCallback* > > _listener;
00087         };
00088 
00089         class DefaultLinkManager : public LinkManager
00090         {
00091         public:
00092                 DefaultLinkManager();
00093                 virtual ~DefaultLinkManager();
00094 
00095                 const std::string getInterface(int index) const;
00096                 const std::list<vaddress> getAddressList(const vinterface &iface, const vaddress::Family f);
00097         };
00098 }
00099 
00100 #endif /* LINKMANAGER_H_ */