|
|
|
|
1 |
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 |
/* |
| 3 |
* Copyright (c) 2012 |
| 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 |
* Authors: Frank Helbert <frank@ime.usp.br>, |
| 19 |
* Luiz Arthur Feitosa dos Santos <luizsan@ime.usp.br> and |
| 20 |
* Rodrigo Campiolo <campiolo@ime.usp.br> |
| 21 |
*/ |
| 22 |
|
| 23 |
#include "ns3/log.h" |
| 24 |
#include "ns3/ipv4-address.h" |
| 25 |
#include "ns3/ipv6-address.h" |
| 26 |
#include "ns3/address-utils.h" |
| 27 |
#include "ns3/nstime.h" |
| 28 |
#include "ns3/inet-socket-address.h" |
| 29 |
#include "ns3/inet6-socket-address.h" |
| 30 |
#include "ns3/socket.h" |
| 31 |
#include "ns3/simulator.h" |
| 32 |
#include "ns3/socket-factory.h" |
| 33 |
#include "ns3/packet.h" |
| 34 |
#include "ns3/uinteger.h" |
| 35 |
#include "ns3/trace-source-accessor.h" |
| 36 |
#include "tcp-echo-client.h" |
| 37 |
|
| 38 |
namespace ns3 { |
| 39 |
|
| 40 |
NS_LOG_COMPONENT_DEFINE ("TcpEchoClientApplication"); |
| 41 |
NS_OBJECT_ENSURE_REGISTERED (TcpEchoClient); |
| 42 |
|
| 43 |
TypeId |
| 44 |
TcpEchoClient::GetTypeId (void) |
| 45 |
{ |
| 46 |
static TypeId tid = TypeId ("ns3::TcpEchoClient") |
| 47 |
.SetParent<Application> () |
| 48 |
.AddConstructor<TcpEchoClient> () |
| 49 |
.AddAttribute ("MaxPackets", |
| 50 |
"The maximum number of packets the application will send", |
| 51 |
UintegerValue (100), |
| 52 |
MakeUintegerAccessor (&TcpEchoClient::m_count), |
| 53 |
MakeUintegerChecker<uint32_t> ()) |
| 54 |
.AddAttribute ("Interval", |
| 55 |
"The time to wait between packets", |
| 56 |
TimeValue (Seconds (1.0)), |
| 57 |
MakeTimeAccessor (&TcpEchoClient::m_interval), |
| 58 |
MakeTimeChecker ()) |
| 59 |
.AddAttribute ("RemoteAddress", |
| 60 |
"The destination address of the outbound packets", |
| 61 |
AddressValue (), |
| 62 |
MakeAddressAccessor (&TcpEchoClient::m_peerAddress), |
| 63 |
MakeAddressChecker ()) |
| 64 |
.AddAttribute ("RemotePort", |
| 65 |
"The destination port of the outbound packets", |
| 66 |
UintegerValue (0), |
| 67 |
MakeUintegerAccessor (&TcpEchoClient::m_peerPort), |
| 68 |
MakeUintegerChecker<uint16_t> ()) |
| 69 |
.AddAttribute ("PacketSize", "Size of echo data in outbound packets", |
| 70 |
UintegerValue (100), |
| 71 |
MakeUintegerAccessor (&TcpEchoClient::SetDataSize, |
| 72 |
&TcpEchoClient::GetDataSize), |
| 73 |
MakeUintegerChecker<uint32_t> ()) |
| 74 |
.AddTraceSource ("Tx", "A new packet is created and is sent", |
| 75 |
MakeTraceSourceAccessor (&TcpEchoClient::m_txTrace)) |
| 76 |
; |
| 77 |
return tid; |
| 78 |
} |
| 79 |
|
| 80 |
TcpEchoClient::TcpEchoClient () |
| 81 |
{ |
| 82 |
NS_LOG_FUNCTION_NOARGS (); |
| 83 |
m_sent = 0; |
| 84 |
m_bytesSent = 0; |
| 85 |
m_recvBack = 0; |
| 86 |
m_bytesRecvBack = 0; |
| 87 |
m_socket = 0; |
| 88 |
m_sendEvent = EventId (); |
| 89 |
m_data = 0; |
| 90 |
m_dataSize = 0; |
| 91 |
} |
| 92 |
|
| 93 |
TcpEchoClient::~TcpEchoClient() |
| 94 |
{ |
| 95 |
NS_LOG_FUNCTION_NOARGS (); |
| 96 |
m_socket = 0; |
| 97 |
|
| 98 |
delete [] m_data; |
| 99 |
m_data = 0; |
| 100 |
m_dataSize = 0; |
| 101 |
} |
| 102 |
|
| 103 |
void |
| 104 |
TcpEchoClient::SetRemote (Address ip, uint16_t port) |
| 105 |
{ |
| 106 |
m_peerAddress = ip; |
| 107 |
m_peerPort = port; |
| 108 |
} |
| 109 |
|
| 110 |
void |
| 111 |
TcpEchoClient::DoDispose (void) |
| 112 |
{ |
| 113 |
NS_LOG_FUNCTION_NOARGS (); |
| 114 |
Application::DoDispose (); |
| 115 |
} |
| 116 |
|
| 117 |
void |
| 118 |
TcpEchoClient::StartApplication (void) |
| 119 |
{ |
| 120 |
NS_LOG_FUNCTION_NOARGS (); |
| 121 |
|
| 122 |
if (m_socket == 0) |
| 123 |
{ |
| 124 |
TypeId tid = TypeId::LookupByName ("ns3::TcpSocketFactory"); |
| 125 |
m_socket = Socket::CreateSocket (GetNode (), tid); |
| 126 |
if (Ipv4Address::IsMatchingType(m_peerAddress) == true) |
| 127 |
{ |
| 128 |
m_socket->Bind(); |
| 129 |
m_socket->Connect (InetSocketAddress (Ipv4Address::ConvertFrom(m_peerAddress), m_peerPort)); |
| 130 |
} |
| 131 |
else if (Ipv6Address::IsMatchingType(m_peerAddress) == true) |
| 132 |
{ |
| 133 |
m_socket->Bind6(); |
| 134 |
m_socket->Connect (Inet6SocketAddress (Ipv6Address::ConvertFrom(m_peerAddress), m_peerPort)); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
m_socket->SetRecvCallback (MakeCallback (&TcpEchoClient::ReceivePacket, this)); |
| 139 |
|
| 140 |
ScheduleTransmit (Seconds (0.)); |
| 141 |
} |
| 142 |
|
| 143 |
void |
| 144 |
TcpEchoClient::StopApplication () |
| 145 |
{ |
| 146 |
NS_LOG_FUNCTION_NOARGS (); |
| 147 |
|
| 148 |
if (m_socket != 0) |
| 149 |
{ |
| 150 |
m_socket->Close (); |
| 151 |
m_socket->SetRecvCallback (MakeNullCallback<void, Ptr<Socket> > ()); |
| 152 |
m_socket = 0; |
| 153 |
} |
| 154 |
|
| 155 |
Simulator::Cancel (m_sendEvent); |
| 156 |
} |
| 157 |
|
| 158 |
void |
| 159 |
TcpEchoClient::SetDataSize (uint32_t dataSize) |
| 160 |
{ |
| 161 |
NS_LOG_FUNCTION (dataSize); |
| 162 |
|
| 163 |
// |
| 164 |
// If the client is setting the echo packet data size this way, we infer |
| 165 |
// that she doesn't care about the contents of the packet at all, so |
| 166 |
// neither will we. |
| 167 |
// |
| 168 |
delete [] m_data; |
| 169 |
m_data = 0; |
| 170 |
m_dataSize = 0; |
| 171 |
m_size = dataSize; |
| 172 |
} |
| 173 |
|
| 174 |
uint32_t |
| 175 |
TcpEchoClient::GetDataSize (void) const |
| 176 |
{ |
| 177 |
NS_LOG_FUNCTION_NOARGS (); |
| 178 |
return m_size; |
| 179 |
} |
| 180 |
|
| 181 |
void |
| 182 |
TcpEchoClient::SetFill (std::string fill) |
| 183 |
{ |
| 184 |
NS_LOG_FUNCTION (fill); |
| 185 |
|
| 186 |
uint32_t dataSize = fill.size () + 1; |
| 187 |
|
| 188 |
if (dataSize != m_dataSize) |
| 189 |
{ |
| 190 |
delete [] m_data; |
| 191 |
m_data = new uint8_t [dataSize]; |
| 192 |
m_dataSize = dataSize; |
| 193 |
} |
| 194 |
|
| 195 |
memcpy (m_data, fill.c_str (), dataSize); |
| 196 |
|
| 197 |
// |
| 198 |
// Overwrite packet size attribute. |
| 199 |
// |
| 200 |
m_size = dataSize; |
| 201 |
} |
| 202 |
|
| 203 |
void |
| 204 |
TcpEchoClient::SetFill (uint8_t fill, uint32_t dataSize) |
| 205 |
{ |
| 206 |
if (dataSize != m_dataSize) |
| 207 |
{ |
| 208 |
delete [] m_data; |
| 209 |
m_data = new uint8_t [dataSize]; |
| 210 |
m_dataSize = dataSize; |
| 211 |
} |
| 212 |
|
| 213 |
memset (m_data, fill, dataSize); |
| 214 |
|
| 215 |
// |
| 216 |
// Overwrite packet size attribute. |
| 217 |
// |
| 218 |
m_size = dataSize; |
| 219 |
} |
| 220 |
|
| 221 |
void |
| 222 |
TcpEchoClient::SetFill (uint8_t *fill, uint32_t fillSize, uint32_t dataSize) |
| 223 |
{ |
| 224 |
if (dataSize != m_dataSize) |
| 225 |
{ |
| 226 |
delete [] m_data; |
| 227 |
m_data = new uint8_t [dataSize]; |
| 228 |
m_dataSize = dataSize; |
| 229 |
} |
| 230 |
|
| 231 |
if (fillSize >= dataSize) |
| 232 |
{ |
| 233 |
memcpy (m_data, fill, dataSize); |
| 234 |
return; |
| 235 |
} |
| 236 |
|
| 237 |
// |
| 238 |
// Do all but the final fill. |
| 239 |
// |
| 240 |
uint32_t filled = 0; |
| 241 |
while (filled + fillSize < dataSize) |
| 242 |
{ |
| 243 |
memcpy (&m_data[filled], fill, fillSize); |
| 244 |
filled += fillSize; |
| 245 |
} |
| 246 |
|
| 247 |
// |
| 248 |
// Last fill may be partial |
| 249 |
// |
| 250 |
memcpy (&m_data[filled], fill, dataSize - filled); |
| 251 |
|
| 252 |
// |
| 253 |
// Overwrite packet size attribute. |
| 254 |
// |
| 255 |
m_size = dataSize; |
| 256 |
} |
| 257 |
|
| 258 |
void |
| 259 |
TcpEchoClient::ScheduleTransmit (Time dt) |
| 260 |
{ |
| 261 |
NS_LOG_FUNCTION_NOARGS (); |
| 262 |
m_sendEvent = Simulator::Schedule (dt, &TcpEchoClient::Send, this); |
| 263 |
} |
| 264 |
|
| 265 |
void |
| 266 |
TcpEchoClient::Send (void) |
| 267 |
{ |
| 268 |
NS_LOG_FUNCTION_NOARGS (); |
| 269 |
|
| 270 |
NS_ASSERT (m_sendEvent.IsExpired ()); |
| 271 |
|
| 272 |
Ptr<Packet> p; |
| 273 |
if (m_dataSize) |
| 274 |
{ |
| 275 |
// |
| 276 |
// If m_dataSize is non-zero, we have a data buffer of the same size that we |
| 277 |
// are expected to copy and send. This state of affairs is created if one of |
| 278 |
// the Fill functions is called. In this case, m_size must have been set |
| 279 |
// to agree with m_dataSize |
| 280 |
// |
| 281 |
NS_ASSERT_MSG (m_dataSize == m_size, "TcpEchoClient::Send(): m_size and m_dataSize inconsistent"); |
| 282 |
NS_ASSERT_MSG (m_data, "TcpEchoClient::Send(): m_dataSize but no m_data"); |
| 283 |
p = Create<Packet> (m_data, m_dataSize); |
| 284 |
m_bytesSent += m_dataSize; |
| 285 |
} |
| 286 |
else |
| 287 |
{ |
| 288 |
// |
| 289 |
// If m_dataSize is zero, the client has indicated that she doesn't care |
| 290 |
// about the data itself either by specifying the data size by setting |
| 291 |
// the corresponding atribute or by not calling a SetFill function. In |
| 292 |
// this case, we don't worry about it either. But we do allow m_size |
| 293 |
// to have a value different from the (zero) m_dataSize. |
| 294 |
// |
| 295 |
p = Create<Packet> (m_size); |
| 296 |
m_bytesSent += m_size; |
| 297 |
} |
| 298 |
// call to the trace sinks before the packet is actually sent, |
| 299 |
// so that tags added to the packet can be sent as well |
| 300 |
m_txTrace (p); |
| 301 |
m_socket->Send (p); |
| 302 |
|
| 303 |
++m_sent; |
| 304 |
|
| 305 |
if (InetSocketAddress::IsMatchingType (m_peerAddress)) |
| 306 |
{ |
| 307 |
NS_LOG_INFO ("Sent " << m_size << " bytes to " << |
| 308 |
InetSocketAddress::ConvertFrom (m_peerAddress)); |
| 309 |
} |
| 310 |
else if (Inet6SocketAddress::IsMatchingType (m_peerAddress)) |
| 311 |
{ |
| 312 |
NS_LOG_INFO ("Sent " << m_size << " bytes to " << |
| 313 |
Inet6SocketAddress::ConvertFrom (m_peerAddress)); |
| 314 |
} |
| 315 |
|
| 316 |
if (m_sent < m_count) |
| 317 |
{ |
| 318 |
ScheduleTransmit (m_interval); |
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
void |
| 323 |
TcpEchoClient::ReceivePacket (Ptr<Socket> socket) |
| 324 |
{ |
| 325 |
NS_LOG_FUNCTION (this << socket); |
| 326 |
Ptr<Packet> packet; |
| 327 |
Address from; |
| 328 |
while ((packet = socket->RecvFrom (from))) |
| 329 |
{ |
| 330 |
if (InetSocketAddress::IsMatchingType (from)) |
| 331 |
{ |
| 332 |
NS_LOG_INFO ("Received " << packet->GetSize () << " bytes from " << |
| 333 |
InetSocketAddress::ConvertFrom (from).GetIpv4 ()); |
| 334 |
} |
| 335 |
else if (Inet6SocketAddress::IsMatchingType (from)) |
| 336 |
{ |
| 337 |
NS_LOG_INFO ("Received " << packet->GetSize () << " bytes from " << |
| 338 |
Inet6SocketAddress::ConvertFrom (from).GetIpv6 ()); |
| 339 |
} |
| 340 |
|
| 341 |
// don't check if data returned is the same data sent earlier |
| 342 |
m_recvBack++; |
| 343 |
m_bytesRecvBack += packet->GetSize (); |
| 344 |
} |
| 345 |
|
| 346 |
if (m_count == m_recvBack) |
| 347 |
{ |
| 348 |
socket->Close(); |
| 349 |
m_socket->SetRecvCallback (MakeNullCallback<void, Ptr<Socket> > ()); |
| 350 |
socket = 0; |
| 351 |
} |
| 352 |
} |
| 353 |
|
| 354 |
} // Namespace ns3 |