A Discrete-Event Network Simulator
API
brite-generic-example.py
Go to the documentation of this file.
1 #
2 # Copyright (c) 2012 The Georgia Institute of Technology
3 #
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License version 2 as
6 # published by the Free Software Foundation;
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 #
17 # Author: Brian Swenson <bswenson3@gatech.edu>
18 # Modified by: Gabriel Ferreira <gabrielcarvfer@gmail.com>
19 #
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("BriteTopologyHelper", ns.LOG_LEVEL_ALL)
31 
32 # BRITE needs a configuration file to build its graph. By default, this
33 # example will use the TD_ASBarabasi_RTWaxman.conf file. There are many others
34 # which can be found in the BRITE/conf_files directory
35 confFile = "src/brite/examples/conf_files/TD_ASBarabasi_RTWaxman.conf"
36 
37 # Invoke the BriteTopologyHelper and pass in a BRITE
38 # configuration file and a seed file. This will use
39 # BRITE to build a graph from which we can build the ns-3 topology
40 bth = ns.BriteTopologyHelper(confFile)
41 bth.AssignStreams(3)
42 
43 p2p = ns.PointToPointHelper()
44 
45 stack = ns.InternetStackHelper()
46 
47 nixRouting = ns.Ipv4NixVectorHelper()
48 stack.SetRoutingHelper(nixRouting)
49 
50 address = ns.Ipv4AddressHelper()
51 address.SetBase("10.0.0.0", "255.255.255.252")
52 
53 bth.BuildBriteTopology(stack)
54 bth.AssignIpv4Addresses(address)
55 
56 print(f"Number of AS created {bth.GetNAs()}")
57 
58 # The BRITE topology generator generates a topology of routers. Here we create
59 # two subnetworks which we attach to router leaf nodes generated by BRITE
60 # use just one node
61 client = ns.NodeContainer()
62 server = ns.NodeContainer()
63 
64 client.Create(1)
65 stack.Install(client)
66 
67 # install client node on last leaf node of AS 0
68 numLeafNodesInAsZero = bth.GetNLeafNodesForAs(0)
69 client.Add(bth.GetLeafNodeForAs(0, numLeafNodesInAsZero - 1))
70 
71 server.Create(1)
72 stack.Install(server)
73 
74 # install server node on last leaf node on AS 1
75 numLeafNodesInAsOne = bth.GetNLeafNodesForAs(1)
76 server.Add(bth.GetLeafNodeForAs(1, numLeafNodesInAsOne - 1))
77 
78 p2p.SetDeviceAttribute("DataRate", ns.StringValue("5Mbps"))
79 p2p.SetChannelAttribute("Delay", ns.StringValue("2ms"))
80 
81 p2pClientDevices = p2p.Install(client)
82 p2pServerDevices = p2p.Install(server)
83 
84 address.SetBase("10.1.0.0", "255.255.0.0")
85 clientInterfaces = address.Assign(p2pClientDevices)
86 
87 address.SetBase("10.2.0.0", "255.255.0.0")
88 serverInterfaces = ns.Ipv4InterfaceContainer()
89 serverInterfaces = address.Assign(p2pServerDevices)
90 
91 echoServer = ns.UdpEchoServerHelper(9)
92 serverApps = echoServer.Install(server.Get(0))
93 serverApps.Start(ns.Seconds(1.0))
94 serverApps.Stop(ns.Seconds(5.0))
95 
96 echoClient = ns.UdpEchoClientHelper(serverInterfaces.GetAddress(0).ConvertTo(), 9)
97 echoClient.SetAttribute("MaxPackets", ns.UintegerValue(1))
98 echoClient.SetAttribute("Interval", ns.TimeValue(ns.Seconds(1.0)))
99 echoClient.SetAttribute("PacketSize", ns.UintegerValue(1024))
100 
101 clientApps = echoClient.Install(client.Get(0))
102 clientApps.Start(ns.Seconds(2.0))
103 clientApps.Stop(ns.Seconds(5.0))
104 
105 asciiTrace = ns.AsciiTraceHelper()
106 p2p.EnableAsciiAll(asciiTrace.CreateFileStream("briteLeaves.tr"))
107 
108 # Run the simulator
109 ns.Simulator.Stop(ns.Seconds(6.0))
110 ns.Simulator.Run()
111 ns.Simulator.Destroy()