A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
nsclick-simple-lan.py
Go to the documentation of this file.
2# This program is free software; you can redistribute it and/or modify
3# it under the terms of the GNU General Public License version 2 as
4# published by the Free Software Foundation;
5#
6# This program is distributed in the hope that it will be useful,
7# but WITHOUT ANY WARRANTY; without even the implied warranty of
8# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9# GNU General Public License for more details.
10#
11# You should have received a copy of the GNU General Public License
12# along with this program; if not, write to the Free Software
13# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14#
15# Authors: Lalith Suresh <suresh.lalith@gmail.com>
16# Modified by: Gabriel Ferreira <gabrielcarvfer@gmail.com>
17#
18
19import os.path
20
21try:
22 from ns import ns
23except ModuleNotFoundError:
24 raise SystemExit(
25 "Error: ns3 Python module not found;"
26 " Python bindings may not be enabled"
27 " or your PYTHONPATH might not be properly configured"
28 )
29
30ns.LogComponentEnable("Ipv4ClickRouting", ns.LOG_LEVEL_ALL)
31ns.LogComponentEnable("Ipv4L3ClickProtocol", ns.LOG_LEVEL_ALL)
32
33clickConfigFolder = os.path.dirname(__file__)
34
35csmaNodes = ns.NodeContainer()
36csmaNodes.Create(2)
37
38# Setup CSMA channel between the nodes
39csma = ns.CsmaHelper()
40csma.SetChannelAttribute("DataRate", ns.DataRateValue(ns.DataRate(5000000)))
41csma.SetChannelAttribute("Delay", ns.TimeValue(ns.MilliSeconds(2)))
42csmaDevices = csma.Install(csmaNodes)
43
44# Install normal internet stack on node B
45internet = ns.InternetStackHelper()
46internet.Install(csmaNodes.Get(1))
47
48# Install Click on node A
49clickinternet = ns.ClickInternetStackHelper()
50clickinternet.SetClickFile(
51 csmaNodes.Get(0), clickConfigFolder + "/nsclick-lan-single-interface.click"
52)
53clickinternet.SetRoutingTableElement(csmaNodes.Get(0), "rt")
54clickinternet.Install(csmaNodes.Get(0))
55
56# Configure IP addresses for the nodes
57ipv4 = ns.Ipv4AddressHelper()
58ipv4.SetBase("172.16.1.0", "255.255.255.0")
59ipv4.Assign(csmaDevices)
60
61# Configure traffic application and sockets
62LocalAddress = ns.InetSocketAddress(ns.Ipv4Address.GetAny(), 50000).ConvertTo()
63packetSinkHelper = ns.PacketSinkHelper("ns3::TcpSocketFactory", LocalAddress)
64recvapp = packetSinkHelper.Install(csmaNodes.Get(1))
65recvapp.Start(ns.Seconds(5.0))
66recvapp.Stop(ns.Seconds(10.0))
67
68onOffHelper = ns.OnOffHelper("ns3::TcpSocketFactory", ns.Address())
69onOffHelper.SetAttribute("OnTime", ns.StringValue("ns3::ConstantRandomVariable[Constant=1]"))
70onOffHelper.SetAttribute("OffTime", ns.StringValue("ns3::ConstantRandomVariable[Constant=0]"))
71
72appcont = ns.ApplicationContainer()
73
74remoteAddress = ns.InetSocketAddress(ns.Ipv4Address("172.16.1.2"), 50000).ConvertTo()
75onOffHelper.SetAttribute("Remote", ns.AddressValue(remoteAddress))
76appcont.Add(onOffHelper.Install(csmaNodes.Get(0)))
77
78appcont.Start(ns.Seconds(5.0))
79appcont.Stop(ns.Seconds(10.0))
80
81# For tracing
82csma.EnablePcap("nsclick-simple-lan", csmaDevices, False)
83
84ns.Simulator.Stop(ns.Seconds(20.0))
85ns.Simulator.Run()
86
87ns.Simulator.Destroy()