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