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