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