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