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 from ns import ns
20 import 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 
33 from ctypes import c_bool, c_int
34 nCsma = c_int(3)
35 verbose = c_bool(True)
36 nWifi = c_int(3)
37 tracing = c_bool(False)
38 
39 cmd = ns.CommandLine(__file__)
40 cmd.AddValue("nCsma", "Number of extra CSMA nodes/devices", nCsma)
41 cmd.AddValue("nWifi", "Number of wifi STA devices", nWifi)
42 cmd.AddValue("verbose", "Tell echo applications to log if true", verbose)
43 cmd.AddValue("tracing", "Enable pcap tracing", tracing)
44 
45 cmd.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.
50 if nWifi.value > 18:
51  print("nWifi should be 18 or less; otherwise grid layout exceeds the bounding box")
52  sys.exit(1)
53 
54 if verbose.value:
55  ns.core.LogComponentEnable("UdpEchoClientApplication", ns.core.LOG_LEVEL_INFO)
56  ns.core.LogComponentEnable("UdpEchoServerApplication", ns.core.LOG_LEVEL_INFO)
57 
58 p2pNodes = ns.network.NodeContainer()
59 p2pNodes.Create(2)
60 
61 pointToPoint = ns.point_to_point.PointToPointHelper()
62 pointToPoint.SetDeviceAttribute("DataRate", ns.core.StringValue("5Mbps"))
63 pointToPoint.SetChannelAttribute("Delay", ns.core.StringValue("2ms"))
64 
65 p2pDevices = pointToPoint.Install(p2pNodes)
66 
67 csmaNodes = ns.network.NodeContainer()
68 csmaNodes.Add(p2pNodes.Get(1))
69 csmaNodes.Create(nCsma.value)
70 
71 csma = ns.csma.CsmaHelper()
72 csma.SetChannelAttribute("DataRate", ns.core.StringValue("100Mbps"))
73 csma.SetChannelAttribute("Delay", ns.core.TimeValue(ns.core.NanoSeconds(6560)))
74 
75 csmaDevices = csma.Install(csmaNodes)
76 
77 wifiStaNodes = ns.network.NodeContainer()
78 wifiStaNodes.Create(nWifi.value)
79 wifiApNode = p2pNodes.Get(0)
80 
81 channel = ns.wifi.YansWifiChannelHelper.Default()
82 phy = ns.wifi.YansWifiPhyHelper()
83 phy.SetChannel(channel.Create())
84 
85 mac = ns.wifi.WifiMacHelper()
86 ssid = ns.wifi.Ssid ("ns-3-ssid")
87 
88 wifi = ns.wifi.WifiHelper()
89 
90 mac.SetType ("ns3::StaWifiMac", "Ssid", ns.wifi.SsidValue(ssid), "ActiveProbing", ns.core.BooleanValue(False))
91 staDevices = wifi.Install(phy, mac, wifiStaNodes)
92 
93 mac.SetType("ns3::ApWifiMac","Ssid", ns.wifi.SsidValue (ssid))
94 apDevices = wifi.Install(phy, mac, wifiApNode)
95 
96 mobility = ns.mobility.MobilityHelper()
97 mobility.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 
101 mobility.SetMobilityModel ("ns3::RandomWalk2dMobilityModel", "Bounds", ns.mobility.RectangleValue(ns.mobility.Rectangle (-50, 50, -50, 50)))
102 mobility.Install(wifiStaNodes)
103 
104 mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel")
105 mobility.Install(wifiApNode)
106 
107 stack = ns.internet.InternetStackHelper()
108 stack.Install(csmaNodes)
109 stack.Install(wifiApNode)
110 stack.Install(wifiStaNodes)
111 
112 address = ns.internet.Ipv4AddressHelper()
113 address.SetBase(ns.network.Ipv4Address("10.1.1.0"), ns.network.Ipv4Mask("255.255.255.0"))
114 p2pInterfaces = address.Assign(p2pDevices)
115 
116 address.SetBase(ns.network.Ipv4Address("10.1.2.0"), ns.network.Ipv4Mask("255.255.255.0"))
117 csmaInterfaces = address.Assign(csmaDevices)
118 
119 address.SetBase(ns.network.Ipv4Address("10.1.3.0"), ns.network.Ipv4Mask("255.255.255.0"))
120 address.Assign(staDevices)
121 address.Assign(apDevices)
122 
123 echoServer = ns.applications.UdpEchoServerHelper(9)
124 
125 serverApps = echoServer.Install(csmaNodes.Get(nCsma.value))
126 serverApps.Start(ns.core.Seconds(1.0))
127 serverApps.Stop(ns.core.Seconds(10.0))
128 
129 echoClient = ns.applications.UdpEchoClientHelper(csmaInterfaces.GetAddress(nCsma.value).ConvertTo(), 9)
130 echoClient.SetAttribute("MaxPackets", ns.core.UintegerValue(1))
131 echoClient.SetAttribute("Interval", ns.core.TimeValue(ns.core.Seconds (1.0)))
132 echoClient.SetAttribute("PacketSize", ns.core.UintegerValue(1024))
133 
134 clientApps = echoClient.Install(wifiStaNodes.Get (nWifi.value - 1))
135 clientApps.Start(ns.core.Seconds(2.0))
136 clientApps.Stop(ns.core.Seconds(10.0))
137 
138 ns.internet.Ipv4GlobalRoutingHelper.PopulateRoutingTables()
139 
140 ns.core.Simulator.Stop(ns.core.Seconds(10.0))
141 
142 if 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 
148 ns.core.Simulator.Run()
149 ns.core.Simulator.Destroy()
150