examples/tutorial/second.py
author Tom Henderson <tomh@tomh.org>
Mon, 28 Sep 2015 20:27:25 -0700
changeset 11676 05ea1489e509
parent 11462 a2f15a2a21f2
permissions -rw-r--r--
bug 2184: Integer overflow in MacLow
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
11462
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
     1
# -*-  Mode: Python; -*-
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
     2
# /*
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
     3
#  * This program is free software; you can redistribute it and/or modify
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
     4
#  * it under the terms of the GNU General Public License version 2 as
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
     5
#  * published by the Free Software Foundation;
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
     6
#  *
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
     7
#  * This program is distributed in the hope that it will be useful,
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
     8
#  * but WITHOUT ANY WARRANTY; without even the implied warranty of
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
     9
#  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    10
#  * GNU General Public License for more details.
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    11
#  *
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    12
#  * You should have received a copy of the GNU General Public License
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    13
#  * along with this program; if not, write to the Free Software
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    14
#  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    15
#  *
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    16
#  * Ported to Python by Mohit P. Tahiliani
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    17
#  */
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    18
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    19
import ns.core
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    20
import ns.network
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    21
import ns.csma
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    22
import ns.internet
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    23
import ns.point_to_point
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    24
import ns.applications
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    25
import sys
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    26
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    27
# // Default Network Topology
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    28
# //
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    29
# //       10.1.1.0
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    30
# // n0 -------------- n1   n2   n3   n4
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    31
# //    point-to-point  |    |    |    |
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    32
# //                    ================
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    33
# //                      LAN 10.1.2.0
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    34
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    35
cmd = ns.core.CommandLine()
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    36
cmd.nCsma = 3
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    37
cmd.verbose = "True"
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    38
cmd.AddValue("nCsma", "Number of \"extra\" CSMA nodes/devices")
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    39
cmd.AddValue("verbose", "Tell echo applications to log if true")
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    40
cmd.Parse(sys.argv)
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    41
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    42
nCsma = int(cmd.nCsma)
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    43
verbose = cmd.verbose
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    44
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    45
if verbose == "True":
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    46
	ns.core.LogComponentEnable("UdpEchoClientApplication", ns.core.LOG_LEVEL_INFO)
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    47
	ns.core.LogComponentEnable("UdpEchoServerApplication", ns.core.LOG_LEVEL_INFO)
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    48
nCsma = 1 if int(nCsma) == 0 else int(nCsma)
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    49
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    50
p2pNodes = ns.network.NodeContainer()
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    51
p2pNodes.Create(2)
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    52
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    53
csmaNodes = ns.network.NodeContainer()
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    54
csmaNodes.Add(p2pNodes.Get(1))
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    55
csmaNodes.Create(nCsma)
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    56
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    57
pointToPoint = ns.point_to_point.PointToPointHelper()
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    58
pointToPoint.SetDeviceAttribute("DataRate", ns.core.StringValue("5Mbps"))
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    59
pointToPoint.SetChannelAttribute("Delay", ns.core.StringValue("2ms"))
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    60
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    61
p2pDevices = pointToPoint.Install(p2pNodes)
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    62
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    63
csma = ns.csma.CsmaHelper()
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    64
csma.SetChannelAttribute("DataRate", ns.core.StringValue("100Mbps"))
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    65
csma.SetChannelAttribute("Delay", ns.core.TimeValue(ns.core.NanoSeconds(6560)))
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    66
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    67
csmaDevices = csma.Install(csmaNodes)
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    68
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    69
stack = ns.internet.InternetStackHelper()
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    70
stack.Install(p2pNodes.Get(0))
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    71
stack.Install(csmaNodes)
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    72
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    73
address = ns.internet.Ipv4AddressHelper()
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    74
address.SetBase(ns.network.Ipv4Address("10.1.1.0"), ns.network.Ipv4Mask("255.255.255.0"))
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    75
p2pInterfaces = address.Assign(p2pDevices)
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    76
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    77
address.SetBase(ns.network.Ipv4Address("10.1.2.0"), ns.network.Ipv4Mask("255.255.255.0"))
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    78
csmaInterfaces = address.Assign(csmaDevices)
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    79
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    80
echoServer = ns.applications.UdpEchoServerHelper(9)
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    81
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    82
serverApps = echoServer.Install(csmaNodes.Get(nCsma))
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    83
serverApps.Start(ns.core.Seconds(1.0))
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    84
serverApps.Stop(ns.core.Seconds(10.0))
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    85
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    86
echoClient = ns.applications.UdpEchoClientHelper(csmaInterfaces.GetAddress(nCsma), 9)
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    87
echoClient.SetAttribute("MaxPackets", ns.core.UintegerValue(1))
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    88
echoClient.SetAttribute("Interval", ns.core.TimeValue(ns.core.Seconds (1.0)))
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    89
echoClient.SetAttribute("PacketSize", ns.core.UintegerValue(1024))
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    90
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    91
clientApps = echoClient.Install(p2pNodes.Get(0))
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    92
clientApps.Start(ns.core.Seconds(2.0))
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    93
clientApps.Stop(ns.core.Seconds(10.0))
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    94
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    95
ns.internet.Ipv4GlobalRoutingHelper.PopulateRoutingTables()
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    96
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    97
pointToPoint.EnablePcapAll("second")
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    98
csma.EnablePcap ("second", csmaDevices.Get (1), True)
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
    99
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
   100
ns.core.Simulator.Run()
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
   101
ns.core.Simulator.Destroy()
a2f15a2a21f2 add second.py and third.py Python examples
Mohit P. Tahiliani <tahiliani.nitk@gmail.com>
parents:
diff changeset
   102