-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathexample-ns3.cc
More file actions
179 lines (145 loc) · 6.13 KB
/
example-ns3.cc
File metadata and controls
179 lines (145 loc) · 6.13 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
* Copyright (c) 2024 Yannik Pilz
*
* SPDX-License-Identifier: GPL-2.0-only
*
* Author: Yannik Pilz <y.pilz@campus.tu-berlin.de>
*/
#include "ns3/applications-module.h"
#include "ns3/buildings-module.h"
#include "ns3/core-module.h"
#include "ns3/internet-module.h"
#include "ns3/mobility-module.h"
#include "ns3/network-module.h"
#include "ns3/propagation-delay-model.h"
#include "ns3/ssid.h"
#include "ns3/yans-wifi-helper.h"
using namespace ns3;
NS_LOG_COMPONENT_DEFINE("ExampleNs3");
int
main(int argc, char* argv[])
{
bool verbose = true;
double frequency = 2437e6;
bool tracing = false;
CommandLine cmd(__FILE__);
cmd.AddValue("verbose", "Enable logging", verbose);
cmd.AddValue("frequency", "Frequency to be used in the simulation", frequency);
cmd.AddValue("tracing", "Enable pcap tracing", tracing);
cmd.Parse(argc, argv);
if (verbose)
{
LogComponentEnable("UdpEchoClientApplication", LOG_LEVEL_INFO);
LogComponentEnable("UdpEchoServerApplication", LOG_LEVEL_INFO);
LogComponentEnable("YansWifiChannel", LOG_DEBUG);
LogComponentEnable("YansWifiChannel", LOG_PREFIX_TIME);
LogComponentEnable("HybridBuildingsPropagationLossModel", LOG_INFO);
LogComponentEnable("BuildingsPropagationLossModel", LOG_INFO);
LogComponentEnable("ItuR1238PropagationLossModel", LOG_INFO);
}
std::cout << "Example scenario with ns3" << std::endl << std::endl;
// Create a building
double x_min = 0.0;
double x_max = 6.0;
double y_min = 0.0;
double y_max = 4.0;
double z_min = 0.0;
double z_max = 2.5;
Ptr<Building> b = CreateObject<Building>();
b->SetBoundaries(Box(x_min, x_max, y_min, y_max, z_min, z_max));
b->SetBuildingType(Building::Office);
b->SetExtWallsType(Building::StoneBlocks);
b->SetNFloors(1);
b->SetNRoomsX(1);
b->SetNRoomsY(1);
// Create nodes
NodeContainer wifiStaNodes;
wifiStaNodes.Create(2);
NodeContainer wifiApNode;
wifiApNode.Create(1);
// Create a channel
Ptr<YansWifiChannel> channel = CreateObject<YansWifiChannel>();
Ptr<ConstantSpeedPropagationDelayModel> delayModel = CreateObject<ConstantSpeedPropagationDelayModel>();
Ptr<HybridBuildingsPropagationLossModel> lossModel = CreateObject<HybridBuildingsPropagationLossModel>();
lossModel->SetFrequency(frequency);
channel->SetPropagationLossModel(lossModel);
channel->SetPropagationDelayModel(delayModel);
// WiFi configuration
YansWifiPhyHelper phy;
phy.SetChannel(channel);
WifiMacHelper mac;
Ssid ssid = Ssid("ns-3-ssid");
WifiHelper wifi;
NetDeviceContainer staDevices;
mac.SetType("ns3::StaWifiMac", "Ssid", SsidValue(ssid), "ActiveProbing", BooleanValue(false));
staDevices = wifi.Install(phy, mac, wifiStaNodes);
NetDeviceContainer apDevices;
mac.SetType("ns3::ApWifiMac", "Ssid", SsidValue(ssid), "BeaconGeneration", BooleanValue(true), "BeaconInterval", TimeValue(Seconds(5.120)), "EnableBeaconJitter", BooleanValue(false));
apDevices = wifi.Install(phy, mac, wifiApNode);
// Mobility configuration
MobilityHelper mobility;
mobility.Install(wifiStaNodes);
mobility.Install(wifiApNode);
BuildingsHelper::Install(wifiStaNodes);
BuildingsHelper::Install(wifiApNode);
wifiStaNodes.Get(0)->GetObject<MobilityModel>()->SetPosition(Vector(5.0, 2.05, 1.0));
wifiStaNodes.Get(1)->GetObject<MobilityModel>()->SetPosition(Vector(5.0, 1.95, 1.0));
wifiApNode.Get(0)->GetObject<MobilityModel>()->SetPosition(Vector(1.0, 2.0, 1.0));
// Set up Internet stack and assign IP addresses
InternetStackHelper stack;
stack.Install(wifiApNode);
stack.Install(wifiStaNodes);
Ipv4AddressHelper address;
address.SetBase("10.1.1.0", "255.255.255.0");
Ipv4InterfaceContainer wifiStaInterfaces = address.Assign(staDevices);
Ipv4InterfaceContainer wifiApInterfaces = address.Assign(apDevices);
// Set up applications
UdpEchoServerHelper echoServer(9);
ApplicationContainer serverApps = echoServer.Install(wifiApNode);
serverApps.Start(Seconds(1.0));
serverApps.Stop(Seconds(10.0));
UdpEchoClientHelper echoClient(wifiApInterfaces.GetAddress(0), 9);
echoClient.SetAttribute("MaxPackets", UintegerValue(2));
echoClient.SetAttribute("Interval", TimeValue(Seconds(1.0)));
echoClient.SetAttribute("PacketSize", UintegerValue(1024));
ApplicationContainer clientApps = echoClient.Install(wifiStaNodes);
clientApps.Start(Seconds(2.0));
clientApps.Stop(Seconds(10.0));
Ipv4GlobalRoutingHelper::PopulateRoutingTables();
// Tracing
if (tracing)
{
phy.SetPcapDataLinkType(WifiPhyHelper::DLT_IEEE802_11_RADIO);
phy.EnablePcap("example-ns3", apDevices.Get(0));
phy.EnablePcap("example-ns3", staDevices.Get(0));
phy.EnablePcap("example-ns3", staDevices.Get(1));
}
if (verbose)
{
// Print node information
std::cout << "----------Node Information----------" << std::endl;
NodeContainer c = NodeContainer::GetGlobal();
for (auto iter = c.Begin(); iter != c.End(); ++iter)
{
std::cout << "NodeID: " << (*iter)->GetId() << ", ";
Ptr<MobilityModel> mobilityModel = (*iter)->GetObject<MobilityModel>();
if (mobilityModel)
{
std::cout << mobilityModel->GetInstanceTypeId().GetName() << " (";
Vector position = mobilityModel->GetPosition();
Vector velocity = mobilityModel->GetVelocity();
std::cout << "Pos: [" << position.x << ", " << position.y << ", " << position.z << "]" << ", ";
std::cout << "Vel: [" << velocity.x << ", " << velocity.y << ", " << velocity.z << "])" << std::endl;
}
else
{
std::cout << "No MobilityModel" << std::endl;
}
}
}
// Simulation
Simulator::Stop(Seconds(5.0));
Simulator::Run();
Simulator::Destroy();
return 0;
}