38 except 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.core.CommandLine()
58 terminals = ns.network.NodeContainer()
61 csmaSwitch = ns.network.NodeContainer()
65 csma = ns.csma.CsmaHelper()
66 csma.SetChannelAttribute(
"DataRate", ns.network.DataRateValue(ns.network.DataRate(5000000)))
67 csma.SetChannelAttribute(
"Delay", ns.core.TimeValue(ns.core.MilliSeconds(2)))
71 terminalDevices = ns.network.NetDeviceContainer()
72 switchDevices = ns.network.NetDeviceContainer()
76 ns.network.NodeContainer(ns.network.NodeContainer(terminals.Get(i)), csmaSwitch)
78 terminalDevices.Add(link.Get(0))
79 switchDevices.Add(link.Get(1))
82 switchNode = csmaSwitch.Get(0)
83 bridgeDevice = ns.bridge.BridgeNetDevice()
84 switchNode.AddDevice(bridgeDevice)
86 for portIter
in range(switchDevices.GetN()):
87 bridgeDevice.AddBridgePort(switchDevices.Get(portIter))
90 internet = ns.internet.InternetStackHelper()
91 internet.Install(terminals)
96 ipv4 = ns.internet.Ipv4AddressHelper()
97 ipv4.SetBase(ns.network.Ipv4Address(
"10.1.1.0"), ns.network.Ipv4Mask(
"255.255.255.0"))
98 ipv4.Assign(terminalDevices)
106 inet_sock_address = ns.network.InetSocketAddress(ns.network.Ipv4Address(
"10.1.1.2"), port)
107 onoff = ns.applications.OnOffHelper(
"ns3::UdpSocketFactory", inet_sock_address.ConvertTo())
108 onoff.SetConstantRate(ns.network.DataRate(
"500kb/s"))
110 app = onoff.Install(ns.network.NodeContainer(terminals.Get(0)))
112 app.Start(ns.core.Seconds(1.0))
113 app.Stop(ns.core.Seconds(10.0))
116 inet_address = ns.network.InetSocketAddress(ns.network.Ipv4Address.GetAny(), port)
117 sink = ns.applications.PacketSinkHelper(
"ns3::UdpSocketFactory", inet_address.ConvertTo())
118 app = sink.Install(ns.network.NodeContainer(terminals.Get(1)))
119 app.Start(ns.core.Seconds(0.0))
124 inet_address = ns.network.InetSocketAddress(ns.network.Ipv4Address(
"10.1.1.1"), port)
125 onoff.SetAttribute(
"Remote", ns.network.AddressValue(inet_address.ConvertTo()))
126 app = onoff.Install(ns.network.NodeContainer(terminals.Get(3)))
127 app.Start(ns.core.Seconds(1.1))
128 app.Stop(ns.core.Seconds(10.0))
130 app = sink.Install(ns.network.NodeContainer(terminals.Get(0)))
131 app.Start(ns.core.Seconds(0.0))
148 csma.EnablePcapAll(
"csma-bridge",
False)
154 ns.core.Simulator.Run()
155 ns.core.Simulator.Destroy()
159 if __name__ ==
"__main__":