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