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