A Discrete-Event Network Simulator
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
25using namespace ns3;
26
27NS_LOG_COMPONENT_DEFINE("StarAnimation");
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 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.");
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.");
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.0));
93 hubApp.Stop(Seconds(10.0));
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 {
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.0));
119 spokeApps.Stop(Seconds(10.0));
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 {
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.");
139 NS_LOG_INFO("Done.");
140
141 return 0;
142}
a polymophic address class
Definition address.h:90
Interface to network animator.
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 Inet6 address class.
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.
Describes an IPv6 address.
static Ipv6Address GetAny()
Get the "any" (::) Ipv6Address.
Describes an IPv6 prefix.
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
AnimationInterface * anim
pointToPoint
Definition first.py:27
Every class exported by the ns3 library is enclosed in the ns3 namespace.