A Discrete-Event Network Simulator
API
topology-example-sim.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License version 2 as
5  * published by the Free Software Foundation;
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15  *
16  * Author: Tommaso Pecorella <tommaso.pecorella@unifi.it>
17  * Author: Valerio Sartini <valesar@gmail.com>
18  *
19  * This program conducts a simple experiment: It builds up a topology based on
20  * either Inet or Orbis trace files. A random node is then chosen, and all the
21  * other nodes will send a packet to it. The TTL is measured and reported as an histogram.
22  *
23  */
24 
25 #include <ctime>
26 
27 #include <sstream>
28 
29 #include "ns3/core-module.h"
30 #include "ns3/network-module.h"
31 #include "ns3/internet-module.h"
32 #include "ns3/point-to-point-module.h"
33 #include "ns3/applications-module.h"
34 #include "ns3/nix-vector-helper.h"
35 
36 #include "ns3/topology-read-module.h"
37 #include <list>
38 
45 // Document the available input files
67 using namespace ns3;
68 
69 NS_LOG_COMPONENT_DEFINE ("TopologyCreationExperiment");
70 
76 static void SinkRx (Ptr<const Packet> p, const Address &ad)
77 {
78  Ipv4Header ipv4;
79  p->PeekHeader (ipv4);
80  std::cout << "TTL: " << (unsigned)ipv4.GetTtl () << std::endl;
81 }
82 
83 
84 // ----------------------------------------------------------------------
85 // -- main
86 // ----------------------------------------------
87 int main (int argc, char *argv[])
88 {
89 
90  std::string format ("Inet");
91  std::string input ("src/topology-read/examples/Inet_small_toposample.txt");
92 
93  // Set up command line parameters used to control the experiment.
94  CommandLine cmd (__FILE__);
95  cmd.AddValue ("format", "Format to use for data input [Orbis|Inet|Rocketfuel].",
96  format);
97  cmd.AddValue ("input", "Name of the input file.",
98  input);
99  cmd.Parse (argc, argv);
100 
101 
102  // ------------------------------------------------------------
103  // -- Read topology data.
104  // --------------------------------------------
105 
106  // Pick a topology reader based in the requested format.
107  TopologyReaderHelper topoHelp;
108  topoHelp.SetFileName (input);
109  topoHelp.SetFileType (format);
110  Ptr<TopologyReader> inFile = topoHelp.GetTopologyReader ();
111 
113 
114  if (inFile != 0)
115  {
116  nodes = inFile->Read ();
117  }
118 
119  if (inFile->LinksSize () == 0)
120  {
121  NS_LOG_ERROR ("Problems reading the topology file. Failing.");
122  return -1;
123  }
124 
125  // ------------------------------------------------------------
126  // -- Create nodes and network stacks
127  // --------------------------------------------
128  NS_LOG_INFO ("creating internet stack");
130 
131  // Setup NixVector Routing
132  Ipv4NixVectorHelper nixRouting;
133  stack.SetRoutingHelper (nixRouting); // has effect on the next Install ()
134  stack.Install (nodes);
135 
136  NS_LOG_INFO ("creating IPv4 addresses");
138  address.SetBase ("10.0.0.0", "255.255.255.252");
139 
140  int totlinks = inFile->LinksSize ();
141 
142  NS_LOG_INFO ("creating node containers");
143  NodeContainer* nc = new NodeContainer[totlinks];
145  int i = 0;
146  for ( iter = inFile->LinksBegin (); iter != inFile->LinksEnd (); iter++, i++ )
147  {
148  nc[i] = NodeContainer (iter->GetFromNode (), iter->GetToNode ());
149  }
150 
151  NS_LOG_INFO ("creating net device containers");
152  NetDeviceContainer* ndc = new NetDeviceContainer[totlinks];
153  PointToPointHelper p2p;
154  for (int i = 0; i < totlinks; i++)
155  {
156  // p2p.SetChannelAttribute ("Delay", TimeValue(MilliSeconds(weight[i])));
157  p2p.SetChannelAttribute ("Delay", StringValue ("2ms"));
158  p2p.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
159  ndc[i] = p2p.Install (nc[i]);
160  }
161 
162  // it crates little subnets, one for each couple of nodes.
163  NS_LOG_INFO ("creating IPv4 interfaces");
164  Ipv4InterfaceContainer* ipic = new Ipv4InterfaceContainer[totlinks];
165  for (int i = 0; i < totlinks; i++)
166  {
167  ipic[i] = address.Assign (ndc[i]);
168  address.NewNetwork ();
169  }
170 
171 
172  uint32_t totalNodes = nodes.GetN ();
173  Ptr<UniformRandomVariable> unifRandom = CreateObject<UniformRandomVariable> ();
174  unifRandom->SetAttribute ("Min", DoubleValue (0));
175  unifRandom->SetAttribute ("Max", DoubleValue (totalNodes - 1));
176 
177  unsigned int randomServerNumber = unifRandom->GetInteger (0, totalNodes - 1);
178 
179  Ptr<Node> randomServerNode = nodes.Get (randomServerNumber);
180  Ptr<Ipv4> ipv4Server = randomServerNode->GetObject<Ipv4> ();
181  Ipv4InterfaceAddress iaddrServer = ipv4Server->GetAddress (1,0);
182  Ipv4Address ipv4AddrServer = iaddrServer.GetLocal ();
183 
184  // ------------------------------------------------------------
185  // -- Send around packets to check the ttl
186  // --------------------------------------------
187  Config::SetDefault ("ns3::Ipv4RawSocketImpl::Protocol", StringValue ("2"));
188  InetSocketAddress dst = InetSocketAddress ( ipv4AddrServer );
189 
190  OnOffHelper onoff = OnOffHelper ("ns3::Ipv4RawSocketFactory", dst);
191  onoff.SetConstantRate (DataRate (15000));
192  onoff.SetAttribute ("PacketSize", UintegerValue (1200));
193 
194  NodeContainer clientNodes;
195  for ( unsigned int i = 0; i < nodes.GetN (); i++ )
196  {
197  if (i != randomServerNumber )
198  {
199  Ptr<Node> clientNode = nodes.Get (i);
200  clientNodes.Add (clientNode);
201  }
202  }
203 
204  ApplicationContainer apps = onoff.Install (clientNodes);
205  apps.Start (Seconds (1.0));
206  apps.Stop (Seconds (2.0));
207 
208  PacketSinkHelper sink = PacketSinkHelper ("ns3::Ipv4RawSocketFactory", dst);
209  apps = sink.Install (randomServerNode);
210  apps.Start (Seconds (0.0));
211  apps.Stop (Seconds (3.0));
212 
213  // we trap the packet sink receiver to extract the TTL.
214  Config::ConnectWithoutContext ("/NodeList/*/ApplicationList/*/$ns3::PacketSink/Rx",
215  MakeCallback (&SinkRx));
216 
217  // ------------------------------------------------------------
218  // -- Run the simulation
219  // --------------------------------------------
220  NS_LOG_INFO ("Run Simulation.");
221  Simulator::Run ();
223 
224  delete[] ipic;
225  delete[] ndc;
226  delete[] nc;
227 
228  NS_LOG_INFO ("Done.");
229 
230  return 0;
231 
232  // end main
233 }
a polymophic address class
Definition: address.h:91
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:229
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:41
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:41
Packet header for IPv4.
Definition: ipv4-header.h:34
uint8_t GetTtl(void) const
Definition: ipv4-header.cc:265
Access to the IPv4 forwarding table, interfaces, and configuration.
Definition: ipv4.h:77
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.
void Add(NodeContainer other)
Append the contents of another NodeContainer to the end of this container.
void SetAttribute(std::string name, const AttributeValue &value)
Set a single attribute, raising fatal errors if unsuccessful.
Definition: object-base.cc:256
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:470
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:43
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:290
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)
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:136
static void Run(void)
Run the simulation.
Definition: simulator.cc:172
Hold variables of type string.
Definition: string.h:41
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:44
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:849
void ConnectWithoutContext(std::string path, const CallbackBase &cb)
Definition: config.cc:901
#define NS_LOG_ERROR(msg)
Use NS_LOG to output a message of level LOG_ERROR.
Definition: log.h:257
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:281
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1244
address
Definition: first.py:44
stack
Definition: first.py:41
nodes
Definition: first.py:32
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Ts... > MakeCallback(R(T::*memPtr)(Ts...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:1648
cmd
Definition: second.py:35
static void SinkRx(Ptr< const Packet > p, const Address &ad)
Print the TTL of received packet.
Ptr< PacketSink > sink
Definition: wifi-tcp.cc:56