A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
third.py
Go to the documentation of this file.
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# Ported to Python by Mohit P. Tahiliani
16#
17
18try:
19 from ns import ns
20except ModuleNotFoundError:
21 raise SystemExit(
22 "Error: ns3 Python module not found;"
23 " Python bindings may not be enabled"
24 " or your PYTHONPATH might not be properly configured"
25 )
26import sys
27from ctypes import c_bool, c_int
28
29# // Default Network Topology
30# //
31# // Wifi 10.1.3.0
32# // AP
33# // * * * *
34# // | | | | 10.1.1.0
35# // n5 n6 n7 n0 -------------- n1 n2 n3 n4
36# // point-to-point | | | |
37# // ================
38# // LAN 10.1.2.0
39
40
41nCsma = c_int(3)
42verbose = c_bool(True)
43nWifi = c_int(3)
44tracing = c_bool(False)
45
46cmd = ns.CommandLine(__file__)
47cmd.AddValue("nCsma", "Number of extra CSMA nodes/devices", nCsma)
48cmd.AddValue("nWifi", "Number of wifi STA devices", nWifi)
49cmd.AddValue("verbose", "Tell echo applications to log if true", verbose)
50cmd.AddValue("tracing", "Enable pcap tracing", tracing)
51
52cmd.Parse(sys.argv)
53
54# The underlying restriction of 18 is due to the grid position
55# allocator's configuration; the grid layout will exceed the
56# bounding box if more than 18 nodes are provided.
57if nWifi.value > 18:
58 print("nWifi should be 18 or less; otherwise grid layout exceeds the bounding box")
59 sys.exit(1)
60
61if verbose.value:
62 ns.LogComponentEnable("UdpEchoClientApplication", ns.LOG_LEVEL_INFO)
63 ns.LogComponentEnable("UdpEchoServerApplication", ns.LOG_LEVEL_INFO)
64
65p2pNodes = ns.NodeContainer()
66p2pNodes.Create(2)
67
68pointToPoint = ns.PointToPointHelper()
69pointToPoint.SetDeviceAttribute("DataRate", ns.StringValue("5Mbps"))
70pointToPoint.SetChannelAttribute("Delay", ns.StringValue("2ms"))
71
72p2pDevices = pointToPoint.Install(p2pNodes)
73
74csmaNodes = ns.NodeContainer()
75csmaNodes.Add(p2pNodes.Get(1))
76csmaNodes.Create(nCsma.value)
77
78csma = ns.CsmaHelper()
79csma.SetChannelAttribute("DataRate", ns.StringValue("100Mbps"))
80csma.SetChannelAttribute("Delay", ns.TimeValue(ns.NanoSeconds(6560)))
81
82csmaDevices = csma.Install(csmaNodes)
83
84wifiStaNodes = ns.NodeContainer()
85wifiStaNodes.Create(nWifi.value)
86wifiApNode = p2pNodes.Get(0)
87
88channel = ns.YansWifiChannelHelper.Default()
89phy = ns.YansWifiPhyHelper()
90phy.SetChannel(channel.Create())
91
92mac = ns.WifiMacHelper()
93ssid = ns.Ssid("ns-3-ssid")
94
95wifi = ns.WifiHelper()
96
97mac.SetType("ns3::StaWifiMac", "Ssid", ns.SsidValue(ssid), "ActiveProbing", ns.BooleanValue(False))
98staDevices = wifi.Install(phy, mac, wifiStaNodes)
99
100mac.SetType("ns3::ApWifiMac", "Ssid", ns.SsidValue(ssid))
101apDevices = wifi.Install(phy, mac, wifiApNode)
102
103mobility = ns.MobilityHelper()
104mobility.SetPositionAllocator(
105 "ns3::GridPositionAllocator",
106 "MinX",
107 ns.DoubleValue(0.0),
108 "MinY",
109 ns.DoubleValue(0.0),
110 "DeltaX",
111 ns.DoubleValue(5.0),
112 "DeltaY",
113 ns.DoubleValue(10.0),
114 "GridWidth",
115 ns.UintegerValue(3),
116 "LayoutType",
117 ns.StringValue("RowFirst"),
118)
119
120mobility.SetMobilityModel(
121 "ns3::RandomWalk2dMobilityModel",
122 "Bounds",
123 ns.RectangleValue(ns.Rectangle(-50, 50, -50, 50)),
124)
125mobility.Install(wifiStaNodes)
126
127mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel")
128mobility.Install(wifiApNode)
129
130stack = ns.InternetStackHelper()
131stack.Install(csmaNodes)
132stack.Install(wifiApNode)
133stack.Install(wifiStaNodes)
134
135address = ns.Ipv4AddressHelper()
136address.SetBase(ns.Ipv4Address("10.1.1.0"), ns.Ipv4Mask("255.255.255.0"))
137p2pInterfaces = address.Assign(p2pDevices)
138
139address.SetBase(ns.Ipv4Address("10.1.2.0"), ns.Ipv4Mask("255.255.255.0"))
140csmaInterfaces = address.Assign(csmaDevices)
141
142address.SetBase(ns.Ipv4Address("10.1.3.0"), ns.Ipv4Mask("255.255.255.0"))
143address.Assign(staDevices)
144address.Assign(apDevices)
145
146echoServer = ns.UdpEchoServerHelper(9)
147
148serverApps = echoServer.Install(csmaNodes.Get(nCsma.value))
149serverApps.Start(ns.Seconds(1.0))
150serverApps.Stop(ns.Seconds(10.0))
151
152echoClient = ns.UdpEchoClientHelper(csmaInterfaces.GetAddress(nCsma.value).ConvertTo(), 9)
153echoClient.SetAttribute("MaxPackets", ns.UintegerValue(1))
154echoClient.SetAttribute("Interval", ns.TimeValue(ns.Seconds(1.0)))
155echoClient.SetAttribute("PacketSize", ns.UintegerValue(1024))
156
157clientApps = echoClient.Install(wifiStaNodes.Get(nWifi.value - 1))
158clientApps.Start(ns.Seconds(2.0))
159clientApps.Stop(ns.Seconds(10.0))
160
161ns.Ipv4GlobalRoutingHelper.PopulateRoutingTables()
162
163ns.Simulator.Stop(ns.Seconds(10.0))
164
165if tracing.value:
166 phy.SetPcapDataLinkType(phy.DLT_IEEE802_11_RADIO)
167 pointToPoint.EnablePcapAll("third")
168 phy.EnablePcap("third", apDevices.Get(0))
169 csma.EnablePcap("third", csmaDevices.Get(0), True)
170
171ns.Simulator.Run()
172ns.Simulator.Destroy()