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.core.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.core.GlobalValue.Bind(
49 "SimulatorImplementationType", ns.core.StringValue("ns3::RealtimeSimulatorImpl")
50 )
51
52 #
53 # Explicitly create the nodes required by the topology (shown above).
54 #
55 print("Create nodes.")
56 n = ns.network.NodeContainer()
57 n.Create(4)
58
59 internet = ns.internet.InternetStackHelper()
60 internet.Install(n)
61
62 #
63 # Explicitly create the channels required by the topology (shown above).
64 #
65 print("Create channels.")
66 csma = ns.csma.CsmaHelper()
67 csma.SetChannelAttribute("DataRate", ns.network.DataRateValue(ns.network.DataRate(5000000)))
68 csma.SetChannelAttribute("Delay", ns.core.TimeValue(ns.core.MilliSeconds(2)))
69 csma.SetDeviceAttribute("Mtu", ns.core.UintegerValue(1400))
70 d = csma.Install(n)
71
72 #
73 # We've got the "hardware" in place. Now we need to add IP addresses.
74 #
75 print("Assign IP Addresses.")
76 ipv4 = ns.internet.Ipv4AddressHelper()
77 ipv4.SetBase(ns.network.Ipv4Address("10.1.1.0"), ns.network.Ipv4Mask("255.255.255.0"))
78 i = ipv4.Assign(d)
79
80 print("Create Applications.")
81
82 #
83 # Create a UdpEchoServer application on node one.
84 #
85 port = 9 # well-known echo port number
86 server = ns.applications.UdpEchoServerHelper(port)
87 apps = server.Install(n.Get(1))
88 apps.Start(ns.core.Seconds(1.0))
89 apps.Stop(ns.core.Seconds(10.0))
90
91 #
92 # Create a UdpEchoClient application to send UDP datagrams from node zero to
93 # node one.
94 #
95 packetSize = 1024
96 maxPacketCount = 500
97 interPacketInterval = ns.core.Seconds(0.01)
98 client = ns.applications.UdpEchoClientHelper(i.GetAddress(1).ConvertTo(), port)
99 client.SetAttribute("MaxPackets", ns.core.UintegerValue(maxPacketCount))
100 client.SetAttribute("Interval", ns.core.TimeValue(interPacketInterval))
101 client.SetAttribute("PacketSize", ns.core.UintegerValue(packetSize))
102 apps = client.Install(n.Get(0))
103 apps.Start(ns.core.Seconds(2.0))
104 apps.Stop(ns.core.Seconds(10.0))
105
106 ascii = ns.network.AsciiTraceHelper()
107 csma.EnableAsciiAll(ascii.CreateFileStream("realtime-udp-echo.tr"))
108 csma.EnablePcapAll("realtime-udp-echo", False)
109
110 #
111 # Now, do the actual simulation.
112 #
113 print("Run Simulation.")
114 ns.core.Simulator.Stop(ns.Seconds(10))
115 ns.core.Simulator.Run()
116 ns.core.Simulator.Destroy()
117 print("Done.")
118
119
120if __name__ == "__main__":
121 import sys
122
123 main(sys.argv)