A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
topology-example-sim.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 * Author: Tommaso Pecorella <tommaso.pecorella@unifi.it>
16 * Author: Valerio Sartini <valesar@gmail.com>
17 *
18 * This program conducts a simple experiment: It builds up a topology based on
19 * either Inet or Orbis trace files. A random node is then chosen, and all the
20 * other nodes will send a packet to it. The TTL is measured and reported as an histogram.
21 *
22 */
23
24#include "ns3/applications-module.h"
25#include "ns3/core-module.h"
26#include "ns3/internet-module.h"
27#include "ns3/network-module.h"
28#include "ns3/nix-vector-helper.h"
29#include "ns3/point-to-point-module.h"
30#include "ns3/topology-read-module.h"
31
32#include <ctime>
33#include <list>
34#include <sstream>
35
50using namespace ns3;
51
52NS_LOG_COMPONENT_DEFINE("TopologyCreationExperiment");
53
59static void
61{
62 Ipv4Header ipv4;
63 p->PeekHeader(ipv4);
64 std::cout << "TTL: " << (unsigned)ipv4.GetTtl() << std::endl;
65}
66
67// ----------------------------------------------------------------------
68// -- main
69// ----------------------------------------------
70int
71main(int argc, char* argv[])
72{
73 std::string format("Inet");
74 std::string input("src/topology-read/examples/Inet_small_toposample.txt");
75
76 LogComponentEnable("TopologyCreationExperiment", LOG_LEVEL_INFO);
77
78 // Set up command line parameters used to control the experiment.
79 CommandLine cmd(__FILE__);
80 cmd.AddValue("format", "Format to use for data input [Orbis|Inet|Rocketfuel].", format);
81 cmd.AddValue("input", "Name of the input file.", input);
82 cmd.Parse(argc, argv);
83
84 // ------------------------------------------------------------
85 // -- Read topology data.
86 // --------------------------------------------
87
88 // Pick a topology reader based in the requested format.
89 TopologyReaderHelper topoHelp;
90 topoHelp.SetFileName(input);
91 topoHelp.SetFileType(format);
92 Ptr<TopologyReader> inFile = topoHelp.GetTopologyReader();
93
95
96 if (inFile)
97 {
98 nodes = inFile->Read();
99 }
100
101 if (inFile->LinksSize() == 0)
102 {
103 NS_LOG_ERROR("Problems reading the topology file. Failing.");
104 return -1;
105 }
106
107 // ------------------------------------------------------------
108 // -- Create nodes and network stacks
109 // --------------------------------------------
110 NS_LOG_INFO("creating internet stack");
112
113 // Setup NixVector Routing
115 stack.SetRoutingHelper(nixRouting); // has effect on the next Install ()
116 stack.Install(nodes);
117
118 NS_LOG_INFO("creating IPv4 addresses");
120 address.SetBase("10.0.0.0", "255.255.255.252");
121
122 int totlinks = inFile->LinksSize();
123
124 NS_LOG_INFO("creating node containers");
125 auto nc = new NodeContainer[totlinks];
127 int i = 0;
128 for (iter = inFile->LinksBegin(); iter != inFile->LinksEnd(); iter++, i++)
129 {
130 nc[i] = NodeContainer(iter->GetFromNode(), iter->GetToNode());
131 }
132
133 NS_LOG_INFO("creating net device containers");
134 auto ndc = new NetDeviceContainer[totlinks];
136 for (int i = 0; i < totlinks; i++)
137 {
138 // p2p.SetChannelAttribute ("Delay", TimeValue(MilliSeconds(weight[i])));
139 p2p.SetChannelAttribute("Delay", StringValue("2ms"));
140 p2p.SetDeviceAttribute("DataRate", StringValue("5Mbps"));
141 ndc[i] = p2p.Install(nc[i]);
142 }
143
144 // it crates little subnets, one for each couple of nodes.
145 NS_LOG_INFO("creating IPv4 interfaces");
146 auto ipic = new Ipv4InterfaceContainer[totlinks];
147 for (int i = 0; i < totlinks; i++)
148 {
149 ipic[i] = address.Assign(ndc[i]);
150 address.NewNetwork();
151 }
152
153 uint32_t totalNodes = nodes.GetN();
154 Ptr<UniformRandomVariable> unifRandom = CreateObject<UniformRandomVariable>();
155 unifRandom->SetAttribute("Min", DoubleValue(0));
156 unifRandom->SetAttribute("Max", DoubleValue(totalNodes - 1));
157
158 unsigned int randomServerNumber = unifRandom->GetInteger(0, totalNodes - 1);
159
160 Ptr<Node> randomServerNode = nodes.Get(randomServerNumber);
161 Ptr<Ipv4> ipv4Server = randomServerNode->GetObject<Ipv4>();
162 Ipv4InterfaceAddress iaddrServer = ipv4Server->GetAddress(1, 0);
163 Ipv4Address ipv4AddrServer = iaddrServer.GetLocal();
164
165 // ------------------------------------------------------------
166 // -- Send around packets to check the ttl
167 // --------------------------------------------
168 Config::SetDefault("ns3::Ipv4RawSocketImpl::Protocol", StringValue("2"));
169 InetSocketAddress dst(ipv4AddrServer);
170
171 OnOffHelper onoff = OnOffHelper("ns3::Ipv4RawSocketFactory", dst);
172 onoff.SetConstantRate(DataRate(15000));
173 onoff.SetAttribute("PacketSize", UintegerValue(1200));
174
175 NodeContainer clientNodes;
176 for (unsigned int i = 0; i < nodes.GetN(); i++)
177 {
178 if (i != randomServerNumber)
179 {
180 Ptr<Node> clientNode = nodes.Get(i);
181 clientNodes.Add(clientNode);
182 }
183 }
184
185 ApplicationContainer apps = onoff.Install(clientNodes);
186 apps.Start(Seconds(1.0));
187 apps.Stop(Seconds(2.0));
188
189 PacketSinkHelper sink = PacketSinkHelper("ns3::Ipv4RawSocketFactory", dst);
190 apps = sink.Install(randomServerNode);
191 apps.Start(Seconds(0.0));
192 apps.Stop(Seconds(3.0));
193
194 // we trap the packet sink receiver to extract the TTL.
195 Config::ConnectWithoutContext("/NodeList/*/ApplicationList/*/$ns3::PacketSink/Rx",
197
198 // ------------------------------------------------------------
199 // -- Run the simulation
200 // --------------------------------------------
201 NS_LOG_INFO("Run Simulation.");
204
205 delete[] ipic;
206 delete[] ndc;
207 delete[] nc;
208
209 NS_LOG_INFO("Done.");
210
211 return 0;
212
213 // end main
214}
a polymophic address class
Definition: address.h:101
holds a vector of ns3::Application pointers.
void Start(Time start) const
Start all of the Applications in this container at the start time given as a parameter.
void Stop(Time stop) const
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter.
Parse command-line arguments.
Definition: command-line.h:232
Class for representing data rates.
Definition: data-rate.h:89
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition: double.h:42
an Inet address class
aggregate IP/TCP/UDP functionality to existing Nodes.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:42
Packet header for IPv4.
Definition: ipv4-header.h:34
Access to the IPv4 forwarding table, interfaces, and configuration.
Definition: ipv4.h:80
a class to store IPv4 address information on an interface
holds a vector of std::pair of Ptr<Ipv4> and interface index.
holds a vector of ns3::NetDevice pointers
Helper class that adds Nix-vector routing to nodes.
keep track of a set of node pointers.
uint32_t GetN() const
Get the number of Ptr<Node> stored in this container.
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.
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:37
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes.
Build a set of PointToPointNetDevice objects.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static void Run()
Run the simulation.
Definition: simulator.cc:178
Hold variables of type string.
Definition: string.h:56
Helper class which makes it easier to configure and use a generic TopologyReader.
void SetFileType(const std::string fileType)
Sets the input file type.
Ptr< TopologyReader > GetTopologyReader()
Gets a Ptr<TopologyReader> to the actual TopologyReader.
void SetFileName(const std::string fileName)
Sets the input file name.
std::list< Link >::const_iterator ConstLinksIterator
Constant iterator to the list of the links.
Hold an unsigned integer type.
Definition: uinteger.h:45
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:894
void ConnectWithoutContext(std::string path, const CallbackBase &cb)
Definition: config.cc:954
#define NS_LOG_ERROR(msg)
Use NS_LOG to output a message of level LOG_ERROR.
Definition: log.h:254
#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:1326
NodeContainer nodes
ns address
Definition: first.py:47
ns stack
Definition: first.py:44
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:302
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:704
@ LOG_LEVEL_INFO
LOG_INFO and above.
Definition: log.h:104
ns cmd
Definition: second.py:40
static void SinkRx(Ptr< const Packet > p, const Address &ad)
Print the TTL of received packet.
Ptr< PacketSink > sink
Pointer to the packet sink application.
Definition: wifi-tcp.cc:55