NS3提供了一种通过命令行传递参数来输出和改变脚本中的变量。
在使用命令行机制时,
一首先要声明一个命令行类的对象然后调用其函数成员Parse:
int main(int argc,char*argv[]){
...
CommandLine cmd;
cmd.Parse(argc,argv);
...
}
这两行代表用户可用命令行来访问代码中全局变量和NS3的属性。
例如在example/tutorial中的first.cc脚本中用到了PointToPoint类,可通过命令行显示该类在使用中所定义的默认值:
二在编译脚本时附加一些额外信息:
./waf --run "scratch/myfirst --PrintHelp"
./waf --run "scratch/myfirst --PrintAttributes=ns3::PointToPointNetDevice"
三运行结果
yan@ysw:~/NS3/ns-allinone-3.25/ns-3.25$ ./waf --run "scratch/myfirst --PrintAttributes=ns3::PointToPointNetDevice"
Waf: Entering directory `/home/yan/NS3/ns-allinone-3.25/ns-3.25/build'
Waf: Leaving directory `/home/yan/NS3/ns-allinone-3.25/ns-3.25/build'
Build commands will be stored in build/compile_commands.json
'build' finished successfully (0.956s)
Attributes for TypeId ns3::PointToPointNetDevice
--ns3::PointToPointNetDevice::Address=[ff:ff:ff:ff:ff:ff]
The MAC address of this device.
--ns3::PointToPointNetDevice::DataRate=[32768bps]
The default data rate for point to point links
--ns3::PointToPointNetDevice::InterframeGap=[+0.0ns]
The time to wait between packet (frame) transmissions
--ns3::PointToPointNetDevice::Mtu=[1500]
The MAC-level Maximum Transmission Unit
--ns3::PointToPointNetDevice::ReceiveErrorModel=[0]
The receiver error model used to simulate packet loss
--ns3::PointToPointNetDevice::TxQueue=[0]
A queue to use as the transmit queue in the device.
可以看到脚本中默认属性DataRate默认值为32768bit/s,帧最大传输单元MTU是1500,网卡地址ff:ff:ff:ff:ff:ff等而在脚本中设这了DataRate为5Mbit/s,意味者实际运行会覆盖原始默认值:
PointToPointHelper pointToPoint;
pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));
如果使用默认的DataRate,只需在脚本中将pointToPoint.SetDeviceAttribute注释掉,再次运行,发现客户端应用程序开始运行的时间没有变化,但是由于发送速度变小故数据传输时间变长。同样的,可以修改其他属性变量如延迟/最大分组发送数目等。
四通过命令行修改属性值
以echoClient.SetAttribute ("MaxPackets", UintegerValue (1));为例说明,原始的这句话就限制了MaxPackets属性是固定的为1,如果要改成命令行编译脚本时自定义可通过:
uint32_t npacks=1;//自定义一个变量
CommandLine cmd;
cmd.AddValue("npacks","Number of packs",npacks);
cmd.Parse(argc,argv);//必须在前三行之后
并
echoClient.SetAttribute ("MaxPackets", UintegerValue (npacks));
再编译myfirts.cc脚本:
./waf --run "scratch/myfirst --PrintHelp"
运行结果如下:
Waf: Entering directory `/home/yan/NS3/ns-allinone-3.25/ns-3.25/build'
Waf: Leaving directory `/home/yan/NS3/ns-allinone-3.25/ns-3.25/build'
Build commands will be stored in build/compile_commands.json
'build' finished successfully (1.061s)
myfirst [Program Arguments] [General Arguments]
Program Arguments:
--npacks: Number of packs [1]
可以看到在显示的最后一行会出现程序参数(Prigram Arguments),--npacks对应cmd.AddValue第一个参数,Number of packs [1]对应cmd.AddValue第二个参数;
运行:
./waf --run "scratch/myfirst --npacks=2"
结果:
At time 2s client sent 1024 bytes to 10.1.1.2 port 9
At time 2.00369s server received 1024 bytes from 10.1.1.1 port 49153
At time 2.00369s server sent 1024 bytes to 10.1.1.1 port 49153
At time 2.00737s client received 1024 bytes from 10.1.1.2 port 9
At time 3s client sent 1024 bytes to 10.1.1.2 port 9
At time 3.00369s server received 1024 bytes from 10.1.1.1 port 49153
At time 3.00369s server sent 1024 bytes to 10.1.1.1 port 49153
相比原有的,这次客户端发送了两次分组而非一次。