src/flow-monitor/examples/wifi-olsr-flowmon.py
author Mitch Watrous
Mon, 13 Aug 2012 16:08:13 -0700
changeset 8966 060dba23e9bb
parent 7291 d39c09dbc3d9
child 8996 d0e0a162a990
permissions -rw-r--r--
Replace src/application usage of RandomVariable with RandomVariableStream
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5223
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
     1
# -*-  Mode: Python; -*-
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
     2
#  Copyright (c) 2009 INESC Porto
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
     3
# 
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
     4
#  This program is free software; you can redistribute it and/or modify
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
     5
#  it under the terms of the GNU General Public License version 2 as
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
     6
#  published by the Free Software Foundation;
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
     7
# 
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
     8
#  This program is distributed in the hope that it will be useful,
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
     9
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    10
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    11
#  GNU General Public License for more details.
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    12
# 
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    13
#  You should have received a copy of the GNU General Public License
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    14
#  along with this program; if not, write to the Free Software
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    15
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    16
# 
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    17
#  Authors: Gustavo Carneiro <gjc@inescporto.pt>
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    18
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    19
import sys
7106
2939fecb7029 Make Python examples use new modular bindings without importing everything
Mitch Watrous <watrous@u.washington.edu>
parents: 6939
diff changeset
    20
2939fecb7029 Make Python examples use new modular bindings without importing everything
Mitch Watrous <watrous@u.washington.edu>
parents: 6939
diff changeset
    21
import ns.applications
2939fecb7029 Make Python examples use new modular bindings without importing everything
Mitch Watrous <watrous@u.washington.edu>
parents: 6939
diff changeset
    22
import ns.core
2939fecb7029 Make Python examples use new modular bindings without importing everything
Mitch Watrous <watrous@u.washington.edu>
parents: 6939
diff changeset
    23
import ns.flow_monitor
2939fecb7029 Make Python examples use new modular bindings without importing everything
Mitch Watrous <watrous@u.washington.edu>
parents: 6939
diff changeset
    24
import ns.internet
2939fecb7029 Make Python examples use new modular bindings without importing everything
Mitch Watrous <watrous@u.washington.edu>
parents: 6939
diff changeset
    25
import ns.mobility
2939fecb7029 Make Python examples use new modular bindings without importing everything
Mitch Watrous <watrous@u.washington.edu>
parents: 6939
diff changeset
    26
import ns.network
2939fecb7029 Make Python examples use new modular bindings without importing everything
Mitch Watrous <watrous@u.washington.edu>
parents: 6939
diff changeset
    27
import ns.olsr
2939fecb7029 Make Python examples use new modular bindings without importing everything
Mitch Watrous <watrous@u.washington.edu>
parents: 6939
diff changeset
    28
import ns.wifi
5223
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    29
6543
c4a419fce478 Bring nodes close together, due to changes in ns-3.9 that bring reduced wifi range
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 5369
diff changeset
    30
DISTANCE = 100 # (m)
5223
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    31
NUM_NODES_SIDE = 3
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    32
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    33
def main(argv):
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    34
7106
2939fecb7029 Make Python examples use new modular bindings without importing everything
Mitch Watrous <watrous@u.washington.edu>
parents: 6939
diff changeset
    35
    cmd = ns.core.CommandLine()
5223
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    36
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    37
    cmd.NumNodesSide = None
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    38
    cmd.AddValue("NumNodesSide", "Grid side number of nodes (total number of nodes will be this number squared)")
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    39
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    40
    cmd.Results = None
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    41
    cmd.AddValue("Results", "Write XML results to file")
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    42
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    43
    cmd.Plot = None
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    44
    cmd.AddValue("Plot", "Plot the results using the matplotlib python module")
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    45
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    46
    cmd.Parse(argv)
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    47
7106
2939fecb7029 Make Python examples use new modular bindings without importing everything
Mitch Watrous <watrous@u.washington.edu>
parents: 6939
diff changeset
    48
    wifi = ns.wifi.WifiHelper.Default()
2939fecb7029 Make Python examples use new modular bindings without importing everything
Mitch Watrous <watrous@u.washington.edu>
parents: 6939
diff changeset
    49
    wifiMac = ns.wifi.NqosWifiMacHelper.Default()
2939fecb7029 Make Python examples use new modular bindings without importing everything
Mitch Watrous <watrous@u.washington.edu>
parents: 6939
diff changeset
    50
    wifiPhy = ns.wifi.YansWifiPhyHelper.Default()
2939fecb7029 Make Python examples use new modular bindings without importing everything
Mitch Watrous <watrous@u.washington.edu>
parents: 6939
diff changeset
    51
    wifiChannel = ns.wifi.YansWifiChannelHelper.Default()
5223
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    52
    wifiPhy.SetChannel(wifiChannel.Create())
7106
2939fecb7029 Make Python examples use new modular bindings without importing everything
Mitch Watrous <watrous@u.washington.edu>
parents: 6939
diff changeset
    53
    ssid = ns.wifi.Ssid("wifi-default")
5223
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    54
    wifi.SetRemoteStationManager("ns3::ArfWifiManager")
6673
ec22aa763e2d Bug 978: Consolidate Wi-Fi MAC high functionality
Dean Armstrong <deanarm@gmail.com>
parents: 6631
diff changeset
    55
    wifiMac.SetType ("ns3::AdhocWifiMac",
7106
2939fecb7029 Make Python examples use new modular bindings without importing everything
Mitch Watrous <watrous@u.washington.edu>
parents: 6939
diff changeset
    56
                     "Ssid", ns.wifi.SsidValue(ssid))
5223
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    57
7106
2939fecb7029 Make Python examples use new modular bindings without importing everything
Mitch Watrous <watrous@u.washington.edu>
parents: 6939
diff changeset
    58
    internet = ns.internet.InternetStackHelper()
2939fecb7029 Make Python examples use new modular bindings without importing everything
Mitch Watrous <watrous@u.washington.edu>
parents: 6939
diff changeset
    59
    list_routing = ns.internet.Ipv4ListRoutingHelper()
2939fecb7029 Make Python examples use new modular bindings without importing everything
Mitch Watrous <watrous@u.washington.edu>
parents: 6939
diff changeset
    60
    olsr_routing = ns.olsr.OlsrHelper()
2939fecb7029 Make Python examples use new modular bindings without importing everything
Mitch Watrous <watrous@u.washington.edu>
parents: 6939
diff changeset
    61
    static_routing = ns.internet.Ipv4StaticRoutingHelper()
5223
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    62
    list_routing.Add(static_routing, 0)
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    63
    list_routing.Add(olsr_routing, 100)
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    64
    internet.SetRoutingHelper(list_routing)
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    65
7106
2939fecb7029 Make Python examples use new modular bindings without importing everything
Mitch Watrous <watrous@u.washington.edu>
parents: 6939
diff changeset
    66
    ipv4Addresses = ns.internet.Ipv4AddressHelper()
2939fecb7029 Make Python examples use new modular bindings without importing everything
Mitch Watrous <watrous@u.washington.edu>
parents: 6939
diff changeset
    67
    ipv4Addresses.SetBase(ns.network.Ipv4Address("10.0.0.0"), ns.network.Ipv4Mask("255.255.255.0"))
5223
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    68
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    69
    port = 9   # Discard port(RFC 863)
7106
2939fecb7029 Make Python examples use new modular bindings without importing everything
Mitch Watrous <watrous@u.washington.edu>
parents: 6939
diff changeset
    70
    onOffHelper = ns.applications.OnOffHelper("ns3::UdpSocketFactory",
2939fecb7029 Make Python examples use new modular bindings without importing everything
Mitch Watrous <watrous@u.washington.edu>
parents: 6939
diff changeset
    71
                                  ns.network.Address(ns.network.InetSocketAddress(ns.network.Ipv4Address("10.0.0.1"), port)))
2939fecb7029 Make Python examples use new modular bindings without importing everything
Mitch Watrous <watrous@u.washington.edu>
parents: 6939
diff changeset
    72
    onOffHelper.SetAttribute("DataRate", ns.network.DataRateValue(ns.network.DataRate("100kbps")))
8966
060dba23e9bb Replace src/application usage of RandomVariable with RandomVariableStream
Mitch Watrous
parents: 7291
diff changeset
    73
    onOffHelper.SetAttribute("OnTime", ns.core.StringValue ("ns3::ConstantRandomVariable[Constant=1]"))
060dba23e9bb Replace src/application usage of RandomVariable with RandomVariableStream
Mitch Watrous
parents: 7291
diff changeset
    74
    onOffHelper.SetAttribute("OffTime", ns.core.StringValue ("ns3::ConstantRandomVariable[Constant=0]"))
5223
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    75
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    76
    addresses = []
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    77
    nodes = []
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    78
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    79
    if cmd.NumNodesSide is None:
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    80
        num_nodes_side = NUM_NODES_SIDE
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    81
    else:
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    82
        num_nodes_side = int(cmd.NumNodesSide)
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    83
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    84
    for xi in range(num_nodes_side):
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    85
        for yi in range(num_nodes_side):
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    86
7106
2939fecb7029 Make Python examples use new modular bindings without importing everything
Mitch Watrous <watrous@u.washington.edu>
parents: 6939
diff changeset
    87
            node = ns.network.Node()
5223
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    88
            nodes.append(node)
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    89
7106
2939fecb7029 Make Python examples use new modular bindings without importing everything
Mitch Watrous <watrous@u.washington.edu>
parents: 6939
diff changeset
    90
            internet.Install(ns.network.NodeContainer(node))
5223
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    91
7106
2939fecb7029 Make Python examples use new modular bindings without importing everything
Mitch Watrous <watrous@u.washington.edu>
parents: 6939
diff changeset
    92
            mobility = ns.mobility.ConstantPositionMobilityModel()
2939fecb7029 Make Python examples use new modular bindings without importing everything
Mitch Watrous <watrous@u.washington.edu>
parents: 6939
diff changeset
    93
            mobility.SetPosition(ns.core.Vector(xi*DISTANCE, yi*DISTANCE, 0))
5223
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    94
            node.AggregateObject(mobility)
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    95
            
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    96
            devices = wifi.Install(wifiPhy, wifiMac, node)
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    97
            ipv4_interfaces = ipv4Addresses.Assign(devices)
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    98
            addresses.append(ipv4_interfaces.GetAddress(0))
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    99
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   100
    for i, node in enumerate(nodes):
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   101
        destaddr = addresses[(len(addresses) - 1 - i) % len(addresses)]
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   102
        #print i, destaddr
7106
2939fecb7029 Make Python examples use new modular bindings without importing everything
Mitch Watrous <watrous@u.washington.edu>
parents: 6939
diff changeset
   103
        onOffHelper.SetAttribute("Remote", ns.network.AddressValue(ns.network.InetSocketAddress(destaddr, port)))
2939fecb7029 Make Python examples use new modular bindings without importing everything
Mitch Watrous <watrous@u.washington.edu>
parents: 6939
diff changeset
   104
        app = onOffHelper.Install(ns.network.NodeContainer(node))
2939fecb7029 Make Python examples use new modular bindings without importing everything
Mitch Watrous <watrous@u.washington.edu>
parents: 6939
diff changeset
   105
        app.Start(ns.core.Seconds(ns.core.UniformVariable(20, 30).GetValue()))
5223
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   106
            
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   107
    #internet.EnablePcapAll("wifi-olsr")
7106
2939fecb7029 Make Python examples use new modular bindings without importing everything
Mitch Watrous <watrous@u.washington.edu>
parents: 6939
diff changeset
   108
    flowmon_helper = ns.flow_monitor.FlowMonitorHelper()
2939fecb7029 Make Python examples use new modular bindings without importing everything
Mitch Watrous <watrous@u.washington.edu>
parents: 6939
diff changeset
   109
    #flowmon_helper.SetMonitorAttribute("StartTime", ns.core.TimeValue(ns.core.Seconds(31)))
5223
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   110
    monitor = flowmon_helper.InstallAll()
7291
d39c09dbc3d9 Make emu and template modules not be built if not appropriate
Mitch Watrous <watrous@u.washington.edu>
parents: 7106
diff changeset
   111
    monitor = flowmon_helper.GetMonitor()
7106
2939fecb7029 Make Python examples use new modular bindings without importing everything
Mitch Watrous <watrous@u.washington.edu>
parents: 6939
diff changeset
   112
    monitor.SetAttribute("DelayBinWidth", ns.core.DoubleValue(0.001))
2939fecb7029 Make Python examples use new modular bindings without importing everything
Mitch Watrous <watrous@u.washington.edu>
parents: 6939
diff changeset
   113
    monitor.SetAttribute("JitterBinWidth", ns.core.DoubleValue(0.001))
2939fecb7029 Make Python examples use new modular bindings without importing everything
Mitch Watrous <watrous@u.washington.edu>
parents: 6939
diff changeset
   114
    monitor.SetAttribute("PacketSizeBinWidth", ns.core.DoubleValue(20))
5223
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   115
7106
2939fecb7029 Make Python examples use new modular bindings without importing everything
Mitch Watrous <watrous@u.washington.edu>
parents: 6939
diff changeset
   116
    ns.core.Simulator.Stop(ns.core.Seconds(44.0))
2939fecb7029 Make Python examples use new modular bindings without importing everything
Mitch Watrous <watrous@u.washington.edu>
parents: 6939
diff changeset
   117
    ns.core.Simulator.Run()
5223
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   118
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   119
    def print_stats(os, st):
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   120
        print >> os, "  Tx Bytes: ", st.txBytes
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   121
        print >> os, "  Rx Bytes: ", st.rxBytes
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   122
        print >> os, "  Tx Packets: ", st.txPackets
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   123
        print >> os, "  Rx Packets: ", st.rxPackets
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   124
        print >> os, "  Lost Packets: ", st.lostPackets
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   125
        if st.rxPackets > 0:
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   126
            print >> os, "  Mean{Delay}: ", (st.delaySum.GetSeconds() / st.rxPackets)
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   127
	    print >> os, "  Mean{Jitter}: ", (st.jitterSum.GetSeconds() / (st.rxPackets-1))
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   128
            print >> os, "  Mean{Hop Count}: ", float(st.timesForwarded) / st.rxPackets + 1
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   129
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   130
        if 0:
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   131
            print >> os, "Delay Histogram"
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   132
            for i in range(st.delayHistogram.GetNBins () ):
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   133
              print >> os, " ",i,"(", st.delayHistogram.GetBinStart (i), "-", \
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   134
                  st.delayHistogram.GetBinEnd (i), "): ", st.delayHistogram.GetBinCount (i)
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   135
            print >> os, "Jitter Histogram"
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   136
            for i in range(st.jitterHistogram.GetNBins () ):
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   137
              print >> os, " ",i,"(", st.jitterHistogram.GetBinStart (i), "-", \
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   138
                  st.jitterHistogram.GetBinEnd (i), "): ", st.jitterHistogram.GetBinCount (i)
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   139
            print >> os, "PacketSize Histogram"
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   140
            for i in range(st.packetSizeHistogram.GetNBins () ):
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   141
              print >> os, " ",i,"(", st.packetSizeHistogram.GetBinStart (i), "-", \
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   142
                  st.packetSizeHistogram.GetBinEnd (i), "): ", st.packetSizeHistogram.GetBinCount (i)
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   143
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   144
        for reason, drops in enumerate(st.packetsDropped):
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   145
            print "  Packets dropped by reason %i: %i" % (reason, drops)
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   146
        #for reason, drops in enumerate(st.bytesDropped):
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   147
        #    print "Bytes dropped by reason %i: %i" % (reason, drops)
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   148
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   149
    monitor.CheckForLostPackets()
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   150
    classifier = flowmon_helper.GetClassifier()
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   151
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   152
    if cmd.Results is None:
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   153
        for flow_id, flow_stats in monitor.GetFlowStats():
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   154
            t = classifier.FindFlow(flow_id)
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   155
            proto = {6: 'TCP', 17: 'UDP'} [t.protocol]
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   156
            print "FlowID: %i (%s %s/%s --> %s/%i)" % \
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   157
                (flow_id, proto, t.sourceAddress, t.sourcePort, t.destinationAddress, t.destinationPort)
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   158
            print_stats(sys.stdout, flow_stats)
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   159
    else:
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   160
        print monitor.SerializeToXmlFile(cmd.Results, True, True)
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   161
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   162
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   163
    if cmd.Plot is not None:
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   164
        import pylab
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   165
        delays = []
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   166
        for flow_id, flow_stats in monitor.GetFlowStats():
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   167
            tupl = classifier.FindFlow(flow_id)
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   168
            if tupl.protocol == 17 and tupl.sourcePort == 698:
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   169
                continue
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   170
            delays.append(flow_stats.delaySum.GetSeconds() / flow_stats.rxPackets)
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   171
        pylab.hist(delays, 20)
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   172
        pylab.xlabel("Delay (s)")
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   173
        pylab.ylabel("Number of Flows")
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   174
        pylab.show()
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   175
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   176
    return 0
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   177
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   178
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   179
if __name__ == '__main__':
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   180
    sys.exit(main(sys.argv))
d6bbde9c6712 Add an example with Wifi, OLSR, and Flow Monitor.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   181