A Discrete-Event Network Simulator
API
tcp-star-server.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 
19 // Default Network topology, 9 nodes in a star
20 /*
21  n2 n3 n4
22  \ | /
23  \|/
24  n1---n0---n5
25  /| \
26  / | \
27  n8 n7 n6
28 */
29 // - CBR Traffic goes from the star "arms" to the "hub"
30 // - Tracing of queues and packet receptions to file
31 // "tcp-star-server.tr"
32 // - pcap traces also generated in the following files
33 // "tcp-star-server-$n-$i.pcap" where n and i represent node and interface
34 // numbers respectively
35 // Usage examples for things you might want to tweak:
36 // ./waf --run="tcp-star-server"
37 // ./waf --run="tcp-star-server --nNodes=25"
38 // ./waf --run="tcp-star-server --ns3::OnOffApplication::DataRate=10000"
39 // ./waf --run="tcp-star-server --ns3::OnOffApplication::PacketSize=500"
40 // See the ns-3 tutorial for more info on the command line:
41 // http://www.nsnam.org/tutorials.html
42 
43 
44 
45 
46 #include <iostream>
47 #include <fstream>
48 #include <string>
49 #include <cassert>
50 
51 #include "ns3/core-module.h"
52 #include "ns3/network-module.h"
53 #include "ns3/internet-module.h"
54 #include "ns3/point-to-point-module.h"
55 #include "ns3/applications-module.h"
56 #include "ns3/ipv4-global-routing-helper.h"
57 
58 using namespace ns3;
59 
60 NS_LOG_COMPONENT_DEFINE ("TcpServer");
61 
62 int
63 main (int argc, char *argv[])
64 {
65  // Users may find it convenient to turn on explicit debugging
66  // for selected modules; the below lines suggest how to do this
67 
68  //LogComponentEnable ("TcpServer", LOG_LEVEL_INFO);
69  //LogComponentEnable ("TcpL4Protocol", LOG_LEVEL_ALL);
70  //LogComponentEnable ("TcpSocketImpl", LOG_LEVEL_ALL);
71  //LogComponentEnable ("PacketSink", LOG_LEVEL_ALL);
72 
73  // Set up some default values for the simulation.
74  Config::SetDefault ("ns3::OnOffApplication::PacketSize", UintegerValue (250));
75  Config::SetDefault ("ns3::OnOffApplication::DataRate", StringValue ("5kb/s"));
76  uint32_t N = 9; //number of nodes in the star
77 
78  // Allow the user to override any of the defaults and the above
79  // Config::SetDefault()s at run-time, via command-line arguments
80  CommandLine cmd (__FILE__);
81  cmd.AddValue ("nNodes", "Number of nodes to place in the star", N);
82  cmd.Parse (argc, argv);
83 
84  // Here, we will create N nodes in a star.
85  NS_LOG_INFO ("Create nodes.");
86  NodeContainer serverNode;
87  NodeContainer clientNodes;
88  serverNode.Create (1);
89  clientNodes.Create (N-1);
90  NodeContainer allNodes = NodeContainer (serverNode, clientNodes);
91 
92  // Install network stacks on the nodes
93  InternetStackHelper internet;
94  internet.Install (allNodes);
95 
96  //Collect an adjacency list of nodes for the p2p topology
97  std::vector<NodeContainer> nodeAdjacencyList (N-1);
98  for(uint32_t i=0; i<nodeAdjacencyList.size (); ++i)
99  {
100  nodeAdjacencyList[i] = NodeContainer (serverNode, clientNodes.Get (i));
101  }
102 
103  // We create the channels first without any IP addressing information
104  NS_LOG_INFO ("Create channels.");
105  PointToPointHelper p2p;
106  p2p.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
107  p2p.SetChannelAttribute ("Delay", StringValue ("2ms"));
108  std::vector<NetDeviceContainer> deviceAdjacencyList (N-1);
109  for(uint32_t i=0; i<deviceAdjacencyList.size (); ++i)
110  {
111  deviceAdjacencyList[i] = p2p.Install (nodeAdjacencyList[i]);
112  }
113 
114  // Later, we add IP addresses.
115  NS_LOG_INFO ("Assign IP Addresses.");
116  Ipv4AddressHelper ipv4;
117  std::vector<Ipv4InterfaceContainer> interfaceAdjacencyList (N-1);
118  for(uint32_t i=0; i<interfaceAdjacencyList.size (); ++i)
119  {
120  std::ostringstream subnet;
121  subnet<<"10.1."<<i+1<<".0";
122  ipv4.SetBase (subnet.str ().c_str (), "255.255.255.0");
123  interfaceAdjacencyList[i] = ipv4.Assign (deviceAdjacencyList[i]);
124  }
125 
126  //Turn on global static routing
128 
129  // Create a packet sink on the star "hub" to receive these packets
130  uint16_t port = 50000;
131  Address sinkLocalAddress (InetSocketAddress (Ipv4Address::GetAny (), port));
132  PacketSinkHelper sinkHelper ("ns3::TcpSocketFactory", sinkLocalAddress);
133  ApplicationContainer sinkApp = sinkHelper.Install (serverNode);
134  sinkApp.Start (Seconds (1.0));
135  sinkApp.Stop (Seconds (10.0));
136 
137  // Create the OnOff applications to send TCP to the server
138  OnOffHelper clientHelper ("ns3::TcpSocketFactory", Address ());
139  clientHelper.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1]"));
140  clientHelper.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0]"));
141 
142  //normally wouldn't need a loop here but the server IP address is different
143  //on each p2p subnet
145  for(uint32_t i=0; i<clientNodes.GetN (); ++i)
146  {
147  AddressValue remoteAddress
148  (InetSocketAddress (interfaceAdjacencyList[i].GetAddress (0), port));
149  clientHelper.SetAttribute ("Remote", remoteAddress);
150  clientApps.Add (clientHelper.Install (clientNodes.Get (i)));
151  }
152  clientApps.Start (Seconds (1.0));
153  clientApps.Stop (Seconds (10.0));
154 
155 
156  //configure tracing
157  AsciiTraceHelper ascii;
158  p2p.EnableAsciiAll (ascii.CreateFileStream ("tcp-star-server.tr"));
159  p2p.EnablePcapAll ("tcp-star-server");
160 
161  NS_LOG_INFO ("Run Simulation.");
162  Simulator::Run ();
164  NS_LOG_INFO ("Done.");
165 
166  return 0;
167 }
holds a vector of ns3::Application pointers.
Manage ASCII trace files for device models.
Definition: trace-helper.h:161
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
NetDeviceContainer Install(NodeContainer c)
static void Run(void)
Run the simulation.
Definition: simulator.cc:172
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
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:281
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes...
cmd
Definition: second.py:35
Ptr< OutputStreamWrapper > CreateFileStream(std::string filename, std::ios::openmode filemode=std::ios::out)
Create and initialize an output stream object we&#39;ll use to write the traced bits. ...
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
uint16_t port
Definition: dsdv-manet.cc:45
a polymophic address class
Definition: address.h:90
void EnablePcapAll(std::string prefix, bool promiscuous=false)
Enable pcap output on each device (which is of the appropriate type) in the set of all nodes created ...
Hold an unsigned integer type.
Definition: uinteger.h:44
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:226
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:136
uint32_t GetN(void) const
Get the number of Ptr<Node> stored in this container.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
keep track of a set of node pointers.
void SetChannelAttribute(std::string name, const AttributeValue &value)
Set an attribute value to be propagated to each Channel created by the helper.
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
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...
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1288
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:849
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
clientApps
Definition: first.py:61
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
void EnableAsciiAll(std::string prefix)
Enable ascii trace output on each device (which is of the appropriate type) in the set of all nodes c...
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.