A Discrete-Event Network Simulator
API
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 "ns3/boolean.h"
33 #include "bulk-send-application.h"
34 
35 namespace ns3 {
36 
37 NS_LOG_COMPONENT_DEFINE ("BulkSendApplication");
38 
39 NS_OBJECT_ENSURE_REGISTERED (BulkSendApplication);
40 
41 TypeId
43 {
44  static TypeId tid = TypeId ("ns3::BulkSendApplication")
46  .SetGroupName("Applications")
47  .AddConstructor<BulkSendApplication> ()
48  .AddAttribute ("SendSize", "The amount of data to send each time.",
49  UintegerValue (512),
51  MakeUintegerChecker<uint32_t> (1))
52  .AddAttribute ("Remote", "The address of the destination",
53  AddressValue (),
56  .AddAttribute ("Local",
57  "The Address on which to bind the socket. If not set, it is generated automatically.",
58  AddressValue (),
61  .AddAttribute ("MaxBytes",
62  "The total number of bytes to send. "
63  "Once these bytes are sent, "
64  "no data is sent again. The value zero means "
65  "that there is no limit.",
66  UintegerValue (0),
68  MakeUintegerChecker<uint64_t> ())
69  .AddAttribute ("Protocol", "The type of protocol to use.",
73  .AddAttribute ("EnableSeqTsSizeHeader",
74  "Add SeqTsSizeHeader to each packet",
75  BooleanValue (false),
78  .AddTraceSource ("Tx", "A new packet is sent",
80  "ns3::Packet::TracedCallback")
81  .AddTraceSource ("TxWithSeqTsSize", "A new packet is created with SeqTsSizeHeader",
83  "ns3::PacketSink::SeqTsSizeCallback")
84  ;
85  return tid;
86 }
87 
88 
90  : m_socket (0),
91  m_connected (false),
92  m_totBytes (0),
93  m_unsentPacket (0)
94 {
95  NS_LOG_FUNCTION (this);
96 }
97 
99 {
100  NS_LOG_FUNCTION (this);
101 }
102 
103 void
105 {
106  NS_LOG_FUNCTION (this << maxBytes);
107  m_maxBytes = maxBytes;
108 }
109 
112 {
113  NS_LOG_FUNCTION (this);
114  return m_socket;
115 }
116 
117 void
119 {
120  NS_LOG_FUNCTION (this);
121 
122  m_socket = 0;
123  m_unsentPacket = 0;
124  // chain up
126 }
127 
128 // Application Methods
129 void BulkSendApplication::StartApplication (void) // Called at time specified by Start
130 {
131  NS_LOG_FUNCTION (this);
132  Address from;
133 
134  // Create the socket if not already
135  if (!m_socket)
136  {
138  int ret = -1;
139 
140  // Fatal error if socket type is not NS3_SOCK_STREAM or NS3_SOCK_SEQPACKET
143  {
144  NS_FATAL_ERROR ("Using BulkSend with an incompatible socket type. "
145  "BulkSend requires SOCK_STREAM or SOCK_SEQPACKET. "
146  "In other words, use TCP instead of UDP.");
147  }
148 
149  if (! m_local.IsInvalid())
150  {
153  "Incompatible peer and local address IP version");
154  ret = m_socket->Bind (m_local);
155  }
156  else
157  {
159  {
160  ret = m_socket->Bind6 ();
161  }
163  {
164  ret = m_socket->Bind ();
165  }
166  }
167 
168  if (ret == -1)
169  {
170  NS_FATAL_ERROR ("Failed to bind socket");
171  }
172 
180  }
181  if (m_connected)
182  {
183  m_socket->GetSockName (from);
184  SendData (from, m_peer);
185  }
186 }
187 
188 void BulkSendApplication::StopApplication (void) // Called at time specified by Stop
189 {
190  NS_LOG_FUNCTION (this);
191 
192  if (m_socket != 0)
193  {
194  m_socket->Close ();
195  m_connected = false;
196  }
197  else
198  {
199  NS_LOG_WARN ("BulkSendApplication found null socket to close in StopApplication");
200  }
201 }
202 
203 
204 // Private helpers
205 
206 void BulkSendApplication::SendData (const Address &from, const Address &to)
207 {
208  NS_LOG_FUNCTION (this);
209 
210  while (m_maxBytes == 0 || m_totBytes < m_maxBytes)
211  { // Time to send more
212 
213  // uint64_t to allow the comparison later.
214  // the result is in a uint32_t range anyway, because
215  // m_sendSize is uint32_t.
216  uint64_t toSend = m_sendSize;
217  // Make sure we don't send too many
218  if (m_maxBytes > 0)
219  {
220  toSend = std::min (toSend, m_maxBytes - m_totBytes);
221  }
222 
223  NS_LOG_LOGIC ("sending packet at " << Simulator::Now ());
224 
225  Ptr<Packet> packet;
226  if (m_unsentPacket)
227  {
228  packet = m_unsentPacket;
229  }
230  else if (m_enableSeqTsSizeHeader)
231  {
232  SeqTsSizeHeader header;
233  header.SetSeq (m_seq++);
234  header.SetSize (toSend);
235  NS_ABORT_IF (toSend < header.GetSerializedSize ());
236  packet = Create<Packet> (toSend - header.GetSerializedSize ());
237  // Trace before adding header, for consistency with PacketSink
238  m_txTraceWithSeqTsSize (packet, from, to, header);
239  packet->AddHeader (header);
240  }
241  else
242  {
243  packet = Create<Packet> (toSend);
244  }
245 
246  int actual = m_socket->Send (packet);
247  if ((unsigned) actual == toSend)
248  {
249  m_totBytes += actual;
250  m_txTrace (packet);
251  m_unsentPacket = 0;
252  }
253  else if (actual == -1)
254  {
255  // We exit this loop when actual < toSend as the send side
256  // buffer is full. The "DataSent" callback will pop when
257  // some buffer space has freed up.
258  NS_LOG_DEBUG ("Unable to send packet; caching for later attempt");
259  m_unsentPacket = packet;
260  break;
261  }
262  else
263  {
264  NS_FATAL_ERROR ("Unexpected return value from m_socket->Send ()");
265  }
266  }
267  // Check if time to close (all sent)
269  {
270  m_socket->Close ();
271  m_connected = false;
272  }
273 }
274 
276 {
277  NS_LOG_FUNCTION (this << socket);
278  NS_LOG_LOGIC ("BulkSendApplication Connection succeeded");
279  m_connected = true;
280  Address from, to;
281  socket->GetSockName (from);
282  socket->GetPeerName (to);
283  SendData (from, to);
284 }
285 
287 {
288  NS_LOG_FUNCTION (this << socket);
289  NS_LOG_LOGIC ("BulkSendApplication, Connection Failed");
290 }
291 
293 {
294  NS_LOG_FUNCTION (this);
295 
296  if (m_connected)
297  { // Only send new data if the connection has completed
298  Address from, to;
299  socket->GetSockName (from);
300  socket->GetPeerName (to);
301  SendData (from, to);
302  }
303 }
304 
305 
306 
307 } // Namespace ns3
Ptr< Socket > GetSocket(void) const
Get the socket this application is attached to.
void SendData(const Address &from, const Address &to)
Send data until the L4 transmission buffer is full.
bool IsInvalid(void) const
Definition: address.cc:68
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
AttributeValue implementation for Boolean.
Definition: boolean.h:36
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
virtual int Bind6()=0
Allocate a local IPv6 endpoint for this socket.
TracedCallback< Ptr< const Packet >, const Address &, const Address &, const SeqTsSizeHeader & > m_txTraceWithSeqTsSize
Callback for tracing the packet Tx events, includes source, destination, the packet sent...
#define min(a, b)
Definition: 80211b.c:42
Header with a sequence, a timestamp, and a "size" attribute.
bool m_enableSeqTsSizeHeader
Enable or disable the SeqTsSizeHeader.
Ptr< const AttributeAccessor > MakeBooleanAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
Definition: boolean.h:85
void ConnectionFailed(Ptr< Socket > socket)
Connection Failed (called by Socket through a callback)
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
virtual int GetSockName(Address &address) const =0
Get socket address.
virtual int ShutdownRecv(void)=0
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:162
virtual void DoDispose(void)
Destructor implementation.
a polymophic address class
Definition: address.h:90
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
virtual void StopApplication(void)
Application specific shutdown code.
Ptr< const AttributeAccessor > MakeAddressAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
Definition: address.h:278
The base class for all ns3 applications.
Definition: application.h:60
Send as much traffic as possible, trying to fill the bandwidth.
virtual void StartApplication(void)
Application specific startup code.
Hold an unsigned integer type.
Definition: uinteger.h:44
AttributeValue implementation for TypeId.
Definition: type-id.h:595
virtual uint32_t GetSerializedSize(void) const override
void DataSend(Ptr< Socket >, uint32_t)
Send more data as soon as some has been transmitted.
static Ptr< Socket > CreateSocket(Ptr< Node > node, TypeId tid)
This method wraps the creation of sockets that is performed on a given node by a SocketFactory specif...
Definition: socket.cc:71
virtual enum Socket::SocketType GetSocketType(void) const =0
Ptr< Node > GetNode() const
Definition: application.cc:104
TypeId m_tid
The type of protocol to use.
static TypeId GetTypeId(void)
Get the type ID.
uint64_t m_totBytes
Total bytes sent so far.
virtual int Connect(const Address &address)=0
Initiate a connection to a remote host.
virtual void DoDispose(void)
Destructor implementation.
Definition: application.cc:83
virtual int Bind(const Address &address)=0
Allocate a local endpoint for this socket.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< const AttributeChecker > MakeTypeIdChecker(void)
Definition: type-id.cc:1227
Address m_local
Local address to bind to.
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:195
void SetSendCallback(Callback< void, Ptr< Socket >, uint32_t > sendCb)
Notify application when space in transmit buffer is added.
Definition: socket.cc:121
NS_LOG_LOGIC("Net device "<< nd<< " is not bridged")
Ptr< Socket > m_socket
Associated socket.
void ConnectionSucceeded(Ptr< Socket > socket)
Connection Succeeded (called by Socket through a callback)
#define NS_ABORT_IF(cond)
Abnormal program termination if a condition is true.
Definition: abort.h:77
Ptr< const AttributeAccessor > MakeTypeIdAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
Definition: type-id.h:595
AttributeValue implementation for Address.
Definition: address.h:278
#define NS_ABORT_MSG_IF(cond, msg)
Abnormal program termination if a condition is true, with a message.
Definition: abort.h:108
#define NS_LOG_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
Definition: log.h:265
void SetMaxBytes(uint64_t maxBytes)
Set the upper bound for the total number of bytes to send.
void SetSeq(uint32_t seq)
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:273
Ptr< const AttributeChecker > MakeBooleanChecker(void)
Definition: boolean.cc:121
bool m_connected
True if connected.
Address m_peer
Peer address.
static bool IsMatchingType(const Address &addr)
If the address match.
void SetSize(uint64_t size)
Set the size information that the header will carry.
static TypeId GetTypeId(void)
Get the type ID.
virtual int GetPeerName(Address &address) const =0
Get the peer address of a connected socket.
void SetConnectCallback(Callback< void, Ptr< Socket > > connectionSucceeded, Callback< void, Ptr< Socket > > connectionFailed)
Specify callbacks to allow the caller to determine if the connection succeeds of fails.
Definition: socket.cc:84
uint32_t m_sendSize
Size of data to send each time.
virtual int Send(Ptr< Packet > p, uint32_t flags)=0
Send data (or dummy data) to the remote host.
virtual int Close(void)=0
Close a socket.
uint64_t m_maxBytes
Limit total number of bytes sent.
Ptr< Packet > m_unsentPacket
Variable to cache unsent packet.
TracedCallback< Ptr< const Packet > > m_txTrace
Traced Callback: sent packets.
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
Definition: uinteger.h:45
a unique identifier for an interface.
Definition: type-id.h:58
Ptr< const AttributeChecker > MakeAddressChecker(void)
Definition: address.cc:172
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:923
static bool IsMatchingType(const Address &address)
Callback< R, Ts... > MakeCallback(R(T::*memPtr)(Ts...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:1642