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
second.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 # Ported to Python by Mohit P. Tahiliani
16 #
17 
18 try:
19  from ns import ns
20 except ModuleNotFoundError:
21  raise SystemExit(
22  "Error: ns3 Python module not found;"
23  " Python bindings may not be enabled"
24  " or your PYTHONPATH might not be properly configured"
25  )
26 import sys
27 from ctypes import c_bool, c_int
28 
29 # // Default Network Topology
30 # //
31 # // 10.1.1.0
32 # // n0 -------------- n1 n2 n3 n4
33 # // point-to-point | | | |
34 # // ================
35 # // LAN 10.1.2.0
36 
37 
38 nCsma = c_int(3)
39 verbose = c_bool(True)
40 cmd = ns.CommandLine(__file__)
41 cmd.AddValue("nCsma", "Number of extra CSMA nodes/devices", nCsma)
42 cmd.AddValue("verbose", "Tell echo applications to log if true", verbose)
43 cmd.Parse(sys.argv)
44 
45 if verbose.value:
46  ns.core.LogComponentEnable("UdpEchoClientApplication", ns.core.LOG_LEVEL_INFO)
47  ns.core.LogComponentEnable("UdpEchoServerApplication", ns.core.LOG_LEVEL_INFO)
48 nCsma.value = 1 if nCsma.value == 0 else nCsma.value
49 
50 p2pNodes = ns.network.NodeContainer()
51 p2pNodes.Create(2)
52 
53 csmaNodes = ns.network.NodeContainer()
54 csmaNodes.Add(p2pNodes.Get(1))
55 csmaNodes.Create(nCsma.value)
56 
57 pointToPoint = ns.point_to_point.PointToPointHelper()
58 pointToPoint.SetDeviceAttribute("DataRate", ns.core.StringValue("5Mbps"))
59 pointToPoint.SetChannelAttribute("Delay", ns.core.StringValue("2ms"))
60 
61 p2pDevices = pointToPoint.Install(p2pNodes)
62 
63 csma = ns.csma.CsmaHelper()
64 csma.SetChannelAttribute("DataRate", ns.core.StringValue("100Mbps"))
65 csma.SetChannelAttribute("Delay", ns.core.TimeValue(ns.core.NanoSeconds(6560)))
66 
67 csmaDevices = csma.Install(csmaNodes)
68 
69 stack = ns.internet.InternetStackHelper()
70 stack.Install(p2pNodes.Get(0))
71 stack.Install(csmaNodes)
72 
73 address = ns.internet.Ipv4AddressHelper()
74 address.SetBase(ns.network.Ipv4Address("10.1.1.0"), ns.network.Ipv4Mask("255.255.255.0"))
75 p2pInterfaces = address.Assign(p2pDevices)
76 
77 address.SetBase(ns.network.Ipv4Address("10.1.2.0"), ns.network.Ipv4Mask("255.255.255.0"))
78 csmaInterfaces = address.Assign(csmaDevices)
79 
80 echoServer = ns.applications.UdpEchoServerHelper(9)
81 
82 serverApps = echoServer.Install(csmaNodes.Get(nCsma.value))
83 serverApps.Start(ns.core.Seconds(1.0))
84 serverApps.Stop(ns.core.Seconds(10.0))
85 
86 echoClient = ns.applications.UdpEchoClientHelper(
87  csmaInterfaces.GetAddress(nCsma.value).ConvertTo(), 9
88 )
89 echoClient.SetAttribute("MaxPackets", ns.core.UintegerValue(1))
90 echoClient.SetAttribute("Interval", ns.core.TimeValue(ns.core.Seconds(1.0)))
91 echoClient.SetAttribute("PacketSize", ns.core.UintegerValue(1024))
92 
93 clientApps = echoClient.Install(p2pNodes.Get(0))
94 clientApps.Start(ns.core.Seconds(2.0))
95 clientApps.Stop(ns.core.Seconds(10.0))
96 
97 ns.internet.Ipv4GlobalRoutingHelper.PopulateRoutingTables()
98 
99 pointToPoint.EnablePcapAll("second")
100 csma.EnablePcap("second", csmaDevices.Get(1), True)
101 
102 ns.core.Simulator.Run()
103 ns.core.Simulator.Destroy()