author | Tom Henderson <tomh@tomh.org> |
Thu, 02 Jul 2009 21:57:00 -0700 | |
changeset 4646 | c25ca2e38845 |
parent 4616 | a84f60b6cd12 |
permissions | -rw-r--r-- |
3366 | 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/simulator-module.h" |
|
53 |
#include "ns3/node-module.h" |
|
54 |
#include "ns3/helper-module.h" |
|
55 |
||
56 |
using namespace ns3; |
|
57 |
||
58 |
NS_LOG_COMPONENT_DEFINE ("TcpServer"); |
|
59 |
||
60 |
int |
|
61 |
main (int argc, char *argv[]) |
|
62 |
{ |
|
63 |
// Users may find it convenient to turn on explicit debugging |
|
64 |
// for selected modules; the below lines suggest how to do this |
|
65 |
||
66 |
//LogComponentEnable ("TcpServer", LOG_LEVEL_INFO); |
|
67 |
//LogComponentEnable ("TcpL4Protocol", LOG_LEVEL_ALL); |
|
68 |
//LogComponentEnable ("TcpSocketImpl", LOG_LEVEL_ALL); |
|
69 |
//LogComponentEnable ("PacketSink", LOG_LEVEL_ALL); |
|
70 |
||
71 |
// Set up some default values for the simulation. |
|
72 |
Config::SetDefault ("ns3::OnOffApplication::PacketSize", UintegerValue (250)); |
|
73 |
Config::SetDefault ("ns3::OnOffApplication::DataRate", StringValue ("5kb/s")); |
|
74 |
uint32_t N = 9; //number of nodes in the star |
|
75 |
||
76 |
// Allow the user to override any of the defaults and the above |
|
77 |
// Config::SetDefault()s at run-time, via command-line arguments |
|
78 |
CommandLine cmd; |
|
79 |
cmd.AddValue("nNodes", "Number of nodes to place in the star", N); |
|
80 |
cmd.Parse (argc, argv); |
|
81 |
||
82 |
// Here, we will create N nodes in a star. |
|
83 |
NS_LOG_INFO ("Create nodes."); |
|
84 |
NodeContainer serverNode; |
|
85 |
NodeContainer clientNodes; |
|
86 |
serverNode.Create(1); |
|
87 |
clientNodes.Create(N-1); |
|
88 |
NodeContainer allNodes = NodeContainer(serverNode, clientNodes); |
|
89 |
||
90 |
// Install network stacks on the nodes |
|
91 |
InternetStackHelper internet; |
|
92 |
internet.Install (allNodes); |
|
93 |
||
94 |
//Collect an adjacency list of nodes for the p2p topology |
|
95 |
std::vector<NodeContainer> nodeAdjacencyList(N-1); |
|
96 |
for(uint32_t i=0; i<nodeAdjacencyList.size(); ++i) |
|
97 |
{ |
|
98 |
nodeAdjacencyList[i] = NodeContainer (serverNode, clientNodes.Get (i)); |
|
99 |
} |
|
100 |
||
101 |
// We create the channels first without any IP addressing information |
|
102 |
NS_LOG_INFO ("Create channels."); |
|
103 |
PointToPointHelper p2p; |
|
3381
3cdd9d60f7c7
bug 232, references to Parameter obsolete
Craig Dowell <craigdo@ee.washington.edu>
parents:
3367
diff
changeset
|
104 |
p2p.SetDeviceAttribute ("DataRate", StringValue ("5Mbps")); |
3cdd9d60f7c7
bug 232, references to Parameter obsolete
Craig Dowell <craigdo@ee.washington.edu>
parents:
3367
diff
changeset
|
105 |
p2p.SetChannelAttribute ("Delay", StringValue ("2ms")); |
3366 | 106 |
std::vector<NetDeviceContainer> deviceAdjacencyList(N-1); |
107 |
for(uint32_t i=0; i<deviceAdjacencyList.size(); ++i) |
|
108 |
{ |
|
109 |
deviceAdjacencyList[i] = p2p.Install (nodeAdjacencyList[i]); |
|
110 |
} |
|
111 |
||
112 |
// Later, we add IP addresses. |
|
113 |
NS_LOG_INFO ("Assign IP Addresses."); |
|
114 |
Ipv4AddressHelper ipv4; |
|
115 |
std::vector<Ipv4InterfaceContainer> interfaceAdjacencyList(N-1); |
|
116 |
for(uint32_t i=0; i<interfaceAdjacencyList.size(); ++i) |
|
117 |
{ |
|
118 |
std::ostringstream subnet; |
|
119 |
subnet<<"10.1."<<i+1<<".0"; |
|
120 |
ipv4.SetBase (subnet.str().c_str(), "255.255.255.0"); |
|
121 |
interfaceAdjacencyList[i] = ipv4.Assign (deviceAdjacencyList[i]); |
|
122 |
} |
|
123 |
||
124 |
//Turn on global static routing |
|
4616
a84f60b6cd12
bug 600: lower the default routing priority of Ipv4GlobalRouting; move to helper
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
4225
diff
changeset
|
125 |
Ipv4GlobalRoutingHelper::PopulateRoutingTables (); |
3366 | 126 |
|
127 |
// Create a packet sink on the star "hub" to receive these packets |
|
128 |
uint16_t port = 50000; |
|
129 |
Address sinkLocalAddress(InetSocketAddress (Ipv4Address::GetAny (), port)); |
|
130 |
PacketSinkHelper sinkHelper ("ns3::TcpSocketFactory", sinkLocalAddress); |
|
131 |
ApplicationContainer sinkApp = sinkHelper.Install (serverNode); |
|
132 |
sinkApp.Start (Seconds (1.0)); |
|
133 |
sinkApp.Stop (Seconds (10.0)); |
|
134 |
||
135 |
// Create the OnOff applications to send TCP to the server |
|
136 |
OnOffHelper clientHelper ("ns3::TcpSocketFactory", Address ()); |
|
137 |
clientHelper.SetAttribute |
|
138 |
("OnTime", RandomVariableValue (ConstantVariable (1))); |
|
139 |
clientHelper.SetAttribute |
|
140 |
("OffTime", RandomVariableValue (ConstantVariable (0))); |
|
141 |
//normally wouldn't need a loop here but the server IP address is different |
|
142 |
//on each p2p subnet |
|
143 |
ApplicationContainer clientApps; |
|
144 |
for(uint32_t i=0; i<clientNodes.GetN(); ++i) |
|
145 |
{ |
|
146 |
AddressValue remoteAddress |
|
147 |
(InetSocketAddress (interfaceAdjacencyList[i].GetAddress (0), port)); |
|
148 |
clientHelper.SetAttribute ("Remote", remoteAddress); |
|
149 |
clientApps.Add(clientHelper.Install (clientNodes.Get(i))); |
|
150 |
} |
|
151 |
clientApps.Start (Seconds (1.0)); |
|
152 |
clientApps.Stop (Seconds (10.0)); |
|
153 |
||
154 |
||
155 |
//configure tracing |
|
156 |
std::ofstream ascii; |
|
3367
8587b5520e63
Fix trace names to match documents in tcp-star-server
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
3366
diff
changeset
|
157 |
ascii.open ("tcp-star-server.tr"); |
8587b5520e63
Fix trace names to match documents in tcp-star-server
Raj Bhattacharjea <raj.b@gatech.edu>
parents:
3366
diff
changeset
|
158 |
PointToPointHelper::EnablePcapAll ("tcp-star-server"); |
3366 | 159 |
PointToPointHelper::EnableAsciiAll (ascii); |
160 |
||
161 |
NS_LOG_INFO ("Run Simulation."); |
|
162 |
Simulator::Run (); |
|
163 |
Simulator::Destroy (); |
|
164 |
NS_LOG_INFO ("Done."); |
|
165 |
||
166 |
return 0; |
|
167 |
} |