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 from ns import ns
20 import sys
21 
22 # // Default Network Topology
23 # //
24 # // 10.1.1.0
25 # // n0 -------------- n1 n2 n3 n4
26 # // point-to-point | | | |
27 # // ================
28 # // LAN 10.1.2.0
29 
30 from ctypes import c_int, c_bool
31 nCsma = c_int(3)
32 verbose = c_bool(True)
33 cmd = ns.CommandLine(__file__)
34 cmd.AddValue("nCsma", "Number of extra CSMA nodes/devices", nCsma)
35 cmd.AddValue("verbose", "Tell echo applications to log if true", verbose)
36 cmd.Parse(sys.argv)
37 
38 if verbose.value:
39  ns.core.LogComponentEnable("UdpEchoClientApplication", ns.core.LOG_LEVEL_INFO)
40  ns.core.LogComponentEnable("UdpEchoServerApplication", ns.core.LOG_LEVEL_INFO)
41 nCsma.value = 1 if nCsma.value == 0 else nCsma.value
42 
43 p2pNodes = ns.network.NodeContainer()
44 p2pNodes.Create(2)
45 
46 csmaNodes = ns.network.NodeContainer()
47 csmaNodes.Add(p2pNodes.Get(1))
48 csmaNodes.Create(nCsma.value)
49 
50 pointToPoint = ns.point_to_point.PointToPointHelper()
51 pointToPoint.SetDeviceAttribute("DataRate", ns.core.StringValue("5Mbps"))
52 pointToPoint.SetChannelAttribute("Delay", ns.core.StringValue("2ms"))
53 
54 p2pDevices = pointToPoint.Install(p2pNodes)
55 
56 csma = ns.csma.CsmaHelper()
57 csma.SetChannelAttribute("DataRate", ns.core.StringValue("100Mbps"))
58 csma.SetChannelAttribute("Delay", ns.core.TimeValue(ns.core.NanoSeconds(6560)))
59 
60 csmaDevices = csma.Install(csmaNodes)
61 
62 stack = ns.internet.InternetStackHelper()
63 stack.Install(p2pNodes.Get(0))
64 stack.Install(csmaNodes)
65 
66 address = ns.internet.Ipv4AddressHelper()
67 address.SetBase(ns.network.Ipv4Address("10.1.1.0"), ns.network.Ipv4Mask("255.255.255.0"))
68 p2pInterfaces = address.Assign(p2pDevices)
69 
70 address.SetBase(ns.network.Ipv4Address("10.1.2.0"), ns.network.Ipv4Mask("255.255.255.0"))
71 csmaInterfaces = address.Assign(csmaDevices)
72 
73 echoServer = ns.applications.UdpEchoServerHelper(9)
74 
75 serverApps = echoServer.Install(csmaNodes.Get(nCsma.value))
76 serverApps.Start(ns.core.Seconds(1.0))
77 serverApps.Stop(ns.core.Seconds(10.0))
78 
79 echoClient = ns.applications.UdpEchoClientHelper(csmaInterfaces.GetAddress(nCsma.value).ConvertTo(), 9)
80 echoClient.SetAttribute("MaxPackets", ns.core.UintegerValue(1))
81 echoClient.SetAttribute("Interval", ns.core.TimeValue(ns.core.Seconds (1.0)))
82 echoClient.SetAttribute("PacketSize", ns.core.UintegerValue(1024))
83 
84 clientApps = echoClient.Install(p2pNodes.Get(0))
85 clientApps.Start(ns.core.Seconds(2.0))
86 clientApps.Stop(ns.core.Seconds(10.0))
87 
88 ns.internet.Ipv4GlobalRoutingHelper.PopulateRoutingTables()
89 
90 pointToPoint.EnablePcapAll("second")
91 csma.EnablePcap ("second", csmaDevices.Get (1), True)
92 
93 ns.core.Simulator.Run()
94 ns.core.Simulator.Destroy()
95