20except ModuleNotFoundError:
22 "Error: ns3 Python module not found;"
23 " Python bindings may not be enabled"
24 " or your PYTHONPATH might not be properly configured"
27from ctypes
import c_bool, c_int
40cmd = ns.CommandLine(__file__)
41cmd.AddValue(
"nCsma",
"Number of extra CSMA nodes/devices", nCsma)
42cmd.AddValue(
"verbose",
"Tell echo applications to log if true", verbose)
46 ns.LogComponentEnable(
"UdpEchoClientApplication", ns.LOG_LEVEL_INFO)
47 ns.LogComponentEnable(
"UdpEchoServerApplication", ns.LOG_LEVEL_INFO)
48nCsma.value = 1
if nCsma.value == 0
else nCsma.value
50p2pNodes = ns.NodeContainer()
53csmaNodes = ns.NodeContainer()
54csmaNodes.Add(p2pNodes.Get(1))
55csmaNodes.Create(nCsma.value)
57pointToPoint = ns.PointToPointHelper()
58pointToPoint.SetDeviceAttribute(
"DataRate", ns.StringValue(
"5Mbps"))
59pointToPoint.SetChannelAttribute(
"Delay", ns.StringValue(
"2ms"))
61p2pDevices = pointToPoint.Install(p2pNodes)
64csma.SetChannelAttribute(
"DataRate", ns.StringValue(
"100Mbps"))
65csma.SetChannelAttribute(
"Delay", ns.TimeValue(ns.NanoSeconds(6560)))
67csmaDevices = csma.Install(csmaNodes)
69stack = ns.InternetStackHelper()
70stack.Install(p2pNodes.Get(0))
71stack.Install(csmaNodes)
73address = ns.Ipv4AddressHelper()
74address.SetBase(ns.Ipv4Address(
"10.1.1.0"), ns.Ipv4Mask(
"255.255.255.0"))
75p2pInterfaces = address.Assign(p2pDevices)
77address.SetBase(ns.Ipv4Address(
"10.1.2.0"), ns.Ipv4Mask(
"255.255.255.0"))
78csmaInterfaces = address.Assign(csmaDevices)
80echoServer = ns.UdpEchoServerHelper(9)
82serverApps = echoServer.Install(csmaNodes.Get(nCsma.value))
83serverApps.Start(ns.Seconds(1.0))
84serverApps.Stop(ns.Seconds(10.0))
86echoClient = ns.UdpEchoClientHelper(csmaInterfaces.GetAddress(nCsma.value).ConvertTo(), 9)
87echoClient.SetAttribute(
"MaxPackets", ns.UintegerValue(1))
88echoClient.SetAttribute(
"Interval", ns.TimeValue(ns.Seconds(1.0)))
89echoClient.SetAttribute(
"PacketSize", ns.UintegerValue(1024))
91clientApps = echoClient.Install(p2pNodes.Get(0))
92clientApps.Start(ns.Seconds(2.0))
93clientApps.Stop(ns.Seconds(10.0))
95ns.Ipv4GlobalRoutingHelper.PopulateRoutingTables()
97pointToPoint.EnablePcapAll(
"second")
98csma.EnablePcap(
"second", csmaDevices.Get(1),
True)
101ns.Simulator.Destroy()