A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
third.cc
Go to the documentation of this file.
1/*
2 * SPDX-License-Identifier: GPL-2.0-only
3 */
4
5#include "ns3/applications-module.h"
6#include "ns3/core-module.h"
7#include "ns3/csma-module.h"
8#include "ns3/internet-module.h"
9#include "ns3/mobility-module.h"
10#include "ns3/network-module.h"
11#include "ns3/point-to-point-module.h"
12#include "ns3/ssid.h"
13#include "ns3/yans-wifi-helper.h"
14
15// Default Network Topology
16//
17// Wifi 10.1.3.0
18// AP
19// * * * *
20// | | | | 10.1.1.0
21// n5 n6 n7 n0 -------------- n1 n2 n3 n4
22// point-to-point | | | |
23// ================
24// LAN 10.1.2.0
25
26using namespace ns3;
27
28NS_LOG_COMPONENT_DEFINE("ThirdScriptExample");
29
30int
31main(int argc, char* argv[])
32{
33 bool verbose = true;
34 uint32_t nCsma = 3;
35 uint32_t nWifi = 3;
36 bool tracing = false;
37
38 CommandLine cmd(__FILE__);
39 cmd.AddValue("nCsma", "Number of \"extra\" CSMA nodes/devices", nCsma);
40 cmd.AddValue("nWifi", "Number of wifi STA devices", nWifi);
41 cmd.AddValue("verbose", "Tell echo applications to log if true", verbose);
42 cmd.AddValue("tracing", "Enable pcap tracing", tracing);
43
44 cmd.Parse(argc, argv);
45
46 // The underlying restriction of 18 is due to the grid position
47 // allocator's configuration; the grid layout will exceed the
48 // bounding box if more than 18 nodes are provided.
49 if (nWifi > 18)
50 {
51 std::cout << "nWifi should be 18 or less; otherwise grid layout exceeds the bounding box"
52 << std::endl;
53 return 1;
54 }
55
56 if (verbose)
57 {
58 LogComponentEnable("UdpEchoClientApplication", LOG_LEVEL_INFO);
59 LogComponentEnable("UdpEchoServerApplication", LOG_LEVEL_INFO);
60 }
61
63 p2pNodes.Create(2);
64
66 pointToPoint.SetDeviceAttribute("DataRate", StringValue("5Mbps"));
67 pointToPoint.SetChannelAttribute("Delay", StringValue("2ms"));
68
70 p2pDevices = pointToPoint.Install(p2pNodes);
71
73 csmaNodes.Add(p2pNodes.Get(1));
74 csmaNodes.Create(nCsma);
75
77 csma.SetChannelAttribute("DataRate", StringValue("100Mbps"));
78 csma.SetChannelAttribute("Delay", TimeValue(NanoSeconds(6560)));
79
81 csmaDevices = csma.Install(csmaNodes);
82
84 wifiStaNodes.Create(nWifi);
86
89 phy.SetChannel(channel.Create());
90
92 Ssid ssid = Ssid("ns-3-ssid");
93
95
97 mac.SetType("ns3::StaWifiMac", "Ssid", SsidValue(ssid), "ActiveProbing", BooleanValue(false));
98 staDevices = wifi.Install(phy, mac, wifiStaNodes);
99
101 mac.SetType("ns3::ApWifiMac", "Ssid", SsidValue(ssid));
102 apDevices = wifi.Install(phy, mac, wifiApNode);
103
105
106 mobility.SetPositionAllocator("ns3::GridPositionAllocator",
107 "MinX",
108 DoubleValue(0.0),
109 "MinY",
110 DoubleValue(0.0),
111 "DeltaX",
112 DoubleValue(5.0),
113 "DeltaY",
114 DoubleValue(10.0),
115 "GridWidth",
116 UintegerValue(3),
117 "LayoutType",
118 StringValue("RowFirst"));
119
120 mobility.SetMobilityModel("ns3::RandomWalk2dMobilityModel",
121 "Bounds",
122 RectangleValue(Rectangle(-50, 50, -50, 50)));
123 mobility.Install(wifiStaNodes);
124
125 mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
126 mobility.Install(wifiApNode);
127
129 stack.Install(csmaNodes);
130 stack.Install(wifiApNode);
131 stack.Install(wifiStaNodes);
132
134
135 address.SetBase("10.1.1.0", "255.255.255.0");
137 p2pInterfaces = address.Assign(p2pDevices);
138
139 address.SetBase("10.1.2.0", "255.255.255.0");
141 csmaInterfaces = address.Assign(csmaDevices);
142
143 address.SetBase("10.1.3.0", "255.255.255.0");
144 address.Assign(staDevices);
145 address.Assign(apDevices);
146
148
150 serverApps.Start(Seconds(1.0));
151 serverApps.Stop(Seconds(10.0));
152
153 UdpEchoClientHelper echoClient(csmaInterfaces.GetAddress(nCsma), 9);
154 echoClient.SetAttribute("MaxPackets", UintegerValue(1));
155 echoClient.SetAttribute("Interval", TimeValue(Seconds(1.0)));
156 echoClient.SetAttribute("PacketSize", UintegerValue(1024));
157
158 ApplicationContainer clientApps = echoClient.Install(wifiStaNodes.Get(nWifi - 1));
159 clientApps.Start(Seconds(2.0));
160 clientApps.Stop(Seconds(10.0));
161
163
165
166 if (tracing)
167 {
168 phy.SetPcapDataLinkType(WifiPhyHelper::DLT_IEEE802_11_RADIO);
169 pointToPoint.EnablePcapAll("third");
170 phy.EnablePcap("third", apDevices.Get(0));
171 csma.EnablePcap("third", csmaDevices.Get(0), true);
172 }
173
176 return 0;
177}
holds a vector of ns3::Application pointers.
Parse command-line arguments.
build a set of CsmaNetDevice objects
Definition csma-helper.h:37
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition double.h:31
aggregate IP/TCP/UDP functionality to existing Nodes.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
static void PopulateRoutingTables()
Build a routing database and initialize the routing tables of the nodes in the simulation.
holds a vector of std::pair of Ptr<Ipv4> and interface index.
Helper class used to assign positions and mobility models to nodes.
holds a vector of ns3::NetDevice pointers
keep track of a set of node pointers.
Build a set of PointToPointNetDevice objects.
a 2d rectangle
Definition rectangle.h:24
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static void Run()
Run the simulation.
Definition simulator.cc:167
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition simulator.cc:175
The IEEE 802.11 SSID Information Element.
Definition ssid.h:25
Hold variables of type string.
Definition string.h:45
Create an application which sends a UDP packet and waits for an echo of this packet.
Create a server application which waits for input UDP packets and sends them back to the original sen...
Hold an unsigned integer type.
Definition uinteger.h:34
helps to create WifiNetDevice objects
create MAC layers for a ns3::WifiNetDevice.
@ DLT_IEEE802_11_RADIO
Include Radiotap link layer information.
manage and create wifi channel objects for the YANS model.
static YansWifiChannelHelper Default()
Create a channel helper in a default working state.
Make it easy to create and manage PHY objects for the YANS model.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
Time NanoSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition nstime.h:1344
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
echoClient
Definition first.py:48
address
Definition first.py:36
serverApps
Definition first.py:43
pointToPoint
Definition first.py:27
echoServer
Definition first.py:41
clientApps
Definition first.py:53
stack
Definition first.py:33
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void LogComponentEnable(const std::string &name, LogLevel level)
Enable the logging output associated with that log component.
Definition log.cc:291
@ LOG_LEVEL_INFO
LOG_INFO and above.
Definition log.h:93
p2pNodes
Definition second.py:39
p2pInterfaces
Definition second.py:64
csmaInterfaces
Definition second.py:67
csmaNodes
Definition second.py:42
p2pDevices
Definition second.py:50
nCsma
Definition second.py:27
csmaDevices
Definition second.py:56
staDevices
Definition third.py:87
ssid
Definition third.py:82
channel
Definition third.py:77
nWifi
Definition third.py:32
mac
Definition third.py:81
wifi
Definition third.py:84
apDevices
Definition third.py:90
wifiApNode
Definition third.py:75
mobility
Definition third.py:92
wifiStaNodes
Definition third.py:73
phy
Definition third.py:78
bool verbose
bool tracing
Flag to enable/disable generation of tracing files.