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  if (m_socket->Bind6 () == -1)
134  {
135  NS_FATAL_ERROR ("Failed to bind socket");
136  }
137  }
139  {
140  if (m_socket->Bind () == -1)
141  {
142  NS_FATAL_ERROR ("Failed to bind socket");
143  }
144  }
145 
153  }
154  if (m_connected)
155  {
156  SendData ();
157  }
158 }
159 
160 void BulkSendApplication::StopApplication (void) // Called at time specified by Stop
161 {
162  NS_LOG_FUNCTION (this);
163 
164  if (m_socket != 0)
165  {
166  m_socket->Close ();
167  m_connected = false;
168  }
169  else
170  {
171  NS_LOG_WARN ("BulkSendApplication found null socket to close in StopApplication");
172  }
173 }
174 
175 
176 // Private helpers
177 
179 {
180  NS_LOG_FUNCTION (this);
181 
182  while (m_maxBytes == 0 || m_totBytes < m_maxBytes)
183  { // Time to send more
184 
185  // uint64_t to allow the comparison later.
186  // the result is in a uint32_t range anyway, because
187  // m_sendSize is uint32_t.
188  uint64_t toSend = m_sendSize;
189  // Make sure we don't send too many
190  if (m_maxBytes > 0)
191  {
192  toSend = std::min (toSend, m_maxBytes - m_totBytes);
193  }
194 
195  NS_LOG_LOGIC ("sending packet at " << Simulator::Now ());
196  Ptr<Packet> packet = Create<Packet> (toSend);
197  int actual = m_socket->Send (packet);
198  if (actual > 0)
199  {
200  m_totBytes += actual;
201  m_txTrace (packet);
202  }
203  // We exit this loop when actual < toSend as the send side
204  // buffer is full. The "DataSent" callback will pop when
205  // some buffer space has freed up.
206  if ((unsigned)actual != toSend)
207  {
208  break;
209  }
210  }
211  // Check if time to close (all sent)
213  {
214  m_socket->Close ();
215  m_connected = false;
216  }
217 }
218 
220 {
221  NS_LOG_FUNCTION (this << socket);
222  NS_LOG_LOGIC ("BulkSendApplication Connection succeeded");
223  m_connected = true;
224  SendData ();
225 }
226 
228 {
229  NS_LOG_FUNCTION (this << socket);
230  NS_LOG_LOGIC ("BulkSendApplication, Connection Failed");
231 }
232 
234 {
235  NS_LOG_FUNCTION (this);
236 
237  if (m_connected)
238  { // Only send new data if the connection has completed
239  SendData ();
240  }
241 }
242 
243 
244 
245 } // Namespace ns3
Ptr< Socket > GetSocket(void) const
Get the socket this application is attached to.
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:45
virtual int Bind6()=0
Allocate a local IPv6 endpoint for this socket.
#define min(a, b)
Definition: 80211b.c:42
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:204
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
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
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 > MakeAddressChecker(void)
Definition: address.cc:172
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:193
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)
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:264
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< const AttributeChecker > MakeTypeIdChecker(void)
Definition: type-id.cc:1225
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:915
static bool IsMatchingType(const Address &address)