Custom "contexts" (or parameters) for connected tracecallbacks?

223 views
Skip to first unread message

Antti Mäkelä

unread,
Aug 30, 2009, 9:22:21 AM8/30/09
to ns-3-users
Hey,

I want to do some traces, and I'm utilizing the trace/sink
functionality for that. However, it seems that Config::Connect
(TraceAccessor, MakeCallBack(&MyFunc)) doesn't allow me to specify any
additional parameters - the connected callback function has to be of
type "void foo (string context, bool tracedbool)".

Now, I'm tracing stuff across multiple objects and wish to process
it a bit before outputting. For that, I need to know where it came
from - yes, that's what context is for, but is there any way to get
something else than the entire path instead? I'd much rather work with
an uint32_t, or even a pointer to a node, than a string.

I suppose I could define the tracedtype as something like struct
tracestruct { uint32_t nodeid; bool tracedbool;}; and set the id at
start and never touch it later, but somehow this seems a bit
troublesome. Yet working with strings feels just equally bothersome.

The actual use case here is that I'm right now setting up bunch of
identical applications running on various nodes

for (int i=0; i<10;i++)
{
Names::Add ("app"+inttostr(i+1), apps.Get(i));
Config::Connect ("/Names/app"+inttostr(i+1)+"/Connected",
MakeCallback (&totalConnectivityTrace));
}

Basically the accessor here is a boolean and just tells if the app
is connected to a peer. Me, as the end-user, am only interested in two
states: ALL apps are connected or not. For that purpose I'm using a
vector<bool> status (10), and when the callback occurs I just want to
set status.at(appindex) to the value sent over (and afterward do
checks on the total state and if necessary, print something).

Do I really have to start messing with string.find() and string.erase
() to get that index from 0 to 9?

Mathieu Lacage

unread,
Sep 15, 2009, 4:56:15 AM9/15/09
to ns-3-...@googlegroups.com
hi,

sorry for this very late answer,

On Sun, Aug 30, 2009 at 3:22 PM, Antti Mäkelä <zar...@gmail.com> wrote:
>
> Hey,
>
>  I want to do some traces, and I'm utilizing the trace/sink
> functionality for that. However, it seems that Config::Connect
> (TraceAccessor, MakeCallBack(&MyFunc)) doesn't allow me to specify any
> additional parameters - the connected callback function has to be of
> type "void foo (string context, bool tracedbool)".
>

There are two ways you can use to add additional context to a callback:

1) use a member method as a callback
2) use MakeBoundCallback on a static non-member function

1) goes along like this:

class MyContext
{
public:
void Setup (int n) {
std::ostringstream oss;
oss << "/Names/app" << n << "/Connected"
Config::Connect (oss.str (), MakeCallback (this,
&MyContext::MyCallbackMethod));
}
private:
void MyCallbackMethod (string context) {
// use m_mycontext here
}
int m_mycontext;
// more context
};

and, then, client code looks like this:

MyContext * ctx = new MyContext ();
ctx->Setup (5);

2) is a bit more complicated to do. There is sample code in
samples/main-ns2-mob.cc

Mathieu
--
Mathieu Lacage <mathieu...@gmail.com>

Antti Mäkelä

unread,
Sep 15, 2009, 8:32:32 AM9/15/09
to ns-3-users
On Sep 15, 11:56 am, Mathieu Lacage <mathieu.lac...@gmail.com> wrote:
> hi,
>
> sorry for this very late answer,

No problem, actually the other thread (linked in bug 665) provided
lots of insight (including the possiblity to define
TracedCallback<Type1, Type2, ...>). Better documentation would help a
lot of course.

Anyway, the first idea of pointing the callback to a member function
of the object itself doesn't really work in my case since I'm hoping
to aggregate information from multiple objects. I am using static non-
member function now, but MakeBoundCallback vs. MakeCallback is new to
me. In Doxygen, http://www.nsnam.org/doxygen/functions_0x6d.html#index_m
doesn't actually show any info on either method actually... -
http://www.nsnam.org/doxygen/group___make_callback.html only is about
MakeCallback and MakeNullCallback, nothing about Bound...

Gustavo Carneiro

unread,
Sep 15, 2009, 9:30:23 AM9/15/09
to ns-3-...@googlegroups.com


2009/9/15 Antti Mäkelä <zar...@gmail.com>


On Sep 15, 11:56 am, Mathieu Lacage <mathieu.lac...@gmail.com> wrote:
> hi,
>
> sorry for this very late answer,

 No problem, actually the other thread (linked in bug 665) provided
lots of insight (including the possiblity to define
TracedCallback<Type1, Type2, ...>). Better documentation would help a
lot of course.

 Anyway, the first idea of pointing the callback to a member function
of the object itself doesn't really work in my case since I'm hoping
to aggregate information from multiple objects.

It works, I have done it before.  It is some work, but works.  Something along these lines:

struct MyData : public RefCountBase
{
   Ptr<This> m_this;
   Ptr<That> m_that;

  void MyMethod (...) {
     // do something with m_this and m_that;
   }
};

main()
{

   Ptr<MyData> data = Create<MyData> ();
   data.m_this = ...;
   data.m_that = ...;

   Config::Connect ("path", MakeCallback (&MyData::MyMethod, data));
}

Incidentally, this is precisely how the Python bindings allow arbitrary ammount of user callback arguments for ns3.Config.Connect().

I am using static non-
member function now, but MakeBoundCallback vs. MakeCallback is new to
me. In Doxygen, http://www.nsnam.org/doxygen/functions_0x6d.html#index_m
doesn't actually show any info on either method actually... -
http://www.nsnam.org/doxygen/group___make_callback.html only is about
MakeCallback and MakeNullCallback, nothing about Bound...




--
Gustavo J. A. M. Carneiro
INESC Porto, Telecommunications and Multimedia Unit
"The universe is always one step beyond logic." -- Frank Herbert

Mathieu Lacage

unread,
Sep 16, 2009, 12:28:47 AM9/16/09
to ns-3-...@googlegroups.com
On Tue, Sep 15, 2009 at 2:32 PM, Antti Mäkelä <zar...@gmail.com> wrote:
>  No problem, actually the other thread (linked in bug 665) provided
> lots of insight (including the possiblity to define
> TracedCallback<Type1, Type2, ...>). Better documentation would help a
> lot of course.
>
>  Anyway, the first idea of pointing the callback to a member function
> of the object itself doesn't really work in my case since I'm hoping
> to aggregate information from multiple objects. I am using static non-

There is nothing which makes it impossible to connect multiple trace
sources to different methods on the same object.

> member function now, but MakeBoundCallback vs. MakeCallback is new to
> me. In Doxygen, http://www.nsnam.org/doxygen/functions_0x6d.html#index_m
> doesn't actually show any info on either method actually... -
> http://www.nsnam.org/doxygen/group___make_callback.html only is about
> MakeCallback and MakeNullCallback, nothing about Bound...

Yes, it's undocumented because it's not really for public consumption
because it's fairly tricky to use right. It works well if you know how
to use it though.

Reply all
Reply to author
Forward
0 new messages