A Discrete-Event Network Simulator
API
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
42// Document the available input files
64using namespace ns3;
65
66NS_LOG_COMPONENT_DEFINE("TopologyCreationExperiment");
67
73static void
75{
76 Ipv4Header ipv4;
77 p->PeekHeader(ipv4);
78 std::cout << "TTL: " << (unsigned)ipv4.GetTtl() << std::endl;
79}
80
81// ----------------------------------------------------------------------
82// -- main
83// ----------------------------------------------
84int
85main(int argc, char* argv[])
86{
87 std::string format("Inet");
88 std::string input("src/topology-read/examples/Inet_small_toposample.txt");
89
90 // Set up command line parameters used to control the experiment.
91 CommandLine cmd(__FILE__);
92 cmd.AddValue("format", "Format to use for data input [Orbis|Inet|Rocketfuel].", format);
93 cmd.AddValue("input", "Name of the input file.", input);
94 cmd.Parse(argc, argv);
95
96 // ------------------------------------------------------------
97 // -- Read topology data.
98 // --------------------------------------------
99
100 // Pick a topology reader based in the requested format.
101 TopologyReaderHelper topoHelp;
102 topoHelp.SetFileName(input);
103 topoHelp.SetFileType(format);
104 Ptr<TopologyReader> inFile = topoHelp.GetTopologyReader();
105
107
108 if (inFile)
109 {
110 nodes = inFile->Read();
111 }
112
113 if (inFile->LinksSize() == 0)
114 {
115 NS_LOG_ERROR("Problems reading the topology file. Failing.");
116 return -1;
117 }
118
119 // ------------------------------------------------------------
120 // -- Create nodes and network stacks
121 // --------------------------------------------
122 NS_LOG_INFO("creating internet stack");
124
125 // Setup NixVector Routing
126 Ipv4NixVectorHelper nixRouting;
127 stack.SetRoutingHelper(nixRouting); // has effect on the next Install ()
128 stack.Install(nodes);
129
130 NS_LOG_INFO("creating IPv4 addresses");
132 address.SetBase("10.0.0.0", "255.255.255.252");
133
134 int totlinks = inFile->LinksSize();
135
136 NS_LOG_INFO("creating node containers");
137 NodeContainer* nc = new NodeContainer[totlinks];
139 int i = 0;
140 for (iter = inFile->LinksBegin(); iter != inFile->LinksEnd(); iter++, i++)
141 {
142 nc[i] = NodeContainer(iter->GetFromNode(), iter->GetToNode());
143 }
144
145 NS_LOG_INFO("creating net device containers");
146 NetDeviceContainer* ndc = new NetDeviceContainer[totlinks];
148 for (int i = 0; i < totlinks; i++)
149 {
150 // p2p.SetChannelAttribute ("Delay", TimeValue(MilliSeconds(weight[i])));
151 p2p.SetChannelAttribute("Delay", StringValue("2ms"));
152 p2p.SetDeviceAttribute("DataRate", StringValue("5Mbps"));
153 ndc[i] = p2p.Install(nc[i]);
154 }
155
156 // it crates little subnets, one for each couple of nodes.
157 NS_LOG_INFO("creating IPv4 interfaces");
159 for (int i = 0; i < totlinks; i++)
160 {
161 ipic[i] = address.Assign(ndc[i]);
162 address.NewNetwork();
163 }
164
165 uint32_t totalNodes = nodes.GetN();
166 Ptr<UniformRandomVariable> unifRandom = CreateObject<UniformRandomVariable>();
167 unifRandom->SetAttribute("Min", DoubleValue(0));
168 unifRandom->SetAttribute("Max", DoubleValue(totalNodes - 1));
169
170 unsigned int randomServerNumber = unifRandom->GetInteger(0, totalNodes - 1);
171
172 Ptr<Node> randomServerNode = nodes.Get(randomServerNumber);
173 Ptr<Ipv4> ipv4Server = randomServerNode->GetObject<Ipv4>();
174 Ipv4InterfaceAddress iaddrServer = ipv4Server->GetAddress(1, 0);
175 Ipv4Address ipv4AddrServer = iaddrServer.GetLocal();
176
177 // ------------------------------------------------------------
178 // -- Send around packets to check the ttl
179 // --------------------------------------------
180 Config::SetDefault("ns3::Ipv4RawSocketImpl::Protocol", StringValue("2"));
181 InetSocketAddress dst = InetSocketAddress(ipv4AddrServer);
182
183 OnOffHelper onoff = OnOffHelper("ns3::Ipv4RawSocketFactory", dst);
184 onoff.SetConstantRate(DataRate(15000));
185 onoff.SetAttribute("PacketSize", UintegerValue(1200));
186
187 NodeContainer clientNodes;
188 for (unsigned int i = 0; i < nodes.GetN(); i++)
189 {
190 if (i != randomServerNumber)
191 {
192 Ptr<Node> clientNode = nodes.Get(i);
193 clientNodes.Add(clientNode);
194 }
195 }
196
197 ApplicationContainer apps = onoff.Install(clientNodes);
198 apps.Start(Seconds(1.0));
199 apps.Stop(Seconds(2.0));
200
201 PacketSinkHelper sink = PacketSinkHelper("ns3::Ipv4RawSocketFactory", dst);
202 apps = sink.Install(randomServerNode);
203 apps.Start(Seconds(0.0));
204 apps.Stop(Seconds(3.0));
205
206 // we trap the packet sink receiver to extract the TTL.
207 Config::ConnectWithoutContext("/NodeList/*/ApplicationList/*/$ns3::PacketSink/Rx",
209
210 // ------------------------------------------------------------
211 // -- Run the simulation
212 // --------------------------------------------
213 NS_LOG_INFO("Run Simulation.");
214 Simulator::Run();
215 Simulator::Destroy();
216
217 delete[] ipic;
218 delete[] ndc;
219 delete[] nc;
220
221 NS_LOG_INFO("Done.");
222
223 return 0;
224
225 // end main
226}
a polymophic address class
Definition: address.h:92
holds a vector of ns3::Application pointers.
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter.
void Stop(Time stop)
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
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:43
Packet header for IPv4.
Definition: ipv4-header.h:34
uint8_t GetTtl() const
Definition: ipv4-header.cc:274
Access to the IPv4 forwarding table, interfaces, and configuration.
Definition: ipv4.h:79
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.
void SetAttribute(std::string name, const AttributeValue &value)
Set a single attribute, raising fatal errors if unsuccessful.
Definition: object-base.cc:258
Ptr< T > GetObject() const
Get a pointer to the requested aggregated Object.
Definition: object.h:471
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:44
void SetConstantRate(DataRate dataRate, uint32_t packetSize=512)
Helper function to set a constant rate source.
ApplicationContainer Install(NodeContainer c) const
Install an ns3::OnOffApplication on each node of the input container configured with all the attribut...
void SetAttribute(std::string name, const AttributeValue &value)
Helper function used to set the underlying application attributes.
uint32_t PeekHeader(Header &header) const
Deserialize but does not remove the header from the internal buffer.
Definition: packet.cc:305
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes.
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)
Hold variables of type string.
Definition: string.h:42
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
uint32_t GetInteger(uint32_t min, uint32_t max)
Get the next random value, as an unsigned integer in the specified range .
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:891
void ConnectWithoutContext(std::string path, const CallbackBase &cb)
Definition: config.cc:951
#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
void(* DataRate)(DataRate oldValue, DataRate newValue)
TracedValue callback signature for DataRate.
Definition: data-rate.h:328
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1338
NodeContainer nodes
address
Definition: first.py:40
stack
Definition: first.py:37
Every class exported by the ns3 library is enclosed in the ns3 namespace.
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:691
cmd
Definition: second.py:33
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