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
sixth.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
24
using namespace
ns3;
25
26
NS_LOG_COMPONENT_DEFINE
(
"SixthScriptExample"
);
27
28
// ===========================================================================
29
//
30
// node 0 node 1
31
// +----------------+ +----------------+
32
// | ns-3 TCP | | ns-3 TCP |
33
// +----------------+ +----------------+
34
// | 10.1.1.1 | | 10.1.1.2 |
35
// +----------------+ +----------------+
36
// | point-to-point | | point-to-point |
37
// +----------------+ +----------------+
38
// | |
39
// +---------------------+
40
// 5 Mbps, 2 ms
41
//
42
//
43
// We want to look at changes in the ns-3 TCP congestion window. We need
44
// to crank up a flow and hook the CongestionWindow attribute on the socket
45
// of the sender. Normally one would use an on-off application to generate a
46
// flow, but this has a couple of problems. First, the socket of the on-off
47
// application is not created until Application Start time, so we wouldn't be
48
// able to hook the socket (now) at configuration time. Second, even if we
49
// could arrange a call after start time, the socket is not public so we
50
// couldn't get at it.
51
//
52
// So, we can cook up a simple version of the on-off application that does what
53
// we want. On the plus side we don't need all of the complexity of the on-off
54
// application. On the minus side, we don't have a helper, so we have to get
55
// a little more involved in the details, but this is trivial.
56
//
57
// So first, we create a socket and do the trace connect on it; then we pass
58
// this socket into the constructor of our simple application which we then
59
// install in the source node.
60
// ===========================================================================
61
//
62
class
MyApp
:
public
Application
63
{
64
public
:
65
MyApp
();
66
virtual
~
MyApp
();
67
68
void
Setup (
Ptr<Socket>
socket,
Address
address
, uint32_t packetSize, uint32_t nPackets,
DataRate
dataRate);
69
70
private
:
71
virtual
void
StartApplication (
void
);
72
virtual
void
StopApplication (
void
);
73
74
void
ScheduleTx (
void
);
75
void
SendPacket
(
void
);
76
77
Ptr<Socket>
m_socket;
78
Address
m_peer;
79
uint32_t m_packetSize;
80
uint32_t m_nPackets;
81
DataRate
m_dataRate;
82
EventId
m_sendEvent;
83
bool
m_running;
84
uint32_t m_packetsSent;
85
};
86
87
MyApp::MyApp
()
88
: m_socket (0),
89
m_peer (),
90
m_packetSize (0),
91
m_nPackets (0),
92
m_dataRate (0),
93
m_sendEvent (),
94
m_running (false),
95
m_packetsSent (0)
96
{
97
}
98
99
MyApp::~MyApp
()
100
{
101
m_socket
= 0;
102
}
103
104
void
105
MyApp::Setup
(
Ptr<Socket>
socket,
Address
address
, uint32_t packetSize, uint32_t nPackets,
DataRate
dataRate)
106
{
107
m_socket
= socket;
108
m_peer
=
address
;
109
m_packetSize
= packetSize;
110
m_nPackets
= nPackets;
111
m_dataRate
= dataRate;
112
}
113
114
void
115
MyApp::StartApplication
(
void
)
116
{
117
m_running
=
true
;
118
m_packetsSent
= 0;
119
m_socket
->
Bind
();
120
m_socket
->
Connect
(
m_peer
);
121
SendPacket
();
122
}
123
124
void
125
MyApp::StopApplication
(
void
)
126
{
127
m_running
=
false
;
128
129
if
(
m_sendEvent
.
IsRunning
())
130
{
131
Simulator::Cancel (
m_sendEvent
);
132
}
133
134
if
(
m_socket
)
135
{
136
m_socket
->
Close
();
137
}
138
}
139
140
void
141
MyApp::SendPacket
(
void
)
142
{
143
Ptr<Packet>
packet = Create<Packet> (
m_packetSize
);
144
m_socket
->
Send
(packet);
145
146
if
(++
m_packetsSent
<
m_nPackets
)
147
{
148
ScheduleTx
();
149
}
150
}
151
152
void
153
MyApp::ScheduleTx
(
void
)
154
{
155
if
(
m_running
)
156
{
157
Time
tNext (Seconds (
m_packetSize
* 8 / static_cast<double> (
m_dataRate
.
GetBitRate
())));
158
m_sendEvent
= Simulator::Schedule (tNext, &
MyApp::SendPacket
,
this
);
159
}
160
}
161
162
static
void
163
CwndChange
(
Ptr<OutputStreamWrapper>
stream, uint32_t oldCwnd, uint32_t newCwnd)
164
{
165
NS_LOG_UNCOND
(
Simulator::Now
().GetSeconds () <<
"\t"
<< newCwnd);
166
*stream->
GetStream
() <<
Simulator::Now
().
GetSeconds
() <<
"\t"
<< oldCwnd <<
"\t"
<< newCwnd << std::endl;
167
}
168
169
static
void
170
RxDrop
(
Ptr<PcapFileWrapper>
file,
Ptr<const Packet>
p)
171
{
172
NS_LOG_UNCOND
(
"RxDrop at "
<<
Simulator::Now
().GetSeconds ());
173
file->
Write
(
Simulator::Now
(), p);
174
}
175
176
int
177
main
(
int
argc,
char
*argv[])
178
{
179
NodeContainer
nodes
;
180
nodes.
Create
(2);
181
182
PointToPointHelper
pointToPoint
;
183
pointToPoint.
SetDeviceAttribute
(
"DataRate"
,
StringValue
(
"5Mbps"
));
184
pointToPoint.
SetChannelAttribute
(
"Delay"
,
StringValue
(
"2ms"
));
185
186
NetDeviceContainer
devices
;
187
devices = pointToPoint.
Install
(nodes);
188
189
Ptr<RateErrorModel>
em = CreateObject<RateErrorModel> ();
190
em->
SetAttribute
(
"ErrorRate"
,
DoubleValue
(0.00001));
191
devices.
Get
(1)->
SetAttribute
(
"ReceiveErrorModel"
,
PointerValue
(em));
192
193
InternetStackHelper
stack
;
194
stack.
Install
(nodes);
195
196
Ipv4AddressHelper
address
;
197
address.
SetBase
(
"10.1.1.0"
,
"255.255.255.252"
);
198
Ipv4InterfaceContainer
interfaces
= address.
Assign
(devices);
199
200
uint16_t sinkPort = 8080;
201
Address
sinkAddress (
InetSocketAddress
(interfaces.
GetAddress
(1), sinkPort));
202
PacketSinkHelper
packetSinkHelper (
"ns3::TcpSocketFactory"
,
InetSocketAddress
(Ipv4Address::GetAny (), sinkPort));
203
ApplicationContainer
sinkApps = packetSinkHelper.
Install
(nodes.
Get
(1));
204
sinkApps.
Start
(
Seconds
(0.));
205
sinkApps.
Stop
(
Seconds
(20.));
206
207
Ptr<Socket>
ns3TcpSocket = Socket::CreateSocket (nodes.
Get
(0), TcpSocketFactory::GetTypeId ());
208
209
Ptr<MyApp>
app = CreateObject<MyApp> ();
210
app->Setup (ns3TcpSocket, sinkAddress, 1040, 1000,
DataRate
(
"1Mbps"
));
211
nodes.
Get
(0)->
AddApplication
(app);
212
app->SetStartTime (
Seconds
(1.));
213
app->SetStopTime (
Seconds
(20.));
214
215
AsciiTraceHelper
asciiTraceHelper;
216
Ptr<OutputStreamWrapper>
stream = asciiTraceHelper.
CreateFileStream
(
"sixth.cwnd"
);
217
ns3TcpSocket->
TraceConnectWithoutContext
(
"CongestionWindow"
,
MakeBoundCallback
(&
CwndChange
, stream));
218
219
PcapHelper
pcapHelper;
220
Ptr<PcapFileWrapper>
file = pcapHelper.
CreateFile
(
"sixth.pcap"
, std::ios::out, PcapHelper::DLT_PPP);
221
devices.
Get
(1)->
TraceConnectWithoutContext
(
"PhyRxDrop"
,
MakeBoundCallback
(&
RxDrop
, file));
222
223
Simulator::Stop (
Seconds
(20));
224
Simulator::Run ();
225
Simulator::Destroy ();
226
227
return
0;
228
}
229
examples
tutorial
sixth.cc
Generated on Fri Aug 30 2013 01:42:44 for ns-3 by
1.8.1.2