how to return values from a callback function ns3

444 views
Skip to first unread message

Wayne Matabar

unread,
Aug 18, 2016, 12:24:46 PM8/18/16
to ns-3-users
I am using the TxRxPointToPoint trace source to get the transmit times on the channel. I config::connect("/ChannelList/2/TxRxPointToPoint", MakeCallback(&ByteTrcae2)) where void ByteTrace2(std::string context, Ptr<NetDevice> nd1, Ptr<NetDevice> nd2, Time t1,Time t2) is a five argument trace function to receive the values. In this case a context, two pointers to netdevices and two Time objects. How do I make these values available to a class object created in the main like so:
Ptr<MyModel> model = CreateObject<MyModel>();? 

Konstantinos

unread,
Aug 18, 2016, 12:39:10 PM8/18/16
to ns-3-users
You make ByteTrace2() a member function of your model, so when it is called, you can use member variables of your model from that class.
To do that, you need to add another parameter in your config::connect() and that's the pointer to your model, something like this:

config::connect("/ChannelList/2/TxRxPointToPoint", MakeCallback(&MyModel::ByteTrcae2), model);

Wayne Matabar

unread,
Aug 18, 2016, 1:56:43 PM8/18/16
to ns-3-users
It seems to break my callback function...
error: prototype for ‘void MyModel::ByteTrace2(std::__cxx11::string, ns3::Ptr<const ns3::Packet>, ns3::Ptr<ns3::NetDevice>, ns3::Ptr<ns3::NetDevice>, ns3::Time, ns3::Time)’ does not match any in class ‘MyModel’
 MyModel::ByteTrace2 (std::string context, Ptr<const Packet> p, Ptr<NetDevice> nd1, Ptr<NetDevice> nd2, Time t1, Time t2)
 ^

I added the ByteTrace2 to the MyModel as  MyModel::Bytrace2(...) and included the 'MyModel' class identifier to the ByteTrace2 definition like so:

void MyModel::ByteTrace2 (std::string context, Ptr<const Packet> p, Ptr<NetDevice> nd1, Ptr<NetDevice> nd2, Time t1, Time t2){...}

Under the class declaration we see:

public:
void ByteTrace2 (std::string, Ptr<const Packet>, Ptr<const NetDevice>, Ptr<const NetDevice>, Time , Time);

Under the Main function:

Main(){...


Ptr<MyModel> model = CreateObject<MyModel>();

  Config::Connect ("/ChannelList/2/TxRxPointToPoint", MakeCallback (&MyModel::ByteTrace2), model);

I remember reading something about a five argument limit when it comes to callbacks?

Wayne

Wayne Matabar

unread,
Aug 22, 2016, 5:49:37 PM8/22/16
to ns-3-users
I found the solution. After re-coding my ByteTrace2(...) to MyModel::ByteTrace2(...), I also (under the main) Config::Connect("/ChannelList/*/TxRxPointToPoint/", MakeCallback(&MyModel::ByteTrace2, model)). Before this I have: Ptr<MyModel> model = CreateObject<MyModel>(); then Names::Add("/Names/MyModel", model);. This all allows me to make the callbacked variables in ByteTrace2 available to MyModel which schedules the simulator sample events every 0.1 seconds.

Wayne
Reply all
Reply to author
Forward
0 new messages