54 import ns.applications
87 ns.core.Config.SetDefault(
"ns3::OnOffApplication::PacketSize", ns.core.StringValue(
"1472"))
88 ns.core.Config.SetDefault(
"ns3::OnOffApplication::DataRate", ns.core.StringValue(
"100kb/s"))
95 cmd = ns.core.CommandLine()
96 cmd.AddValue(
"backboneNodes",
"number of backbone nodes", str(backboneNodes))
97 cmd.AddValue(
"infraNodes",
"number of leaf nodes", str(infraNodes))
98 cmd.AddValue(
"lanNodes",
"number of LAN nodes", str(lanNodes))
99 cmd.AddValue(
"stopTime",
"simulation stop time(seconds)", str(stopTime))
108 print "Use a simulation stop time >= 10 seconds"
120 backbone = ns.network.NodeContainer()
121 backbone.Create(backboneNodes)
126 wifi = ns.wifi.WifiHelper()
127 mac = ns.wifi.NqosWifiMacHelper.Default()
128 mac.SetType(
"ns3::AdhocWifiMac")
129 wifi.SetRemoteStationManager(
"ns3::ConstantRateWifiManager",
130 "DataMode", ns.core.StringValue(
"OfdmRate54Mbps"))
131 wifiPhy = ns.wifi.YansWifiPhyHelper.Default()
132 wifiChannel = ns.wifi.YansWifiChannelHelper.Default()
133 wifiPhy.SetChannel(wifiChannel.Create())
134 backboneDevices = wifi.Install(wifiPhy, mac, backbone)
138 print "Enabling OLSR routing on all backbone nodes"
139 internet = ns.internet.InternetStackHelper()
140 olsr = ns.olsr.OlsrHelper()
141 internet.SetRoutingHelper(olsr);
142 internet.Install(backbone);
149 ipAddrs = ns.internet.Ipv4AddressHelper()
150 ipAddrs.SetBase(ns.network.Ipv4Address(
"192.168.0.0"), ns.network.Ipv4Mask(
"255.255.255.0"))
151 ipAddrs.Assign(backboneDevices)
157 mobility = ns.mobility.MobilityHelper()
158 mobility.SetPositionAllocator(
"ns3::GridPositionAllocator",
159 "MinX", ns.core.DoubleValue(20.0),
160 "MinY", ns.core.DoubleValue(20.0),
161 "DeltaX", ns.core.DoubleValue(20.0),
162 "DeltaY", ns.core.DoubleValue(20.0),
163 "GridWidth", ns.core.UintegerValue(5),
164 "LayoutType", ns.core.StringValue(
"RowFirst"))
165 mobility.SetMobilityModel(
"ns3::RandomDirection2dMobilityModel",
166 "Bounds", ns.mobility.RectangleValue(ns.mobility.Rectangle(-500, 500, -500, 500)),
167 "Speed", ns.core.StringValue (
"ns3::ConstantRandomVariable[Constant=2]"),
168 "Pause", ns.core.StringValue (
"ns3::ConstantRandomVariable[Constant=0.2]"))
169 mobility.Install(backbone)
179 ipAddrs.SetBase(ns.network.Ipv4Address(
"172.16.0.0"), ns.network.Ipv4Mask(
"255.255.255.0"))
181 for i
in range(backboneNodes):
182 print "Configuring local area network for backbone node ", i
188 newLanNodes = ns.network.NodeContainer()
189 newLanNodes.Create(lanNodes - 1)
191 lan = ns.network.NodeContainer(ns.network.NodeContainer(backbone.Get(i)), newLanNodes)
196 csma = ns.csma.CsmaHelper()
197 csma.SetChannelAttribute(
"DataRate", ns.network.DataRateValue(ns.network.DataRate(5000000)))
198 csma.SetChannelAttribute(
"Delay", ns.core.TimeValue(ns.core.MilliSeconds(2)))
199 lanDevices = csma.Install(lan)
203 internet.Install(newLanNodes)
208 ipAddrs.Assign(lanDevices)
218 mobilityLan = ns.mobility.MobilityHelper()
219 positionAlloc = ns.mobility.ListPositionAllocator()
220 for j
in range(newLanNodes.GetN()):
221 positionAlloc.Add(ns.core.Vector(0.0, (j*10 + 10), 0.0))
223 mobilityLan.SetPositionAllocator(positionAlloc)
224 mobilityLan.PushReferenceMobilityModel(backbone.Get(i))
225 mobilityLan.SetMobilityModel(
"ns3::ConstantPositionMobilityModel")
226 mobilityLan.Install(newLanNodes);
236 ipAddrs.SetBase(ns.network.Ipv4Address(
"10.0.0.0"), ns.network.Ipv4Mask(
"255.255.255.0"))
238 for i
in range(backboneNodes):
239 print "Configuring wireless network for backbone node ", i
245 stas = ns.network.NodeContainer()
246 stas.Create(infraNodes - 1)
248 infra = ns.network.NodeContainer(ns.network.NodeContainer(backbone.Get(i)), stas)
252 ssid = ns.wifi.Ssid(
'wifi-infra' + str(i))
253 wifiInfra = ns.wifi.WifiHelper.Default()
254 wifiPhy.SetChannel(wifiChannel.Create())
255 wifiInfra.SetRemoteStationManager(
'ns3::ArfWifiManager')
256 macInfra = ns.wifi.NqosWifiMacHelper.Default();
257 macInfra.SetType(
"ns3::StaWifiMac",
258 "Ssid", ns.wifi.SsidValue(ssid),
259 "ActiveProbing", ns.core.BooleanValue(
False))
262 staDevices = wifiInfra.Install(wifiPhy, macInfra, stas)
264 macInfra.SetType(
"ns3::ApWifiMac",
265 "Ssid", ns.wifi.SsidValue(ssid),
266 "BeaconGeneration", ns.core.BooleanValue(
True),
267 "BeaconInterval", ns.core.TimeValue(ns.core.Seconds(2.5)))
268 apDevices = wifiInfra.Install(wifiPhy, macInfra, backbone.Get(i))
270 infraDevices = ns.network.NetDeviceContainer(apDevices, staDevices)
274 internet.Install(stas)
279 ipAddrs.Assign(infraDevices)
289 subnetAlloc = ns.mobility.ListPositionAllocator()
290 for j
in range(infra.GetN()):
291 subnetAlloc.Add(ns.core.Vector(0.0, j, 0.0))
293 mobility.PushReferenceMobilityModel(backbone.Get(i))
294 mobility.SetPositionAllocator(subnetAlloc)
295 mobility.SetMobilityModel(
"ns3::RandomDirection2dMobilityModel",
296 "Bounds", ns.mobility.RectangleValue(ns.mobility.Rectangle(-10, 10, -10, 10)),
297 "Speed", ns.core.StringValue (
"ns3::ConstantRandomVariable[Constant=3]"),
298 "Pause", ns.core.StringValue (
"ns3::ConstantRandomVariable[Constant=0.4]"))
299 mobility.Install(stas)
309 print "Create Applications."
312 appSource = ns.network.NodeList.GetNode(backboneNodes)
313 lastNodeIndex = backboneNodes + backboneNodes*(lanNodes - 1) + backboneNodes*(infraNodes - 1) - 1
314 appSink = ns.network.NodeList.GetNode(lastNodeIndex)
316 remoteAddr = appSink.GetObject(ns.internet.Ipv4.GetTypeId()).GetAddress(1,0).GetLocal()
318 onoff = ns.applications.OnOffHelper(
"ns3::UdpSocketFactory",
319 ns.network.Address(ns.network.InetSocketAddress(remoteAddr, port)))
320 apps = onoff.Install(ns.network.NodeContainer(appSource))
321 apps.Start(ns.core.Seconds(3))
322 apps.Stop(ns.core.Seconds(stopTime - 1))
325 sink = ns.applications.PacketSinkHelper(
"ns3::UdpSocketFactory",
326 ns.network.InetSocketAddress(ns.network.Ipv4Address.GetAny(), port))
327 apps = sink.Install(ns.network.NodeContainer(appSink))
328 apps.Start(ns.core.Seconds(3))
336 print "Configure Tracing."
337 csma = ns.csma.CsmaHelper()
341 ascii = ns.network.AsciiTraceHelper();
342 stream = ascii.CreateFileStream(
"mixed-wireless.tr");
343 wifiPhy.EnableAsciiAll(stream);
344 csma.EnableAsciiAll(stream);
345 internet.EnableAsciiIpv4All(stream);
348 csma.EnablePcapAll(
"mixed-wireless",
False)
350 wifiPhy.EnablePcap(
"mixed-wireless", backboneDevices)
351 wifiPhy.EnablePcap(
"mixed-wireless", appSink.GetId(), 0)
365 print "Run Simulation."
366 ns.core.Simulator.Stop(ns.core.Seconds(stopTime))
367 ns.core.Simulator.Run()
368 ns.core.Simulator.Destroy()
371 if __name__ ==
'__main__':