文章目录
前言
由于我还是一名大四学生,也是一名ndnSIM的初学者,在文章中难免会出现许多错误。希望初学者看到本文时不要全盘相信,也欢迎大佬们对我的文章进行斧正!
代码分析
首先先从最简单的 ~/newndnSIM/ns-3/src/ndnSIM/examples/ndn-simple.cpp 来切入,对整个ndnSIM的框架进行分析。为了方便读者对比分析,我在这里将代码复制一份到文章中(删去了大段注释)。
// 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);
}
0.头文件
作用
引入头文件。前3个是ns-3的文件,最后是ndnSIM的文件。源代码如下——
#include "ns3/core-module.h" // core模块
#include "ns3/network-module.h" // network模块
#include "ns3/point-to-point-module.h" //point-to-point模块
#include "ns3/ndnSIM-module.h" // ndnSIM自己的模块
详细解析
首先先看前面 include 的文件,我在文章ndnSIM学习(二)——配置VScode的跨文件转到定义中提到,这些文件的地址是相对于 ~/newndnSIM/ns-3/build/ 的
,里面的内容都是清一色的 #inlude 。
这里面include的库中,前3个都是ns-3软件的函数,最后一个才是ndnSIM自己的,里面就只有一行 #include "ndn-all.hpp" ,这里面include了ndnSIM的所有头文件。
1.配置网络初始参数
作用
配置网络的初始参数,如网络数据传输速率、信道时延、队列最大容量。源代码如下——
// setting default parameters for PointToPoint links and channels
Config::SetDefault("ns3::PointToPointNetDevice::DataRate", StringValue("1Mbps")

本文详细分析了ndnSIM框架中的ndn-simple.cpp代码,涵盖网络参数配置、命令行参数解析、节点创建、网络拓扑结构、NDN栈安装、转发策略、Consumer与Producer应用设置,以及仿真执行过程。通过实例展示了代码运行流程,包括兴趣包和数据包的传输时延,帮助读者快速理解ndnSIM的基本工作原理。
最低0.47元/天 解锁文章





