A Discrete-Event Network Simulator
API
udp-trace-client.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2007,2008, 2009 INRIA, UDcast
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: Mohamed Amine Ismail <amine.ismail@sophia.inria.fr>
19  * <amine.ismail@udcast.com>
20  */
21 #include "ns3/log.h"
22 #include "ns3/ipv4-address.h"
23 #include "ns3/nstime.h"
24 #include "ns3/inet-socket-address.h"
25 #include "ns3/inet6-socket-address.h"
26 #include "ns3/socket.h"
27 #include "ns3/simulator.h"
28 #include "ns3/socket-factory.h"
29 #include "ns3/packet.h"
30 #include "ns3/uinteger.h"
31 #include "ns3/string.h"
32 #include "seq-ts-header.h"
33 #include "udp-trace-client.h"
34 #include <cstdlib>
35 #include <cstdio>
36 #include <fstream>
37 
38 namespace ns3 {
39 
40 NS_LOG_COMPONENT_DEFINE ("UdpTraceClient");
41 
42 NS_OBJECT_ENSURE_REGISTERED (UdpTraceClient);
43 
47 struct UdpTraceClient::TraceEntry UdpTraceClient::g_defaultEntries[] = {
48  { 0, 534, 'I'},
49  { 40, 1542, 'P'},
50  { 120, 134, 'B'},
51  { 80, 390, 'B'},
52  { 240, 765, 'P'},
53  { 160, 407, 'B'},
54  { 200, 504, 'B'},
55  { 360, 903, 'P'},
56  { 280, 421, 'B'},
57  { 320, 587, 'B'}
58 };
59 
60 TypeId
62 {
63  static TypeId tid = TypeId ("ns3::UdpTraceClient")
65  .SetGroupName("Applications")
66  .AddConstructor<UdpTraceClient> ()
67  .AddAttribute ("RemoteAddress",
68  "The destination Address of the outbound packets",
69  AddressValue (),
72  .AddAttribute ("RemotePort",
73  "The destination port of the outbound packets",
74  UintegerValue (100),
76  MakeUintegerChecker<uint16_t> ())
77  .AddAttribute ("MaxPacketSize",
78  "The maximum size of a packet (including the SeqTsHeader, 12 bytes).",
79  UintegerValue (1024),
81  MakeUintegerChecker<uint32_t> ())
82  .AddAttribute ("TraceFilename",
83  "Name of file to load a trace from. By default, uses a hardcoded trace.",
84  StringValue (""),
87 
88  ;
89  return tid;
90 }
91 
93 {
94  NS_LOG_FUNCTION (this);
95  m_sent = 0;
96  m_socket = 0;
97  m_sendEvent = EventId ();
98  m_maxPacketSize = 1400;
99 }
100 
102  char *traceFile)
103 {
104  NS_LOG_FUNCTION (this);
105  m_sent = 0;
106  m_socket = 0;
107  m_sendEvent = EventId ();
108  m_peerAddress = ip;
109  m_peerPort = port;
110  m_currentEntry = 0;
111  m_maxPacketSize = 1400;
112  if (traceFile != NULL)
113  {
114  SetTraceFile (traceFile);
115  }
116 }
117 
119 {
120  NS_LOG_FUNCTION (this);
121  m_entries.clear ();
122 }
123 
124 void
126 {
127  NS_LOG_FUNCTION (this << ip << port);
128  m_entries.clear ();
129  m_peerAddress = ip;
130  m_peerPort = port;
131 }
132 
133 void
135 {
136  NS_LOG_FUNCTION (this << addr);
137  m_entries.clear ();
138  m_peerAddress = addr;
139 }
140 
141 void
142 UdpTraceClient::SetTraceFile (std::string traceFile)
143 {
144  NS_LOG_FUNCTION (this << traceFile);
145  if (traceFile == "")
146  {
147  LoadDefaultTrace ();
148  }
149  else
150  {
151  LoadTrace (traceFile);
152  }
153 }
154 
155 void
156 UdpTraceClient::SetMaxPacketSize (uint16_t maxPacketSize)
157 {
158  NS_LOG_FUNCTION (this << maxPacketSize);
159  m_maxPacketSize = maxPacketSize;
160 }
161 
162 
164 {
165  NS_LOG_FUNCTION (this);
166  return m_maxPacketSize;
167 }
168 
169 
170 void
172 {
173  NS_LOG_FUNCTION (this);
175 }
176 
177 void
178 UdpTraceClient::LoadTrace (std::string filename)
179 {
180  NS_LOG_FUNCTION (this << filename);
181  uint32_t time, index, size, prevTime = 0;
182  char frameType;
183  TraceEntry entry;
184  std::ifstream ifTraceFile;
185  ifTraceFile.open (filename.c_str (), std::ifstream::in);
186  m_entries.clear ();
187  if (!ifTraceFile.good ())
188  {
189  LoadDefaultTrace ();
190  }
191  while (ifTraceFile.good ())
192  {
193  ifTraceFile >> index >> frameType >> time >> size;
194  if (frameType == 'B')
195  {
196  entry.timeToSend = 0;
197  }
198  else
199  {
200  entry.timeToSend = time - prevTime;
201  prevTime = time;
202  }
203  entry.packetSize = size;
204  entry.frameType = frameType;
205  m_entries.push_back (entry);
206  }
207  ifTraceFile.close ();
208  m_currentEntry = 0;
209 }
210 
211 void
213 {
214  NS_LOG_FUNCTION (this);
215  uint32_t prevTime = 0;
216  for (uint32_t i = 0; i < (sizeof (g_defaultEntries) / sizeof (struct TraceEntry)); i++)
217  {
218  struct TraceEntry entry = g_defaultEntries[i];
219  if (entry.frameType == 'B')
220  {
221  entry.timeToSend = 0;
222  }
223  else
224  {
225  uint32_t tmp = entry.timeToSend;
226  entry.timeToSend -= prevTime;
227  prevTime = tmp;
228  }
229  m_entries.push_back (entry);
230  }
231  m_currentEntry = 0;
232 }
233 
234 void
236 {
237  NS_LOG_FUNCTION (this);
238 
239  if (m_socket == 0)
240  {
241  TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory");
244  {
245  m_socket->Bind ();
247  }
248  else if (Ipv6Address::IsMatchingType(m_peerAddress) == true)
249  {
250  m_socket->Bind6 ();
252  }
254  {
255  m_socket->Bind ();
257  }
259  {
260  m_socket->Bind6 ();
262  }
263  else
264  {
265  NS_ASSERT_MSG (false, "Incompatible address type: " << m_peerAddress);
266  }
267  }
269  m_socket->SetAllowBroadcast (true);
271 }
272 
273 void
275 {
276  NS_LOG_FUNCTION (this);
278 }
279 
280 void
282 {
283  NS_LOG_FUNCTION (this << size);
284  Ptr<Packet> p;
285  uint32_t packetSize;
286  if (size>12)
287  {
288  packetSize = size - 12; // 12 is the size of the SeqTsHeader
289  }
290  else
291  {
292  packetSize = 0;
293  }
294  p = Create<Packet> (packetSize);
295  SeqTsHeader seqTs;
296  seqTs.SetSeq (m_sent);
297  p->AddHeader (seqTs);
298 
299  std::stringstream addressString;
301  {
302  addressString << Ipv4Address::ConvertFrom (m_peerAddress);
303  }
304  else if (Ipv6Address::IsMatchingType(m_peerAddress) == true)
305  {
306  addressString << Ipv6Address::ConvertFrom (m_peerAddress);
307  }
308  else
309  {
310  addressString << m_peerAddress;
311  }
312 
313  if ((m_socket->Send (p)) >= 0)
314  {
315  ++m_sent;
316  NS_LOG_INFO ("Sent " << size << " bytes to "
317  << addressString.str ());
318  }
319  else
320  {
321  NS_LOG_INFO ("Error while sending " << size << " bytes to "
322  << addressString.str ());
323  }
324 }
325 
326 void
328 {
329  NS_LOG_FUNCTION (this);
330 
332  Ptr<Packet> p;
333  struct TraceEntry *entry = &m_entries[m_currentEntry];
334  do
335  {
336  for (uint32_t i = 0; i < entry->packetSize / m_maxPacketSize; i++)
337  {
339  }
340 
341  uint16_t sizetosend = entry->packetSize % m_maxPacketSize;
342  SendPacket (sizetosend);
343 
344  m_currentEntry++;
345  m_currentEntry %= m_entries.size ();
346  entry = &m_entries[m_currentEntry];
347  }
348  while (entry->timeToSend == 0);
350 }
351 
352 } // Namespace ns3
static bool IsMatchingType(const Address &address)
If the Address matches the type.
Ptr< const AttributeChecker > MakeStringChecker(void)
Definition: string.cc:30
an Inet address class
void LoadDefaultTrace(void)
Load the default trace.
#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.
Hold variables of type string.
Definition: string.h:41
virtual bool SetAllowBroadcast(bool allowBroadcast)=0
Configure whether broadcast datagram transmissions are allowed.
uint32_t packetSize
Size of the frame.
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
Definition: assert.h:67
virtual void StartApplication(void)
Application specific startup code.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:903
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:244
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:321
Callback< R > MakeNullCallback(void)
Definition: callback.h:1635
Address m_peerAddress
Remote peer address.
virtual void StopApplication(void)
Application specific shutdown code.
void SetTraceFile(std::string filename)
Set the trace file to be used by the application.
EventId m_sendEvent
Event to send the next packet.
uint16_t port
Definition: dsdv-manet.cc:44
a polymophic address class
Definition: address.h:90
uint32_t timeToSend
Time to send the frame.
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
static EventId Schedule(Time const &delay, MEM mem_ptr, OBJ obj)
Schedule an event to expire after delay.
Definition: simulator.h:1238
The base class for all ns3 applications.
Definition: application.h:60
Hold an unsigned integer type.
Definition: uinteger.h:44
uint16_t GetMaxPacketSize(void)
Return the maximum packet size.
static struct TraceEntry g_defaultEntries[]
Default trace to send.
Ptr< Node > GetNode() const
Definition: application.cc:104
char frameType
Frame type (I, P or B)
An Inet6 address class.
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
std::vector< struct TraceEntry > m_entries
Entries in the trace to send.
static bool IsMatchingType(const Address &address)
static TypeId GetTypeId(void)
Get the type ID.
Packet header for UDP client/server application.
Definition: seq-ts-header.h:36
virtual int Connect(const Address &address)=0
Initiate a connection to a remote host.
virtual void DoDispose(void)
Destructor implementation.
Definition: application.cc:83
void LoadTrace(std::string filename)
Load a trace file.
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.
uint32_t m_sent
Counter for sent packets.
Ptr< Socket > m_socket
Socket.
void SetMaxPacketSize(uint16_t maxPacketSize)
Set the maximum packet size.
void Send(void)
Send a packet.
Ptr< const AttributeChecker > MakeAddressChecker(void)
Definition: address.cc:172
Default trace to send.
Entry to send.
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Definition: assert.h:90
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:40
AttributeValue implementation for Address.
Definition: address.h:278
uint16_t m_maxPacketSize
Maximum packet size to send (including the SeqTsHeader)
An identifier for simulation events.
Definition: event-id.h:53
uint16_t m_peerPort
Remote peer port.
void SetSeq(uint32_t seq)
void SetRemote(Address ip, uint16_t port)
set the remote address and port
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:895
uint32_t m_currentEntry
Current entry index.
static bool IsMatchingType(const Address &addr)
If the address match.
static const uint32_t packetSize
static Ipv4Address ConvertFrom(const Address &address)
virtual void DoDispose(void)
Destructor implementation.
Ptr< const AttributeAccessor > MakeStringAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
Definition: string.h:42
virtual int Send(Ptr< Packet > p, uint32_t flags)=0
Send data (or dummy data) to the remote host.
void SendPacket(uint32_t size)
Send a packet of a given size.
bool IsExpired(void) const
This method is syntactic sugar for the ns3::Simulator::IsExpired method.
Definition: event-id.cc:59
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)
void AddHeader(const Header &header)
Add header to this packet.
Definition: packet.cc:257
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition: type-id.cc:813
static Ipv6Address ConvertFrom(const Address &address)
Convert the Address object into an Ipv6Address ones.