A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
wireless-animation.cc
Go to the documentation of this file.
1/*
2 * SPDX-License-Identifier: GPL-2.0-only
3 *
4 * Author: Vikas Pushkar (Adapted from third.cc)
5 */
6
7#include "ns3/applications-module.h"
8#include "ns3/basic-energy-source.h"
9#include "ns3/core-module.h"
10#include "ns3/csma-module.h"
11#include "ns3/internet-module.h"
12#include "ns3/mobility-module.h"
13#include "ns3/netanim-module.h"
14#include "ns3/network-module.h"
15#include "ns3/point-to-point-module.h"
16#include "ns3/simple-device-energy-model.h"
17#include "ns3/ssid.h"
18#include "ns3/wifi-radio-energy-model.h"
19#include "ns3/yans-wifi-helper.h"
20
21using namespace ns3;
22using namespace ns3::energy;
23
24NS_LOG_COMPONENT_DEFINE("WirelessAnimationExample");
25
26int
27main(int argc, char* argv[])
28{
29 uint32_t nWifi = 20;
30 CommandLine cmd(__FILE__);
31 cmd.AddValue("nWifi", "Number of wifi STA devices", nWifi);
32
33 cmd.Parse(argc, argv);
34 NodeContainer allNodes;
36 wifiStaNodes.Create(nWifi);
37 allNodes.Add(wifiStaNodes);
39 wifiApNode.Create(1);
40 allNodes.Add(wifiApNode);
41
44 phy.SetChannel(channel.Create());
45
47
49 Ssid ssid = Ssid("ns-3-ssid");
50 mac.SetType("ns3::StaWifiMac", "Ssid", SsidValue(ssid), "ActiveProbing", BooleanValue(false));
51
53 staDevices = wifi.Install(phy, mac, wifiStaNodes);
54 mac.SetType("ns3::ApWifiMac", "Ssid", SsidValue(ssid));
55
57 apDevices = wifi.Install(phy, mac, wifiApNode);
58
60 p2pNodes.Add(wifiApNode);
61 p2pNodes.Create(1);
62 allNodes.Add(p2pNodes.Get(1));
63
65 pointToPoint.SetDeviceAttribute("DataRate", StringValue("5Mbps"));
66 pointToPoint.SetChannelAttribute("Delay", StringValue("2ms"));
67
69 p2pDevices = pointToPoint.Install(p2pNodes);
70
72 csmaNodes.Add(p2pNodes.Get(1));
73 csmaNodes.Create(1);
74 allNodes.Add(csmaNodes.Get(1));
75
77 csma.SetChannelAttribute("DataRate", StringValue("100Mbps"));
78 csma.SetChannelAttribute("Delay", TimeValue(NanoSeconds(6560)));
79
81 csmaDevices = csma.Install(csmaNodes);
82
83 // Mobility
84
86 mobility.SetPositionAllocator("ns3::GridPositionAllocator",
87 "MinX",
88 DoubleValue(10.0),
89 "MinY",
90 DoubleValue(10.0),
91 "DeltaX",
92 DoubleValue(5.0),
93 "DeltaY",
94 DoubleValue(2.0),
95 "GridWidth",
97 "LayoutType",
98 StringValue("RowFirst"));
99 mobility.SetMobilityModel("ns3::RandomWalk2dMobilityModel",
100 "Bounds",
101 RectangleValue(Rectangle(-50, 50, -25, 50)));
102 mobility.Install(wifiStaNodes);
103 mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
104 mobility.Install(wifiApNode);
107
110
111 energySource->SetInitialEnergy(300);
112 energyModel->SetEnergySource(energySource);
113 energySource->AppendDeviceEnergyModel(energyModel);
114
115 // aggregate energy source to node
116 wifiApNode.Get(0)->AggregateObject(energySource);
117
118 // Install internet stack
119
121 stack.Install(allNodes);
122
123 // Install Ipv4 addresses
124
126 address.SetBase("10.1.1.0", "255.255.255.0");
128 p2pInterfaces = address.Assign(p2pDevices);
129 address.SetBase("10.1.2.0", "255.255.255.0");
131 csmaInterfaces = address.Assign(csmaDevices);
132 address.SetBase("10.1.3.0", "255.255.255.0");
133 Ipv4InterfaceContainer staInterfaces;
134 staInterfaces = address.Assign(staDevices);
135 Ipv4InterfaceContainer apInterface;
136 apInterface = address.Assign(apDevices);
137
138 // Install applications
139
142 serverApps.Start(Seconds(1.0));
143 serverApps.Stop(Seconds(15.0));
145 echoClient.SetAttribute("MaxPackets", UintegerValue(10));
146 echoClient.SetAttribute("Interval", TimeValue(Seconds(1.)));
147 echoClient.SetAttribute("PacketSize", UintegerValue(1024));
148 ApplicationContainer clientApps = echoClient.Install(wifiStaNodes);
149 clientApps.Start(Seconds(2.0));
150 clientApps.Stop(Seconds(15.0));
151
154
155 AnimationInterface anim("wireless-animation.xml"); // Mandatory
156 for (uint32_t i = 0; i < wifiStaNodes.GetN(); ++i)
157 {
158 anim.UpdateNodeDescription(wifiStaNodes.Get(i), "STA"); // Optional
159 anim.UpdateNodeColor(wifiStaNodes.Get(i), 255, 0, 0); // Optional
160 }
161 for (uint32_t i = 0; i < wifiApNode.GetN(); ++i)
162 {
163 anim.UpdateNodeDescription(wifiApNode.Get(i), "AP"); // Optional
164 anim.UpdateNodeColor(wifiApNode.Get(i), 0, 255, 0); // Optional
165 }
166 for (uint32_t i = 0; i < csmaNodes.GetN(); ++i)
167 {
168 anim.UpdateNodeDescription(csmaNodes.Get(i), "CSMA"); // Optional
169 anim.UpdateNodeColor(csmaNodes.Get(i), 0, 0, 255); // Optional
170 }
171
172 anim.EnablePacketMetadata(); // Optional
173 anim.EnableIpv4RouteTracking("routingtable-wireless.xml",
174 Seconds(0),
175 Seconds(5),
176 Seconds(0.25)); // Optional
177 anim.EnableWifiMacCounters(Seconds(0), Seconds(10)); // Optional
178 anim.EnableWifiPhyCounters(Seconds(0), Seconds(10)); // Optional
181 return 0;
182}
Interface to network animator.
AnimationInterface & EnableIpv4RouteTracking(std::string fileName, Time startTime, Time stopTime, Time pollInterval=Seconds(5))
Enable tracking of the Ipv4 routing table for all Nodes.
void EnableWifiPhyCounters(Time startTime, Time stopTime, Time pollInterval=Seconds(1))
Enable tracking of Wifi Phy Counters such as TxDrop, RxDrop.
void EnablePacketMetadata(bool enable=true)
Enable Packet metadata.
void EnableWifiMacCounters(Time startTime, Time stopTime, Time pollInterval=Seconds(1))
Enable tracking of Wifi Mac Counters such as Tx, TxDrop, Rx, RxDrop.
void UpdateNodeDescription(Ptr< Node > n, std::string descr)
Helper function to update the description for a given node.
void UpdateNodeColor(Ptr< Node > n, uint8_t r, uint8_t g, uint8_t b)
Helper function to update the node color.
static void SetConstantPosition(Ptr< Node > n, double x, double y, double z=0)
Helper function to set Constant Position for a given node.
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.
void Add(const NodeContainer &nc)
Append the contents of another NodeContainer to the end of this container.
Build a set of PointToPointNetDevice objects.
Smart pointer class similar to boost::intrusive_ptr.
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.
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
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
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
AnimationInterface * anim
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.
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
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