编译、运行脚本和Command Line命令行参数设置
2017-09-04 16:03
471 查看
出处: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例子注释
[cpp] viewplain copy
//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");
//主函数
int
main (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;
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月
部分参考网络资料,未一一列举,敬请原谅!
相关文章推荐
- (二)NS3如何编译、运行脚本和 Command Line命令行参数设置
- 如何在Windows下开发Python:在cmd下运行Python脚本+如何使用Python Shell(command line模式和GUI模式)+如何使用Python IDE
- 如何从Terminal Command Line编译并运行Scope
- Java: How to compile and run Java application in command line(在命令行中编译运行java程序)
- 使用NDK编译并运行C++11的Command line命令行程序
- 编译运行ogre程序时“error C3859: virtual memory range for PCH exceeded; please recompile with a command line option of '-Zm120' or great
- 如何在Windows下开发Python:在cmd下运行Python脚本+如何使用Python Shell(command line模式和GUI模式)+如何使用Python IDE
- 如何从Terminal Command Line编译并运行Scope
- NS3 Command Line Arguments 使用命令行参数改变脚本
- Scala学习1之用sbt和脚本一步编译打包运行scala程序
- apache禁止访问文件或目录执行权限、禁止运行脚本PHP文件的设置方法
- C#中如何获取其他进程的命令行参数 ( How to get other processes's command line argument )
- 设置UltraeEdit使之能编译运行Java程序
- python多线程异步post请求脚本,可以设置持续运行时间、线程数、时间间隔
- 运行QTP测试脚本后,将编译结果写入制定文件(三)
- java_eclipse_设置全局编码_utf-8_编译class指定_运行jar乱码解决_不依赖环境
- 【Python】读取命令行参数、在PyDev中设置Python运行时的参数
- UE的快速编译运行的设置,UE的注册
- LR11设定场景运行脚本,出现Error: missing newline in C:\Administrater\ProgramFiles\文件夹(实际的路径).dat原因及解决方案
- scala脚本编译运行