From 7fc5fc50f2d9410cdd94b48745821a6dbb6705ac Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Wed, 6 Jul 2011 18:51:12 -0400 Subject: [PATCH] bug 1091: replace virtual method with callbacks. --- src/network/model/node.cc | 40 +++++++++++++++++++++++++++++++++++---- src/network/model/node.h | 33 ++++++++++++++++++++++++-------- 2 files changed, 61 insertions(+), 12 deletions(-) diff --git a/src/network/model/node.cc b/src/network/model/node.cc index 2aeb676a6..19244390d 100644 --- a/src/network/model/node.cc +++ b/src/network/model/node.cc @@ -154,6 +154,7 @@ Node::GetNApplications (void) const void Node::DoDispose () { + m_deviceAdditionListeners.clear (); m_handlers.clear (); for (std::vector >::iterator i = m_devices.begin (); i != m_devices.end (); i++) @@ -192,10 +193,6 @@ Node::DoStart (void) Object::DoStart (); } -void -Node::NotifyDeviceAdded (Ptr device) -{} - void Node::RegisterProtocolHandler (ProtocolHandler handler, uint16_t protocolType, @@ -298,5 +295,40 @@ Node::ReceiveFromDevice (Ptr device, Ptr packet, uint16 } return found; } +void +Node::RegisterDeviceAdditionListener (DeviceAdditionListener listener) +{ + m_deviceAdditionListeners.push_back (listener); + // and, then, notify the new listener about all existing devices. + for (std::vector >::const_iterator i = m_devices.begin (); + i != m_devices.end (); ++i) + { + listener (*i); + } +} +void +Node::UnregisterDeviceAdditionListener (DeviceAdditionListener listener) +{ + for (DeviceAdditionListenerList::iterator i = m_deviceAdditionListeners.begin (); + i != m_deviceAdditionListeners.end (); i++) + { + if ((*i).IsEqual (listener)) + { + m_deviceAdditionListeners.erase (i); + break; + } + } +} + +void +Node::NotifyDeviceAdded (Ptr device) +{ + for (DeviceAdditionListenerList::iterator i = m_deviceAdditionListeners.begin (); + i != m_deviceAdditionListeners.end (); i++) + { + (*i) (device); + } +} + } //namespace ns3 diff --git a/src/network/model/node.h b/src/network/model/node.h index 5b3794531..a67d70487 100644 --- a/src/network/model/node.h +++ b/src/network/model/node.h @@ -174,6 +174,27 @@ public: */ void UnregisterProtocolHandler (ProtocolHandler handler); + /** + * A callback invoked whenever a device is added to a node. + */ + typedef Callback > DeviceAdditionListener; + /** + * \param listener the listener to add + * + * Add a new listener to the list of listeners for the device-added + * event. When a new listener is added, it is notified of the existance + * of all already-added devices to make discovery of devices easier. + */ + void RegisterDeviceAdditionListener (DeviceAdditionListener listener); + /** + * \param listener the listener to remove + * + * Remove an existing listener from the list of listeners for the + * device-added event. + */ + void UnregisterDeviceAdditionListener (DeviceAdditionListener listener); + + /** * \returns true if checksums are enabled, false otherwise. @@ -190,13 +211,7 @@ protected: virtual void DoDispose (void); virtual void DoStart (void); private: - - /** - * \param device the device added to this Node. - * - * This method is invoked whenever a user calls Node::AddDevice. - */ - virtual void NotifyDeviceAdded (Ptr device); + void NotifyDeviceAdded (Ptr device); bool NonPromiscReceiveFromDevice (Ptr device, Ptr, uint16_t protocol, const Address &from); bool PromiscReceiveFromDevice (Ptr device, Ptr, uint16_t protocol, const Address &from, const Address &to, NetDevice::PacketType packetType); @@ -212,12 +227,14 @@ private: bool promiscuous; }; typedef std::vector ProtocolHandlerList; + typedef std::vector DeviceAdditionListenerList; + uint32_t m_id; // Node id for this node uint32_t m_sid; // System id for this node std::vector > m_devices; std::vector > m_applications; ProtocolHandlerList m_handlers; - + DeviceAdditionListenerList m_deviceAdditionListeners; }; } //namespace ns3