Problem with callback when counting the received packet numbers

403 views
Skip to first unread message

th_...@qq.com

unread,
Jan 15, 2016, 8:14:13 AM1/15/16
to ns-3-users
Hi,
I am trying to count the received packet number by each node,so I tried callback functions,which is

void ReceivedPacket(Ptr<const Packet> packet ,const Address &)
{
NS_LOG_UNCOND("Received one Packet ");
pktcounter++;    //defined before
std::cout<<"packet number is :"<<pktcounter<<std::endl; 
and call it by 
Config::ConnectWithContext("NodeList/*/ApplicationList/*/$ns3::PacketSink/Rx",MakeCallback(&ReceivedPacket,this ) );

when I run the code,it can run successfully,but the output is packet numbers from all nodes,what can I do to get received packet number from each node?
Thanks
Mark 

Konstantinos

unread,
Jan 15, 2016, 8:32:36 AM1/15/16
to ns-3-users
Hi Mark,

The callback you have used is connected to ALL nodes (that's the meaning of star in the config path "NodeList/*/...")
This causes all nodes that receive a packet to fire the same callback (ReceivePacket).

The solution is to differentiate which node actually fires the callback and this can be easily done by changing the ConnectWithoutContext() with Connect().
You will also need to update the signature of the ReceivePacket function to take into account this change.

Config::Connect("NodeList/*/ApplicationList/*/$ns3::PacketSink/Rx",MakeCallback(&ReceivedPacket, this ) );

void ReceivedPacket(std::string context, Ptr<const Packet> packet ,const Address &)
{
   NS_LOG_UNCOND
(context);
   NS_LOG_UNCOND
("Received one Packet ");

   pktcounter
++;    //defined before
   std
::cout<<"packet number is :"<<pktcounter<<std::endl;
}

The context will be different for each node. Using the information from the context, you can then differentiate the Rx packets per node.
For that, one possible solution is to have a std::map with Key the node ID and value the packet received.

Regards,
K.

th_...@qq.com

unread,
Jan 17, 2016, 7:45:56 AM1/17/16
to ns-3-users
Hi  Konstantinos
Thanks very very much!!
I noticed that I had misunderstood the function,now I am clear.

Mark

在 2016年1月15日星期五 UTC+8下午9:32:36,Konstantinos写道:
Reply all
Reply to author
Forward
0 new messages