A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
wireless-animation.cc
Go to the documentation of this file.
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License version 2 as
4 * published by the Free Software Foundation;
5 *
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
10 *
11 * You should have received a copy of the GNU General Public License
12 * along with this program; if not, write to the Free Software
13 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14 *
15 * Author: Vikas Pushkar (Adapted from third.cc)
16 */
17
18#include "ns3/applications-module.h"
19#include "ns3/basic-energy-source.h"
20#include "ns3/core-module.h"
21#include "ns3/csma-module.h"
22#include "ns3/internet-module.h"
23#include "ns3/mobility-module.h"
24#include "ns3/netanim-module.h"
25#include "ns3/network-module.h"
26#include "ns3/point-to-point-module.h"
27#include "ns3/simple-device-energy-model.h"
28#include "ns3/ssid.h"
29#include "ns3/wifi-radio-energy-model.h"
30#include "ns3/yans-wifi-helper.h"
31
32using namespace ns3;
33using namespace ns3::energy;
34
35NS_LOG_COMPONENT_DEFINE("WirelessAnimationExample");
36
37int
38main(int argc, char* argv[])
39{
40 uint32_t nWifi = 20;
41 CommandLine cmd(__FILE__);
42 cmd.AddValue("nWifi", "Number of wifi STA devices", nWifi);
43
44 cmd.Parse(argc, argv);
45 NodeContainer allNodes;
47 wifiStaNodes.Create(nWifi);
48 allNodes.Add(wifiStaNodes);
50 wifiApNode.Create(1);
51 allNodes.Add(wifiApNode);
52
55 phy.SetChannel(channel.Create());
56
58
60 Ssid ssid = Ssid("ns-3-ssid");
61 mac.SetType("ns3::StaWifiMac", "Ssid", SsidValue(ssid), "ActiveProbing", BooleanValue(false));
62
64 staDevices = wifi.Install(phy, mac, wifiStaNodes);
65 mac.SetType("ns3::ApWifiMac", "Ssid", SsidValue(ssid));
66
68 apDevices = wifi.Install(phy, mac, wifiApNode);
69
71 p2pNodes.Add(wifiApNode);
72 p2pNodes.Create(1);
73 allNodes.Add(p2pNodes.Get(1));
74
76 pointToPoint.SetDeviceAttribute("DataRate", StringValue("5Mbps"));
77 pointToPoint.SetChannelAttribute("Delay", StringValue("2ms"));
78
80 p2pDevices = pointToPoint.Install(p2pNodes);
81
83 csmaNodes.Add(p2pNodes.Get(1));
84 csmaNodes.Create(1);
85 allNodes.Add(csmaNodes.Get(1));
86
88 csma.SetChannelAttribute("DataRate", StringValue("100Mbps"));
89 csma.SetChannelAttribute("Delay", TimeValue(NanoSeconds(6560)));
90
92 csmaDevices = csma.Install(csmaNodes);
93
94 // Mobility
95
97 mobility.SetPositionAllocator("ns3::GridPositionAllocator",
98 "MinX",
99 DoubleValue(10.0),
100 "MinY",
101 DoubleValue(10.0),
102 "DeltaX",
103 DoubleValue(5.0),
104 "DeltaY",
105 DoubleValue(2.0),
106 "GridWidth",
107 UintegerValue(5),
108 "LayoutType",
109 StringValue("RowFirst"));
110 mobility.SetMobilityModel("ns3::RandomWalk2dMobilityModel",
111 "Bounds",
112 RectangleValue(Rectangle(-50, 50, -25, 50)));
113 mobility.Install(wifiStaNodes);
114 mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
115 mobility.Install(wifiApNode);
118
119 Ptr<BasicEnergySource> energySource = CreateObject<BasicEnergySource>();
120 Ptr<WifiRadioEnergyModel> energyModel = CreateObject<WifiRadioEnergyModel>();
121
122 energySource->SetInitialEnergy(300);
123 energyModel->SetEnergySource(energySource);
124 energySource->AppendDeviceEnergyModel(energyModel);
125
126 // aggregate energy source to node
127 wifiApNode.Get(0)->AggregateObject(energySource);
128
129 // Install internet stack
130
132 stack.Install(allNodes);
133
134 // Install Ipv4 addresses
135
137 address.SetBase("10.1.1.0", "255.255.255.0");
139 p2pInterfaces = address.Assign(p2pDevices);
140 address.SetBase("10.1.2.0", "255.255.255.0");
142 csmaInterfaces = address.Assign(csmaDevices);
143 address.SetBase("10.1.3.0", "255.255.255.0");
144 Ipv4InterfaceContainer staInterfaces;
145 staInterfaces = address.Assign(staDevices);
146 Ipv4InterfaceContainer apInterface;
147 apInterface = address.Assign(apDevices);
148
149 // Install applications
150
153 serverApps.Start(Seconds(1.0));
154 serverApps.Stop(Seconds(15.0));
156 echoClient.SetAttribute("MaxPackets", UintegerValue(10));
157 echoClient.SetAttribute("Interval", TimeValue(Seconds(1.)));
158 echoClient.SetAttribute("PacketSize", UintegerValue(1024));
159 ApplicationContainer clientApps = echoClient.Install(wifiStaNodes);
160 clientApps.Start(Seconds(2.0));
161 clientApps.Stop(Seconds(15.0));
162
165
166 AnimationInterface anim("wireless-animation.xml"); // Mandatory
167 for (uint32_t i = 0; i < wifiStaNodes.GetN(); ++i)
168 {
169 anim.UpdateNodeDescription(wifiStaNodes.Get(i), "STA"); // Optional
170 anim.UpdateNodeColor(wifiStaNodes.Get(i), 255, 0, 0); // Optional
171 }
172 for (uint32_t i = 0; i < wifiApNode.GetN(); ++i)
173 {
174 anim.UpdateNodeDescription(wifiApNode.Get(i), "AP"); // Optional
175 anim.UpdateNodeColor(wifiApNode.Get(i), 0, 255, 0); // Optional
176 }
177 for (uint32_t i = 0; i < csmaNodes.GetN(); ++i)
178 {
179 anim.UpdateNodeDescription(csmaNodes.Get(i), "CSMA"); // Optional
180 anim.UpdateNodeColor(csmaNodes.Get(i), 0, 0, 255); // Optional
181 }
182
183 anim.EnablePacketMetadata(); // Optional
184 anim.EnableIpv4RouteTracking("routingtable-wireless.xml",
185 Seconds(0),
186 Seconds(5),
187 Seconds(0.25)); // Optional
188 anim.EnableWifiMacCounters(Seconds(0), Seconds(10)); // Optional
189 anim.EnableWifiPhyCounters(Seconds(0), Seconds(10)); // Optional
192 return 0;
193}
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.
Definition: command-line.h:232
build a set of CsmaNetDevice objects
Definition: csma-helper.h:48
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition: double.h:42
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.
Definition: ptr.h:77
a 2d rectangle
Definition: rectangle.h:35
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static void Run()
Run the simulation.
Definition: simulator.cc:178
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:186
The IEEE 802.11 SSID Information Element.
Definition: ssid.h:36
Hold variables of type string.
Definition: string.h:56
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:45
helps to create WifiNetDevice objects
Definition: wifi-helper.h:324
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:202
Time NanoSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1355
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1319
AnimationInterface * anim
ns echoServer
Definition: first.py:52
ns serverApps
Definition: first.py:54
ns echoClient
Definition: first.py:59
ns clientApps
Definition: first.py:64
ns address
Definition: first.py:47
ns stack
Definition: first.py:44
ns pointToPoint
Definition: first.py:38
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns cmd
Definition: second.py:40
ns csmaInterfaces
Definition: second.py:78
ns p2pNodes
Definition: second.py:50
ns csmaNodes
Definition: second.py:53
ns csma
Definition: second.py:63
ns p2pDevices
Definition: second.py:61
ns p2pInterfaces
Definition: second.py:75
ns csmaDevices
Definition: second.py:67
ns wifi
Definition: third.py:95
ns ssid
Definition: third.py:93
ns apDevices
Definition: third.py:101
ns staDevices
Definition: third.py:98
c_int nWifi
Definition: third.py:43
ns mac
Definition: third.py:92
ns wifiApNode
Definition: third.py:86
ns channel
Definition: third.py:88
ns mobility
Definition: third.py:103
ns wifiStaNodes
Definition: third.py:84
ns phy
Definition: third.py:89