How to find the callback function?

36 views
Skip to first unread message

xinledu18...@gmail.com

unread,
May 3, 2019, 4:57:49 AM5/3/19
to ns-3-users
Hello everyone.

I can find much Callback functions in the source code, and I want to find where to call it.

Such in src/traffic-control/traffic-control-layer.cc, in TrafficControlLayer::Receive metherd, we can see that:

void
TrafficControlLayer::Receive (Ptr<NetDevice> device, Ptr<const Packet> p,
uint16_t protocol, const Address &from, const Address &to,
NetDevice::PacketType packetType)
{
NS_LOG_FUNCTION (this << device << p << protocol << from << to << packetType);

bool found = false;

for (ProtocolHandlerList::iterator i = m_handlers.begin ();
i != m_handlers.end (); i++)
{
if (i->device == 0
|| (i->device != 0 && i->device == device))
{
if (i->protocol == 0
|| i->protocol == protocol)
{
NS_LOG_DEBUG ("Found handler for packet " << p << ", protocol " <<
protocol << " and NetDevice " << device <<
". Send packet up");
i->handler (device, p, protocol, from, to, packetType);//this is the callback function!!
found = true;
}
}
}

NS_ABORT_MSG_IF (!found, "Handler for protocol " << p << " and device " << device <<
" not found. It isn't forwarded up; it dies here.");
}

We can find the source code of i->hander:
/**
* A protocol handler
*
* \param device a pointer to the net device which received the packet
* \param packet the packet received
* \param protocol the 16 bit protocol number associated with this packet.
* This protocol number is expected to be the same protocol number
* given to the Send method by the user on the sender side.
* \param sender the address of the sender
* \param receiver the address of the receiver; Note: this value is
* only valid for promiscuous mode protocol
* handlers. Note: If the L2 protocol does not use L2
* addresses, the address reported here is the value of
* device->GetAddress().
* \param packetType type of packet received
* (broadcast/multicast/unicast/otherhost); Note:
* this value is only valid for promiscuous mode
* protocol handlers.
*/
typedef Callback<void,Ptr<NetDevice>, Ptr<const Packet>,uint16_t,const Address &,
const Address &, NetDevice::PacketType> ProtocolHandler;

I want to know the implement of the function which I call the handler callback function.
How do I find callback functions in general?

thank you.

pdbarnes

unread,
May 3, 2019, 11:25:27 AM5/3/19
to ns-3-users
Please read the Manual chapter on Callbacks, which explains how to read the typedef to declare your handler.

https://www.nsnam.org/docs/manual/html/callbacks.html
Peter

Reply all
Reply to author
Forward
0 new messages