A Discrete-Event Network Simulator
QKDNetSim v2.0 (NS-3 v3.41) @ (+)
API
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
nsclick-simple-lan.py
Go to the documentation of this file.
1 #
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 
19 import os.path
20 
21 try:
22  from ns import ns
23 except 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 
30 ns.LogComponentEnable("Ipv4ClickRouting", ns.LOG_LEVEL_ALL)
31 ns.LogComponentEnable("Ipv4L3ClickProtocol", ns.LOG_LEVEL_ALL)
32 
33 clickConfigFolder = os.path.dirname(__file__)
34 
35 csmaNodes = ns.NodeContainer()
36 csmaNodes.Create(2)
37 
38 # Setup CSMA channel between the nodes
39 csma = ns.CsmaHelper()
40 csma.SetChannelAttribute("DataRate", ns.DataRateValue(ns.DataRate(5000000)))
41 csma.SetChannelAttribute("Delay", ns.TimeValue(ns.MilliSeconds(2)))
42 csmaDevices = csma.Install(csmaNodes)
43 
44 # Install normal internet stack on node B
45 internet = ns.InternetStackHelper()
46 internet.Install(csmaNodes.Get(1))
47 
48 # Install Click on node A
49 clickinternet = ns.ClickInternetStackHelper()
50 clickinternet.SetClickFile(
51  csmaNodes.Get(0), clickConfigFolder + "/nsclick-lan-single-interface.click"
52 )
53 clickinternet.SetRoutingTableElement(csmaNodes.Get(0), "rt")
54 clickinternet.Install(csmaNodes.Get(0))
55 
56 # Configure IP addresses for the nodes
57 ipv4 = ns.Ipv4AddressHelper()
58 ipv4.SetBase("172.16.1.0", "255.255.255.0")
59 ipv4.Assign(csmaDevices)
60 
61 # Configure traffic application and sockets
62 LocalAddress = ns.InetSocketAddress(ns.Ipv4Address.GetAny(), 50000).ConvertTo()
63 packetSinkHelper = ns.PacketSinkHelper("ns3::TcpSocketFactory", LocalAddress)
64 recvapp = packetSinkHelper.Install(csmaNodes.Get(1))
65 recvapp.Start(ns.Seconds(5.0))
66 recvapp.Stop(ns.Seconds(10.0))
67 
68 onOffHelper = ns.OnOffHelper("ns3::TcpSocketFactory", ns.Address())
69 onOffHelper.SetAttribute("OnTime", ns.StringValue("ns3::ConstantRandomVariable[Constant=1]"))
70 onOffHelper.SetAttribute("OffTime", ns.StringValue("ns3::ConstantRandomVariable[Constant=0]"))
71 
72 appcont = ns.ApplicationContainer()
73 
74 remoteAddress = ns.InetSocketAddress(ns.Ipv4Address("172.16.1.2"), 50000).ConvertTo()
75 onOffHelper.SetAttribute("Remote", ns.AddressValue(remoteAddress))
76 appcont.Add(onOffHelper.Install(csmaNodes.Get(0)))
77 
78 appcont.Start(ns.Seconds(5.0))
79 appcont.Stop(ns.Seconds(10.0))
80 
81 # For tracing
82 csma.EnablePcap("nsclick-simple-lan", csmaDevices, False)
83 
84 ns.Simulator.Stop(ns.Seconds(20.0))
85 ns.Simulator.Run()
86 
87 ns.Simulator.Destroy()