A Discrete-Event Network Simulator
API
first.py
Go to the documentation of this file.
1# /*
2# * This program is free software; you can redistribute it and/or modify
3# * it under the terms of the GNU General Public License version 2 as
4# * published by the Free Software Foundation;
5# *
6# * This program is distributed in the hope that it will be useful,
7# * but WITHOUT ANY WARRANTY; without even the implied warranty of
8# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9# * GNU General Public License for more details.
10# *
11# * You should have received a copy of the GNU General Public License
12# * along with this program; if not, write to the Free Software
13# * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14# */
15
16import ns.applications
17import ns.core
18import ns.internet
19import ns.network
20import ns.point_to_point
21
22# // Default Network Topology
23# //
24# // 10.1.1.0
25# // n0 -------------- n1
26# // point-to-point
27# //
28
29ns.core.LogComponentEnable("UdpEchoClientApplication", ns.core.LOG_LEVEL_INFO)
30ns.core.LogComponentEnable("UdpEchoServerApplication", ns.core.LOG_LEVEL_INFO)
31
32nodes = ns.network.NodeContainer()
33nodes.Create(2)
34
35pointToPoint = ns.point_to_point.PointToPointHelper()
36pointToPoint.SetDeviceAttribute("DataRate", ns.core.StringValue("5Mbps"))
37pointToPoint.SetChannelAttribute("Delay", ns.core.StringValue("2ms"))
38
39devices = pointToPoint.Install(nodes)
40
41stack = ns.internet.InternetStackHelper()
42stack.Install(nodes)
43
44address = ns.internet.Ipv4AddressHelper()
45address.SetBase(ns.network.Ipv4Address("10.1.1.0"),
46 ns.network.Ipv4Mask("255.255.255.0"))
47
48interfaces = address.Assign(devices)
49
50echoServer = ns.applications.UdpEchoServerHelper(9)
51
52serverApps = echoServer.Install(nodes.Get(1))
53serverApps.Start(ns.core.Seconds(1.0))
54serverApps.Stop(ns.core.Seconds(10.0))
55
56echoClient = ns.applications.UdpEchoClientHelper(interfaces.GetAddress(1), 9)
57echoClient.SetAttribute("MaxPackets", ns.core.UintegerValue(1))
58echoClient.SetAttribute("Interval", ns.core.TimeValue(ns.core.Seconds(1.0)))
59echoClient.SetAttribute("PacketSize", ns.core.UintegerValue(1024))
60
61clientApps = echoClient.Install(nodes.Get(0))
62clientApps.Start(ns.core.Seconds(2.0))
63clientApps.Stop(ns.core.Seconds(10.0))
64
65ns.core.Simulator.Run()
66ns.core.Simulator.Destroy()
67