ndnSIM 如何使用 scenario 模板 进行 真实场景仿真

ndnSIM 安装完成后,默认在 ns-3 目录下运行启动程序。

根据官网的文档(Getting Started — ndnSIM documentation)说明,仿真场景可以直接在NS-3目录下的 scratch/src / ndnSIM / examples 文件夹中编写。但是这种情况将使用者自己编写的代码和运行框架的src源码混合在了一起,不利于开发。

因此,官网推荐的一种方式是在独立于NS-3或ndnSIM的独立存储库中编写模拟方案。同时提供了模板来编写扩展,模拟方案和度量标准处理脚本:https://github.com/cawka/ndnSIM-scenario-template

后面这种方法将代码写在了和 ns-3、ndnSIM都不相关的仓库(repository)里。,也可以直接运行,但是需要进行额外的安装过程,执行一些命令。同时,相比之下,这种方式只对使用者编写的代码进行编译和链接,能够大大提高运行仿真的速度(大概能快十倍左右)。在提高编译速度的同时,这种方式也能很好地区分代码是自己写的还是模拟器自带的,所以官网上推荐是使用独立的库来编写自己的仿真场景。

前半部分代码如果运行过可以不必再次执行:

mkdir ndnSIM
cd ndnSIM
git clone https://github.com/named-data-ndnSIM/ns-3-dev.git ns-3
git clone https://github.com/named-data-ndnSIM/pybindgen.git pybindgen
git clone --recursive https://github.com/named-data-ndnSIM/ndnSIM.git ns-3/src/ndnSIM

# Build and install NS-3 and ndnSIM
cd ns-3
./waf configure -d optimized
./waf

 如果要使用 scenario 模板 进行 真实场景仿真,需要继续运行:

sudo ./waf install
cd ..

git clone https://github.com/named-data-ndnSIM/scenario-template.git scenario
cd scenario
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig
export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH

./waf configure

./waf --run <scenario>

首先在ns-3文件夹下,将编译好的库安装到系统中( /usr/local 路径),这样在另一个文件夹运行时就可以直接调用编译并安装好的这些库了。

从ns-3文件夹回到父文件夹ndnSIM,通过创建一个叫scenario的文件夹(在目录ndnSIM下,与ns-3同级)并经过一些设置,来使自己编写的所有代码都可以直接放在scenario文件夹里面(包括自己定制的转发策略,consumer与producer应用,仿真场景文件,仿真结果数据记录脚本等)。如果不进行后半部分的命令行配置,则以后自己编写的不同类型的代码文件需要放在不同的文件夹里。

如何在scenario中运行自己的代码

1.将自己的代码保存在 ndnSIM/scenario/scenarios (即GitHub模板下载中的scenarios目录【与waf文件同级】)目录下,文件格式为cpp。

这里以 官方 Examples 中的 ndn-simple.cpp 为例,如果找不到的可以直接复制以下代码:

// ndn-simple.cpp

#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/ndnSIM-module.h"

namespace ns3 {

int
main(int argc, char* argv[])
{
  // setting default parameters for PointToPoint links and channels
  Config::SetDefault("ns3::PointToPointNetDevice::DataRate", StringValue("1Mbps"));
  Config::SetDefault("ns3::PointToPointChannel::Delay", StringValue("10ms"));
  Config::SetDefault("ns3::QueueBase::MaxSize", StringValue("20p"));

  // Read optional command-line parameters (e.g., enable visualizer with ./waf --run=<> --visualize
  CommandLine cmd;
  cmd.Parse(argc, argv);

  // Creating nodes
  NodeContainer nodes;
  nodes.Create(3);

  // Connecting nodes using two links
  PointToPointHelper p2p;
  p2p.Install(nodes.Get(0), nodes.Get(1));
  p2p.Install(nodes.Get(1), nodes.Get(2));

  // Install NDN stack on all nodes
  ndn::StackHelper ndnHelper;
  ndnHelper.SetDefaultRoutes(true);
  ndnHelper.InstallAll();

  // Choosing forwarding strategy
  ndn::StrategyChoiceHelper::InstallAll("/prefix", "/localhost/nfd/strategy/multicast");

  // Installing applications

  // Consumer
  ndn::AppHelper consumerHelper("ns3::ndn::ConsumerCbr");
  // Consumer will request /prefix/0, /prefix/1, ...
  consumerHelper.SetPrefix("/prefix");
  consumerHelper.SetAttribute("Frequency", StringValue("10")); // 10 interests a second
  auto apps = consumerHelper.Install(nodes.Get(0));                        // first node
  apps.Stop(Seconds(10.0)); // stop the consumer app at 10 seconds mark

  // Producer
  ndn::AppHelper producerHelper("ns3::ndn::Producer");
  // Producer will reply to all requests starting with /prefix
  producerHelper.SetPrefix("/prefix");
  producerHelper.SetAttribute("PayloadSize", StringValue("1024"));
  producerHelper.Install(nodes.Get(2)); // last node

  Simulator::Stop(Seconds(20.0));

  Simulator::Run();
  Simulator::Destroy();

  return 0;
}

} // namespace ns3

int
main(int argc, char* argv[])
{
  return ns3::main(argc, argv);
}

将上述代码放在对应的文件夹中,并重命名为mytest.cpp。(cpp文件名就是最终要仿真的场景名称【即 代码中的 <scenario> 】,和文件内部的代码无关)

2. 返回上一级目录,在waf同级目录下执行。

./waf configure # 如果在当前文件夹中未执行过则需要执行一次

./waf --run mytest

编译链接后会有很多 warning ,如果没有红色的 error 就可以不用管,最终出现 'build' finished successfully (11.220s) 说明仿真成功。

也可以使用等号表明要运行的仿真是哪一个。

./waf --run=mytest

当仿真场景的代码没有修改时,仿真会很快开始运行,无需再次编译链接,如上图所示。因而不会产生编译过程中提示的 warning 信息。

在scenario中进行仿真会大大提高代码编写的效率和代码编译速度,而无需像在 ns-3 中编译,需要把所有文件编译一遍。

同时在scenario中也可以使用可视化工具:

./waf --run mytest --vis

注意事项:

1.代码的保存位置不要搞错了,外层是scenario,不带s,内层是scenarios,带s。

2.如果在编写自己场景的代码中需要修改内容请求者(consumer)和内容提供者(producer)等的行为,也需要将修改后的代码(自己修改定制的consumer和producer【包括.hpp和.cpp】)放在scenarios文件夹中。

NDN科研工作者,长期研究,欢迎讨论交流与合作! 

  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 18
    评论
评论 18
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值