36 import ns.applications
50 cmd = ns.core.CommandLine()
57 terminals = ns.network.NodeContainer()
60 csmaSwitch = ns.network.NodeContainer()
64 csma = ns.csma.CsmaHelper()
65 csma.SetChannelAttribute(
"DataRate", ns.network.DataRateValue(ns.network.DataRate(5000000)))
66 csma.SetChannelAttribute(
"Delay", ns.core.TimeValue(ns.core.MilliSeconds(2)))
70 terminalDevices = ns.network.NetDeviceContainer()
71 switchDevices = ns.network.NetDeviceContainer()
74 link = csma.Install(ns.network.NodeContainer(ns.network.NodeContainer(terminals.Get(i)), csmaSwitch))
75 terminalDevices.Add(link.Get(0))
76 switchDevices.Add(link.Get(1))
79 switchNode = csmaSwitch.Get(0)
80 bridgeDevice = ns.bridge.BridgeNetDevice()
81 switchNode.AddDevice(bridgeDevice)
83 for portIter
in range(switchDevices.GetN()):
84 bridgeDevice.AddBridgePort(switchDevices.Get(portIter))
87 internet = ns.internet.InternetStackHelper()
88 internet.Install(terminals)
93 ipv4 = ns.internet.Ipv4AddressHelper()
94 ipv4.SetBase(ns.network.Ipv4Address(
"10.1.1.0"), ns.network.Ipv4Mask(
"255.255.255.0"))
95 ipv4.Assign(terminalDevices)
103 onoff = ns.applications.OnOffHelper(
"ns3::UdpSocketFactory",
104 ns.network.Address(ns.network.InetSocketAddress(ns.network.Ipv4Address(
"10.1.1.2"), port)))
105 onoff.SetConstantRate (ns.network.DataRate (
"500kb/s"))
107 app = onoff.Install(ns.network.NodeContainer(terminals.Get(0)))
109 app.Start(ns.core.Seconds(1.0))
110 app.Stop(ns.core.Seconds(10.0))
113 sink = ns.applications.PacketSinkHelper(
"ns3::UdpSocketFactory",
114 ns.network.Address(ns.network.InetSocketAddress(ns.network.Ipv4Address.GetAny(), port)))
115 app = sink.Install(ns.network.NodeContainer(terminals.Get(1)))
116 app.Start(ns.core.Seconds(0.0))
121 onoff.SetAttribute(
"Remote",
122 ns.network.AddressValue(ns.network.InetSocketAddress(ns.network.Ipv4Address(
"10.1.1.1"), port)))
123 app = onoff.Install(ns.network.NodeContainer(terminals.Get(3)))
124 app.Start(ns.core.Seconds(1.1))
125 app.Stop(ns.core.Seconds(10.0))
127 app = sink.Install(ns.network.NodeContainer(terminals.Get(0)))
128 app.Start(ns.core.Seconds(0.0))
145 csma.EnablePcapAll(
"csma-bridge",
False)
151 ns.core.Simulator.Run()
152 ns.core.Simulator.Destroy()
157 if __name__ ==
'__main__':