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