17except ModuleNotFoundError:
19 "Error: ns3 Python module not found;"
20 " Python bindings may not be enabled"
21 " or your PYTHONPATH might not be properly configured"
30 cmd = ns.CommandLine()
37 ns.GlobalValue.Bind(
"SimulatorImplementationType", ns.StringValue(
"ns3::RealtimeSimulatorImpl"))
42 print(
"Create nodes.")
43 n = ns.NodeContainer()
46 internet = ns.InternetStackHelper()
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))
62 print(
"Assign IP Addresses.")
63 ipv4 = ns.Ipv4AddressHelper()
64 ipv4.SetBase(ns.Ipv4Address(
"10.1.1.0"), ns.Ipv4Mask(
"255.255.255.0"))
67 print(
"Create Applications.")
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))
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))
93 ascii = ns.AsciiTraceHelper()
94 csma.EnableAsciiAll(ascii.CreateFileStream(
"realtime-udp-echo.tr"))
95 csma.EnablePcapAll(
"realtime-udp-echo",
False)
100 print(
"Run Simulation.")
101 ns.Simulator.Stop(ns.Seconds(10))
103 ns.Simulator.Destroy()
107if __name__ ==
"__main__":