A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Docs ▼
Wiki
Manual
Models
Develop ▼
API
Bugs
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
40
NS_LOG_COMPONENT_DEFINE
(
"Star"
);
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."
);
64
PointToPointHelper
pointToPoint
;
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
//
109
Ipv4GlobalRoutingHelper::PopulateRoutingTables
();
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
();
119
Simulator::Destroy
();
120
NS_LOG_INFO
(
"Done."
);
121
122
return
0;
123
}
NS_LOG_COMPONENT_DEFINE
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition:
log.h:205
ns3::InetSocketAddress
an Inet address class
Definition:
inet-socket-address.h:41
ns3::CommandLine
Parse command-line arguments.
Definition:
command-line.h:228
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::ApplicationContainer::Stop
void Stop(Time stop)
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter.
Definition:
application-container.cc:107
ns3::Ipv4AddressHelper
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
Definition:
ipv4-address-helper.h:48
ns3::AddressValue
AttributeValue implementation for Address.
Definition:
address.h:278
ns3::ApplicationContainer::Add
void Add(ApplicationContainer other)
Append the contents of another ApplicationContainer to the end of this container.
Definition:
application-container.cc:67
ns3::Ipv4GlobalRoutingHelper::PopulateRoutingTables
static void PopulateRoutingTables(void)
Build a routing database and initialize the routing tables of the nodes in the simulation.
Definition:
ipv4-global-routing-helper.cc:61
ns3::Address
a polymophic address class
Definition:
address.h:91
ns3::OnOffHelper
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition:
on-off-helper.h:43
NS_LOG_INFO
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition:
log.h:281
second.cmd
cmd
Definition:
second.py:35
ns3::Simulator::Run
static void Run(void)
Run the simulation.
Definition:
simulator.cc:172
ns3::StringValue
Hold variables of type string.
Definition:
string.h:41
ns3::PacketSinkHelper
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes.
Definition:
packet-sink-helper.h:36
ns3::Ipv4Address::GetAny
static Ipv4Address GetAny(void)
Definition:
ipv4-address.cc:395
ns3::ApplicationContainer::Start
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter.
Definition:
application-container.cc:87
ns3::Seconds
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition:
nstime.h:1289
ns3::Simulator::Destroy
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition:
simulator.cc:136
ns3::ApplicationContainer
holds a vector of ns3::Application pointers.
Definition:
application-container.h:43
first.pointToPoint
pointToPoint
Definition:
first.py:35
ns3::PointToPointHelper
Build a set of PointToPointNetDevice objects.
Definition:
point-to-point-helper.h:45
ns3::UintegerValue
Hold an unsigned integer type.
Definition:
uinteger.h:44
ns3::Config::SetDefault
void SetDefault(std::string name, const AttributeValue &value)
Definition:
config.cc:849
ns3::PointToPointStarHelper
A helper to make it easier to create a star topology with PointToPoint links.
Definition:
point-to-point-star.h:45
ns3::InternetStackHelper
aggregate IP/TCP/UDP functionality to existing Nodes.
Definition:
internet-stack-helper.h:88
port
uint16_t port
Definition:
dsdv-manet.cc:45
examples
tcp
star.cc
Generated on Fri Oct 1 2021 17:02:52 for ns-3 by
1.8.20