19 from __future__
import print_function
22 import ns.applications
24 import ns.flow_monitor
40 cmd = ns.core.CommandLine()
42 cmd.NumNodesSide =
None 43 cmd.AddValue(
"NumNodesSide",
"Grid side number of nodes (total number of nodes will be this number squared)")
46 cmd.AddValue(
"Results",
"Write XML results to file")
49 cmd.AddValue(
"Plot",
"Plot the results using the matplotlib python module")
53 wifi = ns.wifi.WifiHelper()
54 wifiMac = ns.wifi.WifiMacHelper()
55 wifiPhy = ns.wifi.YansWifiPhyHelper.Default()
56 wifiChannel = ns.wifi.YansWifiChannelHelper.Default()
57 wifiPhy.SetChannel(wifiChannel.Create())
58 ssid = ns.wifi.Ssid(
"wifi-default")
59 wifi.SetRemoteStationManager(
"ns3::ArfWifiManager")
60 wifiMac.SetType (
"ns3::AdhocWifiMac",
61 "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 onOffHelper = ns.applications.OnOffHelper(
"ns3::UdpSocketFactory",
76 ns.network.Address(ns.network.InetSocketAddress(ns.network.Ipv4Address(
"10.0.0.1"), port)))
77 onOffHelper.SetAttribute(
"DataRate", ns.network.DataRateValue(ns.network.DataRate(
"100kbps")))
78 onOffHelper.SetAttribute(
"OnTime", ns.core.StringValue (
"ns3::ConstantRandomVariable[Constant=1]"))
79 onOffHelper.SetAttribute(
"OffTime", ns.core.StringValue (
"ns3::ConstantRandomVariable[Constant=0]"))
84 if cmd.NumNodesSide
is None:
85 num_nodes_side = NUM_NODES_SIDE
87 num_nodes_side = int(cmd.NumNodesSide)
89 for xi
in range(num_nodes_side):
90 for yi
in range(num_nodes_side):
92 node = ns.network.Node()
95 internet.Install(ns.network.NodeContainer(node))
97 mobility = ns.mobility.ConstantPositionMobilityModel()
98 mobility.SetPosition(ns.core.Vector(xi*DISTANCE, yi*DISTANCE, 0))
99 node.AggregateObject(mobility)
101 devices = wifi.Install(wifiPhy, wifiMac, node)
102 ipv4_interfaces = ipv4Addresses.Assign(devices)
103 addresses.append(ipv4_interfaces.GetAddress(0))
105 for i, node
in enumerate(nodes):
106 destaddr = addresses[(len(addresses) - 1 - i) % len(addresses)]
108 onOffHelper.SetAttribute(
"Remote", ns.network.AddressValue(ns.network.InetSocketAddress(destaddr, port)))
109 app = onOffHelper.Install(ns.network.NodeContainer(node))
110 urv = ns.core.UniformRandomVariable()
111 app.Start(ns.core.Seconds(urv.GetValue(20, 30)))
114 flowmon_helper = ns.flow_monitor.FlowMonitorHelper()
116 monitor = flowmon_helper.InstallAll()
117 monitor = flowmon_helper.GetMonitor()
118 monitor.SetAttribute(
"DelayBinWidth", ns.core.DoubleValue(0.001))
119 monitor.SetAttribute(
"JitterBinWidth", ns.core.DoubleValue(0.001))
120 monitor.SetAttribute(
"PacketSizeBinWidth", ns.core.DoubleValue(20))
122 ns.core.Simulator.Stop(ns.core.Seconds(44.0))
123 ns.core.Simulator.Run()
125 def print_stats(os, st):
126 print (
" Tx Bytes: ", st.txBytes, file=os)
127 print (
" Rx Bytes: ", st.rxBytes, file=os)
128 print (
" Tx Packets: ", st.txPackets, file=os)
129 print (
" Rx Packets: ", st.rxPackets, file=os)
130 print (
" Lost Packets: ", st.lostPackets, file=os)
132 print (
" Mean{Delay}: ", (st.delaySum.GetSeconds() / st.rxPackets), file=os)
133 print (
" Mean{Jitter}: ", (st.jitterSum.GetSeconds() / (st.rxPackets-1)), file=os)
134 print (
" Mean{Hop Count}: ", float(st.timesForwarded) / st.rxPackets + 1, file=os)
137 print (
"Delay Histogram", file=os)
138 for i
in range(st.delayHistogram.GetNBins () ):
139 print (
" ",i,
"(", st.delayHistogram.GetBinStart (i),
"-", \
140 st.delayHistogram.GetBinEnd (i),
"): ", st.delayHistogram.GetBinCount (i), file=os)
141 print (
"Jitter Histogram", file=os)
142 for i
in range(st.jitterHistogram.GetNBins () ):
143 print (
" ",i,
"(", st.jitterHistogram.GetBinStart (i),
"-", \
144 st.jitterHistogram.GetBinEnd (i),
"): ", st.jitterHistogram.GetBinCount (i), file=os)
145 print (
"PacketSize Histogram", file=os)
146 for i
in range(st.packetSizeHistogram.GetNBins () ):
147 print (
" ",i,
"(", st.packetSizeHistogram.GetBinStart (i),
"-", \
148 st.packetSizeHistogram.GetBinEnd (i),
"): ", st.packetSizeHistogram.GetBinCount (i), file=os)
150 for reason, drops
in enumerate(st.packetsDropped):
151 print (
" Packets dropped by reason %i: %i" % (reason, drops), file=os)
155 monitor.CheckForLostPackets()
156 classifier = flowmon_helper.GetClassifier()
158 if cmd.Results
is None:
159 for flow_id, flow_stats
in monitor.GetFlowStats():
160 t = classifier.FindFlow(flow_id)
161 proto = {6:
'TCP', 17:
'UDP'} [t.protocol]
162 print (
"FlowID: %i (%s %s/%s --> %s/%i)" % \
163 (flow_id, proto, t.sourceAddress, t.sourcePort, t.destinationAddress, t.destinationPort))
164 print_stats(sys.stdout, flow_stats)
166 print (monitor.SerializeToXmlFile(cmd.Results,
True,
True))
169 if cmd.Plot
is not None:
172 for flow_id, flow_stats
in monitor.GetFlowStats():
173 tupl = classifier.FindFlow(flow_id)
174 if tupl.protocol == 17
and tupl.sourcePort == 698:
176 delays.append(flow_stats.delaySum.GetSeconds() / flow_stats.rxPackets)
177 pylab.hist(delays, 20)
178 pylab.xlabel(
"Delay (s)")
179 pylab.ylabel(
"Number of Flows")
185 if __name__ ==
'__main__':
186 sys.exit(main(sys.argv))