A Discrete-Event Network Simulator
API
 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 
41 TypeId
43 {
44  static TypeId tid = TypeId ("ns3::BulkSendApplication")
46  .AddConstructor<BulkSendApplication> ()
47  .AddAttribute ("SendSize", "The amount of data to send each time.",
48  UintegerValue (512),
49  MakeUintegerAccessor (&BulkSendApplication::m_sendSize),
50  MakeUintegerChecker<uint32_t> (1))
51  .AddAttribute ("Remote", "The address of the destination",
52  AddressValue (),
53  MakeAddressAccessor (&BulkSendApplication::m_peer),
54  MakeAddressChecker ())
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),
61  MakeUintegerAccessor (&BulkSendApplication::m_maxBytes),
62  MakeUintegerChecker<uint32_t> ())
63  .AddAttribute ("Protocol", "The type of protocol to use.",
65  MakeTypeIdAccessor (&BulkSendApplication::m_tid),
66  MakeTypeIdChecker ())
67  .AddTraceSource ("Tx", "A new packet is created and is sent",
69  ;
70  return tid;
71 }
72 
73 
75  : m_socket (0),
76  m_connected (false),
77  m_totBytes (0)
78 {
79  NS_LOG_FUNCTION (this);
80 }
81 
83 {
84  NS_LOG_FUNCTION (this);
85 }
86 
87 void
89 {
90  NS_LOG_FUNCTION (this << maxBytes);
91  m_maxBytes = maxBytes;
92 }
93 
96 {
97  NS_LOG_FUNCTION (this);
98  return m_socket;
99 }
100 
101 void
103 {
104  NS_LOG_FUNCTION (this);
105 
106  m_socket = 0;
107  // chain up
109 }
110 
111 // Application Methods
112 void BulkSendApplication::StartApplication (void) // Called at time specified by Start
113 {
114  NS_LOG_FUNCTION (this);
115 
116  // Create the socket if not already
117  if (!m_socket)
118  {
120 
121  // Fatal error if socket type is not NS3_SOCK_STREAM or NS3_SOCK_SEQPACKET
124  {
125  NS_FATAL_ERROR ("Using BulkSend with an incompatible socket type. "
126  "BulkSend requires SOCK_STREAM or SOCK_SEQPACKET. "
127  "In other words, use TCP instead of UDP.");
128  }
129 
131  {
132  m_socket->Bind6 ();
133  }
135  {
136  m_socket->Bind ();
137  }
138 
146  }
147  if (m_connected)
148  {
149  SendData ();
150  }
151 }
152 
153 void BulkSendApplication::StopApplication (void) // Called at time specified by Stop
154 {
155  NS_LOG_FUNCTION (this);
156 
157  if (m_socket != 0)
158  {
159  m_socket->Close ();
160  m_connected = false;
161  }
162  else
163  {
164  NS_LOG_WARN ("BulkSendApplication found null socket to close in StopApplication");
165  }
166 }
167 
168 
169 // Private helpers
170 
172 {
173  NS_LOG_FUNCTION (this);
174 
175  while (m_maxBytes == 0 || m_totBytes < m_maxBytes)
176  { // Time to send more
177  uint32_t toSend = m_sendSize;
178  // Make sure we don't send too many
179  if (m_maxBytes > 0)
180  {
181  toSend = std::min (m_sendSize, m_maxBytes - m_totBytes);
182  }
183  NS_LOG_LOGIC ("sending packet at " << Simulator::Now ());
184  Ptr<Packet> packet = Create<Packet> (toSend);
185  m_txTrace (packet);
186  int actual = m_socket->Send (packet);
187  if (actual > 0)
188  {
189  m_totBytes += actual;
190  }
191  // We exit this loop when actual < toSend as the send side
192  // buffer is full. The "DataSent" callback will pop when
193  // some buffer space has freed ip.
194  if ((unsigned)actual != toSend)
195  {
196  break;
197  }
198  }
199  // Check if time to close (all sent)
201  {
202  m_socket->Close ();
203  m_connected = false;
204  }
205 }
206 
208 {
209  NS_LOG_FUNCTION (this << socket);
210  NS_LOG_LOGIC ("BulkSendApplication Connection succeeded");
211  m_connected = true;
212  SendData ();
213 }
214 
216 {
217  NS_LOG_FUNCTION (this << socket);
218  NS_LOG_LOGIC ("BulkSendApplication, Connection Failed");
219 }
220 
222 {
223  NS_LOG_FUNCTION (this);
224 
225  if (m_connected)
226  { // Only send new data if the connection has completed
228  }
229 }
230 
231 
232 
233 } // Namespace ns3
void SendData()
Send data until the L4 transmission buffer is full.
uint32_t m_totBytes
Total bytes sent so far.
#define NS_LOG_FUNCTION(parameters)
Definition: log.h:345
virtual int Bind6()=0
Allocate a local IPv6 endpoint for this socket.
void ConnectionFailed(Ptr< Socket > socket)
Connection Failed (called by Socket through a callback)
NS_OBJECT_ENSURE_REGISTERED(NullMessageSimulatorImpl)
virtual int ShutdownRecv(void)=0
uint32_t m_maxBytes
Limit total number of bytes sent.
virtual void DoDispose(void)
This method is called by Object::Dispose or by the object's destructor, whichever comes first...
NS_LOG_COMPONENT_DEFINE("BulkSendApplication")
#define NS_FATAL_ERROR(msg)
fatal error handling
Definition: fatal-error.h:72
virtual void StopApplication(void)
Application specific shutdown code.
The base class for all ns3 applications.
Definition: application.h:61
virtual void StartApplication(void)
Application specific startup code.
Hold an unsigned integer type.
Definition: uinteger.h:46
Ptr< Node > GetNode() const
Definition: application.cc:104
hold objects of type ns3::TypeId
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1238
void SetMaxBytes(uint32_t maxBytes)
Set the upper bound for the total number of bytes to send.
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)
Definition: log.h:368
TypeId m_tid
The type of protocol to use.
static TypeId GetTypeId(void)
Get the type ID.
virtual int Connect(const Address &address)=0
Initiate a connection to a remote host.
virtual void DoDispose(void)
This method is called by Object::Dispose or by the object's destructor, whichever comes first...
Definition: application.cc:83
virtual int Bind(const Address &address)=0
Allocate a local endpoint for this socket.
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
static EventId ScheduleNow(MEM mem_ptr, OBJ obj)
Schedule an event to expire Now.
Definition: simulator.h:985
static Time Now(void)
Return the "current simulation time".
Definition: simulator.cc:180
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)
hold objects of type ns3::Address
#define NS_LOG_WARN(msg)
Definition: log.h:280
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.
Ptr< Socket > GetSocket(void) const
Get the socket this application is attached to.
TracedCallback< Ptr< const Packet > > m_txTrace
Traced Callback: sent packets.
a unique identifier for an interface.
Definition: type-id.h:49
TypeId SetParent(TypeId tid)
Definition: type-id.cc:611
static bool IsMatchingType(const Address &address)