A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Portuguese
Docs ▼
Wiki
Manual
Models
Develop ▼
API
Bugs
API
Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Properties
Friends
Macros
Groups
Pages
seventh.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
#include <fstream>
18
#include "ns3/core-module.h"
19
#include "ns3/network-module.h"
20
#include "ns3/internet-module.h"
21
#include "ns3/point-to-point-module.h"
22
#include "ns3/applications-module.h"
23
#include "ns3/stats-module.h"
24
25
using namespace
ns3;
26
27
NS_LOG_COMPONENT_DEFINE
(
"SeventhScriptExample"
);
28
29
// ===========================================================================
30
//
31
// node 0 node 1
32
// +----------------+ +----------------+
33
// | ns-3 TCP | | ns-3 TCP |
34
// +----------------+ +----------------+
35
// | 10.1.1.1 | | 10.1.1.2 |
36
// +----------------+ +----------------+
37
// | point-to-point | | point-to-point |
38
// +----------------+ +----------------+
39
// | |
40
// +---------------------+
41
// 5 Mbps, 2 ms
42
//
43
//
44
// We want to look at changes in the ns-3 TCP congestion window. We need
45
// to crank up a flow and hook the CongestionWindow attribute on the socket
46
// of the sender. Normally one would use an on-off application to generate a
47
// flow, but this has a couple of problems. First, the socket of the on-off
48
// application is not created until Application Start time, so we wouldn't be
49
// able to hook the socket (now) at configuration time. Second, even if we
50
// could arrange a call after start time, the socket is not public so we
51
// couldn't get at it.
52
//
53
// So, we can cook up a simple version of the on-off application that does what
54
// we want. On the plus side we don't need all of the complexity of the on-off
55
// application. On the minus side, we don't have a helper, so we have to get
56
// a little more involved in the details, but this is trivial.
57
//
58
// So first, we create a socket and do the trace connect on it; then we pass
59
// this socket into the constructor of our simple application which we then
60
// install in the source node.
61
// ===========================================================================
62
//
63
class
MyApp
:
public
Application
64
{
65
public
:
66
MyApp
();
67
virtual
~
MyApp
();
68
69
void
Setup (
Ptr<Socket>
socket,
Address
address
, uint32_t packetSize, uint32_t nPackets,
DataRate
dataRate);
70
71
private
:
72
virtual
void
StartApplication (
void
);
73
virtual
void
StopApplication (
void
);
74
75
void
ScheduleTx (
void
);
76
void
SendPacket
(
void
);
77
78
Ptr<Socket>
m_socket;
79
Address
m_peer;
80
uint32_t m_packetSize;
81
uint32_t m_nPackets;
82
DataRate
m_dataRate;
83
EventId
m_sendEvent;
84
bool
m_running;
85
uint32_t m_packetsSent;
86
};
87
88
MyApp::MyApp
()
89
: m_socket (0),
90
m_peer (),
91
m_packetSize (0),
92
m_nPackets (0),
93
m_dataRate (0),
94
m_sendEvent (),
95
m_running (false),
96
m_packetsSent (0)
97
{
98
}
99
100
MyApp::~MyApp
()
101
{
102
m_socket
= 0;
103
}
104
105
void
106
MyApp::Setup
(
Ptr<Socket>
socket,
Address
address
, uint32_t packetSize, uint32_t nPackets,
DataRate
dataRate)
107
{
108
m_socket
= socket;
109
m_peer
=
address
;
110
m_packetSize
= packetSize;
111
m_nPackets
= nPackets;
112
m_dataRate
= dataRate;
113
}
114
115
void
116
MyApp::StartApplication
(
void
)
117
{
118
m_running
=
true
;
119
m_packetsSent
= 0;
120
if
(InetSocketAddress::IsMatchingType (
m_peer
))
121
{
122
m_socket
->
Bind
();
123
}
124
else
125
{
126
m_socket
->
Bind6
();
127
}
128
m_socket
->
Connect
(
m_peer
);
129
SendPacket
();
130
}
131
132
void
133
MyApp::StopApplication
(
void
)
134
{
135
m_running
=
false
;
136
137
if
(
m_sendEvent
.
IsRunning
())
138
{
139
Simulator::Cancel (
m_sendEvent
);
140
}
141
142
if
(
m_socket
)
143
{
144
m_socket
->
Close
();
145
}
146
}
147
148
void
149
MyApp::SendPacket
(
void
)
150
{
151
Ptr<Packet>
packet = Create<Packet> (
m_packetSize
);
152
m_socket
->
Send
(packet);
153
154
if
(++
m_packetsSent
<
m_nPackets
)
155
{
156
ScheduleTx
();
157
}
158
}
159
160
void
161
MyApp::ScheduleTx
(
void
)
162
{
163
if
(
m_running
)
164
{
165
Time
tNext (Seconds (
m_packetSize
* 8 / static_cast<double> (
m_dataRate
.
GetBitRate
())));
166
m_sendEvent
= Simulator::Schedule (tNext, &
MyApp::SendPacket
,
this
);
167
}
168
}
169
170
static
void
171
CwndChange
(
Ptr<OutputStreamWrapper>
stream, uint32_t oldCwnd, uint32_t newCwnd)
172
{
173
NS_LOG_UNCOND
(
Simulator::Now
().GetSeconds () <<
"\t"
<< newCwnd);
174
*stream->
GetStream
() <<
Simulator::Now
().
GetSeconds
() <<
"\t"
<< oldCwnd <<
"\t"
<< newCwnd << std::endl;
175
}
176
177
static
void
178
RxDrop
(
Ptr<PcapFileWrapper>
file,
Ptr<const Packet>
p)
179
{
180
NS_LOG_UNCOND
(
"RxDrop at "
<<
Simulator::Now
().GetSeconds ());
181
file->
Write
(
Simulator::Now
(), p);
182
}
183
184
int
185
main
(
int
argc,
char
*argv[])
186
{
187
bool
useV6 =
false
;
188
189
CommandLine
cmd;
190
cmd.
AddValue
(
"useIpv6"
,
"Use Ipv6"
, useV6);
191
cmd.
Parse
(argc, argv);
192
193
NodeContainer
nodes
;
194
nodes.
Create
(2);
195
196
PointToPointHelper
pointToPoint
;
197
pointToPoint.
SetDeviceAttribute
(
"DataRate"
,
StringValue
(
"5Mbps"
));
198
pointToPoint.
SetChannelAttribute
(
"Delay"
,
StringValue
(
"2ms"
));
199
200
NetDeviceContainer
devices
;
201
devices = pointToPoint.
Install
(nodes);
202
203
Ptr<RateErrorModel>
em = CreateObject<RateErrorModel> ();
204
em->
SetAttribute
(
"ErrorRate"
,
DoubleValue
(0.00001));
205
devices.
Get
(1)->
SetAttribute
(
"ReceiveErrorModel"
,
PointerValue
(em));
206
207
InternetStackHelper
stack
;
208
stack.
Install
(nodes);
209
210
uint16_t sinkPort = 8080;
211
Address
sinkAddress;
212
Address
anyAddress;
213
std::string probeName;
214
std::string probeTrace;
215
if
(useV6 ==
false
)
216
{
217
Ipv4AddressHelper
address
;
218
address.
SetBase
(
"10.1.1.0"
,
"255.255.255.0"
);
219
Ipv4InterfaceContainer
interfaces
= address.
Assign
(devices);
220
sinkAddress =
InetSocketAddress
(interfaces.
GetAddress
(1), sinkPort);
221
anyAddress =
InetSocketAddress
(Ipv4Address::GetAny (), sinkPort);
222
probeName =
"ns3::Ipv4PacketProbe"
;
223
probeTrace =
"/NodeList/*/$ns3::Ipv4L3Protocol/Tx"
;
224
}
225
else
226
{
227
Ipv6AddressHelper
address
;
228
address.
SetBase
(
"2001:0000:f00d:cafe::"
,
Ipv6Prefix
(64));
229
Ipv6InterfaceContainer
interfaces
= address.
Assign
(devices);
230
sinkAddress =
Inet6SocketAddress
(interfaces.
GetAddress
(1,1), sinkPort);
231
anyAddress =
Inet6SocketAddress
(Ipv6Address::GetAny (), sinkPort);
232
probeName =
"ns3::Ipv6PacketProbe"
;
233
probeTrace =
"/NodeList/*/$ns3::Ipv6L3Protocol/Tx"
;
234
}
235
236
PacketSinkHelper
packetSinkHelper (
"ns3::TcpSocketFactory"
, anyAddress);
237
ApplicationContainer
sinkApps = packetSinkHelper.
Install
(nodes.
Get
(1));
238
sinkApps.
Start
(
Seconds
(0.));
239
sinkApps.
Stop
(
Seconds
(20.));
240
241
Ptr<Socket>
ns3TcpSocket = Socket::CreateSocket (nodes.
Get
(0), TcpSocketFactory::GetTypeId ());
242
243
Ptr<MyApp>
app = CreateObject<MyApp> ();
244
app->Setup (ns3TcpSocket, sinkAddress, 1040, 1000,
DataRate
(
"1Mbps"
));
245
nodes.
Get
(0)->
AddApplication
(app);
246
app->SetStartTime (
Seconds
(1.));
247
app->SetStopTime (
Seconds
(20.));
248
249
AsciiTraceHelper
asciiTraceHelper;
250
Ptr<OutputStreamWrapper>
stream = asciiTraceHelper.
CreateFileStream
(
"seventh.cwnd"
);
251
ns3TcpSocket->
TraceConnectWithoutContext
(
"CongestionWindow"
,
MakeBoundCallback
(&
CwndChange
, stream));
252
253
PcapHelper
pcapHelper;
254
Ptr<PcapFileWrapper>
file = pcapHelper.
CreateFile
(
"seventh.pcap"
, std::ios::out, PcapHelper::DLT_PPP);
255
devices.
Get
(1)->
TraceConnectWithoutContext
(
"PhyRxDrop"
,
MakeBoundCallback
(&
RxDrop
, file));
256
257
// Use GnuplotHelper to plot the packet byte count over time
258
GnuplotHelper
plotHelper;
259
260
// Configure the plot. The first argument is the file name prefix
261
// for the output files generated. The second, third, and fourth
262
// arguments are, respectively, the plot title, x-axis, and y-axis labels
263
plotHelper.
ConfigurePlot
(
"seventh-packet-byte-count"
,
264
"Packet Byte Count vs. Time"
,
265
"Time (Seconds)"
,
266
"Packet Byte Count"
);
267
268
// Specify the probe type, probe path (in configuration namespace), and
269
// probe output trace source ("OutputBytes") to plot. The fourth argument
270
// specifies the name of the data series label on the plot. The last
271
// argument formats the plot by specifying where the key should be placed.
272
plotHelper.
PlotProbe
(probeName,
273
probeTrace,
274
"OutputBytes"
,
275
"Packet Byte Count"
,
276
GnuplotAggregator::KEY_BELOW);
277
278
// Use FileHelper to write out the packet byte count over time
279
FileHelper
fileHelper;
280
281
// Configure the file to be written, and the formatting of output data.
282
fileHelper.
ConfigureFile
(
"seventh-packet-byte-count"
,
283
FileAggregator::FORMATTED);
284
285
// Set the labels for this formatted output file.
286
fileHelper.
Set2dFormat
(
"Time (Seconds) = %.3e\tPacket Byte Count = %.0f"
);
287
288
// Specify the probe type, probe path (in configuration namespace), and
289
// probe output trace source ("OutputBytes") to write.
290
fileHelper.
WriteProbe
(probeName,
291
probeTrace,
292
"OutputBytes"
);
293
294
Simulator::Stop (
Seconds
(20));
295
Simulator::Run ();
296
Simulator::Destroy ();
297
298
return
0;
299
}
300
examples
tutorial
seventh.cc
Generated on Fri Aug 30 2013 01:42:44 for ns-3 by
1.8.1.2