9except ModuleNotFoundError:
11 "Error: ns3 Python module not found;"
12 " Python bindings may not be enabled"
13 " or your PYTHONPATH might not be properly configured"
16from ctypes
import c_bool, c_int
29cmd = ns.CommandLine(__file__)
30cmd.AddValue(
"nCsma",
"Number of extra CSMA nodes/devices", nCsma)
31cmd.AddValue(
"verbose",
"Tell echo applications to log if true", verbose)
35 ns.LogComponentEnable(
"UdpEchoClientApplication", ns.LOG_LEVEL_INFO)
36 ns.LogComponentEnable(
"UdpEchoServerApplication", ns.LOG_LEVEL_INFO)
37nCsma.value = 1
if nCsma.value == 0
else nCsma.value
39p2pNodes = ns.NodeContainer()
42csmaNodes = ns.NodeContainer()
43csmaNodes.Add(p2pNodes.Get(1))
44csmaNodes.Create(nCsma.value)
46pointToPoint = ns.PointToPointHelper()
47pointToPoint.SetDeviceAttribute(
"DataRate", ns.StringValue(
"5Mbps"))
48pointToPoint.SetChannelAttribute(
"Delay", ns.StringValue(
"2ms"))
50p2pDevices = pointToPoint.Install(p2pNodes)
53csma.SetChannelAttribute(
"DataRate", ns.StringValue(
"100Mbps"))
54csma.SetChannelAttribute(
"Delay", ns.TimeValue(ns.NanoSeconds(6560)))
56csmaDevices = csma.Install(csmaNodes)
58stack = ns.InternetStackHelper()
59stack.Install(p2pNodes.Get(0))
60stack.Install(csmaNodes)
62address = ns.Ipv4AddressHelper()
63address.SetBase(ns.Ipv4Address(
"10.1.1.0"), ns.Ipv4Mask(
"255.255.255.0"))
64p2pInterfaces = address.Assign(p2pDevices)
66address.SetBase(ns.Ipv4Address(
"10.1.2.0"), ns.Ipv4Mask(
"255.255.255.0"))
67csmaInterfaces = address.Assign(csmaDevices)
69echoServer = ns.UdpEchoServerHelper(9)
71serverApps = echoServer.Install(csmaNodes.Get(nCsma.value))
72serverApps.Start(ns.Seconds(1))
73serverApps.Stop(ns.Seconds(10))
75echoClient = ns.UdpEchoClientHelper(csmaInterfaces.GetAddress(nCsma.value).ConvertTo(), 9)
76echoClient.SetAttribute(
"MaxPackets", ns.UintegerValue(1))
77echoClient.SetAttribute(
"Interval", ns.TimeValue(ns.Seconds(1)))
78echoClient.SetAttribute(
"PacketSize", ns.UintegerValue(1024))
80clientApps = echoClient.Install(p2pNodes.Get(0))
81clientApps.Start(ns.Seconds(2))
82clientApps.Stop(ns.Seconds(10))
84ns.Ipv4GlobalRoutingHelper.PopulateRoutingTables()
86pointToPoint.EnablePcapAll(
"second")
87csma.EnablePcap(
"second", csmaDevices.Get(1),
True)