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