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.core.LogComponentEnable(
"UdpEchoClientApplication", ns.core.LOG_LEVEL_INFO)
47 ns.core.LogComponentEnable(
"UdpEchoServerApplication", ns.core.LOG_LEVEL_INFO)
48nCsma.value = 1
if nCsma.value == 0
else nCsma.value
50p2pNodes = ns.network.NodeContainer()
53csmaNodes = ns.network.NodeContainer()
54csmaNodes.Add(p2pNodes.Get(1))
55csmaNodes.Create(nCsma.value)
57pointToPoint = ns.point_to_point.PointToPointHelper()
58pointToPoint.SetDeviceAttribute(
"DataRate", ns.core.StringValue(
"5Mbps"))
59pointToPoint.SetChannelAttribute(
"Delay", ns.core.StringValue(
"2ms"))
61p2pDevices = pointToPoint.Install(p2pNodes)
63csma = ns.csma.CsmaHelper()
64csma.SetChannelAttribute(
"DataRate", ns.core.StringValue(
"100Mbps"))
65csma.SetChannelAttribute(
"Delay", ns.core.TimeValue(ns.core.NanoSeconds(6560)))
67csmaDevices = csma.Install(csmaNodes)
69stack = ns.internet.InternetStackHelper()
70stack.Install(p2pNodes.Get(0))
71stack.Install(csmaNodes)
73address = ns.internet.Ipv4AddressHelper()
74address.SetBase(ns.network.Ipv4Address(
"10.1.1.0"), ns.network.Ipv4Mask(
"255.255.255.0"))
75p2pInterfaces = address.Assign(p2pDevices)
77address.SetBase(ns.network.Ipv4Address(
"10.1.2.0"), ns.network.Ipv4Mask(
"255.255.255.0"))
78csmaInterfaces = address.Assign(csmaDevices)
80echoServer = ns.applications.UdpEchoServerHelper(9)
82serverApps = echoServer.Install(csmaNodes.Get(nCsma.value))
83serverApps.Start(ns.core.Seconds(1.0))
84serverApps.Stop(ns.core.Seconds(10.0))
86echoClient = ns.applications.UdpEchoClientHelper(
87 csmaInterfaces.GetAddress(nCsma.value).ConvertTo(), 9
89echoClient.SetAttribute(
"MaxPackets", ns.core.UintegerValue(1))
90echoClient.SetAttribute(
"Interval", ns.core.TimeValue(ns.core.Seconds(1.0)))
91echoClient.SetAttribute(
"PacketSize", ns.core.UintegerValue(1024))
93clientApps = echoClient.Install(p2pNodes.Get(0))
94clientApps.Start(ns.core.Seconds(2.0))
95clientApps.Stop(ns.core.Seconds(10.0))
97ns.internet.Ipv4GlobalRoutingHelper.PopulateRoutingTables()
99pointToPoint.EnablePcapAll(
"second")
100csma.EnablePcap(
"second", csmaDevices.Get(1),
True)
102ns.core.Simulator.Run()
103ns.core.Simulator.Destroy()