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