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 "bulk-send-application.h"
33 
34 namespace ns3 {
35 
36 NS_LOG_COMPONENT_DEFINE ("BulkSendApplication");
37 
38 NS_OBJECT_ENSURE_REGISTERED (BulkSendApplication);
39 
40 TypeId
42 {
43  static TypeId tid = TypeId ("ns3::BulkSendApplication")
45  .SetGroupName("Applications")
46  .AddConstructor<BulkSendApplication> ()
47  .AddAttribute ("SendSize", "The amount of data to send each time.",
48  UintegerValue (512),
50  MakeUintegerChecker<uint32_t> (1))
51  .AddAttribute ("Remote", "The address of the destination",
52  AddressValue (),
55  .AddAttribute ("MaxBytes",
56  "The total number of bytes to send. "
57  "Once these bytes are sent, "
58  "no data is sent again. The value zero means "
59  "that there is no limit.",
60  UintegerValue (0),
62  MakeUintegerChecker<uint64_t> ())
63  .AddAttribute ("Protocol", "The type of protocol to use.",
67  .AddTraceSource ("Tx", "A new packet is created and is sent",
69  "ns3::Packet::TracedCallback")
70  ;
71  return tid;
72 }
73 
74 
76  : m_socket (0),
77  m_connected (false),
78  m_totBytes (0)
79 {
80  NS_LOG_FUNCTION (this);
81 }
82 
84 {
85  NS_LOG_FUNCTION (this);
86 }
87 
88 void
90 {
91  NS_LOG_FUNCTION (this << maxBytes);
92  m_maxBytes = maxBytes;
93 }
94 
97 {
98  NS_LOG_FUNCTION (this);
99  return m_socket;
100 }
101 
102 void
104 {
105  NS_LOG_FUNCTION (this);
106 
107  m_socket = 0;
108  // chain up
110 }
111 
112 // Application Methods
113 void BulkSendApplication::StartApplication (void) // Called at time specified by Start
114 {
115  NS_LOG_FUNCTION (this);
116 
117  // Create the socket if not already
118  if (!m_socket)
119  {
121 
122  // Fatal error if socket type is not NS3_SOCK_STREAM or NS3_SOCK_SEQPACKET
125  {
126  NS_FATAL_ERROR ("Using BulkSend with an incompatible socket type. "
127  "BulkSend requires SOCK_STREAM or SOCK_SEQPACKET. "
128  "In other words, use TCP instead of UDP.");
129  }
130 
132  {
133  m_socket->Bind6 ();
134  }
136  {
137  m_socket->Bind ();
138  }
139 
147  }
148  if (m_connected)
149  {
150  SendData ();
151  }
152 }
153 
154 void BulkSendApplication::StopApplication (void) // Called at time specified by Stop
155 {
156  NS_LOG_FUNCTION (this);
157 
158  if (m_socket != 0)
159  {
160  m_socket->Close ();
161  m_connected = false;
162  }
163  else
164  {
165  NS_LOG_WARN ("BulkSendApplication found null socket to close in StopApplication");
166  }
167 }
168 
169 
170 // Private helpers
171 
173 {
174  NS_LOG_FUNCTION (this);
175 
176  while (m_maxBytes == 0 || m_totBytes < m_maxBytes)
177  { // Time to send more
178 
179  // uint64_t to allow the comparison later.
180  // the result is in a uint32_t range anyway, because
181  // m_sendSize is uint32_t.
182  uint64_t toSend = m_sendSize;
183  // Make sure we don't send too many
184  if (m_maxBytes > 0)
185  {
186  toSend = std::min (toSend, m_maxBytes - m_totBytes);
187  }
188 
189  NS_LOG_LOGIC ("sending packet at " << Simulator::Now ());
190  Ptr<Packet> packet = Create<Packet> (toSend);
191  m_txTrace (packet);
192  int actual = m_socket->Send (packet);
193  if (actual > 0)
194  {
195  m_totBytes += actual;
196  }
197  // We exit this loop when actual < toSend as the send side
198  // buffer is full. The "DataSent" callback will pop when
199  // some buffer space has freed ip.
200  if ((unsigned)actual != toSend)
201  {
202  break;
203  }
204  }
205  // Check if time to close (all sent)
207  {
208  m_socket->Close ();
209  m_connected = false;
210  }
211 }
212 
214 {
215  NS_LOG_FUNCTION (this << socket);
216  NS_LOG_LOGIC ("BulkSendApplication Connection succeeded");
217  m_connected = true;
218  SendData ();
219 }
220 
222 {
223  NS_LOG_FUNCTION (this << socket);
224  NS_LOG_LOGIC ("BulkSendApplication, Connection Failed");
225 }
226 
228 {
229  NS_LOG_FUNCTION (this);
230 
231  if (m_connected)
232  { // Only send new data if the connection has completed
233  SendData ();
234  }
235 }
236 
237 
238 
239 } // Namespace ns3
void SendData()
Send data until the L4 transmission buffer is full.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:44
virtual int Bind6()=0
Allocate a local IPv6 endpoint for this socket.
#define min(a, b)
Definition: 80211b.c:44
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:201
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.
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
Ptr< Node > GetNode() const
Definition: application.cc:104
AttributeValue implementation for TypeId.
Definition: type-id.h:608
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1489
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
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:252
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 > MakeAddressChecker(void)
Definition: address.cc:172
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:224
void SetSendCallback(Callback< void, Ptr< Socket >, uint32_t > sendCb)
Notify application when space in transmit buffer is added.
Definition: socket.cc:121
Ptr< Socket > m_socket
Associated socket.
void ConnectionSucceeded(Ptr< Socket > socket)
Connection Succeeded (called by Socket through a callback)
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:608
AttributeValue implementation for Address.
Definition: address.h:278
#define NS_LOG_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
Definition: log.h:228
void SetMaxBytes(uint64_t maxBytes)
Set the upper bound for the total number of bytes to send.
bool m_connected
True if connected.
Address m_peer
Peer address.
static bool IsMatchingType(const Address &addr)
If the address match.
static TypeId GetTypeId(void)
Get the type ID.
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< Socket > GetSocket(void) const
Get the socket this application is attached to.
Ptr< const AttributeChecker > MakeTypeIdChecker(void)
Definition: type-id.cc:1202
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
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:904
static bool IsMatchingType(const Address &address)