A Discrete-Event Network Simulator
API
star.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  */
17 
18 #include "ns3/core-module.h"
19 #include "ns3/network-module.h"
20 #include "ns3/netanim-module.h"
21 #include "ns3/internet-module.h"
22 #include "ns3/point-to-point-module.h"
23 #include "ns3/applications-module.h"
24 #include "ns3/point-to-point-layout-module.h"
25 
26 // Network topology (default)
27 //
28 // n2 n3 n4 .
29 // \ | / .
30 // \|/ .
31 // n1--- n0---n5 .
32 // /|\ .
33 // / | \ .
34 // n8 n7 n6 .
35 //
36 
37 
38 using namespace ns3;
39 
41 
42 int
43 main (int argc, char *argv[])
44 {
45 
46  //
47  // Set up some default values for the simulation.
48  //
49  Config::SetDefault ("ns3::OnOffApplication::PacketSize", UintegerValue (137));
50 
51  // ??? try and stick 15kb/s into the data rate
52  Config::SetDefault ("ns3::OnOffApplication::DataRate", StringValue ("14kb/s"));
53 
54  //
55  // Default number of nodes in the star. Overridable by command line argument.
56  //
57  uint32_t nSpokes = 8;
58 
59  CommandLine cmd (__FILE__);
60  cmd.AddValue ("nSpokes", "Number of nodes to place in the star", nSpokes);
61  cmd.Parse (argc, argv);
62 
63  NS_LOG_INFO ("Build star topology.");
65  pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
66  pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));
67  PointToPointStarHelper star (nSpokes, pointToPoint);
68 
69  NS_LOG_INFO ("Install internet stack on all nodes.");
70  InternetStackHelper internet;
71  star.InstallStack (internet);
72 
73  NS_LOG_INFO ("Assign IP Addresses.");
74  star.AssignIpv4Addresses (Ipv4AddressHelper ("10.1.1.0", "255.255.255.0"));
75 
76  NS_LOG_INFO ("Create applications.");
77  //
78  // Create a packet sink on the star "hub" to receive packets.
79  //
80  uint16_t port = 50000;
81  Address hubLocalAddress (InetSocketAddress (Ipv4Address::GetAny (), port));
82  PacketSinkHelper packetSinkHelper ("ns3::TcpSocketFactory", hubLocalAddress);
83  ApplicationContainer hubApp = packetSinkHelper.Install (star.GetHub ());
84  hubApp.Start (Seconds (1.0));
85  hubApp.Stop (Seconds (10.0));
86 
87  //
88  // Create OnOff applications to send TCP to the hub, one on each spoke node.
89  //
90  OnOffHelper onOffHelper ("ns3::TcpSocketFactory", Address ());
91  onOffHelper.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1]"));
92  onOffHelper.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0]"));
93 
94  ApplicationContainer spokeApps;
95 
96  for (uint32_t i = 0; i < star.SpokeCount (); ++i)
97  {
98  AddressValue remoteAddress (InetSocketAddress (star.GetHubIpv4Address (i), port));
99  onOffHelper.SetAttribute ("Remote", remoteAddress);
100  spokeApps.Add (onOffHelper.Install (star.GetSpokeNode (i)));
101  }
102  spokeApps.Start (Seconds (1.0));
103  spokeApps.Stop (Seconds (10.0));
104 
105  NS_LOG_INFO ("Enable static global routing.");
106  //
107  // Turn on global static routing so we can actually be routed across the star.
108  //
110 
111  NS_LOG_INFO ("Enable pcap tracing.");
112  //
113  // Do pcap tracing on all point-to-point devices on all nodes.
114  //
115  pointToPoint.EnablePcapAll ("star");
116 
117  NS_LOG_INFO ("Run Simulation.");
118  Simulator::Run ();
120  NS_LOG_INFO ("Done.");
121 
122  return 0;
123 }
a polymophic address class
Definition: address.h:91
AttributeValue implementation for Address.
Definition: address.h:278
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 Add(ApplicationContainer other)
Append the contents of another ApplicationContainer to the end of this container.
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
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.
static Ipv4Address GetAny(void)
static void PopulateRoutingTables(void)
Build a routing database and initialize the routing tables of the nodes in the simulation.
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:43
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes.
Build a set of PointToPointNetDevice objects.
A helper to make it easier to create a star topology with PointToPoint links.
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
Hold an unsigned integer type.
Definition: uinteger.h:44
uint16_t port
Definition: dsdv-manet.cc:45
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:849
#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
pointToPoint
Definition: first.py:35
Every class exported by the ns3 library is enclosed in the ns3 namespace.
cmd
Definition: second.py:35