A Discrete-Event Network Simulator
API
brite-generic-example.cc
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  */
16 
17 #include "Brite.h"
18 
19 #include "ns3/applications-module.h"
20 #include "ns3/brite-module.h"
21 #include "ns3/core-module.h"
22 #include "ns3/internet-module.h"
23 #include "ns3/mobility-module.h"
24 #include "ns3/network-module.h"
25 #include "ns3/nix-vector-helper.h"
26 #include "ns3/point-to-point-module.h"
27 
28 #include <fstream>
29 #include <iostream>
30 #include <string>
31 
32 using namespace ns3;
33 
34 NS_LOG_COMPONENT_DEFINE("BriteExample");
35 
36 int
37 main(int argc, char* argv[])
38 {
39  LogComponentEnable("UdpEchoClientApplication", LOG_LEVEL_ALL);
40  LogComponentEnable("UdpEchoServerApplication", LOG_LEVEL_ALL);
41 
42  LogComponentEnable("BriteExample", LOG_LEVEL_ALL);
43 
44  // BRITE needs a configuration file to build its graph. By default, this
45  // example will use the TD_ASBarabasi_RTWaxman.conf file. There are many others
46  // which can be found in the BRITE/conf_files directory
47  std::string confFile = "src/brite/examples/conf_files/TD_ASBarabasi_RTWaxman.conf";
48  bool tracing = false;
49  bool nix = false;
50 
51  CommandLine cmd(__FILE__);
52  cmd.AddValue("confFile", "BRITE conf file", confFile);
53  cmd.AddValue("tracing", "Enable or disable ascii tracing", tracing);
54  cmd.AddValue("nix", "Enable or disable nix-vector routing", nix);
55 
56  cmd.Parse(argc, argv);
57 
58  nix = false;
59 
60  // Invoke the BriteTopologyHelper and pass in a BRITE
61  // configuration file and a seed file. This will use
62  // BRITE to build a graph from which we can build the ns-3 topology
63  BriteTopologyHelper bth(confFile);
64  bth.AssignStreams(3);
65 
67 
69 
70  if (nix)
71  {
72  Ipv4NixVectorHelper nixRouting;
73  stack.SetRoutingHelper(nixRouting);
74  }
75 
77  address.SetBase("10.0.0.0", "255.255.255.252");
78 
79  bth.BuildBriteTopology(stack);
80  bth.AssignIpv4Addresses(address);
81 
82  NS_LOG_INFO("Number of AS created " << bth.GetNAs());
83 
84  // The BRITE topology generator generates a topology of routers. Here we create
85  // two subnetworks which we attach to router leaf nodes generated by BRITE
86  // Any NS3 topology may be used to attach to the BRITE leaf nodes but here we
87  // use just one node
88 
89  NodeContainer client;
90  NodeContainer server;
91 
92  client.Create(1);
93  stack.Install(client);
94 
95  // install client node on last leaf node of AS 0
96  int numLeafNodesInAsZero = bth.GetNLeafNodesForAs(0);
97  client.Add(bth.GetLeafNodeForAs(0, numLeafNodesInAsZero - 1));
98 
99  server.Create(1);
100  stack.Install(server);
101 
102  // install server node on last leaf node on AS 1
103  int numLeafNodesInAsOne = bth.GetNLeafNodesForAs(1);
104  server.Add(bth.GetLeafNodeForAs(1, numLeafNodesInAsOne - 1));
105 
106  p2p.SetDeviceAttribute("DataRate", StringValue("5Mbps"));
107  p2p.SetChannelAttribute("Delay", StringValue("2ms"));
108 
109  NetDeviceContainer p2pClientDevices;
110  NetDeviceContainer p2pServerDevices;
111 
112  p2pClientDevices = p2p.Install(client);
113  p2pServerDevices = p2p.Install(server);
114 
115  address.SetBase("10.1.0.0", "255.255.0.0");
116  Ipv4InterfaceContainer clientInterfaces;
117  clientInterfaces = address.Assign(p2pClientDevices);
118 
119  address.SetBase("10.2.0.0", "255.255.0.0");
120  Ipv4InterfaceContainer serverInterfaces;
121  serverInterfaces = address.Assign(p2pServerDevices);
122 
124  ApplicationContainer serverApps = echoServer.Install(server.Get(0));
125  serverApps.Start(Seconds(1.0));
126  serverApps.Stop(Seconds(5.0));
127 
128  UdpEchoClientHelper echoClient(serverInterfaces.GetAddress(0), 9);
129  echoClient.SetAttribute("MaxPackets", UintegerValue(1));
130  echoClient.SetAttribute("Interval", TimeValue(Seconds(1.)));
131  echoClient.SetAttribute("PacketSize", UintegerValue(1024));
132 
133  ApplicationContainer clientApps = echoClient.Install(client.Get(0));
134  clientApps.Start(Seconds(2.0));
135  clientApps.Stop(Seconds(5.0));
136 
137  if (!nix)
138  {
140  }
141 
142  if (tracing)
143  {
144  AsciiTraceHelper ascii;
145  p2p.EnableAsciiAll(ascii.CreateFileStream("briteLeaves.tr"));
146  }
147  // Run the simulator
148  Simulator::Stop(Seconds(6.0));
149  Simulator::Run();
151 
152  return 0;
153 }
holds a vector of ns3::Application pointers.
void EnableAsciiAll(std::string prefix)
Enable ascii trace output on each device (which is of the appropriate type) in the set of all nodes c...
Manage ASCII trace files for device models.
Definition: trace-helper.h:173
Ptr< OutputStreamWrapper > CreateFileStream(std::string filename, std::ios::openmode filemode=std::ios::out)
Create and initialize an output stream object we'll use to write the traced bits.
Interface with BRITE, the Boston university Representative Internet Topology gEnerator.
Parse command-line arguments.
Definition: command-line.h:232
aggregate IP/TCP/UDP functionality to existing Nodes.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
static void PopulateRoutingTables()
Build a routing database and initialize the routing tables of the nodes in the simulation.
holds a vector of std::pair of Ptr<Ipv4> and interface index.
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
holds a vector of ns3::NetDevice pointers
Helper class that adds Nix-vector routing to nodes.
keep track of a set of node pointers.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
void Add(const NodeContainer &nc)
Append the contents of another NodeContainer to the end of this container.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
Build a set of PointToPointNetDevice objects.
void SetDeviceAttribute(std::string name, const AttributeValue &value)
Set an attribute value to be propagated to each NetDevice created by the helper.
void SetChannelAttribute(std::string name, const AttributeValue &value)
Set an attribute value to be propagated to each Channel created by the helper.
NetDeviceContainer Install(NodeContainer c)
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:140
static void Run()
Run the simulation.
Definition: simulator.cc:176
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:184
Hold variables of type string.
Definition: string.h:56
AttributeValue implementation for Time.
Definition: nstime.h:1423
Create an application which sends a UDP packet and waits for an echo of this packet.
Create a server application which waits for input UDP packets and sends them back to the original sen...
Hold an unsigned integer type.
Definition: uinteger.h:45
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:275
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1336
echoClient
Definition: first.py:53
address
Definition: first.py:40
serverApps
Definition: first.py:48
echoServer
Definition: first.py:46
clientApps
Definition: first.py:58
stack
Definition: first.py:37
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void LogComponentEnable(const std::string &name, LogLevel level)
Enable the logging output associated with that log component.
Definition: log.cc:305
@ LOG_LEVEL_ALL
Print everything.
Definition: log.h:116
cmd
Definition: second.py:33
bool tracing
Flag to enable/disable generation of tracing files.