A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
third.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 
17 #include "ns3/core-module.h"
18 #include "ns3/point-to-point-module.h"
19 #include "ns3/network-module.h"
20 #include "ns3/applications-module.h"
21 #include "ns3/wifi-module.h"
22 #include "ns3/mobility-module.h"
23 #include "ns3/csma-module.h"
24 #include "ns3/internet-module.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 
48  CommandLine cmd;
49  cmd.AddValue ("nCsma", "Number of \"extra\" CSMA nodes/devices", nCsma);
50  cmd.AddValue ("nWifi", "Number of wifi STA devices", nWifi);
51  cmd.AddValue ("verbose", "Tell echo applications to log if true", verbose);
52 
53  cmd.Parse (argc,argv);
54 
55  if (nWifi > 18)
56  {
57  std::cout << "Number of wifi nodes " << nWifi <<
58  " specified exceeds the mobility bounding box" << std::endl;
59  exit (1);
60  }
61 
62  if (verbose)
63  {
64  LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);
65  LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);
66  }
67 
68  NodeContainer p2pNodes;
69  p2pNodes.Create (2);
70 
72  pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
73  pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));
74 
75  NetDeviceContainer p2pDevices;
76  p2pDevices = pointToPoint.Install (p2pNodes);
77 
78  NodeContainer csmaNodes;
79  csmaNodes.Add (p2pNodes.Get (1));
80  csmaNodes.Create (nCsma);
81 
82  CsmaHelper csma;
83  csma.SetChannelAttribute ("DataRate", StringValue ("100Mbps"));
84  csma.SetChannelAttribute ("Delay", TimeValue (NanoSeconds (6560)));
85 
86  NetDeviceContainer csmaDevices;
87  csmaDevices = csma.Install (csmaNodes);
88 
89  NodeContainer wifiStaNodes;
90  wifiStaNodes.Create (nWifi);
91  NodeContainer wifiApNode = p2pNodes.Get (0);
92 
95  phy.SetChannel (channel.Create ());
96 
98  wifi.SetRemoteStationManager ("ns3::AarfWifiManager");
99 
101 
102  Ssid ssid = Ssid ("ns-3-ssid");
103  mac.SetType ("ns3::StaWifiMac",
104  "Ssid", SsidValue (ssid),
105  "ActiveProbing", BooleanValue (false));
106 
107  NetDeviceContainer staDevices;
108  staDevices = wifi.Install (phy, mac, wifiStaNodes);
109 
110  mac.SetType ("ns3::ApWifiMac",
111  "Ssid", SsidValue (ssid));
112 
113  NetDeviceContainer apDevices;
114  apDevices = wifi.Install (phy, mac, wifiApNode);
115 
116  MobilityHelper mobility;
117 
118  mobility.SetPositionAllocator ("ns3::GridPositionAllocator",
119  "MinX", DoubleValue (0.0),
120  "MinY", DoubleValue (0.0),
121  "DeltaX", DoubleValue (5.0),
122  "DeltaY", DoubleValue (10.0),
123  "GridWidth", UintegerValue (3),
124  "LayoutType", StringValue ("RowFirst"));
125 
126  mobility.SetMobilityModel ("ns3::RandomWalk2dMobilityModel",
127  "Bounds", RectangleValue (Rectangle (-50, 50, -50, 50)));
128  mobility.Install (wifiStaNodes);
129 
130  mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
131  mobility.Install (wifiApNode);
132 
134  stack.Install (csmaNodes);
135  stack.Install (wifiApNode);
136  stack.Install (wifiStaNodes);
137 
139 
140  address.SetBase ("10.1.1.0", "255.255.255.0");
141  Ipv4InterfaceContainer p2pInterfaces;
142  p2pInterfaces = address.Assign (p2pDevices);
143 
144  address.SetBase ("10.1.2.0", "255.255.255.0");
145  Ipv4InterfaceContainer csmaInterfaces;
146  csmaInterfaces = address.Assign (csmaDevices);
147 
148  address.SetBase ("10.1.3.0", "255.255.255.0");
149  address.Assign (staDevices);
150  address.Assign (apDevices);
151 
153 
154  ApplicationContainer serverApps = echoServer.Install (csmaNodes.Get (nCsma));
155  serverApps.Start (Seconds (1.0));
156  serverApps.Stop (Seconds (10.0));
157 
158  UdpEchoClientHelper echoClient (csmaInterfaces.GetAddress (nCsma), 9);
159  echoClient.SetAttribute ("MaxPackets", UintegerValue (1));
160  echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0)));
161  echoClient.SetAttribute ("PacketSize", UintegerValue (1024));
162 
164  echoClient.Install (wifiStaNodes.Get (nWifi - 1));
165  clientApps.Start (Seconds (2.0));
166  clientApps.Stop (Seconds (10.0));
167 
169 
170  Simulator::Stop (Seconds (10.0));
171 
172  pointToPoint.EnablePcapAll ("third");
173  phy.EnablePcap ("third", apDevices.Get (0));
174  csma.EnablePcap ("third", csmaDevices.Get (0), true);
175 
176  Simulator::Run ();
178  return 0;
179 }