-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathhoc.cc
137 lines (110 loc) · 3.54 KB
/
hoc.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/applications-module.h"
#include "ns3/wifi-module.h"
#include "ns3/mobility-module.h"
#include "ns3/internet-module.h"
#include "ns3/yans-wifi-helper.h"
#include "ns3/ssid.h"
#include "ns3/netanim-module.h"
using namespace ns3;
NS_LOG_COMPONENT_DEFINE("AdHocNetwork");
int main(int argc, char *argv[]) {
// 设置时间分辨率
Time::SetResolution(Time::NS);
// 激活日志组件
LogComponentEnable("AdHocNetwork", LOG_LEVEL_INFO);
LogComponentEnable("PacketSink", LOG_LEVEL_ALL);
// ad hoc 设备数量
uint32_t nAdHoc = 6;
// 命令行参数
CommandLine cmd;
cmd.AddValue("nAdHoc", "Number of WiFi AD devices", nAdHoc);
cmd.Parse(argc, argv);
// 节点
NodeContainer adHocNodes;
adHocNodes.Create(nAdHoc);
// 创建信道和物理信息
YansWifiChannelHelper channel = YansWifiChannelHelper::Default();
YansWifiPhyHelper phy = YansWifiPhyHelper::Default();
phy.SetChannel(channel.Create());
// 创建 WiFi
WifiHelper wifi;
wifi.SetStandard(WIFI_PHY_STANDARD_80211a);
wifi.SetRemoteStationManager(
"ns3::ConstantRateWifiManager",
"DataMode",
StringValue("OfdmRate6Mbps")
);
// 设置 mac
WifiMacHelper mac;
mac.SetType(
"ns3::AdhocWifiMac",
"Slot",
StringValue("0.1s")
);
// 创建设备
NetDeviceContainer adHocDevices;
adHocDevices = wifi.Install(phy, mac, adHocNodes);
// 位置信息
MobilityHelper mobility;
mobility.SetPositionAllocator(
"ns3::GridPositionAllocator",
"MinX", DoubleValue(0.0),
"MinY", DoubleValue(0.0),
"DeltaX", DoubleValue(5.0),
"DeltaY", DoubleValue(5.0),
"GridWidth", UintegerValue(10),
"LayoutType", StringValue("RowFirst")
);
mobility.SetMobilityModel(
"ns3::RandomWalk2dMobilityModel",
"Bounds",
RectangleValue(
Rectangle(-500, 500, -500, 500)
)
);
mobility.Install(adHocNodes);
// 安装 internet 协议栈
InternetStackHelper internet;
internet.Install(adHocNodes);
// 设置 ip 地址
Ipv4AddressHelper address;
address.SetBase("10.1.1.0", "255.255.255.0");
Ipv4InterfaceContainer interface;
interface = address.Assign(adHocDevices);
// 应用层
NS_LOG_INFO("Create Applications");
uint16_t port = 9999;
OnOffHelper onOff1(
"ns3::TcpSocketFactory",
Address(InetSocketAddress(interface.GetAddress(0), port))
);
onOff1.SetAttribute("OnTime", StringValue("ns3::ConstantRandomVariable[Constant=1]"));
onOff1.SetAttribute("OffTime", StringValue("ns3::ConstantRandomVariable[Constant=0]"));
ApplicationContainer apps1 = onOff1.Install(adHocNodes);
apps1.Start(Seconds(1.0));
apps1.Stop(Seconds(15.0));
PacketSinkHelper sinkHelper(
"ns3::TcpSocketFactory",
Address(InetSocketAddress(Ipv4Address::GetAny(), port))
);
ApplicationContainer apps2 = sinkHelper.Install(adHocNodes.Get(0));
apps2.Start(Seconds(1.0));
apps2.Stop(Seconds(15.0));
// 全局路由表
Ipv4GlobalRoutingHelper::PopulateRoutingTables();
// trace
AsciiTraceHelper ascii;
phy.EnableAsciiAll(ascii.CreateFileStream("hoc.tr"));
// 先停止模拟器
Simulator::Stop(Seconds(15.0));
// pcap
phy.EnablePcap("hoc", adHocDevices.Get(0));
// NetAnim
AnimationInterface anim("hoc.xml");
// 启动模拟器
Simulator::Run();
Simulator::Destroy();
return 0;
}