adding callback in third.cc

319 views
Skip to first unread message

高路

unread,
Apr 24, 2014, 5:42:13 PM4/24/14
to ns-3-...@googlegroups.com
hi, I study the tutorial ch 7.2.2 and add callback code into the third.cc. the code is :
  /*std::ostringstream oss;
  oss <<
  "/NodeList/" << wifiStaNodes.Get (nWifi - 1)->GetId () <<
  "/$ns3::MobilityModel/CourseChange";*/
  //Config::Connect (oss.str (), MakeCallback (&CourseChange));

  Ptr<Node> theObject =wifiStaNodes.Get (nWifi - 1);
  std::cout<<"traced node id: "<<theObject->GetId()<<std::endl;
  theObject->TraceConnectWithoutContext("CourseChange", MakeCallback(&CourseChange));

when i used config system, the trace sink can output the position information of mobile node 7. but i used the second method, and the trace sink output no result.
why?
Please help me . thank u

Konstantinos

unread,
Apr 24, 2014, 5:55:59 PM4/24/14
to ns-3-...@googlegroups.com
Yes, because "theObject" should be the object that provides the corresponding trace source. For the CourseChange, that is the MobilityModel installed on the node and not the node itself.

So something like this should work:

Ptr<Node> theNode = wifiStaNodes.Get(nWifi-1);
Ptr<MobilityModel> theObject = theNode->GetObject<MobilityModel>();

theObject
->TraceConnectWithoutContext("CourseChange", MakeCallback(&CourseChange));

Also, be aware when this trace source fires. If you use a constant mobility model it will not fire!

Regards,
K.

高路

unread,
Apr 25, 2014, 3:52:50 AM4/25/14
to ns-3-...@googlegroups.com
thank you, Konstantinos. i correct my code on the basis of your advice, but there is something wrong. the error information :

msg="Incompatible types. (feed to "c++filt -t" if needed)
got=ns3::FunctorCallbackImpl<void (*)(std::string, ns3::Ptr<ns3::MobilityModel const>), void, std::string, ns3::Ptr<ns3::MobilityModel const>, ns3::empty, ns3::empty, ns3::empty, ns3::empty, ns3::empty, ns3::empty, ns3::empty>
expected=ns3::CallbackImpl<void, ns3::Ptr<ns3::MobilityModel const>, ns3::empty, ns3::empty, ns3::empty, ns3::empty, ns3::empty, ns3::empty, ns3::empty, ns3::empty>*", file=./ns3/callback.h, line=1195
terminate called without an active exception
Command ['/home/highway/workspace/ns-allinone-3.19/ns-3.19/build/scratch/third'] terminated with signal SIGIOT. Run it under a debugger to get more information (./waf --run <program> --command-template="gdb --args %s <args>").

i only add the trace code to the third.cc and don't correct any other code.

在 2014年4月24日星期四UTC+1下午10时55分59秒,Konstantinos写道:

Konstantinos

unread,
Apr 25, 2014, 4:42:05 AM4/25/14
to ns-3-...@googlegroups.com
What is the definition of your CourseChange method?

I just tested third.cc with the above code and this CourseChange without a problem

static void
CourseChange (Ptr<const MobilityModel> mobility)
{
 
Vector pos = mobility->GetPosition (); // Get position
 
Vector vel = mobility->GetVelocity (); // Get velocity

 
// Prints position and velocities
  std
::cout << Simulator::Now () << " POS: x=" << pos.x << ", y=" << pos.y
     
<< ", z=" << pos.z << "; VEL:" << vel.x << ", y=" << vel.y
     
<< ", z=" << vel.z << std::endl;
}

高路

unread,
Apr 25, 2014, 6:06:44 AM4/25/14
to ns-3-...@googlegroups.com
ok, thank you very much. and i have a question: if i add a function as trace sink , how to set the parameters of the trace sink based on the trace source?

在 2014年4月25日星期五UTC+1上午9时42分05秒,Konstantinos写道:

Konstantinos

unread,
Apr 25, 2014, 6:33:35 AM4/25/14
to ns-3-...@googlegroups.com
I would recommend to read ns-3 documentation 


In principle the parameters have to contain the same as in the trace source, they may contain more if you use "context" and if you have a boundcallback.
Reply all
Reply to author
Forward
0 new messages