A Discrete-Event Network Simulator
API
star-animation.cc
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License version 2 as
4  * published by the Free Software Foundation;
5  *
6  * This program is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9  * GNU General Public License for more details.
10  *
11  * You should have received a copy of the GNU General Public License
12  * along with this program; if not, write to the Free Software
13  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14  *
15  */
16 
17 #include "ns3/applications-module.h"
18 #include "ns3/core-module.h"
19 #include "ns3/internet-module.h"
20 #include "ns3/netanim-module.h"
21 #include "ns3/network-module.h"
22 #include "ns3/point-to-point-layout-module.h"
23 #include "ns3/point-to-point-module.h"
24 
25 // Network topology (default)
26 //
27 // n2 n3 n4 .
28 // \ | / .
29 // \|/ .
30 // n1--- n0---n5 .
31 // /|\ .
32 // / | \ .
33 // n8 n7 n6 .
34 //
35 
36 using namespace ns3;
37 
38 NS_LOG_COMPONENT_DEFINE("StarAnimation");
39 
40 int
41 main(int argc, char* argv[])
42 {
43  //
44  // Set up some default values for the simulation.
45  //
46  Config::SetDefault("ns3::OnOffApplication::PacketSize", UintegerValue(137));
47 
48  // ??? try and stick 15kb/s into the data rate
49  Config::SetDefault("ns3::OnOffApplication::DataRate", StringValue("14kb/s"));
50 
51  //
52  // Default number of nodes in the star. Overridable by command line argument.
53  //
54  uint32_t nSpokes = 8;
55  std::string animFile = "star-animation.xml";
56  uint8_t useIpv6 = 0;
57  Ipv6Address ipv6AddressBase = Ipv6Address("2001::");
58  Ipv6Prefix ipv6AddressPrefix = Ipv6Prefix(64);
59 
60  CommandLine cmd(__FILE__);
61  cmd.AddValue("nSpokes", "Number of spoke nodes to place in the star", nSpokes);
62  cmd.AddValue("animFile", "File Name for Animation Output", animFile);
63  cmd.AddValue("useIpv6", "use Ipv6", useIpv6);
64 
65  cmd.Parse(argc, argv);
66 
67  NS_LOG_INFO("Build star topology.");
69  pointToPoint.SetDeviceAttribute("DataRate", StringValue("5Mbps"));
70  pointToPoint.SetChannelAttribute("Delay", StringValue("2ms"));
71  PointToPointStarHelper star(nSpokes, pointToPoint);
72 
73  NS_LOG_INFO("Install internet stack on all nodes.");
75  star.InstallStack(internet);
76 
77  NS_LOG_INFO("Assign IP Addresses.");
78  if (useIpv6 == 0)
79  {
80  star.AssignIpv4Addresses(Ipv4AddressHelper("10.1.1.0", "255.255.255.0"));
81  }
82  else
83  {
84  star.AssignIpv6Addresses(ipv6AddressBase, ipv6AddressPrefix);
85  }
86 
87  NS_LOG_INFO("Create applications.");
88  //
89  // Create a packet sink on the star "hub" to receive packets.
90  //
91  uint16_t port = 50000;
92  Address hubLocalAddress;
93  if (useIpv6 == 0)
94  {
95  hubLocalAddress = InetSocketAddress(Ipv4Address::GetAny(), port);
96  }
97  else
98  {
99  hubLocalAddress = Inet6SocketAddress(Ipv6Address::GetAny(), port);
100  }
101  PacketSinkHelper packetSinkHelper("ns3::TcpSocketFactory", hubLocalAddress);
102  ApplicationContainer hubApp = packetSinkHelper.Install(star.GetHub());
103  hubApp.Start(Seconds(1.0));
104  hubApp.Stop(Seconds(10.0));
105 
106  //
107  // Create OnOff applications to send TCP to the hub, one on each spoke node.
108  //
109  OnOffHelper onOffHelper("ns3::TcpSocketFactory", Address());
110  onOffHelper.SetAttribute("OnTime", StringValue("ns3::ConstantRandomVariable[Constant=1]"));
111  onOffHelper.SetAttribute("OffTime", StringValue("ns3::ConstantRandomVariable[Constant=0]"));
112 
113  ApplicationContainer spokeApps;
114 
115  for (uint32_t i = 0; i < star.SpokeCount(); ++i)
116  {
117  AddressValue remoteAddress;
118  if (useIpv6 == 0)
119  {
120  remoteAddress = AddressValue(InetSocketAddress(star.GetHubIpv4Address(i), port));
121  }
122  else
123  {
124  remoteAddress = AddressValue(Inet6SocketAddress(star.GetHubIpv6Address(i), port));
125  }
126  onOffHelper.SetAttribute("Remote", remoteAddress);
127  spokeApps.Add(onOffHelper.Install(star.GetSpokeNode(i)));
128  }
129  spokeApps.Start(Seconds(1.0));
130  spokeApps.Stop(Seconds(10.0));
131 
132  NS_LOG_INFO("Enable static global routing.");
133  //
134  // Turn on global static routing so we can actually be routed across the star.
135  //
136  if (useIpv6 == 0)
137  {
139  }
140 
141  // Set the bounding box for animation
142  star.BoundingBox(1, 1, 100, 100);
143 
144  // Create the animation object and configure for specified output
145  AnimationInterface anim(animFile);
146 
147  NS_LOG_INFO("Run Simulation.");
148  Simulator::Run();
150  NS_LOG_INFO("Done.");
151 
152  return 0;
153 }
a polymophic address class
Definition: address.h:101
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.
Definition: command-line.h:232
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.
Definition: ipv6-address.h:49
static Ipv6Address GetAny()
Get the "any" (::) Ipv6Address.
Describes an IPv6 prefix.
Definition: ipv6-address.h:455
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:44
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:142
static void Run()
Run the simulation.
Definition: simulator.cc:178
Hold variables of type string.
Definition: string.h:56
Hold an unsigned integer type.
Definition: uinteger.h:45
uint16_t port
Definition: dsdv-manet.cc:44
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:890
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:275
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
AnimationInterface * anim
pointToPoint
Definition: first.py:38
Every class exported by the ns3 library is enclosed in the ns3 namespace.
cmd
Definition: second.py:40