examples/realtime/realtime-udp-echo.py
author Tom Henderson <tomh@tomh.org>
Fri, 20 Aug 2010 12:17:19 -0700
changeset 6589 9c325569fb01
parent 6542 ae05357f4906
permissions -rw-r--r--
Help waf to guess release versions of nsc and pybindgen
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6542
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
     1
#
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
     2
# * This program is free software; you can redistribute it and/or modify
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
     3
# * it under the terms of the GNU General Public License version 2 as
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
     4
# * published by the Free Software Foundation;
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
     5
# *
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
     6
# * This program is distributed in the hope that it will be useful,
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
     7
# * but WITHOUT ANY WARRANTY; without even the implied warranty of
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
     8
# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
     9
# * GNU General Public License for more details.
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    10
# *
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    11
# * You should have received a copy of the GNU General Public License
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    12
# * along with this program; if not, write to the Free Software
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    13
# * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    14
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    15
# Network topology
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    16
#
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    17
#       n0    n1   n2   n3
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    18
#       |     |    |    |
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    19
#       =================
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    20
#              LAN
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    21
#
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    22
# - UDP flows from n0 to n1 and back
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    23
# - DropTail queues 
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    24
# - Tracing of queues and packet receptions to file "udp-echo.tr"
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    25
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    26
import ns3
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    27
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    28
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    29
def main(argv):
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    30
  #
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    31
  # Allow the user to override any of the defaults and the above Bind() at
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    32
  # run-time, via command-line arguments
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    33
  #
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    34
  cmd = ns3.CommandLine()
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    35
  cmd.Parse(argv)
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    36
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    37
  #
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    38
  # But since this is a realtime script, don't allow the user to mess with
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    39
  # that.
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    40
  #
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    41
  ns3.GlobalValue.Bind("SimulatorImplementationType", ns3.StringValue("ns3::RealtimeSimulatorImpl"))
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    42
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    43
  #
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    44
  # Explicitly create the nodes required by the topology (shown above).
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    45
  #
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    46
  print "Create nodes."
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    47
  n = ns3.NodeContainer()
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    48
  n.Create(4)
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    49
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    50
  internet = ns3.InternetStackHelper()
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    51
  internet.Install(n)
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    52
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    53
  #
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    54
  # Explicitly create the channels required by the topology (shown above).
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    55
  #
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    56
  print ("Create channels.")
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    57
  csma = ns3.CsmaHelper()
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    58
  csma.SetChannelAttribute("DataRate", ns3.DataRateValue(ns3.DataRate(5000000)))
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    59
  csma.SetChannelAttribute("Delay", ns3.TimeValue(ns3.MilliSeconds(2)));
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    60
  csma.SetDeviceAttribute("Mtu", ns3.UintegerValue(1400))
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    61
  d = csma.Install(n)
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    62
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    63
  #
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    64
  # We've got the "hardware" in place.  Now we need to add IP addresses.
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    65
  #
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    66
  print ("Assign IP Addresses.")
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    67
  ipv4 = ns3.Ipv4AddressHelper()
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    68
  ipv4.SetBase(ns3.Ipv4Address("10.1.1.0"), ns3.Ipv4Mask("255.255.255.0"))
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    69
  i = ipv4.Assign(d)
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    70
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    71
  print ("Create Applications.")
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    72
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    73
  #
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    74
  # Create a UdpEchoServer application on node one.
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    75
  #
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    76
  port = 9  # well-known echo port number
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    77
  server = ns3.UdpEchoServerHelper(port)
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    78
  apps = server.Install(n.Get(1))
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    79
  apps.Start(ns3.Seconds(1.0))
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    80
  apps.Stop(ns3.Seconds(10.0))
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    81
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    82
  #
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    83
  # Create a UdpEchoClient application to send UDP datagrams from node zero to
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    84
  # node one.
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    85
  #
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    86
  packetSize = 1024
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    87
  maxPacketCount = 500
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    88
  interPacketInterval = ns3.Seconds(0.01)
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    89
  client = ns3.UdpEchoClientHelper(i.GetAddress (1), port)
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    90
  client.SetAttribute("MaxPackets", ns3.UintegerValue(maxPacketCount))
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    91
  client.SetAttribute("Interval", ns3.TimeValue(interPacketInterval))
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    92
  client.SetAttribute("PacketSize", ns3.UintegerValue(packetSize))
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    93
  apps = client.Install(n.Get(0))
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    94
  apps.Start(ns3.Seconds(2.0))
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    95
  apps.Stop(ns3.Seconds(10.0))
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    96
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    97
  ascii = ns3.AsciiTraceHelper()
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    98
  csma.EnableAsciiAll(ascii.CreateFileStream("realtime-udp-echo.tr"))
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    99
  csma.EnablePcapAll("realtime-udp-echo", False)
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   100
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   101
  #
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   102
  # Now, do the actual simulation.
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   103
  #
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   104
  print ("Run Simulation.")
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   105
  ns3.Simulator.Run()
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   106
  ns3.Simulator.Destroy()
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   107
  print ("Done.")
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   108
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   109
if __name__ == '__main__':
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   110
  import sys
ae05357f4906 realtime-udp-echo.cc ported to python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   111
  main(sys.argv)