A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
wireless-animation.cc
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 
81  PointToPointHelper pointToPoint;
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 
120  InternetStackHelper stack;
121  stack.Install (allNodes);
122 
123  // Install Ipv4 addresses
124 
125  Ipv4AddressHelper address;
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 
140  UdpEchoServerHelper echoServer (9);
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 
161  AnimationInterface anim ("wireless-animation.xml"); // Mandatory
162 
163  anim.EnablePacketMetadata (true); // Optional
164  anim.EnableIpv4RouteTracking ("routingtable-wireless.xml", Seconds(0), Seconds(5), Seconds(0.25)); //Optional
165  Simulator::Run ();
167  return 0;
168 }
Time NanoSeconds(uint64_t ns)
create ns3::Time instances in units of nanoseconds.
Definition: nstime.h:629
holds a vector of ns3::Application pointers.
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<Ipv4> 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:68
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 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())
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
#define NS_LOG_COMPONENT_DEFINE(name)
Definition: log.h:122
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:92
NetDeviceContainer Install(const WifiPhyHelper &phy, const WifiMacHelper &mac, NodeContainer c) const
Definition: wifi-helper.cc:97
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)
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:700
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)
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 argumentsInstances of this class can be used to parse command-line arguments: user...
Definition: command-line.h:50
static void Destroy(void)
Definition: simulator.cc:121
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())
void Install(std::string nodeName) const
manage and create wifi channel objects for the yans model.
Definition: ssid.h:35
build a set of CsmaNetDevice objects
Definition: csma-helper.h:46
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...
Time Seconds(double seconds)
create ns3::Time instances in units of seconds.
Definition: nstime.h:586
void Add(NodeContainer other)
Append the contents of another NodeContainer to the end of this container.
void AddValue(const std::string &name, const std::string &help, T &value)
Definition: command-line.h:134
static void Stop(void)
Definition: simulator.cc:164
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
hold objects of type ns3::Ssid
void Parse(int argc, char *argv[]) const
Definition: command-line.cc:84
Interface to network animator.
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.
void SetAttribute(std::string name, const AttributeValue &value)
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:60