A Discrete-Event Network Simulator
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
25using namespace ns3;
26
28
29int
30main(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.");
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.");
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;
68 PacketSinkHelper packetSinkHelper("ns3::TcpSocketFactory", hubLocalAddress);
69 ApplicationContainer hubApp = packetSinkHelper.Install(star.GetHub());
70 hubApp.Start(Seconds(1.0));
71 hubApp.Stop(Seconds(10.0));
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.0));
89 spokeApps.Stop(Seconds(10.0));
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 //
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.");
106 NS_LOG_INFO("Done.");
107
108 return 0;
109}
a polymophic address class
Definition address.h:90
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.
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.
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:131
static void Run()
Run the simulation.
Definition simulator.cc:167
Hold variables of type string.
Definition string.h:45
Hold an unsigned integer type.
Definition uinteger.h:34
uint16_t port
Definition dsdv-manet.cc:33
void SetDefault(std::string name, const AttributeValue &value)
Definition config.cc:883
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition log.h:264
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
pointToPoint
Definition first.py:27
Every class exported by the ns3 library is enclosed in the ns3 namespace.