A Discrete-Event Network Simulator
API
star-animation.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 ("StarAnimation");
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  std::string animFile = "star-animation.xml";
59  uint8_t useIpv6 = 0;
60  Ipv6Address ipv6AddressBase = Ipv6Address("2001::");
61  Ipv6Prefix ipv6AddressPrefix = Ipv6Prefix(64);
62 
64  cmd.AddValue ("nSpokes", "Number of spoke nodes to place in the star", nSpokes);
65  cmd.AddValue ("animFile", "File Name for Animation Output", animFile);
66  cmd.AddValue ("useIpv6", "use Ipv6", useIpv6);
67 
68  cmd.Parse (argc, argv);
69 
70  NS_LOG_INFO ("Build star topology.");
72  pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
73  pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));
74  PointToPointStarHelper star (nSpokes, pointToPoint);
75 
76  NS_LOG_INFO ("Install internet stack on all nodes.");
77  InternetStackHelper internet;
78  star.InstallStack (internet);
79 
80  NS_LOG_INFO ("Assign IP Addresses.");
81  if (useIpv6 == 0)
82  {
83  star.AssignIpv4Addresses (Ipv4AddressHelper ("10.1.1.0", "255.255.255.0"));
84  }
85  else
86  {
87  star.AssignIpv6Addresses (ipv6AddressBase, ipv6AddressPrefix);
88  }
89 
90  NS_LOG_INFO ("Create applications.");
91  //
92  // Create a packet sink on the star "hub" to receive packets.
93  //
94  uint16_t port = 50000;
95  Address hubLocalAddress;
96  if (useIpv6 == 0)
97  {
98  hubLocalAddress = InetSocketAddress (Ipv4Address::GetAny (), port);
99  }
100  else
101  {
102  hubLocalAddress = Inet6SocketAddress (Ipv6Address::GetAny (), port);
103  }
104  PacketSinkHelper packetSinkHelper ("ns3::TcpSocketFactory", hubLocalAddress);
105  ApplicationContainer hubApp = packetSinkHelper.Install (star.GetHub ());
106  hubApp.Start (Seconds (1.0));
107  hubApp.Stop (Seconds (10.0));
108 
109  //
110  // Create OnOff applications to send TCP to the hub, one on each spoke node.
111  //
112  OnOffHelper onOffHelper ("ns3::TcpSocketFactory", Address ());
113  onOffHelper.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1]"));
114  onOffHelper.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0]"));
115 
116  ApplicationContainer spokeApps;
117 
118  for (uint32_t i = 0; i < star.SpokeCount (); ++i)
119  {
120  AddressValue remoteAddress;
121  if (useIpv6 == 0)
122  {
123  remoteAddress = AddressValue(InetSocketAddress (star.GetHubIpv4Address (i), port));
124  }
125  else
126  {
127  remoteAddress = AddressValue(Inet6SocketAddress (star.GetHubIpv6Address (i), port));
128  }
129  onOffHelper.SetAttribute ("Remote", remoteAddress);
130  spokeApps.Add (onOffHelper.Install (star.GetSpokeNode (i)));
131  }
132  spokeApps.Start (Seconds (1.0));
133  spokeApps.Stop (Seconds (10.0));
134 
135  NS_LOG_INFO ("Enable static global routing.");
136  //
137  // Turn on global static routing so we can actually be routed across the star.
138  //
139  if (useIpv6 == 0)
140  {
142  }
143 
144  // Set the bounding box for animation
145  star.BoundingBox (1, 1, 100, 100);
146 
147  // Create the animation object and configure for specified output
148  AnimationInterface anim (animFile);
149 
150  NS_LOG_INFO ("Run Simulation.");
151  Simulator::Run ();
153  NS_LOG_INFO ("Done.");
154 
155  return 0;
156 }
holds a vector of ns3::Application pointers.
tuple pointToPoint
Definition: first.py:28
an Inet address class
static Ipv4Address GetAny(void)
static void PopulateRoutingTables(void)
Build a routing database and initialize the routing tables of the nodes in the simulation.
Hold variables of type string.
Definition: string.h:41
void Add(ApplicationContainer other)
Append the contents of another ApplicationContainer to the end of this container. ...
A helper to make it easier to create a star topology with PointToPoint links.
static void Run(void)
Run the simulation.
Definition: simulator.cc:226
static Ipv6Address GetAny()
Get the "any" (::) Ipv6Address.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
aggregate IP/TCP/UDP functionality to existing Nodes.
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:277
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes...
Build a set of PointToPointNetDevice objects.
void SetDeviceAttribute(std::string name, const AttributeValue &value)
Set an attribute value to be propagated to each NetDevice created by the helper.
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:42
tuple cmd
Definition: second.py:35
uint16_t port
Definition: dsdv-manet.cc:44
a polymophic address class
Definition: address.h:90
Hold an unsigned integer type.
Definition: uinteger.h:44
An Inet6 address class.
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter...
Parse command-line arguments.
Definition: command-line.h:205
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:190
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void SetChannelAttribute(std::string name, const AttributeValue &value)
Set an attribute value to be propagated to each Channel created by the helper.
Describes an IPv6 address.
Definition: ipv6-address.h:48
AttributeValue implementation for Address.
Definition: address.h:278
void Stop(Time stop)
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter...
void AddValue(const std::string &name, const std::string &help, T &value)
Add a program argument, assigning to POD.
Definition: command-line.h:498
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:993
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:782
Interface to network animator.
Describes an IPv6 prefix.
Definition: ipv6-address.h:394
void Parse(int argc, char *argv[])
Parse the program arguments.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.