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
21from ns import ns
22
23ns.LogComponentEnable("Ipv4ClickRouting", ns.LOG_LEVEL_ALL)
24ns.LogComponentEnable("Ipv4L3ClickProtocol", ns.LOG_LEVEL_ALL)
25
26clickConfigFolder = os.path.dirname(__file__)
27
28csmaNodes = ns.NodeContainer()
29csmaNodes.Create(2)
30
31# Setup CSMA channel between the nodes
32csma = ns.CsmaHelper()
33csma.SetChannelAttribute("DataRate", ns.DataRateValue(ns.DataRate(5000000)))
34csma.SetChannelAttribute("Delay", ns.TimeValue(ns.MilliSeconds(2)))
35csmaDevices = csma.Install(csmaNodes)
36
37# Install normal internet stack on node B
38internet = ns.InternetStackHelper()
39internet.Install(csmaNodes.Get(1))
40
41# Install Click on node A
42clickinternet = ns.ClickInternetStackHelper()
43clickinternet.SetClickFile(csmaNodes.Get(0),
44 clickConfigFolder + "/nsclick-lan-single-interface.click")
45clickinternet.SetRoutingTableElement(csmaNodes.Get(0), "rt")
46clickinternet.Install(csmaNodes.Get(0))
47
48# Configure IP addresses for the nodes
49ipv4 = ns.Ipv4AddressHelper()
50ipv4.SetBase("172.16.1.0", "255.255.255.0")
51ipv4.Assign(csmaDevices)
52
53# Configure traffic application and sockets
54LocalAddress = ns.InetSocketAddress(ns.Ipv4Address.GetAny(), 50000).ConvertTo()
55packetSinkHelper = ns.PacketSinkHelper("ns3::TcpSocketFactory", LocalAddress)
56recvapp = packetSinkHelper.Install(csmaNodes.Get(1))
57recvapp.Start(ns.Seconds(5.0))
58recvapp.Stop(ns.Seconds(10.0))
59
60onOffHelper = ns.OnOffHelper("ns3::TcpSocketFactory", ns.Address())
61onOffHelper.SetAttribute("OnTime", ns.StringValue("ns3::ConstantRandomVariable[Constant=1]"))
62onOffHelper.SetAttribute("OffTime", ns.StringValue("ns3::ConstantRandomVariable[Constant=0]"))
63
64appcont = ns.ApplicationContainer()
65
66remoteAddress = ns.InetSocketAddress(ns.Ipv4Address("172.16.1.2"), 50000).ConvertTo()
67onOffHelper.SetAttribute("Remote", ns.AddressValue(remoteAddress))
68appcont.Add(onOffHelper.Install(csmaNodes.Get(0)))
69
70appcont.Start(ns.Seconds(5.0))
71appcont.Stop(ns.Seconds(10.0))
72
73# For tracing
74csma.EnablePcap("nsclick-simple-lan", csmaDevices, False)
75
76ns.Simulator.Stop(ns.Seconds(20.0))
77ns.Simulator.Run()
78
79ns.Simulator.Destroy()