在NS-3中要实现移动路径追踪,首先应该想到MobilityModel移动模型,要实现追踪分析,就要用到trace sources.
The list of all trace sources:
http://www.nsnam.org/doxygen/group___trace_source_list.html
在trace sources中找到MobilityModel,看到下面有一个可以trace的属性:
CourseChange: The value of the position and/or velocity vector changed
表示可以分析移动后的速度、位置。
进入MobilityModel的TypeId ns3::MobilityModel::GetTypeId
http://www.nsnam.org/doxygen/classns3_1_1_mobility_model.html#a3d1346ba74cc805812fac131070ba439
可以看到: This object is accessible through the following paths with Config::Set and Config::Connect:
我们先来看一下Config::Connect方法:
http://www.nsnam.org/doxygen/namespacens3_1_1_config.html#a4014f151241cd0939b6cb64409605736
void ns3::Config::Connect ( std::string path, const CallbackBase & cb )
第一项 path,就对应上面的 /NodeList/[i]/...这个路径。
上面这么多路径,应该选择哪一个呢? 根据你使用的网络来定,比如用的是Aloha网络,就用那个了。
我用的是Wifi网络,第一个是通用的,所以用第一个/NodeList/[i]/$ns3::MobilityModel
Config::Connect第二个参数是CallbackBase
所以,我们要调用一个MakeCallback函数,才能返回CallbackBase类型
一个例子:
#include ...
...
//自定义一个全局函数,用于MakeCallback调用
void
CourseChange (std::string context, Ptr<const MobilityModel> model)
{
Vector position = model->GetPosition ();
NS_LOG_UNCOND (context << "x = " << position.x << ", y= " << position.y);
}
...
int main (int argc ,char *argv[])
{
...
std::ostringstream oss;
/** 下面的 wifiStaNodes.Get (nWifi - 1)->GetId ()的方法,就是得到节点的编号,
*比如: Ptr<Node> m_node;
* m_node->GetId ();
* 方法见: http://www.nsnam.org/doxygen/classns3_1_1_node.html
*路径与/NodeList/[i]/$ns3::MobilityModel 相同
*/
oss << "/NodeList/" << wifiStaNodes.Get (nWifi - 1)->GetId ()
<< "/$ns3::MobilityModel/CourseChange";
Config::Connect (oss.str (), MakeCallback (&CourseChange));
...
}