23except ModuleNotFoundError:
25 "Error: ns3 Python module not found;"
26 " Python bindings may not be enabled"
27 " or your PYTHONPATH might not be properly configured"
30ns.LogComponentEnable(
"OpenFlowInterface", ns.LOG_LEVEL_ALL)
31ns.LogComponentEnable(
"OpenFlowSwitchNetDevice", ns.LOG_LEVEL_ALL)
33terminals = ns.NodeContainer()
36csmaSwitch = ns.NodeContainer()
40csma.SetChannelAttribute(
"DataRate", ns.DataRateValue(5000000))
41csma.SetChannelAttribute(
"Delay", ns.TimeValue(ns.MilliSeconds(2)))
43terminalDevices = ns.NetDeviceContainer()
44switchDevices = ns.NetDeviceContainer()
46 container = ns.NodeContainer()
47 container.Add(terminals.Get(i))
48 container.Add(csmaSwitch)
49 link = csma.Install(container)
50 terminalDevices.Add(link.Get(0))
51 switchDevices.Add(link.Get(1))
53switchNode = csmaSwitch.Get(0)
54swtch = ns.OpenFlowSwitchHelper()
55controller = ns.ofi.DropController()
57swtch.Install(switchNode, switchDevices, controller)
61internet = ns.InternetStackHelper()
62internet.Install(terminals)
64ipv4 = ns.Ipv4AddressHelper()
65ipv4.SetBase(
"10.1.1.0",
"255.255.255.0")
66ipv4.Assign(terminalDevices)
70onoff = ns.OnOffHelper(
71 "ns3::UdpSocketFactory", ns.InetSocketAddress(ns.Ipv4Address(
"10.1.1.2"), port).ConvertTo()
73onoff.SetConstantRate(ns.DataRate(
"500kb/s"))
75app = onoff.Install(terminals.Get(0))
77app.Start(ns.Seconds(1.0))
78app.Stop(ns.Seconds(10.0))
80sink = ns.PacketSinkHelper(
81 "ns3::UdpSocketFactory", ns.InetSocketAddress(ns.Ipv4Address.GetAny(), port).ConvertTo()
83app = sink.Install(terminals.Get(1))
84app.Start(ns.Seconds(0.0))
87 "Remote", ns.AddressValue(ns.InetSocketAddress(ns.Ipv4Address(
"10.1.1.1"), port).ConvertTo())
89app = onoff.Install(terminals.Get(3))
90app.Start(ns.Seconds(1.1))
91app.Stop(ns.Seconds(10.0))
93app = sink.Install(terminals.Get(0))
94app.Start(ns.Seconds(0.0))