A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
20 #include "ns3/core-module.h"
21 #include "ns3/point-to-point-module.h"
22 #include "ns3/csma-module.h"
23 #include "ns3/network-module.h"
24 #include "ns3/applications-module.h"
25 #include "ns3/wifi-module.h"
26 #include "ns3/mobility-module.h"
27 #include "ns3/internet-module.h"
28 #include "ns3/netanim-module.h"
29 
30 
31 
32 using namespace ns3;
33 
34 NS_LOG_COMPONENT_DEFINE ("WirelessAnimationExample");
35 
36 int
37 main (int argc, char *argv[])
38 {
39  uint32_t nWifi = 20;
40  CommandLine cmd;
41  cmd.AddValue ("nWifi", "Number of wifi STA devices", nWifi);
42 
43 
44  cmd.Parse (argc,argv);
45  NodeContainer allNodes;
46  NodeContainer wifiStaNodes;
47  wifiStaNodes.Create (nWifi);
48  allNodes.Add (wifiStaNodes);
49  NodeContainer wifiApNode ;
50  wifiApNode.Create (1);
51  allNodes.Add (wifiApNode);
52 
55  phy.SetChannel (channel.Create ());
56 
58  wifi.SetRemoteStationManager ("ns3::AarfWifiManager");
59 
61 
62  Ssid ssid = Ssid ("ns-3-ssid");
63  mac.SetType ("ns3::StaWifiMac",
64  "Ssid", SsidValue (ssid),
65  "ActiveProbing", BooleanValue (false));
66 
67  NetDeviceContainer staDevices;
68  staDevices = wifi.Install (phy, mac, wifiStaNodes);
69  mac.SetType ("ns3::ApWifiMac",
70  "Ssid", SsidValue (ssid));
71 
72  NetDeviceContainer apDevices;
73  apDevices = wifi.Install (phy, mac, wifiApNode);
74 
75 
76  NodeContainer p2pNodes;
77  p2pNodes.Add (wifiApNode);
78  p2pNodes.Create (1);
79  allNodes.Add (p2pNodes.Get (1));
80 
82  pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
83  pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));
84 
85  NetDeviceContainer p2pDevices;
86  p2pDevices = pointToPoint.Install (p2pNodes);
87 
88  NodeContainer csmaNodes;
89  csmaNodes.Add (p2pNodes.Get (1));
90  csmaNodes.Create (1);
91  allNodes.Add (csmaNodes.Get (1));
92 
93  CsmaHelper csma;
94  csma.SetChannelAttribute ("DataRate", StringValue ("100Mbps"));
95  csma.SetChannelAttribute ("Delay", TimeValue (NanoSeconds (6560)));
96 
97  NetDeviceContainer csmaDevices;
98  csmaDevices = csma.Install (csmaNodes);
99 
100  // Mobility
101 
102  MobilityHelper mobility;
103  mobility.SetPositionAllocator ("ns3::GridPositionAllocator",
104  "MinX", DoubleValue (10.0),
105  "MinY", DoubleValue (10.0),
106  "DeltaX", DoubleValue (5.0),
107  "DeltaY", DoubleValue (2.0),
108  "GridWidth", UintegerValue (5),
109  "LayoutType", StringValue ("RowFirst"));
110  mobility.SetMobilityModel ("ns3::RandomWalk2dMobilityModel",
111  "Bounds", RectangleValue (Rectangle (-50, 50, -25, 50)));
112  mobility.Install (wifiStaNodes);
113  mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
114  mobility.Install (wifiApNode);
115  AnimationInterface::SetConstantPosition (p2pNodes.Get (1), 10, 30);
116  AnimationInterface::SetConstantPosition (csmaNodes.Get (1), 10, 33);
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");
127  Ipv4InterfaceContainer p2pInterfaces;
128  p2pInterfaces = address.Assign (p2pDevices);
129  address.SetBase ("10.1.2.0", "255.255.255.0");
130  Ipv4InterfaceContainer csmaInterfaces;
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 
141  ApplicationContainer serverApps = echoServer.Install (csmaNodes.Get (1));
142  serverApps.Start (Seconds (1.0));
143  serverApps.Stop (Seconds (15.0));
144  UdpEchoClientHelper echoClient (csmaInterfaces.GetAddress (1), 9);
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 
153  Simulator::Stop (Seconds (15.0));
154  AnimationInterface::SetNodeDescription (wifiApNode, "AP"); // Optional
155  AnimationInterface::SetNodeDescription (wifiStaNodes, "STA"); // Optional
156  AnimationInterface::SetNodeDescription (csmaNodes, "CSMA"); // Optional
157  AnimationInterface::SetNodeColor (wifiApNode, 0, 255, 0); // Optional
158  AnimationInterface::SetNodeColor (wifiStaNodes, 255, 0, 0); // Optional
159  AnimationInterface::SetNodeColor (csmaNodes, 0, 0, 255); // Optional
160  AnimationInterface::SetBoundary (0, 0, 35, 35); // Optional
161 
162  AnimationInterface anim ("wireless-animation.xml"); // Mandatory
163 
164  anim.EnablePacketMetadata (true); // Optional
165  anim.EnableIpv4RouteTracking ("routingtable-wireless.xml", Seconds(0), Seconds(5), Seconds(0.25)); //Optional
166  Simulator::Run ();
168  return 0;
169 }
holds a vector of ns3::Application pointers.
tuple pointToPoint
Definition: first.py:28
void SetChannelAttribute(std::string n1, const AttributeValue &v1)
Definition: csma-helper.cc:69
Hold a bool native type.
Definition: boolean.h:38
holds a vector of std::pair of Ptr and interface index.
Ptr< YansWifiChannel > Create(void) const
void SetRemoteStationManager(std::string type, std::string n0="", const AttributeValue &v0=EmptyAttributeValue(), std::string n1="", const AttributeValue &v1=EmptyAttributeValue(), std::string n2="", const AttributeValue &v2=EmptyAttributeValue(), std::string n3="", const AttributeValue &v3=EmptyAttributeValue(), std::string n4="", const AttributeValue &v4=EmptyAttributeValue(), std::string n5="", const AttributeValue &v5=EmptyAttributeValue(), std::string n6="", const AttributeValue &v6=EmptyAttributeValue(), std::string n7="", const AttributeValue &v7=EmptyAttributeValue())
Definition: wifi-helper.cc:69
static void PopulateRoutingTables(void)
Build a routing database and initialize the routing tables of the nodes in the simulation. Makes all nodes in the simulation into routers.
hold variables of type string
Definition: string.h:19
Make it easy to create and manage PHY objects for the yans model.
static YansWifiChannelHelper Default(void)
NetDeviceContainer Install(NodeContainer c)
void EnablePacketMetadata(bool enable)
Enable Packet metadata.
void SetType(std::string type, std::string n0="", const AttributeValue &v0=EmptyAttributeValue(), std::string n1="", const AttributeValue &v1=EmptyAttributeValue(), std::string n2="", const AttributeValue &v2=EmptyAttributeValue(), std::string n3="", const AttributeValue &v3=EmptyAttributeValue(), std::string n4="", const AttributeValue &v4=EmptyAttributeValue(), std::string n5="", const AttributeValue &v5=EmptyAttributeValue(), std::string n6="", const AttributeValue &v6=EmptyAttributeValue(), std::string n7="", const AttributeValue &v7=EmptyAttributeValue())
tuple echoServer
Definition: first.py:42
static void SetNodeColor(Ptr< Node > n, uint8_t r, uint8_t g, uint8_t b)
Helper function to set the node color.
create an application which sends a udp packet and waits for an echo of this packet ...
static void Run(void)
Definition: simulator.cc:157
aggregate IP/TCP/UDP functionality to existing Nodes.
NetDeviceContainer Install(Ptr< Node > node) const
Definition: csma-helper.cc:215
Build a set of PointToPointNetDevice objects.
static YansWifiPhyHelper Default(void)
void SetDeviceAttribute(std::string name, const AttributeValue &value)
helps to create WifiNetDevice objects
Definition: wifi-helper.h:88
NetDeviceContainer Install(const WifiPhyHelper &phy, const WifiMacHelper &mac, NodeContainer c) const
Definition: wifi-helper.cc:98
tuple clientApps
Definition: first.py:53
ApplicationContainer Install(Ptr< Node > node) const
static void SetConstantPosition(Ptr< Node > n, double x, double y, double z=0)
Helper function to set Constant Position for a given node.
hold objects of type ns3::Rectangle
void SetChannel(Ptr< YansWifiChannel > channel)
static void SetBoundary(double minX, double minY, double maxX, double maxY)
Helper function to set the topology boundary rectangle.
Create a server application which waits for input udp packets and sends them back to the original sen...
void Install(Ptr< Node > node) const
"Layout" a single node according to the current position allocator type.
hold objects of type ns3::Time
Definition: nstime.h:828
Hold an unsigned integer type.
Definition: uinteger.h:46
holds a vector of ns3::NetDevice pointers
static void SetNodeDescription(Ptr< Node > n, std::string descr)
Helper function to set a brief description for a given node.
static NqosWifiMacHelper Default(void)
AnimationInterface & EnableIpv4RouteTracking(std::string fileName, Time startTime, Time stopTime, Time pollInterval=Seconds(5))
Enable tracking of the Ipv4 routing table for all Nodes.
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter...
create non QoS-enabled MAC layers for a ns3::WifiNetDevice.
Parse command-line arguments.
Definition: command-line.h:152
static void Destroy(void)
Definition: simulator.cc:121
tuple echoClient
Definition: first.py:48
tuple serverApps
Definition: first.py:44
keep track of a set of node pointers.
void SetMobilityModel(std::string type, std::string n1="", const AttributeValue &v1=EmptyAttributeValue(), std::string n2="", const AttributeValue &v2=EmptyAttributeValue(), std::string n3="", const AttributeValue &v3=EmptyAttributeValue(), std::string n4="", const AttributeValue &v4=EmptyAttributeValue(), std::string n5="", const AttributeValue &v5=EmptyAttributeValue(), std::string n6="", const AttributeValue &v6=EmptyAttributeValue(), std::string n7="", const AttributeValue &v7=EmptyAttributeValue(), std::string n8="", const AttributeValue &v8=EmptyAttributeValue(), std::string n9="", const AttributeValue &v9=EmptyAttributeValue())
manage and create wifi channel objects for the yans model.
tuple stack
Definition: first.py:34
Definition: ssid.h:35
build a set of CsmaNetDevice objects
Definition: csma-helper.h:46
int main(int argc, char *argv[])
void SetChannelAttribute(std::string name, const AttributeValue &value)
Helper class used to assign positions and mobility models to nodes.
void Stop(Time stop)
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter...
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
void Add(NodeContainer other)
Append the contents of another NodeContainer to the end of this container.
NS_LOG_COMPONENT_DEFINE("PacketLossCounter")
void AddValue(const std::string &name, const std::string &help, T &value)
Definition: command-line.h:408
static void Stop(void)
Definition: simulator.cc:165
Ptr< Node > Get(uint32_t i) const
Get the Ptr stored in this container at a given index.
hold objects of type ns3::Ssid
Interface to network animator.
void Parse(int argc, char *argv[])
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
tuple address
Definition: first.py:37
void SetPositionAllocator(Ptr< PositionAllocator > allocator)
Hold an floating point type.
Definition: double.h:41
a 2d rectangle
Definition: rectangle.h:33
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
static WifiHelper Default(void)
Definition: wifi-helper.cc:61