A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
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 }
holds a vector of ns3::Application pointers.
tuple pointToPoint
Definition: first.py:28
an Inet address class
static Ipv4Address GetAny(void)
static void PopulateRoutingTables(void)
Build a routing database and initialize the routing tables of the nodes in the simulation.
Hold variables of type string.
Definition: string.h:41
void Add(ApplicationContainer other)
Append the contents of another ApplicationContainer to the end of this container. ...
A helper to make it easier to create a star topology with PointToPoint links.
static void Run(void)
Run the simulation.
Definition: simulator.cc:200
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
aggregate IP/TCP/UDP functionality to existing Nodes.
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:244
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.
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:42
tuple cmd
Definition: second.py:35
uint16_t port
Definition: dsdv-manet.cc:44
a polymophic address class
Definition: address.h:90
void EnablePcapAll(std::string prefix, bool promiscuous=false)
Enable pcap output on each device (which is of the appropriate type) in the set of all nodes created ...
Hold an unsigned integer type.
Definition: uinteger.h:44
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter...
Parse command-line arguments.
Definition: command-line.h:201
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:164
void SetChannelAttribute(std::string name, const AttributeValue &value)
Set an attribute value to be propagated to each Channel created by the helper.
AttributeValue implementation for Address.
void Stop(Time stop)
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter...
void AddValue(const std::string &name, const std::string &help, T &value)
Add a program argument, assigning to POD.
Definition: command-line.h:491
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:895
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:774
void Parse(int argc, char *argv[])
Parse the program arguments.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.