19 from __future__
import print_function
25 except ModuleNotFoundError:
27 "Error: ns3 Python module not found;"
28 " Python bindings may not be enabled"
29 " or your PYTHONPATH might not be properly configured"
37 from ctypes
import c_bool, c_char_p, c_int, create_string_buffer
39 NumNodesSide = c_int(2)
42 ResultsBuffer = create_string_buffer(b
"output.xml", BUFFLEN)
43 Results = c_char_p(ResultsBuffer.raw)
45 cmd = ns.CommandLine(__file__)
48 "Grid side number of nodes (total number of nodes will be this number squared)",
51 cmd.AddValue(
"Results",
"Write XML results to file", Results, BUFFLEN)
52 cmd.AddValue(
"Plot",
"Plot the results using the matplotlib python module", Plot)
55 wifi = ns.CreateObject(
"WifiHelper")
56 wifiMac = ns.CreateObject(
"WifiMacHelper")
57 wifiPhy = ns.CreateObject(
"YansWifiPhyHelper")
58 wifiChannel = ns.wifi.YansWifiChannelHelper.Default()
59 wifiPhy.SetChannel(wifiChannel.Create())
60 ssid = ns.wifi.Ssid(
"wifi-default")
61 wifiMac.SetType(
"ns3::AdhocWifiMac",
"Ssid", ns.wifi.SsidValue(ssid))
63 internet = ns.internet.InternetStackHelper()
64 list_routing = ns.internet.Ipv4ListRoutingHelper()
65 olsr_routing = ns.olsr.OlsrHelper()
66 static_routing = ns.internet.Ipv4StaticRoutingHelper()
67 list_routing.Add(static_routing, 0)
68 list_routing.Add(olsr_routing, 100)
69 internet.SetRoutingHelper(list_routing)
71 ipv4Addresses = ns.internet.Ipv4AddressHelper()
72 ipv4Addresses.SetBase(ns.network.Ipv4Address(
"10.0.0.0"), ns.network.Ipv4Mask(
"255.255.255.0"))
75 inetAddress = ns.network.InetSocketAddress(ns.network.Ipv4Address(
"10.0.0.1"), port)
76 onOffHelper = ns.applications.OnOffHelper(
"ns3::UdpSocketFactory", inetAddress.ConvertTo())
77 onOffHelper.SetAttribute(
"DataRate", ns.network.DataRateValue(ns.network.DataRate(
"100kbps")))
78 onOffHelper.SetAttribute(
79 "OnTime", ns.core.StringValue(
"ns3::ConstantRandomVariable[Constant=1]")
81 onOffHelper.SetAttribute(
82 "OffTime", ns.core.StringValue(
"ns3::ConstantRandomVariable[Constant=0]")
88 if NumNodesSide.value == 2:
89 num_nodes_side = NUM_NODES_SIDE
91 num_nodes_side = NumNodesSide.value
93 nodes = ns.NodeContainer(num_nodes_side * num_nodes_side)
95 for xi
in range(num_nodes_side):
96 for yi
in range(num_nodes_side):
97 node = nodes.Get(accumulator)
99 container = ns.network.NodeContainer(node)
100 internet.Install(container)
102 mobility = ns.CreateObject(
"ConstantPositionMobilityModel")
103 mobility.SetPosition(ns.core.Vector(xi * DISTANCE, yi * DISTANCE, 0))
104 node.AggregateObject(mobility)
106 device = wifi.Install(wifiPhy, wifiMac, node)
107 ipv4_interfaces = ipv4Addresses.Assign(device)
108 addresses.append(ipv4_interfaces.GetAddress(0))
110 for i, node
in [(i, nodes.Get(i))
for i
in range(nodes.GetN())]:
111 destaddr = addresses[(len(addresses) - 1 - i) % len(addresses)]
113 onOffHelper.SetAttribute(
115 ns.network.AddressValue(ns.network.InetSocketAddress(destaddr, port).ConvertTo()),
117 container = ns.network.NodeContainer(node)
118 app = onOffHelper.Install(container)
119 urv = ns.CreateObject(
"UniformRandomVariable")
120 startDelay = ns.Seconds(urv.GetValue(20, 30))
121 app.Start(startDelay)
124 flowmon_helper = ns.flow_monitor.FlowMonitorHelper()
126 monitor = flowmon_helper.InstallAll()
127 monitor = flowmon_helper.GetMonitor()
128 monitor.SetAttribute(
"DelayBinWidth", ns.core.DoubleValue(0.001))
129 monitor.SetAttribute(
"JitterBinWidth", ns.core.DoubleValue(0.001))
130 monitor.SetAttribute(
"PacketSizeBinWidth", ns.core.DoubleValue(20))
132 ns.core.Simulator.Stop(ns.core.Seconds(44.0))
133 ns.core.Simulator.Run()
135 def print_stats(os, st):
136 print(
" Tx Bytes: ", st.txBytes, file=os)
137 print(
" Rx Bytes: ", st.rxBytes, file=os)
138 print(
" Tx Packets: ", st.txPackets, file=os)
139 print(
" Rx Packets: ", st.rxPackets, file=os)
140 print(
" Lost Packets: ", st.lostPackets, file=os)
142 print(
" Mean{Delay}: ", (st.delaySum.GetSeconds() / st.rxPackets), file=os)
143 print(
" Mean{Jitter}: ", (st.jitterSum.GetSeconds() / (st.rxPackets - 1)), file=os)
144 print(
" Mean{Hop Count}: ", float(st.timesForwarded) / st.rxPackets + 1, file=os)
147 print(
"Delay Histogram", file=os)
148 for i
in range(st.delayHistogram.GetNBins()):
153 st.delayHistogram.GetBinStart(i),
155 st.delayHistogram.GetBinEnd(i),
157 st.delayHistogram.GetBinCount(i),
160 print(
"Jitter Histogram", file=os)
161 for i
in range(st.jitterHistogram.GetNBins()):
166 st.jitterHistogram.GetBinStart(i),
168 st.jitterHistogram.GetBinEnd(i),
170 st.jitterHistogram.GetBinCount(i),
173 print(
"PacketSize Histogram", file=os)
174 for i
in range(st.packetSizeHistogram.GetNBins()):
179 st.packetSizeHistogram.GetBinStart(i),
181 st.packetSizeHistogram.GetBinEnd(i),
183 st.packetSizeHistogram.GetBinCount(i),
187 for reason, drops
in enumerate(st.packetsDropped):
188 print(
" Packets dropped by reason %i: %i" % (reason, drops), file=os)
192 monitor.CheckForLostPackets()
193 classifier = flowmon_helper.GetClassifier()
195 if Results.value != b
"output.xml":
196 for flow_id, flow_stats
in monitor.GetFlowStats():
197 t = classifier.FindFlow(flow_id)
198 proto = {6:
"TCP", 17:
"UDP"}[t.protocol]
200 "FlowID: %i (%s %s/%s --> %s/%i)"
206 t.destinationAddress,
210 print_stats(sys.stdout, flow_stats)
212 res = monitor.SerializeToXmlFile(Results.value.decode(
"utf-8"),
True,
True)
219 for flow_id, flow_stats
in monitor.GetFlowStats():
220 tupl = classifier.FindFlow(flow_id)
221 if tupl.protocol == 17
and tupl.sourcePort == 698:
223 delays.append(flow_stats.delaySum.GetSeconds() / flow_stats.rxPackets)
224 pylab.hist(delays, 20)
225 pylab.xlabel(
"Delay (s)")
226 pylab.ylabel(
"Number of Flows")
232 if __name__ ==
"__main__":
233 sys.exit(main(sys.argv))