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