Using a callback to pass parameters

809 views
Skip to first unread message

Diogo Carreira

unread,
Feb 26, 2013, 10:44:09 AM2/26/13
to ns-3-users
Hi, i have some available parameters in mac layer ( sta-wifi-mac.cc)
and i want to hava access to them in one application i created (so i
can calculate something).
I heard that using a callback is one possible solution, but how can i
use it?
best regards,
diogo

Rubén Martínez

unread,
Mar 6, 2013, 6:35:14 AM3/6/13
to ns-3-...@googlegroups.com
Hello.

Let's supose you want to pass 2 uint32_t variables to that application, you should first add a new TracedCallback to sta-wifi-mac.h , like this:

TracedCallback<uint32_t,uint32_t> m_Mycallback;

(just replace the type and number of desired parameters for whatever you need in the template)

Then you should modifiy the GetTypeId(void) function definition of sta-wifi-ma.cc to include a new TraceSource:

     .AddTraceSource ("NewCallback"," pass parameters to application ",
                           MakeTraceSourceAccessor (&StaWifiMac::m_Mycallback))


Now in your application you should declare a function that receives exactly these parameters:

void MyApplication::MyFunction(uint32_t,uint32_t)

Next, in some of the functions of your application (maybe StartApplication would be a good choice ) you should use the Config::Connect function to connect to the TraceSource.

  Config::ConnectWithoutContext (oss.str (),MakeCallback (&MyApplication::MyFunction,this));

Finally, in sta-wifi-mac.cc whenever you call:

m_Mycallback(parameter1, parameter2);

MyApplication::MyFunction will be invoked with those parameters.

I hope that helps.



--
You received this message because you are subscribed to the Google Groups "ns-3-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ns-3-users+...@googlegroups.com.
To post to this group, send email to ns-3-...@googlegroups.com.
Visit this group at http://groups.google.com/group/ns-3-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.



Diogo Carreira

unread,
Mar 14, 2013, 10:58:28 AM3/14/13
to ns-3-...@googlegroups.com
Hi, thanks for your help. i did all of that but im getting error when i do :
Config::ConnectWithoutContext (oss.str (),MakeCallback (&MyApplication::MyFunction,this)); in my application:

/src/applications/model/Connection_manager.cc: In member function ‘virtual void ns3::Connection_manager::StartApplication()’:
../src/applications/model/Connection_manager.cc:175:3: error: ‘Config’ has not been declared
../src/applications/model/Connection_manager.cc:175:34: error: ‘oss’ was not declared in this scope

can you help me? 
thank you

Rubén Martínez

unread,
Mar 15, 2013, 12:38:42 PM3/15/13
to ns-3-...@googlegroups.com
The first error can be fixed by including config.h

#include "ns3/config.h"
 
To fix the second one you should take a look at this:

http://www.nsnam.org/docs/release/3.14/tutorial/singlehtml/index.html#using-the-config-subsystem-to-connect-to-trace-sources


--

Diogo Carreira

unread,
Mar 18, 2013, 10:29:28 AM3/18/13
to ns-3-...@googlegroups.com
Hi, i defined all that and its ok. The program runs without any errror, the problem is that is not entering in My_APP::My_function.
The cause could be the config:connect :
I did this in StartApplication() :

Config::ConnectWithoutContext("/$ns3::StaWifiMac/NewCallback",MakeCallback (&Connection_manager::MyFunction,this));

I think i need to specify better the way no? 
Diogo

Konstantinos

unread,
Mar 18, 2013, 11:19:28 AM3/18/13
to ns-3-...@googlegroups.com
I can spot many problems with this callback.

1) The path to the StaWifiMac is wrong. you have missed the node & device.

Config Paths

ns3::StaWifiMac is accessible through the following paths with Config::Set and Config::Connect:

  • /NodeList/[i]/DeviceList/[i]/$ns3::WifiNetDevice/Mac/$ns3::RegularWifiMac/$ns3::StaWifiMac
  • /NodeList/[i]/DeviceList/[i]/$ns3::WifiNetDevice/Mac/$ns3::StaWifiMac

2) Without having access to your code, I can say that the NewCallback is wrong since there is no trace source currently named NewCallback. It could be that you have created a trace source named NewCallback.

3) My_function is a member of My_APP or Connection_manager? If it is the first, then again this is wrong, if it is the second, then it's correct.

4) Where does "this" point? It should point to an instance of the class that has My_Function as member.

Hope that is clear now.

Diogo Carreira

unread,
Mar 19, 2013, 5:35:56 AM3/19/13
to ns-3-...@googlegroups.com
Thank both of you for the help! It was the path that was not correct! Now is ok, thank you one more time
Reply all
Reply to author
Forward
0 new messages