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-animation.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
(
"StarAnimation"
);
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
std::string
animFile
=
"star-animation.xml"
;
45
uint8_t
useIpv6
= 0;
46
Ipv6Address
ipv6AddressBase
=
Ipv6Address
(
"2001::"
);
47
Ipv6Prefix
ipv6AddressPrefix
=
Ipv6Prefix
(64);
48
49
CommandLine
cmd
(
__FILE__
);
50
cmd
.AddValue(
"nSpokes"
,
"Number of spoke nodes to place in the star"
,
nSpokes
);
51
cmd
.AddValue(
"animFile"
,
"File Name for Animation Output"
,
animFile
);
52
cmd
.AddValue(
"useIpv6"
,
"use Ipv6"
,
useIpv6
);
53
54
cmd
.Parse(
argc
,
argv
);
55
56
NS_LOG_INFO
(
"Build star topology."
);
57
PointToPointHelper
pointToPoint
;
58
pointToPoint
.SetDeviceAttribute(
"DataRate"
,
StringValue
(
"5Mbps"
));
59
pointToPoint
.SetChannelAttribute(
"Delay"
,
StringValue
(
"2ms"
));
60
PointToPointStarHelper
star
(
nSpokes
, pointToPoint);
61
62
NS_LOG_INFO
(
"Install internet stack on all nodes."
);
63
InternetStackHelper
internet
;
64
star
.InstallStack(internet);
65
66
NS_LOG_INFO
(
"Assign IP Addresses."
);
67
if
(
useIpv6
== 0)
68
{
69
star
.AssignIpv4Addresses(
Ipv4AddressHelper
(
"10.1.1.0"
,
"255.255.255.0"
));
70
}
71
else
72
{
73
star
.AssignIpv6Addresses(
ipv6AddressBase
,
ipv6AddressPrefix
);
74
}
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
;
82
if
(
useIpv6
== 0)
83
{
84
hubLocalAddress
=
InetSocketAddress
(
Ipv4Address::GetAny
(),
port
);
85
}
86
else
87
{
88
hubLocalAddress
=
Inet6SocketAddress
(
Ipv6Address::GetAny
(),
port
);
89
}
90
PacketSinkHelper
packetSinkHelper
(
"ns3::TcpSocketFactory"
,
hubLocalAddress
);
91
ApplicationContainer
hubApp
=
packetSinkHelper
.Install(
star
.GetHub());
92
hubApp
.Start(
Seconds
(1));
93
hubApp
.Stop(
Seconds
(10));
94
95
//
96
// Create OnOff applications to send TCP to the hub, one on each spoke node.
97
//
98
OnOffHelper
onOffHelper
(
"ns3::TcpSocketFactory"
,
Address
());
99
onOffHelper
.SetAttribute(
"OnTime"
,
StringValue
(
"ns3::ConstantRandomVariable[Constant=1]"
));
100
onOffHelper
.SetAttribute(
"OffTime"
,
StringValue
(
"ns3::ConstantRandomVariable[Constant=0]"
));
101
102
ApplicationContainer
spokeApps
;
103
104
for
(
uint32_t
i
= 0;
i
<
star
.SpokeCount(); ++
i
)
105
{
106
AddressValue
remoteAddress
;
107
if
(
useIpv6
== 0)
108
{
109
remoteAddress
=
AddressValue
(
InetSocketAddress
(
star
.GetHubIpv4Address(
i
),
port
));
110
}
111
else
112
{
113
remoteAddress
=
AddressValue
(
Inet6SocketAddress
(
star
.GetHubIpv6Address(
i
),
port
));
114
}
115
onOffHelper
.SetAttribute(
"Remote"
, remoteAddress);
116
spokeApps
.Add(
onOffHelper
.Install(
star
.GetSpokeNode(
i
)));
117
}
118
spokeApps
.Start(
Seconds
(1));
119
spokeApps
.Stop(
Seconds
(10));
120
121
NS_LOG_INFO
(
"Enable static global routing."
);
122
//
123
// Turn on global static routing so we can actually be routed across the star.
124
//
125
if
(
useIpv6
== 0)
126
{
127
Ipv4GlobalRoutingHelper::PopulateRoutingTables
();
128
}
129
130
// Set the bounding box for animation
131
star
.BoundingBox(1, 1, 100, 100);
132
133
// Create the animation object and configure for specified output
134
AnimationInterface
anim
(
animFile
);
135
136
NS_LOG_INFO
(
"Run Simulation."
);
137
Simulator::Run
();
138
Simulator::Destroy
();
139
NS_LOG_INFO
(
"Done."
);
140
141
return
0;
142
}
ns3::Address
a polymophic address class
Definition
address.h:90
ns3::AddressValue
AttributeValue implementation for Address.
Definition
address.h:275
ns3::AnimationInterface
Interface to network animator.
Definition
animation-interface.h:77
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::Inet6SocketAddress
An Inet6 address class.
Definition
inet6-socket-address.h:27
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::Ipv6Address
Describes an IPv6 address.
Definition
ipv6-address.h:38
ns3::Ipv6Address::GetAny
static Ipv6Address GetAny()
Get the "any" (::) Ipv6Address.
Definition
ipv6-address.cc:728
ns3::Ipv6Prefix
Describes an IPv6 prefix.
Definition
ipv6-address.h:444
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
anim
AnimationInterface * anim
Definition
lr-wpan-bootstrap.cc:45
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
src
netanim
examples
star-animation.cc
Generated on Mon Dec 15 2025 15:22:00 for ns-3 by
1.9.8