A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
second.py
Go to the documentation of this file.
2# SPDX-License-Identifier: GPL-2.0-only
3#
4# Ported to Python by Mohit P. Tahiliani
5#
6
7try:
8 from ns import ns
9except ModuleNotFoundError:
10 raise SystemExit(
11 "Error: ns3 Python module not found;"
12 " Python bindings may not be enabled"
13 " or your PYTHONPATH might not be properly configured"
14 )
15import sys
16from ctypes import c_bool, c_int
17
18# // Default Network Topology
19# //
20# // 10.1.1.0
21# // n0 -------------- n1 n2 n3 n4
22# // point-to-point | | | |
23# // ================
24# // LAN 10.1.2.0
25
26
27nCsma = c_int(3)
28verbose = c_bool(True)
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)
32cmd.Parse(sys.argv)
33
34if verbose.value:
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
38
39p2pNodes = ns.NodeContainer()
40p2pNodes.Create(2)
41
42csmaNodes = ns.NodeContainer()
43csmaNodes.Add(p2pNodes.Get(1))
44csmaNodes.Create(nCsma.value)
45
46pointToPoint = ns.PointToPointHelper()
47pointToPoint.SetDeviceAttribute("DataRate", ns.StringValue("5Mbps"))
48pointToPoint.SetChannelAttribute("Delay", ns.StringValue("2ms"))
49
50p2pDevices = pointToPoint.Install(p2pNodes)
51
52csma = ns.CsmaHelper()
53csma.SetChannelAttribute("DataRate", ns.StringValue("100Mbps"))
54csma.SetChannelAttribute("Delay", ns.TimeValue(ns.NanoSeconds(6560)))
55
56csmaDevices = csma.Install(csmaNodes)
57
58stack = ns.InternetStackHelper()
59stack.Install(p2pNodes.Get(0))
60stack.Install(csmaNodes)
61
62address = ns.Ipv4AddressHelper()
63address.SetBase(ns.Ipv4Address("10.1.1.0"), ns.Ipv4Mask("255.255.255.0"))
64p2pInterfaces = address.Assign(p2pDevices)
65
66address.SetBase(ns.Ipv4Address("10.1.2.0"), ns.Ipv4Mask("255.255.255.0"))
67csmaInterfaces = address.Assign(csmaDevices)
68
69echoServer = ns.UdpEchoServerHelper(9)
70
71serverApps = echoServer.Install(csmaNodes.Get(nCsma.value))
72serverApps.Start(ns.Seconds(1))
73serverApps.Stop(ns.Seconds(10))
74
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))
79
80clientApps = echoClient.Install(p2pNodes.Get(0))
81clientApps.Start(ns.Seconds(2))
82clientApps.Stop(ns.Seconds(10))
83
84ns.Ipv4GlobalRoutingHelper.PopulateRoutingTables()
85
86pointToPoint.EnablePcapAll("second")
87csma.EnablePcap("second", csmaDevices.Get(1), True)
88
89ns.Simulator.Run()
90ns.Simulator.Destroy()