examples/flowmon.py
author Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
Thu May 07 18:17:17 2009 +0100 (9 months ago)
changeset 3972 4c6cb7db6d87
parent 3964 efdf773a79d0
child 3975 9e833fd91f0d
permissions -rw-r--r--
Add a readme file explaining the purpose of this branch
     1 # -*-  Mode: Python; -*-
     2 #  Copyright (c) 2009 INESC Porto
     3 # 
     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;
     7 # 
     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.
    12 # 
    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
    16 # 
    17 #  Authors: Gustavo Carneiro <gjc@inescporto.pt>
    18 
    19 import sys
    20 import ns3
    21 
    22 DISTANCE = 150 # (m)
    23 NUM_NODES_SIDE = 3
    24 
    25 def main(argv):
    26 
    27     cmd = ns3.CommandLine()
    28 
    29     cmd.Viz = None
    30     cmd.AddValue("Viz", "Enable visualizer")
    31 
    32     cmd.NumNodesSide = None
    33     cmd.AddValue("NumNodesSide", "Grid side number of nodes (total number of nodes will be this number squared)")
    34 
    35     cmd.Parse(argv)
    36 
    37     ns3.RandomVariable.UseGlobalSeed(1, 1, 2, 3, 5, 8)
    38 
    39     #ns3.Config.SetDefault("ns3::Ipv4L3Protocol::CalcChecksum", ns3.BooleanValue(True))
    40 
    41     channel = ns3.WifiChannel()
    42     channel.SetPropagationDelayModel(ns3.ConstantSpeedPropagationDelayModel())
    43     log = ns3.LogDistancePropagationLossModel()
    44     log.SetReferenceModel(ns3.FriisPropagationLossModel())
    45     channel.SetPropagationLossModel(log)
    46 
    47     wifi = ns3.WifiHelper()
    48     wifi.SetMac("ns3::AdhocWifiMac")
    49     wifi.SetPhy("ns3::WifiPhy");
    50     wifi.SetRemoteStationManager ("ns3::ArfWifiManager");
    51 
    52     internet = ns3.InternetStackHelper()
    53     ipv4Addresses = ns3.Ipv4AddressHelper()
    54     ipv4Addresses.SetBase(ns3.Ipv4Address("10.0.0.0"), ns3.Ipv4Mask("255.255.255.255"))
    55     
    56     olsrHelper = ns3.OlsrHelper()
    57 
    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)))
    64 
    65     addresses = []
    66     nodes = []
    67 
    68     if cmd.NumNodesSide is None:
    69         num_nodes_side = NUM_NODES_SIDE
    70     else:
    71         num_nodes_side = int(cmd.NumNodesSide)
    72 
    73     for xi in range(num_nodes_side):
    74         for yi in range(num_nodes_side):
    75 
    76             node = ns3.Node()
    77             nodes.append(node)
    78 
    79             internet.Install(ns3.NodeContainer(node))
    80 
    81             mobility = ns3.StaticMobilityModel()
    82             mobility.SetPosition(ns3.Vector(xi*DISTANCE, yi*DISTANCE, 0))
    83             node.AggregateObject(mobility)
    84             
    85             devices = wifi.Install(ns3.NodeContainer(node), channel)
    86             ipv4_interfaces = ipv4Addresses.Assign(devices)
    87             addresses.append(ipv4_interfaces.GetAddress(0))
    88             
    89             olsrHelper.Install(ns3.NodeContainer(node))
    90 
    91     for i, node in enumerate(nodes):
    92         destaddr = addresses[(len(addresses) - 1 - i) % len(addresses)]
    93         #print i, destaddr
    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()))
    97             
    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))
   105 
   106     ns3.Simulator.Stop(ns3.Seconds(44.0))
   107     if cmd.Viz is not None:
   108         import visualizer
   109         visualizer.start()
   110     else:
   111         ns3.Simulator.Run()
   112 
   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
   119         if st.rxPackets > 0:
   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
   123 
   124         if 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)
   137 
   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)
   142 
   143     monitor.CheckForLostPackets()
   144     classifier = flowmon_helper.GetClassifier()
   145 
   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)
   152 
   153 
   154     return 0
   155 
   156 
   157 if __name__ == '__main__':
   158     sys.exit(main(sys.argv))
   159