出处:http://blog.csdn.net/finded/article/details/45337725
7. 编译和运行脚本主要步骤
1) 将编写的脚本复制到ns-3.22/scratch目录下(可以在ubuntu窗口界面直接复制)
进入ns3目录: /ns-3.22
$ cp examples/tutorial/first.cc scratch/myfirst.cc将脚本复制到scratch目录下
2) 构建(编译)
$ ./waf
3) 运行
$ ./waf --run scratch/myfirst
(可能会有运行权限问题,可在root下运行)
7.1 ns3中first.cc例子注释
7.2 CommandLine命令行参数
仿真一般是为了收集各种不同条件下的数据,常常需要改变一些变量。NS-3提供了Command Line参数接口,可以在运行时对脚本中的变量进行设置,免去了每次更改变量后要重新编译和构建脚本的麻烦。
1) 修改已有属性变量
在脚本中添加语句
int main (int argc, char *argv[])
{
...
CommandLine cmd;
cmd.Parse (argc, argv);//将命令行输入的参数作为类CommandLine的参数进行分析
...
}
这样可以在shell中使用某些附加参数如PrintHelp:
$~/ns-3.2.1 > ./waf --run "scratch/example --PrintHelp"
这条命令将会列出example当前可用的命令参数:
Entering directory '/home/craigdo/repos/ns-3-dev/build'
Compilation finished successfully
--PrintHelp: Print this help message.
--PrintGroups: Print the list of groups.
--PrintTypeIds: Print all TypeIds.
--PrintGroup=[group]: Print all TypeIds of group.
--PrintAttributes=[typeid]: Print all attributes of typeid.
--PrintGlobals: Print the list of globals.
从输出中(倒数第二行)我们知道可以打印某些类的属性:
$~/ns-3.2.1 > ./waf --run "scratch/example --PrintAttributes=ns3::PointToPointNetDevice"
这条命令将会列出类型为PointToPointNetDevice的设备的属性:
--ns3::PointToPointNetDevice::DataRate=[32768bps]:
The default data rate for point topoint links
知道了属性名称,我们也可以使用命令更改这个属性:前提须把脚本中赋值语句注释/删除
$~/ns-3.2.1>./waf--run"scratch/example --ns3::PointToPointNetDevice::DataRate=5Mbps"
2) 添加自己的变量
使用CommandLine::AddValue添加自己的变量,通过钩挂自己的变量将其与命令行相关联,使之成为CommandLine可以使用的参数,
在脚本中main函数开始添加
CommandLinecmd;
cmd.AddValue("nPackets","Number of packets to echo", nPackets); //(属性名称,属性说明,变量)
cmd.Parse(argc,argv);
这样在shell中我们可以在命令中更改这个属性:
$~/ns-3.2.1 > ./waf --run "scratch/example --nPackets=2"
参考文献:
[1] http://blog.sina.com.cn/s/articlelist_1923709774_8_1.html
[2] ns-3project. ns-3 Software Tutorial. ns-3 project. 2015年2月26日
[3] ns-3project. ns-3 Reference Manual. ns-3 project. 2015年2月26日
[4]《ns-3网络模拟器基础及应用》,人民邮电出版社,马春光 姚建盛,2014年1月
部分参考网络资料,未一一列举,敬请原谅!