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