23 import ns.point_to_point
24 import ns.applications
35 cmd = ns.core.CommandLine()
38 cmd.AddValue(
"nCsma",
"Number of \"extra\" CSMA nodes/devices")
39 cmd.AddValue(
"verbose",
"Tell echo applications to log if true")
42 nCsma = int(cmd.nCsma)
46 ns.core.LogComponentEnable(
"UdpEchoClientApplication", ns.core.LOG_LEVEL_INFO)
47 ns.core.LogComponentEnable(
"UdpEchoServerApplication", ns.core.LOG_LEVEL_INFO)
48 nCsma = 1
if int(nCsma) == 0
else int(nCsma)
50 p2pNodes = ns.network.NodeContainer()
53 csmaNodes = ns.network.NodeContainer()
54 csmaNodes.Add(p2pNodes.Get(1))
55 csmaNodes.Create(nCsma)
57 pointToPoint = ns.point_to_point.PointToPointHelper()
58 pointToPoint.SetDeviceAttribute(
"DataRate", ns.core.StringValue(
"5Mbps"))
59 pointToPoint.SetChannelAttribute(
"Delay", ns.core.StringValue(
"2ms"))
61 p2pDevices = pointToPoint.Install(p2pNodes)
63 csma = ns.csma.CsmaHelper()
64 csma.SetChannelAttribute(
"DataRate", ns.core.StringValue(
"100Mbps"))
65 csma.SetChannelAttribute(
"Delay", ns.core.TimeValue(ns.core.NanoSeconds(6560)))
67 csmaDevices = csma.Install(csmaNodes)
69 stack = ns.internet.InternetStackHelper()
70 stack.Install(p2pNodes.Get(0))
71 stack.Install(csmaNodes)
73 address = ns.internet.Ipv4AddressHelper()
74 address.SetBase(ns.network.Ipv4Address(
"10.1.1.0"), ns.network.Ipv4Mask(
"255.255.255.0"))
75 p2pInterfaces = address.Assign(p2pDevices)
77 address.SetBase(ns.network.Ipv4Address(
"10.1.2.0"), ns.network.Ipv4Mask(
"255.255.255.0"))
78 csmaInterfaces = address.Assign(csmaDevices)
80 echoServer = ns.applications.UdpEchoServerHelper(9)
82 serverApps = echoServer.Install(csmaNodes.Get(nCsma))
83 serverApps.Start(ns.core.Seconds(1.0))
84 serverApps.Stop(ns.core.Seconds(10.0))
86 echoClient = ns.applications.UdpEchoClientHelper(csmaInterfaces.GetAddress(nCsma), 9)
87 echoClient.SetAttribute(
"MaxPackets", ns.core.UintegerValue(1))
88 echoClient.SetAttribute(
"Interval", ns.core.TimeValue(ns.core.Seconds (1.0)))
89 echoClient.SetAttribute(
"PacketSize", ns.core.UintegerValue(1024))
91 clientApps = echoClient.Install(p2pNodes.Get(0))
92 clientApps.Start(ns.core.Seconds(2.0))
93 clientApps.Stop(ns.core.Seconds(10.0))
95 ns.internet.Ipv4GlobalRoutingHelper.PopulateRoutingTables()
97 pointToPoint.EnablePcapAll(
"second")
98 csma.EnablePcap (
"second", csmaDevices.Get (1),
True)
100 ns.core.Simulator.Run()
101 ns.core.Simulator.Destroy()