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()
56 wifiChannel = ns.wifi.YansWifiChannelHelper.Default()
57 wifiPhy.SetChannel(wifiChannel.Create())
58 ssid = ns.wifi.Ssid(
"wifi-default")
59 wifiMac.SetType (
"ns3::AdhocWifiMac",
60 "Ssid", ns.wifi.SsidValue(ssid))
62 internet = ns.internet.InternetStackHelper()
63 list_routing = ns.internet.Ipv4ListRoutingHelper()
64 olsr_routing = ns.olsr.OlsrHelper()
65 static_routing = ns.internet.Ipv4StaticRoutingHelper()
66 list_routing.Add(static_routing, 0)
67 list_routing.Add(olsr_routing, 100)
68 internet.SetRoutingHelper(list_routing)
70 ipv4Addresses = ns.internet.Ipv4AddressHelper()
71 ipv4Addresses.SetBase(ns.network.Ipv4Address(
"10.0.0.0"), ns.network.Ipv4Mask(
"255.255.255.0"))
74 onOffHelper = ns.applications.OnOffHelper(
"ns3::UdpSocketFactory",
75 ns.network.Address(ns.network.InetSocketAddress(ns.network.Ipv4Address(
"10.0.0.1"), port)))
76 onOffHelper.SetAttribute(
"DataRate", ns.network.DataRateValue(ns.network.DataRate(
"100kbps")))
77 onOffHelper.SetAttribute(
"OnTime", ns.core.StringValue (
"ns3::ConstantRandomVariable[Constant=1]"))
78 onOffHelper.SetAttribute(
"OffTime", ns.core.StringValue (
"ns3::ConstantRandomVariable[Constant=0]"))
83 if cmd.NumNodesSide
is None:
84 num_nodes_side = NUM_NODES_SIDE
86 num_nodes_side = int(cmd.NumNodesSide)
88 for xi
in range(num_nodes_side):
89 for yi
in range(num_nodes_side):
91 node = ns.network.Node()
94 internet.Install(ns.network.NodeContainer(node))
96 mobility = ns.mobility.ConstantPositionMobilityModel()
97 mobility.SetPosition(ns.core.Vector(xi*DISTANCE, yi*DISTANCE, 0))
98 node.AggregateObject(mobility)
100 devices = wifi.Install(wifiPhy, wifiMac, node)
101 ipv4_interfaces = ipv4Addresses.Assign(devices)
102 addresses.append(ipv4_interfaces.GetAddress(0))
104 for i, node
in enumerate(nodes):
105 destaddr = addresses[(len(addresses) - 1 - i) % len(addresses)]
107 onOffHelper.SetAttribute(
"Remote", ns.network.AddressValue(ns.network.InetSocketAddress(destaddr, port)))
108 app = onOffHelper.Install(ns.network.NodeContainer(node))
109 urv = ns.core.UniformRandomVariable()
110 app.Start(ns.core.Seconds(urv.GetValue(20, 30)))
113 flowmon_helper = ns.flow_monitor.FlowMonitorHelper()
115 monitor = flowmon_helper.InstallAll()
116 monitor = flowmon_helper.GetMonitor()
117 monitor.SetAttribute(
"DelayBinWidth", ns.core.DoubleValue(0.001))
118 monitor.SetAttribute(
"JitterBinWidth", ns.core.DoubleValue(0.001))
119 monitor.SetAttribute(
"PacketSizeBinWidth", ns.core.DoubleValue(20))
121 ns.core.Simulator.Stop(ns.core.Seconds(44.0))
122 ns.core.Simulator.Run()
124 def print_stats(os, st):
125 print (
" Tx Bytes: ", st.txBytes, file=os)
126 print (
" Rx Bytes: ", st.rxBytes, file=os)
127 print (
" Tx Packets: ", st.txPackets, file=os)
128 print (
" Rx Packets: ", st.rxPackets, file=os)
129 print (
" Lost Packets: ", st.lostPackets, file=os)
131 print (
" Mean{Delay}: ", (st.delaySum.GetSeconds() / st.rxPackets), file=os)
132 print (
" Mean{Jitter}: ", (st.jitterSum.GetSeconds() / (st.rxPackets-1)), file=os)
133 print (
" Mean{Hop Count}: ", float(st.timesForwarded) / st.rxPackets + 1, file=os)
136 print (
"Delay Histogram", file=os)
137 for i
in range(st.delayHistogram.GetNBins () ):
138 print (
" ",i,
"(", st.delayHistogram.GetBinStart (i),
"-", \
139 st.delayHistogram.GetBinEnd (i),
"): ", st.delayHistogram.GetBinCount (i), file=os)
140 print (
"Jitter Histogram", file=os)
141 for i
in range(st.jitterHistogram.GetNBins () ):
142 print (
" ",i,
"(", st.jitterHistogram.GetBinStart (i),
"-", \
143 st.jitterHistogram.GetBinEnd (i),
"): ", st.jitterHistogram.GetBinCount (i), file=os)
144 print (
"PacketSize Histogram", file=os)
145 for i
in range(st.packetSizeHistogram.GetNBins () ):
146 print (
" ",i,
"(", st.packetSizeHistogram.GetBinStart (i),
"-", \
147 st.packetSizeHistogram.GetBinEnd (i),
"): ", st.packetSizeHistogram.GetBinCount (i), file=os)
149 for reason, drops
in enumerate(st.packetsDropped):
150 print (
" Packets dropped by reason %i: %i" % (reason, drops), file=os)
154 monitor.CheckForLostPackets()
155 classifier = flowmon_helper.GetClassifier()
157 if cmd.Results
is None:
158 for flow_id, flow_stats
in monitor.GetFlowStats():
159 t = classifier.FindFlow(flow_id)
160 proto = {6:
'TCP', 17:
'UDP'} [t.protocol]
161 print (
"FlowID: %i (%s %s/%s --> %s/%i)" % \
162 (flow_id, proto, t.sourceAddress, t.sourcePort, t.destinationAddress, t.destinationPort))
163 print_stats(sys.stdout, flow_stats)
165 print (monitor.SerializeToXmlFile(cmd.Results,
True,
True))
168 if cmd.Plot
is not None:
171 for flow_id, flow_stats
in monitor.GetFlowStats():
172 tupl = classifier.FindFlow(flow_id)
173 if tupl.protocol == 17
and tupl.sourcePort == 698:
175 delays.append(flow_stats.delaySum.GetSeconds() / flow_stats.rxPackets)
176 pylab.hist(delays, 20)
177 pylab.xlabel(
"Delay (s)")
178 pylab.ylabel(
"Number of Flows")
184 if __name__ ==
'__main__':
185 sys.exit(main(sys.argv))