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