38except ModuleNotFoundError:
40 "Error: ns3 Python module not found;"
41 " Python bindings may not be enabled"
42 " or your PYTHONPATH might not be properly configured"
51 cmd = ns.CommandLine()
58 terminals = ns.NodeContainer()
61 csmaSwitch = ns.NodeContainer()
65 csma = ns.CsmaHelper()
66 csma.SetChannelAttribute(
"DataRate", ns.DataRateValue(ns.DataRate(5000000)))
67 csma.SetChannelAttribute(
"Delay", ns.TimeValue(ns.MilliSeconds(2)))
71 terminalDevices = ns.NetDeviceContainer()
72 switchDevices = ns.NetDeviceContainer()
75 link = csma.Install(ns.NodeContainer(ns.NodeContainer(terminals.Get(i)), csmaSwitch))
76 terminalDevices.Add(link.Get(0))
77 switchDevices.Add(link.Get(1))
80 switchNode = csmaSwitch.Get(0)
81 bridgeDevice = ns.BridgeNetDevice()
82 switchNode.AddDevice(bridgeDevice)
84 for portIter
in range(switchDevices.GetN()):
85 bridgeDevice.AddBridgePort(switchDevices.Get(portIter))
88 internet = ns.InternetStackHelper()
89 internet.Install(terminals)
94 ipv4 = ns.Ipv4AddressHelper()
95 ipv4.SetBase(ns.Ipv4Address(
"10.1.1.0"), ns.Ipv4Mask(
"255.255.255.0"))
96 ipv4.Assign(terminalDevices)
104 inet_sock_address = ns.InetSocketAddress(ns.Ipv4Address(
"10.1.1.2"), port)
105 onoff = ns.OnOffHelper(
"ns3::UdpSocketFactory", inet_sock_address.ConvertTo())
106 onoff.SetConstantRate(ns.DataRate(
"500kb/s"))
108 app = onoff.Install(ns.NodeContainer(terminals.Get(0)))
110 app.Start(ns.Seconds(1.0))
111 app.Stop(ns.Seconds(10.0))
114 inet_address = ns.InetSocketAddress(ns.Ipv4Address.GetAny(), port)
115 sink = ns.PacketSinkHelper(
"ns3::UdpSocketFactory", inet_address.ConvertTo())
116 app = sink.Install(ns.NodeContainer(terminals.Get(1)))
117 app.Start(ns.Seconds(0.0))
122 inet_address = ns.InetSocketAddress(ns.Ipv4Address(
"10.1.1.1"), port)
123 onoff.SetAttribute(
"Remote", ns.AddressValue(inet_address.ConvertTo()))
124 app = onoff.Install(ns.NodeContainer(terminals.Get(3)))
125 app.Start(ns.Seconds(1.1))
126 app.Stop(ns.Seconds(10.0))
128 app = sink.Install(ns.NodeContainer(terminals.Get(0)))
129 app.Start(ns.Seconds(0.0))
146 csma.EnablePcapAll(
"csma-bridge",
False)
153 ns.Simulator.Destroy()
157if __name__ ==
"__main__":