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