1 # -*- Mode: Python; -*-
2 # Copyright (c) 2009 INESC Porto
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License version 2 as
6 # published by the Free Software Foundation;
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 # Authors: Gustavo Carneiro <gjc@inescporto.pt>
27 cmd = ns3.CommandLine()
30 cmd.AddValue("Viz", "Enable visualizer")
32 cmd.NumNodesSide = None
33 cmd.AddValue("NumNodesSide", "Grid side number of nodes (total number of nodes will be this number squared)")
37 ns3.RandomVariable.UseGlobalSeed(1, 1, 2, 3, 5, 8)
39 #ns3.Config.SetDefault("ns3::Ipv4L3Protocol::CalcChecksum", ns3.BooleanValue(True))
41 channel = ns3.WifiChannel()
42 channel.SetPropagationDelayModel(ns3.ConstantSpeedPropagationDelayModel())
43 log = ns3.LogDistancePropagationLossModel()
44 log.SetReferenceModel(ns3.FriisPropagationLossModel())
45 channel.SetPropagationLossModel(log)
47 wifi = ns3.WifiHelper()
48 wifi.SetMac("ns3::AdhocWifiMac")
49 wifi.SetPhy("ns3::WifiPhy");
50 wifi.SetRemoteStationManager ("ns3::ArfWifiManager");
52 internet = ns3.InternetStackHelper()
53 ipv4Addresses = ns3.Ipv4AddressHelper()
54 ipv4Addresses.SetBase(ns3.Ipv4Address("10.0.0.0"), ns3.Ipv4Mask("255.255.255.255"))
56 olsrHelper = ns3.OlsrHelper()
58 port = 9 # Discard port(RFC 863)
59 onOffHelper = ns3.OnOffHelper("ns3::UdpSocketFactory",
60 ns3.Address(ns3.InetSocketAddress(ns3.Ipv4Address("10.0.0.1"), port)))
61 onOffHelper.SetAttribute("DataRate", ns3.DataRateValue(ns3.DataRate("100kbps")))
62 onOffHelper.SetAttribute("OnTime", ns3.RandomVariableValue(ns3.ConstantVariable(1)))
63 onOffHelper.SetAttribute("OffTime", ns3.RandomVariableValue(ns3.ConstantVariable(0)))
68 if cmd.NumNodesSide is None:
69 num_nodes_side = NUM_NODES_SIDE
71 num_nodes_side = int(cmd.NumNodesSide)
73 for xi in range(num_nodes_side):
74 for yi in range(num_nodes_side):
79 internet.Install(ns3.NodeContainer(node))
81 mobility = ns3.StaticMobilityModel()
82 mobility.SetPosition(ns3.Vector(xi*DISTANCE, yi*DISTANCE, 0))
83 node.AggregateObject(mobility)
85 devices = wifi.Install(ns3.NodeContainer(node), channel)
86 ipv4_interfaces = ipv4Addresses.Assign(devices)
87 addresses.append(ipv4_interfaces.GetAddress(0))
89 olsrHelper.Install(ns3.NodeContainer(node))
91 for i, node in enumerate(nodes):
92 destaddr = addresses[(len(addresses) - 1 - i) % len(addresses)]
94 onOffHelper.SetAttribute("Remote", ns3.AddressValue(ns3.InetSocketAddress(destaddr, port)))
95 app = onOffHelper.Install(ns3.NodeContainer(node))
96 app.Start(ns3.Seconds(ns3.UniformVariable(20, 30).GetValue()))
98 #internet.EnablePcapAll("wifi-olsr")
99 flowmon_helper = ns3.FlowMonitorHelper()
100 #flowmon_helper.SetMonitorAttribute("StartTime", ns3.TimeValue(ns3.Seconds(31)))
101 monitor = flowmon_helper.InstallAll()
102 monitor.SetAttribute("DelayBinWidth", ns3.DoubleValue(0.001))
103 monitor.SetAttribute("JitterBinWidth", ns3.DoubleValue(0.001))
104 monitor.SetAttribute("PacketSizeBinWidth", ns3.DoubleValue(20))
106 ns3.Simulator.Stop(ns3.Seconds(44.0))
107 if cmd.Viz is not None:
113 def print_stats(os, st):
114 print >> os, " Tx Bytes: ", st.txBytes
115 print >> os, " Rx Bytes: ", st.rxBytes
116 print >> os, " Tx Packets: ", st.txPackets
117 print >> os, " Rx Packets: ", st.rxPackets
118 print >> os, " Lost Packets: ", st.lostPackets
120 print >> os, " Mean{Delay}: ", (st.delaySum.GetSeconds() / st.rxPackets)
121 print >> os, " Mean{Jitter}: ", (st.jitterSum.GetSeconds() / (st.rxPackets-1))
122 print >> os, " Mean{Hop Count}: ", float(st.timesForwarded) / st.rxPackets + 1
125 print >> os, "Delay Histogram"
126 for i in range(st.delayHistogram.GetNBins () ):
127 print >> os, " ",i,"(", st.delayHistogram.GetBinStart (i), "-", \
128 st.delayHistogram.GetBinEnd (i), "): ", st.delayHistogram.GetBinCount (i)
129 print >> os, "Jitter Histogram"
130 for i in range(st.jitterHistogram.GetNBins () ):
131 print >> os, " ",i,"(", st.jitterHistogram.GetBinStart (i), "-", \
132 st.jitterHistogram.GetBinEnd (i), "): ", st.jitterHistogram.GetBinCount (i)
133 print >> os, "PacketSize Histogram"
134 for i in range(st.packetSizeHistogram.GetNBins () ):
135 print >> os, " ",i,"(", st.packetSizeHistogram.GetBinStart (i), "-", \
136 st.packetSizeHistogram.GetBinEnd (i), "): ", st.packetSizeHistogram.GetBinCount (i)
138 for reason, drops in enumerate(st.packetsDropped):
139 print " Packets dropped by reason %i: %i" % (reason, drops)
140 #for reason, drops in enumerate(st.bytesDropped):
141 # print "Bytes dropped by reason %i: %i" % (reason, drops)
143 monitor.CheckForLostPackets()
144 classifier = flowmon_helper.GetClassifier()
146 for flow_id, flow_stats in monitor.GetFlowStats():
147 t = classifier.FindFlow(flow_id)
148 proto = {6: 'TCP', 17: 'UDP'} [t.protocol]
149 print "FlowID: %i (%s %s/%s --> %s/%i)" % \
150 (flow_id, proto, t.sourceAddress, t.sourcePort, t.destinationAddress, t.destinationPort)
151 print_stats(sys.stdout, flow_stats)
157 if __name__ == '__main__':
158 sys.exit(main(sys.argv))