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 
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 }