12except ModuleNotFoundError:
14 "Error: ns3 Python module not found;"
15 " Python bindings may not be enabled"
16 " or your PYTHONPATH might not be properly configured"
19ns.LogComponentEnable(
"OpenFlowInterface", ns.LOG_LEVEL_ALL)
20ns.LogComponentEnable(
"OpenFlowSwitchNetDevice", ns.LOG_LEVEL_ALL)
22terminals = ns.NodeContainer()
25csmaSwitch = ns.NodeContainer()
29csma.SetChannelAttribute(
"DataRate", ns.DataRateValue(5000000))
30csma.SetChannelAttribute(
"Delay", ns.TimeValue(ns.MilliSeconds(2)))
32terminalDevices = ns.NetDeviceContainer()
33switchDevices = ns.NetDeviceContainer()
35 container = ns.NodeContainer()
36 container.Add(terminals.Get(i))
37 container.Add(csmaSwitch)
38 link = csma.Install(container)
39 terminalDevices.Add(link.Get(0))
40 switchDevices.Add(link.Get(1))
42switchNode = csmaSwitch.Get(0)
43swtch = ns.OpenFlowSwitchHelper()
44controller = ns.ofi.DropController()
46swtch.Install(switchNode, switchDevices, controller)
50internet = ns.InternetStackHelper()
51internet.Install(terminals)
53ipv4 = ns.Ipv4AddressHelper()
54ipv4.SetBase(
"10.1.1.0",
"255.255.255.0")
55ipv4.Assign(terminalDevices)
59onoff = ns.OnOffHelper(
60 "ns3::UdpSocketFactory", ns.InetSocketAddress(ns.Ipv4Address(
"10.1.1.2"), port).ConvertTo()
62onoff.SetConstantRate(ns.DataRate(
"500kb/s"))
64app = onoff.Install(terminals.Get(0))
66app.Start(ns.Seconds(1.0))
67app.Stop(ns.Seconds(10.0))
69sink = ns.PacketSinkHelper(
70 "ns3::UdpSocketFactory", ns.InetSocketAddress(ns.Ipv4Address.GetAny(), port).ConvertTo()
72app = sink.Install(terminals.Get(1))
73app.Start(ns.Seconds(0.0))
76 "Remote", ns.AddressValue(ns.InetSocketAddress(ns.Ipv4Address(
"10.1.1.1"), port).ConvertTo())
78app = onoff.Install(terminals.Get(3))
79app.Start(ns.Seconds(1.1))
80app.Stop(ns.Seconds(10.0))
82app = sink.Install(terminals.Get(0))
83app.Start(ns.Seconds(0.0))