A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
udp-echo-client.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright 2007 University of Washington
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 #include "ns3/log.h"
19 #include "ns3/ipv4-address.h"
20 #include "ns3/ipv6-address.h"
21 #include "ns3/nstime.h"
22 #include "ns3/inet-socket-address.h"
23 #include "ns3/inet6-socket-address.h"
24 #include "ns3/socket.h"
25 #include "ns3/simulator.h"
26 #include "ns3/socket-factory.h"
27 #include "ns3/packet.h"
28 #include "ns3/uinteger.h"
29 #include "ns3/trace-source-accessor.h"
30 #include "udp-echo-client.h"
31 
32 namespace ns3 {
33 
34 NS_LOG_COMPONENT_DEFINE ("UdpEchoClientApplication")
35  ;
36 NS_OBJECT_ENSURE_REGISTERED (UdpEchoClient)
37  ;
38 
39 TypeId
41 {
42  static TypeId tid = TypeId ("ns3::UdpEchoClient")
44  .AddConstructor<UdpEchoClient> ()
45  .AddAttribute ("MaxPackets",
46  "The maximum number of packets the application will send",
47  UintegerValue (100),
48  MakeUintegerAccessor (&UdpEchoClient::m_count),
49  MakeUintegerChecker<uint32_t> ())
50  .AddAttribute ("Interval",
51  "The time to wait between packets",
52  TimeValue (Seconds (1.0)),
53  MakeTimeAccessor (&UdpEchoClient::m_interval),
54  MakeTimeChecker ())
55  .AddAttribute ("RemoteAddress",
56  "The destination Address of the outbound packets",
57  AddressValue (),
58  MakeAddressAccessor (&UdpEchoClient::m_peerAddress),
59  MakeAddressChecker ())
60  .AddAttribute ("RemotePort",
61  "The destination port of the outbound packets",
62  UintegerValue (0),
63  MakeUintegerAccessor (&UdpEchoClient::m_peerPort),
64  MakeUintegerChecker<uint16_t> ())
65  .AddAttribute ("PacketSize", "Size of echo data in outbound packets",
66  UintegerValue (100),
67  MakeUintegerAccessor (&UdpEchoClient::SetDataSize,
69  MakeUintegerChecker<uint32_t> ())
70  .AddTraceSource ("Tx", "A new packet is created and is sent",
72  ;
73  return tid;
74 }
75 
77 {
78  NS_LOG_FUNCTION (this);
79  m_sent = 0;
80  m_socket = 0;
81  m_sendEvent = EventId ();
82  m_data = 0;
83  m_dataSize = 0;
84 }
85 
87 {
88  NS_LOG_FUNCTION (this);
89  m_socket = 0;
90 
91  delete [] m_data;
92  m_data = 0;
93  m_dataSize = 0;
94 }
95 
96 void
98 {
99  NS_LOG_FUNCTION (this << ip << port);
100  m_peerAddress = ip;
101  m_peerPort = port;
102 }
103 
104 void
106 {
107  NS_LOG_FUNCTION (this << ip << port);
108  m_peerAddress = Address (ip);
109  m_peerPort = port;
110 }
111 
112 void
114 {
115  NS_LOG_FUNCTION (this << ip << port);
116  m_peerAddress = Address (ip);
117  m_peerPort = port;
118 }
119 
120 void
122 {
123  NS_LOG_FUNCTION (this);
125 }
126 
127 void
129 {
130  NS_LOG_FUNCTION (this);
131 
132  if (m_socket == 0)
133  {
134  TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory");
137  {
138  m_socket->Bind();
140  }
141  else if (Ipv6Address::IsMatchingType(m_peerAddress) == true)
142  {
143  m_socket->Bind6();
145  }
146  }
147 
149 
150  ScheduleTransmit (Seconds (0.));
151 }
152 
153 void
155 {
156  NS_LOG_FUNCTION (this);
157 
158  if (m_socket != 0)
159  {
160  m_socket->Close ();
162  m_socket = 0;
163  }
164 
166 }
167 
168 void
169 UdpEchoClient::SetDataSize (uint32_t dataSize)
170 {
171  NS_LOG_FUNCTION (this << dataSize);
172 
173  //
174  // If the client is setting the echo packet data size this way, we infer
175  // that she doesn't care about the contents of the packet at all, so
176  // neither will we.
177  //
178  delete [] m_data;
179  m_data = 0;
180  m_dataSize = 0;
181  m_size = dataSize;
182 }
183 
184 uint32_t
186 {
187  NS_LOG_FUNCTION (this);
188  return m_size;
189 }
190 
191 void
192 UdpEchoClient::SetFill (std::string fill)
193 {
194  NS_LOG_FUNCTION (this << fill);
195 
196  uint32_t dataSize = fill.size () + 1;
197 
198  if (dataSize != m_dataSize)
199  {
200  delete [] m_data;
201  m_data = new uint8_t [dataSize];
202  m_dataSize = dataSize;
203  }
204 
205  memcpy (m_data, fill.c_str (), dataSize);
206 
207  //
208  // Overwrite packet size attribute.
209  //
210  m_size = dataSize;
211 }
212 
213 void
214 UdpEchoClient::SetFill (uint8_t fill, uint32_t dataSize)
215 {
216  NS_LOG_FUNCTION (this << fill << dataSize);
217  if (dataSize != m_dataSize)
218  {
219  delete [] m_data;
220  m_data = new uint8_t [dataSize];
221  m_dataSize = dataSize;
222  }
223 
224  memset (m_data, fill, dataSize);
225 
226  //
227  // Overwrite packet size attribute.
228  //
229  m_size = dataSize;
230 }
231 
232 void
233 UdpEchoClient::SetFill (uint8_t *fill, uint32_t fillSize, uint32_t dataSize)
234 {
235  NS_LOG_FUNCTION (this << fill << fillSize << dataSize);
236  if (dataSize != m_dataSize)
237  {
238  delete [] m_data;
239  m_data = new uint8_t [dataSize];
240  m_dataSize = dataSize;
241  }
242 
243  if (fillSize >= dataSize)
244  {
245  memcpy (m_data, fill, dataSize);
246  m_size = dataSize;
247  return;
248  }
249 
250  //
251  // Do all but the final fill.
252  //
253  uint32_t filled = 0;
254  while (filled + fillSize < dataSize)
255  {
256  memcpy (&m_data[filled], fill, fillSize);
257  filled += fillSize;
258  }
259 
260  //
261  // Last fill may be partial
262  //
263  memcpy (&m_data[filled], fill, dataSize - filled);
264 
265  //
266  // Overwrite packet size attribute.
267  //
268  m_size = dataSize;
269 }
270 
271 void
273 {
274  NS_LOG_FUNCTION (this << dt);
276 }
277 
278 void
280 {
281  NS_LOG_FUNCTION (this);
282 
284 
285  Ptr<Packet> p;
286  if (m_dataSize)
287  {
288  //
289  // If m_dataSize is non-zero, we have a data buffer of the same size that we
290  // are expected to copy and send. This state of affairs is created if one of
291  // the Fill functions is called. In this case, m_size must have been set
292  // to agree with m_dataSize
293  //
294  NS_ASSERT_MSG (m_dataSize == m_size, "UdpEchoClient::Send(): m_size and m_dataSize inconsistent");
295  NS_ASSERT_MSG (m_data, "UdpEchoClient::Send(): m_dataSize but no m_data");
296  p = Create<Packet> (m_data, m_dataSize);
297  }
298  else
299  {
300  //
301  // If m_dataSize is zero, the client has indicated that it doesn't care
302  // about the data itself either by specifying the data size by setting
303  // the corresponding attribute or by not calling a SetFill function. In
304  // this case, we don't worry about it either. But we do allow m_size
305  // to have a value different from the (zero) m_dataSize.
306  //
307  p = Create<Packet> (m_size);
308  }
309  // call to the trace sinks before the packet is actually sent,
310  // so that tags added to the packet can be sent as well
311  m_txTrace (p);
312  m_socket->Send (p);
313 
314  ++m_sent;
315 
317  {
318  NS_LOG_INFO ("At time " << Simulator::Now ().GetSeconds () << "s client sent " << m_size << " bytes to " <<
320  }
322  {
323  NS_LOG_INFO ("At time " << Simulator::Now ().GetSeconds () << "s client sent " << m_size << " bytes to " <<
325  }
326 
327  if (m_sent < m_count)
328  {
330  }
331 }
332 
333 void
335 {
336  NS_LOG_FUNCTION (this << socket);
337  Ptr<Packet> packet;
338  Address from;
339  while ((packet = socket->RecvFrom (from)))
340  {
342  {
343  NS_LOG_INFO ("At time " << Simulator::Now ().GetSeconds () << "s client received " << packet->GetSize () << " bytes from " <<
344  InetSocketAddress::ConvertFrom (from).GetIpv4 () << " port " <<
346  }
347  else if (Inet6SocketAddress::IsMatchingType (from))
348  {
349  NS_LOG_INFO ("At time " << Simulator::Now ().GetSeconds () << "s client received " << packet->GetSize () << " bytes from " <<
350  Inet6SocketAddress::ConvertFrom (from).GetIpv6 () << " port " <<
352  }
353  }
354 }
355 
356 } // Namespace ns3
static bool IsMatchingType(const Address &address)
If the Address matches the type.
Ipv6Address GetIpv6(void) const
Get the IPv6 address.
keep track of time values and allow control of global simulation resolution
Definition: nstime.h:81
an Inet address class
Ipv4Address GetIpv4(void) const
#define NS_LOG_FUNCTION(parameters)
Definition: log.h:345
TracedCallback< Ptr< const Packet > > m_txTrace
Callbacks for tracing the packet Tx events.
uint32_t m_dataSize
packet payload size (must be equal to m_size)
NS_LOG_COMPONENT_DEFINE("GrantedTimeWindowMpiInterface")
virtual int Bind6()=0
Allocate a local IPv6 endpoint for this socket.
Ptr< Socket > m_socket
Socket.
virtual void DoDispose(void)
This method is called by Object::Dispose or by the object's destructor, whichever comes first...
void HandleRead(Ptr< Socket > socket)
Handle a packet reception.
#define NS_ASSERT(condition)
Definition: assert.h:64
NS_OBJECT_ENSURE_REGISTERED(NullMessageSimulatorImpl)
uint32_t GetSize(void) const
Definition: packet.h:650
#define NS_LOG_INFO(msg)
Definition: log.h:298
static void Cancel(const EventId &id)
Set the cancel bit on this event: the event's associated function will not be invoked when it expires...
Definition: simulator.cc:268
Callback< R > MakeNullCallback(void)
Definition: callback.h:1395
static EventId Schedule(Time const &time, MEM mem_ptr, OBJ obj)
Schedule an event to expire at the relative time "time" is reached.
Definition: simulator.h:824
virtual void StopApplication(void)
Application specific shutdown code.
void SetRemote(Ipv4Address ip, uint16_t port)
set the remote address and port
uint16_t port
Definition: dsdv-manet.cc:44
a polymophic address class
Definition: address.h:86
void ScheduleTransmit(Time dt)
Schedule the next packet transmission.
The base class for all ns3 applications.
Definition: application.h:61
hold objects of type ns3::Time
Definition: nstime.h:961
uint8_t * m_data
packet payload data
Hold an unsigned integer type.
Definition: uinteger.h:46
Ptr< Node > GetNode() const
Definition: application.cc:104
An Inet6 address class.
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1238
void SetRecvCallback(Callback< void, Ptr< Socket > >)
Notify application when new data is available to be read.
Definition: socket.cc:128
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
static bool IsMatchingType(const Address &address)
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 void StartApplication(void)
Application specific startup code.
virtual int Bind(const Address &address)=0
Allocate a local endpoint for this socket.
static InetSocketAddress ConvertFrom(const Address &address)
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
uint32_t m_count
Maximum number of packets the application will send.
static TypeId GetTypeId(void)
Get the type ID.
Time m_interval
Packet inter-send time.
static Time Now(void)
Return the "current simulation time".
Definition: simulator.cc:180
void SetDataSize(uint32_t dataSize)
Set the data size of the packet (the number of bytes that are sent as data to the server)...
void Send(void)
Send a packet.
#define NS_ASSERT_MSG(condition, message)
Definition: assert.h:86
Describes an IPv6 address.
Definition: ipv6-address.h:46
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:38
hold objects of type ns3::Address
EventId m_sendEvent
Event to send the next packet.
an identifier for simulation events.
Definition: event-id.h:46
uint32_t GetDataSize(void) const
Get the number of data bytes that will be sent to the server.
static Inet6SocketAddress ConvertFrom(const Address &addr)
Convert the address to a InetSocketAddress.
Address m_peerAddress
Remote peer address.
static bool IsMatchingType(const Address &addr)
If the address match.
uint32_t m_sent
Counter for sent packets.
uint16_t GetPort(void) const
Get the port.
Ptr< const AttributeChecker > MakeTimeChecker(const Time min, const Time max)
Helper to make a Time checker with bounded range.
Definition: time.cc:452
uint16_t GetPort(void) const
static Ipv4Address ConvertFrom(const Address &address)
virtual Ptr< Packet > RecvFrom(uint32_t maxSize, uint32_t flags, Address &fromAddress)=0
Read a single packet from the socket and retrieve the sender address.
uint32_t m_size
Size of the sent packet.
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.
bool IsExpired(void) const
This method is syntactic sugar for the ns3::Simulator::isExpired method.
Definition: event-id.cc:53
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)
uint16_t m_peerPort
Remote peer port.
void SetFill(std::string fill)
Set the data fill of the packet (what is sent as data to the server) to the zero-terminated contents ...
static TypeId LookupByName(std::string name)
Definition: type-id.cc:536
static Ipv6Address ConvertFrom(const Address &address)
Convert the Address object into an Ipv6Address ones.