**
**
本文主要介绍了一个经典的ndnsim仿真实验,
cd ndnSIM
cd ns-3
./waf configure -d optimized
./waf
sudo ./waf install
否则error停不下来的
cd ndnSIM
cd myscenario
./waf configure --debug
./waf
否则error来得奇奇怪怪,显示跟usr/下某个路径里的文件有冲突,博主我费了一番功夫解决不掉,最后误打误撞搞定了。
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig
export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
**
**
拓扑图
#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/ndnSIM-module.h"
namespace ns3 {
/**
*
* /------\ 0 0 /------\
* | c1 |<-----+ +----->| p1 |
* \------/ \ / \------/
* \ /-----\ /
* /------\ 0 \ +==>| r12 |<==+ / 0 /------\
* | c2 |<--+ \ / \-----/ \ / +-->| p2 |
* \------/ \ \ | | / / \------/
* \ | | 1Mbps links | | /
* \ 1 v0 v5 1v 2v 3 /
* +->/------\ /------\<-+
* 2| r1 |<===============>| r2 |4
* +->\------/4 0\------/<-+
* / 3^ ^5 \
* / | | \
* /------\ 0 / / \ \ 0 /------\
* | c3 |<--+ / \ +-->| p3 |
* \------/ / \ \------/
* / "All consumer-router and" \
* /------\ 0 / "router-producer links are" \ 0 /------\
* | c4 |<-----+ "10Mbps" +---->| p4 |
* \------/ \------/
*
* 节点附近的数字表示端口ID。端口ID根据拓扑文件中链路的顺序定义
*
*
*日志记录模式运行:
* NS_LOG=ndn.Consumer:ndn.Producer ./waf --run=ndn-congestion-alt-topo-plugin
* 可视化运行
*/ ./waf --run=ndn-congestion-alt-topo-plugin --vis
*同时记录日志以及可视化运行:
* NS_LOG=ndn.Consumer:ndn.Producer ./waf --run=ndn-congestion-alt-topo-plugin --vis
int
main(int argc, char* argv[])
{
CommandLine cmd;
cmd.Parse(argc, argv);
AnnotatedTopologyReader topologyReader("", 1);
topologyReader.SetFileName("src/ndnSIM/examples/topologies/topo-11-node-two-bottlenecks.txt");
topologyReader.Read();
//注意,使用官方给出的拓扑文件即可,如果自己的模板,注意修改路径
// Install NDN stack on all nodes
ndn::StackHelper ndnHelper;
ndnHelper.SetOldContentStore("ns3::ndn::cs::Lru", "MaxSize",
"1"); // ! Attention ! If set to 0, then MaxSize is infinite
//这里用了OldContentStore
//导致可视化界面里CS表什么也不会显示
//大概是和ns-3的pybinding(可视化模组)有兼容问题
ndnHelper.InstallAll();
// Set BestRoute strategy
ndn::StrategyChoiceHelper::InstallAll("/", "/localhost/nfd/strategy/best-route");
// Getting containers for the consumer/producer
//container相当于一个指针集合,可以一次对大量指针操作
//这里用container可以大大简化后面setprefix和install的工作量
Ptr consumers[4] = {Names::Find("c1"), Names::Find("c2"),
Names::Find("c3"), Names::Find("c4")};
Ptr producers[4] = {Names::Find("p1"), Names::Find("p2"),
Names::Find("p3"), Names::Find("p4")};
//Names::Find命令和拓扑文件搭配使用,很方便就完成了对producer和consumer的声明
if (consumers[0] == 0 || consumers[1] == 0 || consumers[2] == 0 || consumers[3] == 0
|| producers[0] == 0 || producers[1] == 0 || producers[2] == 0 || producers[3] == 0) {
NS_FATAL_ERROR("Error in topology: one nodes c1, c2, c3, c4, p1, p2, p3, or p4 is missing");
}
for (int i = 0; i < 4; i++) {
std::string prefix = "/data/" + Names::FindName(producers[i]);
//配对命名,一个producer和consumer具有一样的名字前缀,防止无关producer对兴趣包的响应
/
// install consumer app on consumer node c_i to request data from producer p_i //
/
ndn::AppHelper consumerHelper("ns3::ndn::ConsumerCbr");
consumerHelper.SetAttribute("Frequency", StringValue("10")); // 100 interests a second
consumerHelper.SetPrefix(prefix);
ApplicationContainer consumer = consumerHelper.Install(consumers[i]);
consumer.Start(Seconds(i)); // start consumers at 0s, 1s, 2s, 3s
consumer.Stop(Seconds(19 - i)); // stop consumers at 19s, 18s, 17s, 16s
//应用启动时间每一对不同,间隔1秒
///
// install producer app on producer node p_i //
///
ndn::AppHelper producerHelper("ns3::ndn::Producer");
producerHelper.SetAttribute("PayloadSize", StringValue("1024"));
// install producer that will satisfy Interests in /dst1 namespace
producerHelper.SetPrefix(prefix);
ApplicationContainer producer = producerHelper.Install(producers[i]);
// when Start/Stop time is not specified, the application is running throughout the simulation
}
// Manually configure FIB routes
//注意!这里没有用gloubalroutinghelper而是手动配置了路由
//这两种方法是等效的,官网有说明
ndn::FibHelper::AddRoute("c1", "/data", "n1", 1); // link to n1
ndn::FibHelper::AddRoute("c2", "/data", "n1", 1); // link to n1
ndn::FibHelper::AddRoute("c3", "/data", "n1", 1); // link to n1
ndn::FibHelper::AddRoute("c4", "/data", "n1", 1); // link to n1
ndn::FibHelper::AddRoute("n1", "/data", "n2", 1); // link to n2
ndn::FibHelper::AddRoute("n1", "/data", "n12", 2); // link to n12
ndn::FibHelper::AddRoute("n12", "/data", "n2", 1); // link to n2
ndn::FibHelper::AddRoute("n2", "/data/p1", "p1", 1); // link to p1
ndn::FibHelper::AddRoute("n2", "/data/p2", "p2", 1); // link to p2
ndn::FibHelper::AddRoute("n2", "/data/p3", "p3", 1); // link to p3
ndn::FibHelper::AddRoute("n2", "/data/p4", "p4", 1); // link to p4
// Schedule simulation time and run the simulation
//simulator终止时间没什么用,过了20秒是producer和consumer自己stop了,其他实验(e.g ndn-//simple)里根本不会停止
Simulator::Stop(Seconds(20.0));
Simulator::Run();
Simulator::Destroy();
return 0;
}
} // namespace ns3
int
main(int argc, char* argv[])
{
return ns3::main(argc, argv);
}
**
**
git clone --recursive https://github.com/named-data-ndnSIM/scenario-template--recursive myscenario
即可创建路径为ndnSIM/myscenario的属于你自己的模板
然后可以把自己的扩展库放在extensions里,仿真代码放在scenario里
**
**
编译的时候小心几个地方
cd ndnSIM
cd ns-3
./waf configure -d optimized
./waf
sudo ./waf install
否则error停不下来的
cd ndnSIM
cd myscenario
./waf configure --debug
./waf
否则error来得奇奇怪怪,显示跟usr/下某个路径里的文件有冲突,博主我费了一番功夫解决不掉,最后误打误撞搞定了。