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
bulk-send-application.cc
Go to the documentation of this file.
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
* Copyright (c) 2010 Georgia Institute of Technology
4
*
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License version 2 as
7
* published by the Free Software Foundation;
8
*
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
13
*
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
*
18
* Author: George F. Riley <riley@ece.gatech.edu>
19
*/
20
21
#include "ns3/log.h"
22
#include "ns3/address.h"
23
#include "ns3/node.h"
24
#include "ns3/nstime.h"
25
#include "ns3/socket.h"
26
#include "ns3/simulator.h"
27
#include "ns3/socket-factory.h"
28
#include "ns3/packet.h"
29
#include "ns3/uinteger.h"
30
#include "ns3/trace-source-accessor.h"
31
#include "ns3/tcp-socket-factory.h"
32
#include "
bulk-send-application.h
"
33
34
NS_LOG_COMPONENT_DEFINE
(
"BulkSendApplication"
);
35
36
namespace
ns3 {
37
38
NS_OBJECT_ENSURE_REGISTERED
(BulkSendApplication);
39
40
TypeId
41
BulkSendApplication::GetTypeId
(
void
)
42
{
43
static
TypeId
tid =
TypeId
(
"ns3::BulkSendApplication"
)
44
.
SetParent
<
Application
> ()
45
.AddConstructor<BulkSendApplication> ()
46
.AddAttribute (
"SendSize"
,
"The amount of data to send each time."
,
47
UintegerValue
(512),
48
MakeUintegerAccessor (&
BulkSendApplication::m_sendSize
),
49
MakeUintegerChecker<uint32_t> (1))
50
.AddAttribute (
"Remote"
,
"The address of the destination"
,
51
AddressValue
(),
52
MakeAddressAccessor (&
BulkSendApplication::m_peer
),
53
MakeAddressChecker ())
54
.AddAttribute (
"MaxBytes"
,
55
"The total number of bytes to send. "
56
"Once these bytes are sent, "
57
"no data is sent again. The value zero means "
58
"that there is no limit."
,
59
UintegerValue
(0),
60
MakeUintegerAccessor (&
BulkSendApplication::m_maxBytes
),
61
MakeUintegerChecker<uint32_t> ())
62
.AddAttribute (
"Protocol"
,
"The type of protocol to use."
,
63
TypeIdValue
(
TcpSocketFactory::GetTypeId
()),
64
MakeTypeIdAccessor (&
BulkSendApplication::m_tid
),
65
MakeTypeIdChecker ())
66
.AddTraceSource (
"Tx"
,
"A new packet is created and is sent"
,
67
MakeTraceSourceAccessor
(&
BulkSendApplication::m_txTrace
))
68
;
69
return
tid;
70
}
71
72
73
BulkSendApplication::BulkSendApplication
()
74
: m_socket (0),
75
m_connected (false),
76
m_totBytes (0)
77
{
78
NS_LOG_FUNCTION
(
this
);
79
}
80
81
BulkSendApplication::~BulkSendApplication
()
82
{
83
NS_LOG_FUNCTION
(
this
);
84
}
85
86
void
87
BulkSendApplication::SetMaxBytes
(uint32_t maxBytes)
88
{
89
NS_LOG_FUNCTION
(
this
<< maxBytes);
90
m_maxBytes
= maxBytes;
91
}
92
93
Ptr<Socket>
94
BulkSendApplication::GetSocket
(
void
)
const
95
{
96
NS_LOG_FUNCTION
(
this
);
97
return
m_socket
;
98
}
99
100
void
101
BulkSendApplication::DoDispose
(
void
)
102
{
103
NS_LOG_FUNCTION
(
this
);
104
105
m_socket
= 0;
106
// chain up
107
Application::DoDispose
();
108
}
109
110
// Application Methods
111
void
BulkSendApplication::StartApplication
(
void
)
// Called at time specified by Start
112
{
113
NS_LOG_FUNCTION
(
this
);
114
115
// Create the socket if not already
116
if
(!
m_socket
)
117
{
118
m_socket
=
Socket::CreateSocket
(
GetNode
(),
m_tid
);
119
120
// Fatal error if socket type is not NS3_SOCK_STREAM or NS3_SOCK_SEQPACKET
121
if
(
m_socket
->
GetSocketType
() !=
Socket::NS3_SOCK_STREAM
&&
122
m_socket
->
GetSocketType
() !=
Socket::NS3_SOCK_SEQPACKET
)
123
{
124
NS_FATAL_ERROR
(
"Using BulkSend with an incompatible socket type. "
125
"BulkSend requires SOCK_STREAM or SOCK_SEQPACKET. "
126
"In other words, use TCP instead of UDP."
);
127
}
128
129
if
(
Inet6SocketAddress::IsMatchingType
(
m_peer
))
130
{
131
m_socket
->
Bind6
();
132
}
133
else
if
(
InetSocketAddress::IsMatchingType
(
m_peer
))
134
{
135
m_socket
->
Bind
();
136
}
137
138
m_socket
->
Connect
(
m_peer
);
139
m_socket
->
ShutdownRecv
();
140
m_socket
->
SetConnectCallback
(
141
MakeCallback
(&
BulkSendApplication::ConnectionSucceeded
,
this
),
142
MakeCallback
(&
BulkSendApplication::ConnectionFailed
,
this
));
143
m_socket
->
SetSendCallback
(
144
MakeCallback
(&
BulkSendApplication::DataSend
,
this
));
145
}
146
if
(
m_connected
)
147
{
148
SendData
();
149
}
150
}
151
152
void
BulkSendApplication::StopApplication
(
void
)
// Called at time specified by Stop
153
{
154
NS_LOG_FUNCTION
(
this
);
155
156
if
(
m_socket
!= 0)
157
{
158
m_socket
->
Close
();
159
m_connected
=
false
;
160
}
161
else
162
{
163
NS_LOG_WARN
(
"BulkSendApplication found null socket to close in StopApplication"
);
164
}
165
}
166
167
168
// Private helpers
169
170
void
BulkSendApplication::SendData
(
void
)
171
{
172
NS_LOG_FUNCTION
(
this
);
173
174
while
(
m_maxBytes
== 0 ||
m_totBytes
<
m_maxBytes
)
175
{
// Time to send more
176
uint32_t toSend =
m_sendSize
;
177
// Make sure we don't send too many
178
if
(
m_maxBytes
> 0)
179
{
180
toSend = std::min (
m_sendSize
,
m_maxBytes
-
m_totBytes
);
181
}
182
NS_LOG_LOGIC
(
"sending packet at "
<<
Simulator::Now
());
183
Ptr<Packet>
packet = Create<Packet> (toSend);
184
m_txTrace
(packet);
185
int
actual =
m_socket
->
Send
(packet);
186
if
(actual > 0)
187
{
188
m_totBytes
+= actual;
189
}
190
// We exit this loop when actual < toSend as the send side
191
// buffer is full. The "DataSent" callback will pop when
192
// some buffer space has freed ip.
193
if
((
unsigned
)actual != toSend)
194
{
195
break
;
196
}
197
}
198
// Check if time to close (all sent)
199
if
(
m_totBytes
==
m_maxBytes
&&
m_connected
)
200
{
201
m_socket
->
Close
();
202
m_connected
=
false
;
203
}
204
}
205
206
void
BulkSendApplication::ConnectionSucceeded
(
Ptr<Socket>
socket)
207
{
208
NS_LOG_FUNCTION
(
this
<< socket);
209
NS_LOG_LOGIC
(
"BulkSendApplication Connection succeeded"
);
210
m_connected
=
true
;
211
SendData
();
212
}
213
214
void
BulkSendApplication::ConnectionFailed
(
Ptr<Socket>
socket)
215
{
216
NS_LOG_FUNCTION
(
this
<< socket);
217
NS_LOG_LOGIC
(
"BulkSendApplication, Connection Failed"
);
218
}
219
220
void
BulkSendApplication::DataSend
(
Ptr<Socket>
, uint32_t)
221
{
222
NS_LOG_FUNCTION
(
this
);
223
224
if
(
m_connected
)
225
{
// Only send new data if the connection has completed
226
Simulator::ScheduleNow
(&
BulkSendApplication::SendData
,
this
);
227
}
228
}
229
230
231
232
}
// Namespace ns3
src
applications
model
bulk-send-application.cc
Generated on Tue May 14 2013 11:08:16 for ns-3 by
1.8.1.2