bug 1091: replace virtual method with callbacks.
authorMathieu Lacage <mathieu.lacage@gmail.com>
Wed, 06 Jul 2011 18:51:12 -0400
changeset 7352 e440347eff27
parent 7351 3c68eb316f5f
child 7353 09fccf6195ea
bug 1091: replace virtual method with callbacks.
src/network/model/node.cc
src/network/model/node.h
--- a/src/network/model/node.cc	Tue Jul 05 16:53:34 2011 -0700
+++ b/src/network/model/node.cc	Wed Jul 06 18:51:12 2011 -0400
@@ -154,6 +154,7 @@
 void 
 Node::DoDispose ()
 {
+  m_deviceAdditionListeners.clear ();
   m_handlers.clear ();
   for (std::vector<Ptr<NetDevice> >::iterator i = m_devices.begin ();
        i != m_devices.end (); i++)
@@ -192,10 +193,6 @@
   Object::DoStart ();
 }
 
-void 
-Node::NotifyDeviceAdded (Ptr<NetDevice> device)
-{}
-
 void
 Node::RegisterProtocolHandler (ProtocolHandler handler, 
                                uint16_t protocolType,
@@ -298,5 +295,40 @@
     }
   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<Ptr<NetDevice> >::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<NetDevice> device)
+{
+  for (DeviceAdditionListenerList::iterator i = m_deviceAdditionListeners.begin ();
+       i != m_deviceAdditionListeners.end (); i++)
+    {
+      (*i) (device);
+    }  
+}
+ 
 
 } //namespace ns3
--- a/src/network/model/node.h	Tue Jul 05 16:53:34 2011 -0700
+++ b/src/network/model/node.h	Wed Jul 06 18:51:12 2011 -0400
@@ -174,6 +174,27 @@
    */
   void UnregisterProtocolHandler (ProtocolHandler handler);
 
+  /**
+   * A callback invoked whenever a device is added to a node.
+   */
+  typedef Callback<void,Ptr<NetDevice> > 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 @@
   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<NetDevice> device);
+  void NotifyDeviceAdded (Ptr<NetDevice> device);
   bool NonPromiscReceiveFromDevice (Ptr<NetDevice> device, Ptr<const Packet>, uint16_t protocol, const Address &from);
   bool PromiscReceiveFromDevice (Ptr<NetDevice> device, Ptr<const Packet>, uint16_t protocol,
                                  const Address &from, const Address &to, NetDevice::PacketType packetType);
@@ -212,12 +227,14 @@
     bool promiscuous;
   };
   typedef std::vector<struct Node::ProtocolHandlerEntry> ProtocolHandlerList;
+  typedef std::vector<DeviceAdditionListener> DeviceAdditionListenerList;
+
   uint32_t    m_id;         // Node id for this node
   uint32_t    m_sid;        // System id for this node
   std::vector<Ptr<NetDevice> > m_devices;
   std::vector<Ptr<Application> > m_applications;
   ProtocolHandlerList m_handlers;
-
+  DeviceAdditionListenerList m_deviceAdditionListeners;
 };
 
 } //namespace ns3