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