27except ModuleNotFoundError:
29 "Error: ns3 Python module not found;"
30 " Python bindings may not be enabled"
31 " or your PYTHONPATH might not be properly configured"
40 cmd = ns.CommandLine()
47 terminals = ns.NodeContainer()
50 csmaSwitch = ns.NodeContainer()
54 csma = ns.CsmaHelper()
55 csma.SetChannelAttribute(
"DataRate", ns.DataRateValue(ns.DataRate(5000000)))
56 csma.SetChannelAttribute(
"Delay", ns.TimeValue(ns.MilliSeconds(2)))
60 terminalDevices = ns.NetDeviceContainer()
61 switchDevices = ns.NetDeviceContainer()
64 link = csma.Install(ns.NodeContainer(ns.NodeContainer(terminals.Get(i)), csmaSwitch))
65 terminalDevices.Add(link.Get(0))
66 switchDevices.Add(link.Get(1))
69 switchNode = csmaSwitch.Get(0)
70 bridgeDevice = ns.BridgeNetDevice()
71 switchNode.AddDevice(bridgeDevice)
73 for portIter
in range(switchDevices.GetN()):
74 bridgeDevice.AddBridgePort(switchDevices.Get(portIter))
77 internet = ns.InternetStackHelper()
78 internet.Install(terminals)
83 ipv4 = ns.Ipv4AddressHelper()
84 ipv4.SetBase(ns.Ipv4Address(
"10.1.1.0"), ns.Ipv4Mask(
"255.255.255.0"))
85 ipv4.Assign(terminalDevices)
93 inet_sock_address = ns.InetSocketAddress(ns.Ipv4Address(
"10.1.1.2"), port)
94 onoff = ns.OnOffHelper(
"ns3::UdpSocketFactory", inet_sock_address.ConvertTo())
95 onoff.SetConstantRate(ns.DataRate(
"500kb/s"))
97 app = onoff.Install(ns.NodeContainer(terminals.Get(0)))
99 app.Start(ns.Seconds(1.0))
100 app.Stop(ns.Seconds(10.0))
103 inet_address = ns.InetSocketAddress(ns.Ipv4Address.GetAny(), port)
104 sink = ns.PacketSinkHelper(
"ns3::UdpSocketFactory", inet_address.ConvertTo())
105 app = sink.Install(ns.NodeContainer(terminals.Get(1)))
106 app.Start(ns.Seconds(0.0))
111 inet_address = ns.InetSocketAddress(ns.Ipv4Address(
"10.1.1.1"), port)
112 onoff.SetAttribute(
"Remote", ns.AddressValue(inet_address.ConvertTo()))
113 app = onoff.Install(ns.NodeContainer(terminals.Get(3)))
114 app.Start(ns.Seconds(1.1))
115 app.Stop(ns.Seconds(10.0))
117 app = sink.Install(ns.NodeContainer(terminals.Get(0)))
118 app.Start(ns.Seconds(0.0))
135 csma.EnablePcapAll(
"csma-bridge",
False)
142 ns.Simulator.Destroy()
146if __name__ ==
"__main__":