A Discrete-Event Network Simulator
QKDNetSim v2.0 (NS-3 v3.41) @ (+)
API
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
third.py
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 # Ported to Python by Mohit P. Tahiliani
16 #
17 
18 try:
19  from ns import ns
20 except 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  )
26 import sys
27 from 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 
41 nCsma = c_int(3)
42 verbose = c_bool(True)
43 nWifi = c_int(3)
44 tracing = c_bool(False)
45 
46 cmd = ns.CommandLine(__file__)
47 cmd.AddValue("nCsma", "Number of extra CSMA nodes/devices", nCsma)
48 cmd.AddValue("nWifi", "Number of wifi STA devices", nWifi)
49 cmd.AddValue("verbose", "Tell echo applications to log if true", verbose)
50 cmd.AddValue("tracing", "Enable pcap tracing", tracing)
51 
52 cmd.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.
57 if nWifi.value > 18:
58  print("nWifi should be 18 or less; otherwise grid layout exceeds the bounding box")
59  sys.exit(1)
60 
61 if verbose.value:
62  ns.core.LogComponentEnable("UdpEchoClientApplication", ns.core.LOG_LEVEL_INFO)
63  ns.core.LogComponentEnable("UdpEchoServerApplication", ns.core.LOG_LEVEL_INFO)
64 
65 p2pNodes = ns.network.NodeContainer()
66 p2pNodes.Create(2)
67 
68 pointToPoint = ns.point_to_point.PointToPointHelper()
69 pointToPoint.SetDeviceAttribute("DataRate", ns.core.StringValue("5Mbps"))
70 pointToPoint.SetChannelAttribute("Delay", ns.core.StringValue("2ms"))
71 
72 p2pDevices = pointToPoint.Install(p2pNodes)
73 
74 csmaNodes = ns.network.NodeContainer()
75 csmaNodes.Add(p2pNodes.Get(1))
76 csmaNodes.Create(nCsma.value)
77 
78 csma = ns.csma.CsmaHelper()
79 csma.SetChannelAttribute("DataRate", ns.core.StringValue("100Mbps"))
80 csma.SetChannelAttribute("Delay", ns.core.TimeValue(ns.core.NanoSeconds(6560)))
81 
82 csmaDevices = csma.Install(csmaNodes)
83 
84 wifiStaNodes = ns.network.NodeContainer()
85 wifiStaNodes.Create(nWifi.value)
86 wifiApNode = p2pNodes.Get(0)
87 
88 channel = ns.wifi.YansWifiChannelHelper.Default()
89 phy = ns.wifi.YansWifiPhyHelper()
90 phy.SetChannel(channel.Create())
91 
92 mac = ns.wifi.WifiMacHelper()
93 ssid = ns.wifi.Ssid("ns-3-ssid")
94 
95 wifi = ns.wifi.WifiHelper()
96 
97 mac.SetType(
98  "ns3::StaWifiMac", "Ssid", ns.wifi.SsidValue(ssid), "ActiveProbing", ns.core.BooleanValue(False)
99 )
100 staDevices = wifi.Install(phy, mac, wifiStaNodes)
101 
102 mac.SetType("ns3::ApWifiMac", "Ssid", ns.wifi.SsidValue(ssid))
103 apDevices = wifi.Install(phy, mac, wifiApNode)
104 
105 mobility = ns.mobility.MobilityHelper()
106 mobility.SetPositionAllocator(
107  "ns3::GridPositionAllocator",
108  "MinX",
109  ns.core.DoubleValue(0.0),
110  "MinY",
111  ns.core.DoubleValue(0.0),
112  "DeltaX",
113  ns.core.DoubleValue(5.0),
114  "DeltaY",
115  ns.core.DoubleValue(10.0),
116  "GridWidth",
117  ns.core.UintegerValue(3),
118  "LayoutType",
119  ns.core.StringValue("RowFirst"),
120 )
121 
122 mobility.SetMobilityModel(
123  "ns3::RandomWalk2dMobilityModel",
124  "Bounds",
125  ns.mobility.RectangleValue(ns.mobility.Rectangle(-50, 50, -50, 50)),
126 )
127 mobility.Install(wifiStaNodes)
128 
129 mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel")
130 mobility.Install(wifiApNode)
131 
132 stack = ns.internet.InternetStackHelper()
133 stack.Install(csmaNodes)
134 stack.Install(wifiApNode)
135 stack.Install(wifiStaNodes)
136 
137 address = ns.internet.Ipv4AddressHelper()
138 address.SetBase(ns.network.Ipv4Address("10.1.1.0"), ns.network.Ipv4Mask("255.255.255.0"))
139 p2pInterfaces = address.Assign(p2pDevices)
140 
141 address.SetBase(ns.network.Ipv4Address("10.1.2.0"), ns.network.Ipv4Mask("255.255.255.0"))
142 csmaInterfaces = address.Assign(csmaDevices)
143 
144 address.SetBase(ns.network.Ipv4Address("10.1.3.0"), ns.network.Ipv4Mask("255.255.255.0"))
145 address.Assign(staDevices)
146 address.Assign(apDevices)
147 
148 echoServer = ns.applications.UdpEchoServerHelper(9)
149 
150 serverApps = echoServer.Install(csmaNodes.Get(nCsma.value))
151 serverApps.Start(ns.core.Seconds(1.0))
152 serverApps.Stop(ns.core.Seconds(10.0))
153 
154 echoClient = ns.applications.UdpEchoClientHelper(
155  csmaInterfaces.GetAddress(nCsma.value).ConvertTo(), 9
156 )
157 echoClient.SetAttribute("MaxPackets", ns.core.UintegerValue(1))
158 echoClient.SetAttribute("Interval", ns.core.TimeValue(ns.core.Seconds(1.0)))
159 echoClient.SetAttribute("PacketSize", ns.core.UintegerValue(1024))
160 
161 clientApps = echoClient.Install(wifiStaNodes.Get(nWifi.value - 1))
162 clientApps.Start(ns.core.Seconds(2.0))
163 clientApps.Stop(ns.core.Seconds(10.0))
164 
165 ns.internet.Ipv4GlobalRoutingHelper.PopulateRoutingTables()
166 
167 ns.core.Simulator.Stop(ns.core.Seconds(10.0))
168 
169 if tracing.value:
170  phy.SetPcapDataLinkType(phy.DLT_IEEE802_11_RADIO)
171  pointToPoint.EnablePcapAll("third")
172  phy.EnablePcap("third", apDevices.Get(0))
173  csma.EnablePcap("third", csmaDevices.Get(0), True)
174 
175 ns.core.Simulator.Run()
176 ns.core.Simulator.Destroy()