A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
nsclick-simple-lan.py
Go to the documentation of this file.
2# SPDX-License-Identifier: GPL-2.0-only
3#
4# Authors: Lalith Suresh <suresh.lalith@gmail.com>
5# Modified by: Gabriel Ferreira <gabrielcarvfer@gmail.com>
6#
7
8import os.path
9
10try:
11 from ns import ns
12except ModuleNotFoundError:
13 raise SystemExit(
14 "Error: ns3 Python module not found;"
15 " Python bindings may not be enabled"
16 " or your PYTHONPATH might not be properly configured"
17 )
18
19ns.LogComponentEnable("Ipv4ClickRouting", ns.LOG_LEVEL_ALL)
20ns.LogComponentEnable("Ipv4L3ClickProtocol", ns.LOG_LEVEL_ALL)
21
22clickConfigFolder = os.path.dirname(__file__)
23
24csmaNodes = ns.NodeContainer()
25csmaNodes.Create(2)
26
27# Setup CSMA channel between the nodes
28csma = ns.CsmaHelper()
29csma.SetChannelAttribute("DataRate", ns.DataRateValue(ns.DataRate(5000000)))
30csma.SetChannelAttribute("Delay", ns.TimeValue(ns.MilliSeconds(2)))
31csmaDevices = csma.Install(csmaNodes)
32
33# Install normal internet stack on node B
34internet = ns.InternetStackHelper()
35internet.Install(csmaNodes.Get(1))
36
37# Install Click on node A
38clickinternet = ns.ClickInternetStackHelper()
39clickinternet.SetClickFile(
40 csmaNodes.Get(0), clickConfigFolder + "/nsclick-lan-single-interface.click"
41)
42clickinternet.SetRoutingTableElement(csmaNodes.Get(0), "rt")
43clickinternet.Install(csmaNodes.Get(0))
44
45# Configure IP addresses for the nodes
46ipv4 = ns.Ipv4AddressHelper()
47ipv4.SetBase("172.16.1.0", "255.255.255.0")
48ipv4.Assign(csmaDevices)
49
50# Configure traffic application and sockets
51LocalAddress = ns.InetSocketAddress(ns.Ipv4Address.GetAny(), 50000).ConvertTo()
52packetSinkHelper = ns.PacketSinkHelper("ns3::TcpSocketFactory", LocalAddress)
53recvapp = packetSinkHelper.Install(csmaNodes.Get(1))
54recvapp.Start(ns.Seconds(5.0))
55recvapp.Stop(ns.Seconds(10.0))
56
57onOffHelper = ns.OnOffHelper("ns3::TcpSocketFactory", ns.Address())
58onOffHelper.SetAttribute("OnTime", ns.StringValue("ns3::ConstantRandomVariable[Constant=1]"))
59onOffHelper.SetAttribute("OffTime", ns.StringValue("ns3::ConstantRandomVariable[Constant=0]"))
60
61appcont = ns.ApplicationContainer()
62
63remoteAddress = ns.InetSocketAddress(ns.Ipv4Address("172.16.1.2"), 50000).ConvertTo()
64onOffHelper.SetAttribute("Remote", ns.AddressValue(remoteAddress))
65appcont.Add(onOffHelper.Install(csmaNodes.Get(0)))
66
67appcont.Start(ns.Seconds(5.0))
68appcont.Stop(ns.Seconds(10.0))
69
70# For tracing
71csma.EnablePcap("nsclick-simple-lan", csmaDevices, False)
72
73ns.Simulator.Stop(ns.Seconds(20.0))
74ns.Simulator.Run()
75
76ns.Simulator.Destroy()