A Discrete-Event Network Simulator
API
realtime-udp-echo.py
Go to the documentation of this file.
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# Network topology
16#
17# n0 n1 n2 n3
18# | | | |
19# =================
20# LAN
21#
22# - UDP flows from n0 to n1 and back
23# - DropTail queues
24# - Tracing of queues and packet receptions to file "udp-echo.tr"
25
26import ns.applications
27import ns.core
28import ns.csma
29import ns.internet
30import ns.network
31
32def main(argv):
33 #
34 # Allow the user to override any of the defaults and the above Bind() at
35 # run-time, via command-line arguments
36 #
37 cmd = ns.core.CommandLine()
38 cmd.Parse(argv)
39
40 #
41 # But since this is a realtime script, don't allow the user to mess with
42 # that.
43 #
44 ns.core.GlobalValue.Bind("SimulatorImplementationType", ns.core.StringValue("ns3::RealtimeSimulatorImpl"))
45
46 #
47 # Explicitly create the nodes required by the topology (shown above).
48 #
49 print ("Create nodes.")
50 n = ns.network.NodeContainer()
51 n.Create(4)
52
53 internet = ns.internet.InternetStackHelper()
54 internet.Install(n)
55
56 #
57 # Explicitly create the channels required by the topology (shown above).
58 #
59 print ("Create channels.")
60 csma = ns.csma.CsmaHelper()
61 csma.SetChannelAttribute("DataRate", ns.network.DataRateValue(ns.network.DataRate(5000000)))
62 csma.SetChannelAttribute("Delay", ns.core.TimeValue(ns.core.MilliSeconds(2)));
63 csma.SetDeviceAttribute("Mtu", ns.core.UintegerValue(1400))
64 d = csma.Install(n)
65
66 #
67 # We've got the "hardware" in place. Now we need to add IP addresses.
68 #
69 print ("Assign IP Addresses.")
70 ipv4 = ns.internet.Ipv4AddressHelper()
71 ipv4.SetBase(ns.network.Ipv4Address("10.1.1.0"), ns.network.Ipv4Mask("255.255.255.0"))
72 i = ipv4.Assign(d)
73
74 print ("Create Applications.")
75
76 #
77 # Create a UdpEchoServer application on node one.
78 #
79 port = 9 # well-known echo port number
80 server = ns.applications.UdpEchoServerHelper(port)
81 apps = server.Install(n.Get(1))
82 apps.Start(ns.core.Seconds(1.0))
83 apps.Stop(ns.core.Seconds(10.0))
84
85 #
86 # Create a UdpEchoClient application to send UDP datagrams from node zero to
87 # node one.
88 #
89 packetSize = 1024
90 maxPacketCount = 500
91 interPacketInterval = ns.core.Seconds(0.01)
92 client = ns.applications.UdpEchoClientHelper(i.GetAddress (1), port)
93 client.SetAttribute("MaxPackets", ns.core.UintegerValue(maxPacketCount))
94 client.SetAttribute("Interval", ns.core.TimeValue(interPacketInterval))
95 client.SetAttribute("PacketSize", ns.core.UintegerValue(packetSize))
96 apps = client.Install(n.Get(0))
97 apps.Start(ns.core.Seconds(2.0))
98 apps.Stop(ns.core.Seconds(10.0))
99
100 ascii = ns.network.AsciiTraceHelper()
101 csma.EnableAsciiAll(ascii.CreateFileStream("realtime-udp-echo.tr"))
102 csma.EnablePcapAll("realtime-udp-echo", False)
103
104 #
105 # Now, do the actual simulation.
106 #
107 print ("Run Simulation.")
108 ns.core.Simulator.Run()
109 ns.core.Simulator.Destroy()
110 print ("Done.")
111
112if __name__ == '__main__':
113 import sys
114 main(sys.argv)