54 import ns.applications
79 cmd = ns.core.CommandLine()
80 cmd.backboneNodes = 10
89 ns.core.Config.SetDefault(
"ns3::OnOffApplication::PacketSize", ns.core.StringValue(
"1472"))
90 ns.core.Config.SetDefault(
"ns3::OnOffApplication::DataRate", ns.core.StringValue(
"100kb/s"))
98 cmd.AddValue(
"backboneNodes",
"number of backbone nodes")
99 cmd.AddValue(
"infraNodes",
"number of leaf nodes")
100 cmd.AddValue(
"lanNodes",
"number of LAN nodes")
101 cmd.AddValue(
"stopTime",
"simulation stop time(seconds)")
109 backboneNodes = int(cmd.backboneNodes)
110 infraNodes = int(cmd.infraNodes)
111 lanNodes = int(cmd.lanNodes)
112 stopTime = int(cmd.stopTime)
115 print (
"Use a simulation stop time >= 10 seconds")
127 backbone = ns.network.NodeContainer()
128 backbone.Create(backboneNodes)
133 wifi = ns.wifi.WifiHelper()
134 mac = ns.wifi.WifiMacHelper()
135 mac.SetType(
"ns3::AdhocWifiMac")
136 wifi.SetRemoteStationManager(
"ns3::ConstantRateWifiManager",
137 "DataMode", ns.core.StringValue(
"OfdmRate54Mbps"))
138 wifiPhy = ns.wifi.YansWifiPhyHelper()
139 wifiPhy.SetPcapDataLinkType(wifiPhy.DLT_IEEE802_11_RADIO)
140 wifiChannel = ns.wifi.YansWifiChannelHelper.Default()
141 wifiPhy.SetChannel(wifiChannel.Create())
142 backboneDevices = wifi.Install(wifiPhy, mac, backbone)
146 print (
"Enabling OLSR routing on all backbone nodes")
147 internet = ns.internet.InternetStackHelper()
148 olsr = ns.olsr.OlsrHelper()
149 internet.SetRoutingHelper(olsr);
150 internet.Install(backbone);
157 ipAddrs = ns.internet.Ipv4AddressHelper()
158 ipAddrs.SetBase(ns.network.Ipv4Address(
"192.168.0.0"), ns.network.Ipv4Mask(
"255.255.255.0"))
159 ipAddrs.Assign(backboneDevices)
165 mobility = ns.mobility.MobilityHelper()
166 mobility.SetPositionAllocator(
"ns3::GridPositionAllocator",
167 "MinX", ns.core.DoubleValue(20.0),
168 "MinY", ns.core.DoubleValue(20.0),
169 "DeltaX", ns.core.DoubleValue(20.0),
170 "DeltaY", ns.core.DoubleValue(20.0),
171 "GridWidth", ns.core.UintegerValue(5),
172 "LayoutType", ns.core.StringValue(
"RowFirst"))
173 mobility.SetMobilityModel(
"ns3::RandomDirection2dMobilityModel",
174 "Bounds", ns.mobility.RectangleValue(ns.mobility.Rectangle(-500, 500, -500, 500)),
175 "Speed", ns.core.StringValue (
"ns3::ConstantRandomVariable[Constant=2]"),
176 "Pause", ns.core.StringValue (
"ns3::ConstantRandomVariable[Constant=0.2]"))
177 mobility.Install(backbone)
187 ipAddrs.SetBase(ns.network.Ipv4Address(
"172.16.0.0"), ns.network.Ipv4Mask(
"255.255.255.0"))
189 for i
in range(backboneNodes):
190 print (
"Configuring local area network for backbone node ", i)
196 newLanNodes = ns.network.NodeContainer()
197 newLanNodes.Create(lanNodes - 1)
199 lan = ns.network.NodeContainer(ns.network.NodeContainer(backbone.Get(i)), newLanNodes)
204 csma = ns.csma.CsmaHelper()
205 csma.SetChannelAttribute(
"DataRate", ns.network.DataRateValue(ns.network.DataRate(5000000)))
206 csma.SetChannelAttribute(
"Delay", ns.core.TimeValue(ns.core.MilliSeconds(2)))
207 lanDevices = csma.Install(lan)
211 internet.Install(newLanNodes)
216 ipAddrs.Assign(lanDevices)
226 mobilityLan = ns.mobility.MobilityHelper()
227 positionAlloc = ns.mobility.ListPositionAllocator()
228 for j
in range(newLanNodes.GetN()):
229 positionAlloc.Add(ns.core.Vector(0.0, (j*10 + 10), 0.0))
231 mobilityLan.SetPositionAllocator(positionAlloc)
232 mobilityLan.PushReferenceMobilityModel(backbone.Get(i))
233 mobilityLan.SetMobilityModel(
"ns3::ConstantPositionMobilityModel")
234 mobilityLan.Install(newLanNodes);
244 ipAddrs.SetBase(ns.network.Ipv4Address(
"10.0.0.0"), ns.network.Ipv4Mask(
"255.255.255.0"))
246 for i
in range(backboneNodes):
247 print (
"Configuring wireless network for backbone node ", i)
253 stas = ns.network.NodeContainer()
254 stas.Create(infraNodes - 1)
256 infra = ns.network.NodeContainer(ns.network.NodeContainer(backbone.Get(i)), stas)
260 ssid = ns.wifi.Ssid(
'wifi-infra' + str(i))
261 wifiInfra = ns.wifi.WifiHelper()
262 wifiPhy.SetChannel(wifiChannel.Create())
263 wifiInfra.SetRemoteStationManager(
'ns3::ArfWifiManager')
264 macInfra = ns.wifi.WifiMacHelper();
265 macInfra.SetType(
"ns3::StaWifiMac",
266 "Ssid", ns.wifi.SsidValue(ssid))
269 staDevices = wifiInfra.Install(wifiPhy, macInfra, stas)
271 macInfra.SetType(
"ns3::ApWifiMac",
272 "Ssid", ns.wifi.SsidValue(ssid))
273 apDevices = wifiInfra.Install(wifiPhy, macInfra, backbone.Get(i))
275 infraDevices = ns.network.NetDeviceContainer(apDevices, staDevices)
279 internet.Install(stas)
284 ipAddrs.Assign(infraDevices)
294 subnetAlloc = ns.mobility.ListPositionAllocator()
295 for j
in range(infra.GetN()):
296 subnetAlloc.Add(ns.core.Vector(0.0, j, 0.0))
298 mobility.PushReferenceMobilityModel(backbone.Get(i))
299 mobility.SetPositionAllocator(subnetAlloc)
300 mobility.SetMobilityModel(
"ns3::RandomDirection2dMobilityModel",
301 "Bounds", ns.mobility.RectangleValue(ns.mobility.Rectangle(-10, 10, -10, 10)),
302 "Speed", ns.core.StringValue (
"ns3::ConstantRandomVariable[Constant=3]"),
303 "Pause", ns.core.StringValue (
"ns3::ConstantRandomVariable[Constant=0.4]"))
304 mobility.Install(stas)
314 print (
"Create Applications.")
317 appSource = ns.network.NodeList.GetNode(backboneNodes)
318 lastNodeIndex = backboneNodes + backboneNodes*(lanNodes - 1) + backboneNodes*(infraNodes - 1) - 1
319 appSink = ns.network.NodeList.GetNode(lastNodeIndex)
321 remoteAddr = appSink.GetObject(ns.internet.Ipv4.GetTypeId()).GetAddress(1,0).GetLocal()
323 onoff = ns.applications.OnOffHelper(
"ns3::UdpSocketFactory",
324 ns.network.Address(ns.network.InetSocketAddress(remoteAddr, port)))
325 apps = onoff.Install(ns.network.NodeContainer(appSource))
326 apps.Start(ns.core.Seconds(3))
327 apps.Stop(ns.core.Seconds(stopTime - 1))
330 sink = ns.applications.PacketSinkHelper(
"ns3::UdpSocketFactory",
331 ns.network.InetSocketAddress(ns.network.Ipv4Address.GetAny(), port))
332 apps = sink.Install(ns.network.NodeContainer(appSink))
333 apps.Start(ns.core.Seconds(3))
341 print (
"Configure Tracing.")
342 csma = ns.csma.CsmaHelper()
346 ascii = ns.network.AsciiTraceHelper();
347 stream = ascii.CreateFileStream(
"mixed-wireless.tr");
348 wifiPhy.EnableAsciiAll(stream);
349 csma.EnableAsciiAll(stream);
350 internet.EnableAsciiIpv4All(stream);
353 csma.EnablePcapAll(
"mixed-wireless",
False)
355 wifiPhy.EnablePcap(
"mixed-wireless", backboneDevices)
356 wifiPhy.EnablePcap(
"mixed-wireless", appSink.GetId(), 0)
370 print (
"Run Simulation.")
371 ns.core.Simulator.Stop(ns.core.Seconds(stopTime))
372 ns.core.Simulator.Run()
373 ns.core.Simulator.Destroy()
376 if __name__ ==
'__main__':