(二)NS3如何编译、运行脚本和 Command Line命令行参数设置

二、编译、运行脚本和Command Line命令行参数设置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例子注释 //first.cc: 2个节点间的点到点通信 //头文件包含#include "ns3/core-module.h"#include "ns3/network-module.h"#include "ns3/internet-module.h"#include "ns3/point-to-point-module.h"#include "ns3/applications-module.h"//ns3命名空间using namespace ns3;//日志定义NS_LOG_COMPONENT_DEFINE ("FirstScriptExample");//主函数intmain (int argc, char *argv[]){ //时间分辨率 Time::SetResolution (Time::NS); //使日志组件生效:组件–UdpEchoClientApplication–级别INFO LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO); LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO); //********生成网络节点******** // 可以简单看成部分掏空通信部分内容的计算机,可以加入协议栈、应用及外设的网卡等。 //节点容器类(包含许多方法):是一个helper帮助类,能够一次操作多个节点。 //如:利用其对象(变量)作为设备helper类对象的参数,,可以一次在安装设备到多个节点。 NodeContainer nodes; nodes.Create (2);//利用该容器类的创建节点方法,创建两个节点//********物理连接计算机******** // 抽象:物理实体=网络设备+信道,两者一一对应。 PointToPointHelper pointToPoint;//点到点通信助手类,通过所包含方法能设置网络设备和信道属性 pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));//调用成员函数 pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));//网络设备容器,即安装了网络设备和信道的节点 NetDeviceContainer devices; devices = pointToPoint.Install (nodes);//********安装协议栈********// InternetStackHelper stack;//网络协议栈帮助类。属于拓扑帮助类 stack.Install (nodes);//为每个节点安装协议栈,IP层 //ipv4地址帮助类,属于拓扑帮助类 Ipv4AddressHelper address; address.SetBase ("10.1.1.0", "255.255.255.0"); //ipv4接口容器类,为了以后引用方便,给网络设备容器类配置地址。结果存在ipv4接口容器类的对象中。 Ipv4InterfaceContainer interfaces = address.Assign (devices); //********安装应用层********// // UDP服务器设置 UdpEchoServerHelper echoServer (9); ApplicationContainer serverApps = echoServer.Install (nodes.Get (1)); serverApps.Start (Seconds (1.0)); serverApps.Stop (Seconds (10.0)); // UDP客户机 UdpEchoClientHelper echoClient (interfaces.GetAddress (1), 9);//远端服务器地址和端口 echoClient.SetAttribute ("MaxPackets", UintegerValue (1)); echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0))); echoClient.SetAttribute ("PacketSize", UintegerValue (1024)); ApplicationContainer clientApps = echoClient.Install (nodes.Get (0)); clientApps.Start (Seconds (2.0)); clientApps.Stop (Seconds (10.0)); //没有加入Simulation::Stop(Seconds(11.0));仿真结束时间 //因为first.cc例子的事件队列中事件会自动操作完; //对于一直有事件产生的仿真(类似操作系统),必须设置仿真结束时间 Simulator::Run (); Simulator::Destroy (); return 0;}7.2 CommandLine命令行参数

仿真一般是为了收集各种不同条件下的数据,常常需要改变一些变量。NS-3提供了Command Line参数接口,可以在运行时对脚本中的变量进行设置,免去了每次更改变量后要重新编译和构建脚本的麻烦。

1) 修改已有属性变量

在脚本中添加语句

int main (int argc, char *argv[])

{

CommandLine cmd;

}

这样可以在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]

[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月

部分参考网络资料,未一一列举,敬请原谅!

想念我的时候,不要忘记我也在想念你。

(二)NS3如何编译、运行脚本和 Command Line命令行参数设置

相关文章:

你感兴趣的文章:

标签云: