A Discrete-Event Network Simulator
API
third.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 
16 #include "ns3/applications-module.h"
17 #include "ns3/core-module.h"
18 #include "ns3/csma-module.h"
19 #include "ns3/internet-module.h"
20 #include "ns3/mobility-module.h"
21 #include "ns3/network-module.h"
22 #include "ns3/point-to-point-module.h"
23 #include "ns3/ssid.h"
24 #include "ns3/yans-wifi-helper.h"
25 
26 // Default Network Topology
27 //
28 // Wifi 10.1.3.0
29 // AP
30 // * * * *
31 // | | | | 10.1.1.0
32 // n5 n6 n7 n0 -------------- n1 n2 n3 n4
33 // point-to-point | | | |
34 // ================
35 // LAN 10.1.2.0
36 
37 using namespace ns3;
38 
39 NS_LOG_COMPONENT_DEFINE("ThirdScriptExample");
40 
41 int
42 main(int argc, char* argv[])
43 {
44  bool verbose = true;
45  uint32_t nCsma = 3;
46  uint32_t nWifi = 3;
47  bool tracing = false;
48 
49  CommandLine cmd(__FILE__);
50  cmd.AddValue("nCsma", "Number of \"extra\" CSMA nodes/devices", nCsma);
51  cmd.AddValue("nWifi", "Number of wifi STA devices", nWifi);
52  cmd.AddValue("verbose", "Tell echo applications to log if true", verbose);
53  cmd.AddValue("tracing", "Enable pcap tracing", tracing);
54 
55  cmd.Parse(argc, argv);
56 
57  // The underlying restriction of 18 is due to the grid position
58  // allocator's configuration; the grid layout will exceed the
59  // bounding box if more than 18 nodes are provided.
60  if (nWifi > 18)
61  {
62  std::cout << "nWifi should be 18 or less; otherwise grid layout exceeds the bounding box"
63  << std::endl;
64  return 1;
65  }
66 
67  if (verbose)
68  {
69  LogComponentEnable("UdpEchoClientApplication", LOG_LEVEL_INFO);
70  LogComponentEnable("UdpEchoServerApplication", LOG_LEVEL_INFO);
71  }
72 
74  p2pNodes.Create(2);
75 
77  pointToPoint.SetDeviceAttribute("DataRate", StringValue("5Mbps"));
78  pointToPoint.SetChannelAttribute("Delay", StringValue("2ms"));
79 
81  p2pDevices = pointToPoint.Install(p2pNodes);
82 
84  csmaNodes.Add(p2pNodes.Get(1));
85  csmaNodes.Create(nCsma);
86 
88  csma.SetChannelAttribute("DataRate", StringValue("100Mbps"));
89  csma.SetChannelAttribute("Delay", TimeValue(NanoSeconds(6560)));
90 
92  csmaDevices = csma.Install(csmaNodes);
93 
95  wifiStaNodes.Create(nWifi);
97 
100  phy.SetChannel(channel.Create());
101 
103  Ssid ssid = Ssid("ns-3-ssid");
104 
106 
108  mac.SetType("ns3::StaWifiMac", "Ssid", SsidValue(ssid), "ActiveProbing", BooleanValue(false));
109  staDevices = wifi.Install(phy, mac, wifiStaNodes);
110 
112  mac.SetType("ns3::ApWifiMac", "Ssid", SsidValue(ssid));
113  apDevices = wifi.Install(phy, mac, wifiApNode);
114 
116 
117  mobility.SetPositionAllocator("ns3::GridPositionAllocator",
118  "MinX",
119  DoubleValue(0.0),
120  "MinY",
121  DoubleValue(0.0),
122  "DeltaX",
123  DoubleValue(5.0),
124  "DeltaY",
125  DoubleValue(10.0),
126  "GridWidth",
127  UintegerValue(3),
128  "LayoutType",
129  StringValue("RowFirst"));
130 
131  mobility.SetMobilityModel("ns3::RandomWalk2dMobilityModel",
132  "Bounds",
133  RectangleValue(Rectangle(-50, 50, -50, 50)));
134  mobility.Install(wifiStaNodes);
135 
136  mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
137  mobility.Install(wifiApNode);
138 
140  stack.Install(csmaNodes);
141  stack.Install(wifiApNode);
142  stack.Install(wifiStaNodes);
143 
145 
146  address.SetBase("10.1.1.0", "255.255.255.0");
148  p2pInterfaces = address.Assign(p2pDevices);
149 
150  address.SetBase("10.1.2.0", "255.255.255.0");
153 
154  address.SetBase("10.1.3.0", "255.255.255.0");
155  address.Assign(staDevices);
156  address.Assign(apDevices);
157 
159 
161  serverApps.Start(Seconds(1.0));
162  serverApps.Stop(Seconds(10.0));
163 
165  echoClient.SetAttribute("MaxPackets", UintegerValue(1));
166  echoClient.SetAttribute("Interval", TimeValue(Seconds(1.0)));
167  echoClient.SetAttribute("PacketSize", UintegerValue(1024));
168 
170  clientApps.Start(Seconds(2.0));
171  clientApps.Stop(Seconds(10.0));
172 
174 
175  Simulator::Stop(Seconds(10.0));
176 
177  if (tracing)
178  {
179  phy.SetPcapDataLinkType(WifiPhyHelper::DLT_IEEE802_11_RADIO);
180  pointToPoint.EnablePcapAll("third");
181  phy.EnablePcap("third", apDevices.Get(0));
182  csma.EnablePcap("third", csmaDevices.Get(0), true);
183  }
184 
185  Simulator::Run();
187  return 0;
188 }
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.
Build a set of PointToPointNetDevice objects.
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.
@ DLT_IEEE802_11_RADIO
Include Radiotap link layer information.
Definition: wifi-helper.h:178
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:1362
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
echoClient
Definition: first.py:59
address
Definition: first.py:47
serverApps
Definition: first.py:54
pointToPoint
Definition: first.py:38
echoServer
Definition: first.py:52
clientApps
Definition: first.py:64
stack
Definition: first.py:44
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void LogComponentEnable(const std::string &name, LogLevel level)
Enable the logging output associated with that log component.
Definition: log.cc:302
@ LOG_LEVEL_INFO
LOG_INFO and above.
Definition: log.h:104
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
nCsma
Definition: second.py:38
cmd
Definition: second.py:40
csmaDevices
Definition: second.py:67
staDevices
Definition: third.py:100
ssid
Definition: third.py:93
channel
Definition: third.py:88
nWifi
Definition: third.py:43
mac
Definition: third.py:92
wifi
Definition: third.py:95
apDevices
Definition: third.py:103
wifiApNode
Definition: third.py:86
mobility
Definition: third.py:105
wifiStaNodes
Definition: third.py:84
phy
Definition: third.py:89
bool verbose
bool tracing
Flag to enable/disable generation of tracing files.