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