28except ModuleNotFoundError:
30 "Error: ns3 Python module not found;"
31 " Python bindings may not be enabled"
32 " or your PYTHONPATH might not be properly configured"
41 cmd = ns.core.CommandLine()
48 ns.core.GlobalValue.Bind(
49 "SimulatorImplementationType", ns.core.StringValue(
"ns3::RealtimeSimulatorImpl")
55 print(
"Create nodes.")
56 n = ns.network.NodeContainer()
59 internet = ns.internet.InternetStackHelper()
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))
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"))
80 print(
"Create Applications.")
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))
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))
106 ascii = ns.network.AsciiTraceHelper()
107 csma.EnableAsciiAll(ascii.CreateFileStream(
"realtime-udp-echo.tr"))
108 csma.EnablePcapAll(
"realtime-udp-echo",
False)
113 print(
"Run Simulation.")
114 ns.core.Simulator.Stop(ns.Seconds(10))
115 ns.core.Simulator.Run()
116 ns.core.Simulator.Destroy()
120if __name__ ==
"__main__":