bug 1091: replace virtual method with callbacks.

This commit is contained in:
Mathieu Lacage
2011-07-06 18:51:12 -04:00
parent f91c663c93
commit 7fc5fc50f2
2 changed files with 61 additions and 12 deletions

View File

@@ -154,6 +154,7 @@ Node::GetNApplications (void) const
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 @@ Node::DoStart (void)
Object::DoStart ();
}
void
Node::NotifyDeviceAdded (Ptr<NetDevice> device)
{}
void
Node::RegisterProtocolHandler (ProtocolHandler handler,
uint16_t protocolType,
@@ -298,5 +295,40 @@ Node::ReceiveFromDevice (Ptr<NetDevice> device, Ptr<const Packet> 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<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

View File

@@ -174,6 +174,27 @@ public:
*/
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 @@ 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<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 @@ private:
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