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.Default()
139 wifiChannel = ns.wifi.YansWifiChannelHelper.Default()
140 wifiPhy.SetChannel(wifiChannel.Create())
141 backboneDevices = wifi.Install(wifiPhy, mac, backbone)
145 print (
"Enabling OLSR routing on all backbone nodes")
146 internet = ns.internet.InternetStackHelper()
147 olsr = ns.olsr.OlsrHelper()
148 internet.SetRoutingHelper(olsr);
149 internet.Install(backbone);
156 ipAddrs = ns.internet.Ipv4AddressHelper()
157 ipAddrs.SetBase(ns.network.Ipv4Address(
"192.168.0.0"), ns.network.Ipv4Mask(
"255.255.255.0"))
158 ipAddrs.Assign(backboneDevices)
164 mobility = ns.mobility.MobilityHelper()
165 mobility.SetPositionAllocator(
"ns3::GridPositionAllocator",
166 "MinX", ns.core.DoubleValue(20.0),
167 "MinY", ns.core.DoubleValue(20.0),
168 "DeltaX", ns.core.DoubleValue(20.0),
169 "DeltaY", ns.core.DoubleValue(20.0),
170 "GridWidth", ns.core.UintegerValue(5),
171 "LayoutType", ns.core.StringValue(
"RowFirst"))
172 mobility.SetMobilityModel(
"ns3::RandomDirection2dMobilityModel",
173 "Bounds", ns.mobility.RectangleValue(ns.mobility.Rectangle(-500, 500, -500, 500)),
174 "Speed", ns.core.StringValue (
"ns3::ConstantRandomVariable[Constant=2]"),
175 "Pause", ns.core.StringValue (
"ns3::ConstantRandomVariable[Constant=0.2]"))
176 mobility.Install(backbone)
186 ipAddrs.SetBase(ns.network.Ipv4Address(
"172.16.0.0"), ns.network.Ipv4Mask(
"255.255.255.0"))
188 for i
in range(backboneNodes):
189 print (
"Configuring local area network for backbone node ", i)
195 newLanNodes = ns.network.NodeContainer()
196 newLanNodes.Create(lanNodes - 1)
198 lan = ns.network.NodeContainer(ns.network.NodeContainer(backbone.Get(i)), newLanNodes)
203 csma = ns.csma.CsmaHelper()
204 csma.SetChannelAttribute(
"DataRate", ns.network.DataRateValue(ns.network.DataRate(5000000)))
205 csma.SetChannelAttribute(
"Delay", ns.core.TimeValue(ns.core.MilliSeconds(2)))
206 lanDevices = csma.Install(lan)
210 internet.Install(newLanNodes)
215 ipAddrs.Assign(lanDevices)
225 mobilityLan = ns.mobility.MobilityHelper()
226 positionAlloc = ns.mobility.ListPositionAllocator()
227 for j
in range(newLanNodes.GetN()):
228 positionAlloc.Add(ns.core.Vector(0.0, (j*10 + 10), 0.0))
230 mobilityLan.SetPositionAllocator(positionAlloc)
231 mobilityLan.PushReferenceMobilityModel(backbone.Get(i))
232 mobilityLan.SetMobilityModel(
"ns3::ConstantPositionMobilityModel")
233 mobilityLan.Install(newLanNodes);
243 ipAddrs.SetBase(ns.network.Ipv4Address(
"10.0.0.0"), ns.network.Ipv4Mask(
"255.255.255.0"))
245 for i
in range(backboneNodes):
246 print (
"Configuring wireless network for backbone node ", i)
252 stas = ns.network.NodeContainer()
253 stas.Create(infraNodes - 1)
255 infra = ns.network.NodeContainer(ns.network.NodeContainer(backbone.Get(i)), stas)
259 ssid = ns.wifi.Ssid(
'wifi-infra' + str(i))
260 wifiInfra = ns.wifi.WifiHelper()
261 wifiPhy.SetChannel(wifiChannel.Create())
262 wifiInfra.SetRemoteStationManager(
'ns3::ArfWifiManager')
263 macInfra = ns.wifi.WifiMacHelper();
264 macInfra.SetType(
"ns3::StaWifiMac",
265 "Ssid", ns.wifi.SsidValue(ssid))
268 staDevices = wifiInfra.Install(wifiPhy, macInfra, stas)
270 macInfra.SetType(
"ns3::ApWifiMac",
271 "Ssid", ns.wifi.SsidValue(ssid),
272 "BeaconInterval", ns.core.TimeValue(ns.core.Seconds(2.5)))
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__':