A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Documentation ▼
Installation
Manual
Models
Contributing
Wiki
Development ▼
API Docs
Issue Tracker
Merge Requests
API
Loading...
Searching...
No Matches
star.cc
Go to the documentation of this file.
1
/*
2
* SPDX-License-Identifier: GPL-2.0-only
3
*
4
*/
5
6
#include "ns3/applications-module.h"
7
#include "ns3/core-module.h"
8
#include "ns3/internet-module.h"
9
#include "ns3/netanim-module.h"
10
#include "ns3/network-module.h"
11
#include "ns3/point-to-point-layout-module.h"
12
#include "ns3/point-to-point-module.h"
13
14
// Network topology (default)
15
//
16
// n2 n3 n4 .
17
// \ | / .
18
// \|/ .
19
// n1--- n0---n5 .
20
// /|\ .
21
// / | \ .
22
// n8 n7 n6 .
23
//
24
25
using namespace
ns3
;
26
27
NS_LOG_COMPONENT_DEFINE
(
"Star"
);
28
29
int
30
main(
int
argc
,
char
*
argv
[])
31
{
32
//
33
// Set up some default values for the simulation.
34
//
35
Config::SetDefault
(
"ns3::OnOffApplication::PacketSize"
,
UintegerValue
(137));
36
37
// ??? try and stick 15kb/s into the data rate
38
Config::SetDefault
(
"ns3::OnOffApplication::DataRate"
,
StringValue
(
"14kb/s"
));
39
40
//
41
// Default number of nodes in the star. Overridable by command line argument.
42
//
43
uint32_t
nSpokes
= 8;
44
45
CommandLine
cmd
(
__FILE__
);
46
cmd
.AddValue(
"nSpokes"
,
"Number of nodes to place in the star"
,
nSpokes
);
47
cmd
.Parse(
argc
,
argv
);
48
49
NS_LOG_INFO
(
"Build star topology."
);
50
PointToPointHelper
pointToPoint
;
51
pointToPoint
.SetDeviceAttribute(
"DataRate"
,
StringValue
(
"5Mbps"
));
52
pointToPoint
.SetChannelAttribute(
"Delay"
,
StringValue
(
"2ms"
));
53
PointToPointStarHelper
star
(
nSpokes
, pointToPoint);
54
55
NS_LOG_INFO
(
"Install internet stack on all nodes."
);
56
InternetStackHelper
internet
;
57
star
.InstallStack(internet);
58
59
NS_LOG_INFO
(
"Assign IP Addresses."
);
60
star
.AssignIpv4Addresses(
Ipv4AddressHelper
(
"10.1.1.0"
,
"255.255.255.0"
));
61
62
NS_LOG_INFO
(
"Create applications."
);
63
//
64
// Create a packet sink on the star "hub" to receive packets.
65
//
66
uint16_t
port
= 50000;
67
Address
hubLocalAddress
(
InetSocketAddress
(
Ipv4Address::GetAny
(),
port
));
68
PacketSinkHelper
packetSinkHelper
(
"ns3::TcpSocketFactory"
,
hubLocalAddress
);
69
ApplicationContainer
hubApp
=
packetSinkHelper
.Install(
star
.GetHub());
70
hubApp
.Start(
Seconds
(1));
71
hubApp
.Stop(
Seconds
(10));
72
73
//
74
// Create OnOff applications to send TCP to the hub, one on each spoke node.
75
//
76
OnOffHelper
onOffHelper
(
"ns3::TcpSocketFactory"
,
Address
());
77
onOffHelper
.SetAttribute(
"OnTime"
,
StringValue
(
"ns3::ConstantRandomVariable[Constant=1]"
));
78
onOffHelper
.SetAttribute(
"OffTime"
,
StringValue
(
"ns3::ConstantRandomVariable[Constant=0]"
));
79
80
ApplicationContainer
spokeApps
;
81
82
for
(
uint32_t
i
= 0;
i
<
star
.SpokeCount(); ++
i
)
83
{
84
AddressValue
remoteAddress
(
InetSocketAddress
(
star
.GetHubIpv4Address(
i
),
port
));
85
onOffHelper
.SetAttribute(
"Remote"
, remoteAddress);
86
spokeApps
.Add(
onOffHelper
.Install(
star
.GetSpokeNode(
i
)));
87
}
88
spokeApps
.Start(
Seconds
(1));
89
spokeApps
.Stop(
Seconds
(10));
90
91
NS_LOG_INFO
(
"Enable static global routing."
);
92
//
93
// Turn on global static routing so we can actually be routed across the star.
94
//
95
Ipv4GlobalRoutingHelper::PopulateRoutingTables
();
96
97
NS_LOG_INFO
(
"Enable pcap tracing."
);
98
//
99
// Do pcap tracing on all point-to-point devices on all nodes.
100
//
101
pointToPoint
.EnablePcapAll(
"star"
);
102
103
NS_LOG_INFO
(
"Run Simulation."
);
104
Simulator::Run
();
105
Simulator::Destroy
();
106
NS_LOG_INFO
(
"Done."
);
107
108
return
0;
109
}
ns3::Address
a polymophic address class
Definition
address.h:90
ns3::AddressValue
AttributeValue implementation for Address.
Definition
address.h:275
ns3::ApplicationContainer
holds a vector of ns3::Application pointers.
Definition
application-container.h:33
ns3::CommandLine
Parse command-line arguments.
Definition
command-line.h:221
ns3::InetSocketAddress
an Inet address class
Definition
inet-socket-address.h:31
ns3::InternetStackHelper
aggregate IP/TCP/UDP functionality to existing Nodes.
Definition
internet-stack-helper.h:81
ns3::Ipv4AddressHelper
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
Definition
ipv4-address-helper.h:38
ns3::Ipv4Address::GetAny
static Ipv4Address GetAny()
Definition
ipv4-address.cc:368
ns3::Ipv4GlobalRoutingHelper::PopulateRoutingTables
static void PopulateRoutingTables()
Build a routing database and initialize the routing tables of the nodes in the simulation.
Definition
ipv4-global-routing-helper.cc:50
ns3::OnOffHelper
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition
on-off-helper.h:26
ns3::PacketSinkHelper
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes.
Definition
packet-sink-helper.h:23
ns3::PointToPointHelper
Build a set of PointToPointNetDevice objects.
Definition
point-to-point-helper.h:33
ns3::PointToPointStarHelper
A helper to make it easier to create a star topology with PointToPoint links.
Definition
point-to-point-star.h:34
ns3::Simulator::Destroy
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition
simulator.cc:131
ns3::Simulator::Run
static void Run()
Run the simulation.
Definition
simulator.cc:167
ns3::StringValue
Hold variables of type string.
Definition
string.h:45
ns3::UintegerValue
Hold an unsigned integer type.
Definition
uinteger.h:34
uint32_t
port
uint16_t port
Definition
dsdv-manet.cc:33
ns3::Config::SetDefault
void SetDefault(std::string name, const AttributeValue &value)
Definition
config.cc:883
NS_LOG_COMPONENT_DEFINE
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition
log.h:191
NS_LOG_INFO
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition
log.h:264
ns3::Create
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition
ptr.h:436
ns3::Seconds
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition
nstime.h:1344
first.pointToPoint
pointToPoint
Definition
first.py:27
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
nsclick-simple-lan.remoteAddress
remoteAddress
Definition
nsclick-simple-lan.py:63
nsclick-simple-lan.onOffHelper
onOffHelper
Definition
nsclick-simple-lan.py:57
nsclick-simple-lan.internet
internet
Definition
nsclick-simple-lan.py:34
nsclick-simple-lan.packetSinkHelper
packetSinkHelper
Definition
nsclick-simple-lan.py:52
second.cmd
cmd
Definition
second.py:29
examples
tcp
star.cc
Generated on Mon Dec 15 2025 15:21:47 for ns-3 by
1.9.8