A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 NS_OBJECT_ENSURE_REGISTERED (UdpTraceClient);
42 
46 struct UdpTraceClient::TraceEntry UdpTraceClient::g_defaultEntries[] = {
47  { 0, 534, 'I'},
48  { 40, 1542, 'P'},
49  { 120, 134, 'B'},
50  { 80, 390, 'B'},
51  { 240, 765, 'P'},
52  { 160, 407, 'B'},
53  { 200, 504, 'B'},
54  { 360, 903, 'P'},
55  { 280, 421, 'B'},
56  { 320, 587, 'B'}
57 };
58 
59 TypeId
61 {
62  static TypeId tid = TypeId ("ns3::UdpTraceClient")
64  .AddConstructor<UdpTraceClient> ()
65  .AddAttribute ("RemoteAddress",
66  "The destination Address of the outbound packets",
67  AddressValue (),
68  MakeAddressAccessor (&UdpTraceClient::m_peerAddress),
69  MakeAddressChecker ())
70  .AddAttribute ("RemotePort",
71  "The destination port of the outbound packets",
72  UintegerValue (100),
73  MakeUintegerAccessor (&UdpTraceClient::m_peerPort),
74  MakeUintegerChecker<uint16_t> ())
75  .AddAttribute ("MaxPacketSize",
76  "The maximum size of a packet (including the SeqTsHeader, 12 bytes).",
77  UintegerValue (1024),
78  MakeUintegerAccessor (&UdpTraceClient::m_maxPacketSize),
79  MakeUintegerChecker<uint32_t> ())
80  .AddAttribute ("TraceFilename",
81  "Name of file to load a trace from. By default, uses a hardcoded trace.",
82  StringValue (""),
83  MakeStringAccessor (&UdpTraceClient::SetTraceFile),
84  MakeStringChecker ())
85 
86  ;
87  return tid;
88 }
89 
91 {
92  NS_LOG_FUNCTION (this);
93  m_sent = 0;
94  m_socket = 0;
95  m_sendEvent = EventId ();
96  m_maxPacketSize = 1400;
97 }
98 
100  char *traceFile)
101 {
102  NS_LOG_FUNCTION (this);
103  m_sent = 0;
104  m_socket = 0;
105  m_sendEvent = EventId ();
106  m_peerAddress = ip;
107  m_peerPort = port;
108  m_currentEntry = 0;
109  m_maxPacketSize = 1400;
110  if (traceFile != NULL)
111  {
112  SetTraceFile (traceFile);
113  }
114 }
115 
117 {
118  NS_LOG_FUNCTION (this);
119  m_entries.clear ();
120 }
121 
122 void
124 {
125  NS_LOG_FUNCTION (this << ip << port);
126  m_entries.clear ();
127  m_peerAddress = ip;
128  m_peerPort = port;
129 }
130 
131 void
133 {
134  NS_LOG_FUNCTION (this << ip << port);
135  m_entries.clear ();
136  m_peerAddress = Address (ip);
137  m_peerPort = port;
138 }
139 
140 void
142 {
143  NS_LOG_FUNCTION (this << ip << port);
144  m_entries.clear ();
145  m_peerAddress = Address (ip);
146  m_peerPort = port;
147 }
148 
149 void
150 UdpTraceClient::SetTraceFile (std::string traceFile)
151 {
152  NS_LOG_FUNCTION (this << traceFile);
153  if (traceFile == "")
154  {
155  LoadDefaultTrace ();
156  }
157  else
158  {
159  LoadTrace (traceFile);
160  }
161 }
162 
163 void
164 UdpTraceClient::SetMaxPacketSize (uint16_t maxPacketSize)
165 {
166  NS_LOG_FUNCTION (this << maxPacketSize);
167  m_maxPacketSize = maxPacketSize;
168 }
169 
170 
172 {
173  NS_LOG_FUNCTION (this);
174  return m_maxPacketSize;
175 }
176 
177 
178 void
180 {
181  NS_LOG_FUNCTION (this);
183 }
184 
185 void
186 UdpTraceClient::LoadTrace (std::string filename)
187 {
188  NS_LOG_FUNCTION (this << filename);
189  uint32_t time, index, size, prevTime = 0;
190  char frameType;
191  TraceEntry entry;
192  std::ifstream ifTraceFile;
193  ifTraceFile.open (filename.c_str (), std::ifstream::in);
194  m_entries.clear ();
195  if (!ifTraceFile.good ())
196  {
197  LoadDefaultTrace ();
198  }
199  while (ifTraceFile.good ())
200  {
201  ifTraceFile >> index >> frameType >> time >> size;
202  if (frameType == 'B')
203  {
204  entry.timeToSend = 0;
205  }
206  else
207  {
208  entry.timeToSend = time - prevTime;
209  prevTime = time;
210  }
211  entry.packetSize = size;
212  entry.frameType = frameType;
213  m_entries.push_back (entry);
214  }
215  ifTraceFile.close ();
216  m_currentEntry = 0;
217 }
218 
219 void
221 {
222  NS_LOG_FUNCTION (this);
223  uint32_t prevTime = 0;
224  for (uint32_t i = 0; i < (sizeof (g_defaultEntries) / sizeof (struct TraceEntry)); i++)
225  {
226  struct TraceEntry entry = g_defaultEntries[i];
227  if (entry.frameType == 'B')
228  {
229  entry.timeToSend = 0;
230  }
231  else
232  {
233  uint32_t tmp = entry.timeToSend;
234  entry.timeToSend -= prevTime;
235  prevTime = tmp;
236  }
237  m_entries.push_back (entry);
238  }
239  m_currentEntry = 0;
240 }
241 
242 void
244 {
245  NS_LOG_FUNCTION (this);
246 
247  if (m_socket == 0)
248  {
249  TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory");
252  {
253  m_socket->Bind ();
255  }
256  else if (Ipv6Address::IsMatchingType(m_peerAddress) == true)
257  {
258  m_socket->Bind6 ();
260  }
261  }
263  m_sendEvent = Simulator::Schedule (Seconds (0.0), &UdpTraceClient::Send, this);
264 }
265 
266 void
268 {
269  NS_LOG_FUNCTION (this);
271 }
272 
273 void
275 {
276  NS_LOG_FUNCTION (this << size);
277  Ptr<Packet> p;
278  uint32_t packetSize;
279  if (size>12)
280  {
281  packetSize = size - 12; // 12 is the size of the SeqTsHeader
282  }
283  else
284  {
285  packetSize = 0;
286  }
287  p = Create<Packet> (packetSize);
288  SeqTsHeader seqTs;
289  seqTs.SetSeq (m_sent);
290  p->AddHeader (seqTs);
291 
292  std::stringstream addressString;
294  {
295  addressString << Ipv4Address::ConvertFrom (m_peerAddress);
296  }
297  else if (Ipv6Address::IsMatchingType(m_peerAddress) == true)
298  {
299  addressString << Ipv6Address::ConvertFrom (m_peerAddress);
300  }
301  else
302  {
303  addressString << m_peerAddress;
304  }
305 
306  if ((m_socket->Send (p)) >= 0)
307  {
308  ++m_sent;
309  NS_LOG_INFO ("Sent " << size << " bytes to "
310  << addressString.str ());
311  }
312  else
313  {
314  NS_LOG_INFO ("Error while sending " << size << " bytes to "
315  << addressString.str ());
316  }
317 }
318 
319 void
321 {
322  NS_LOG_FUNCTION (this);
323 
325  Ptr<Packet> p;
326  struct TraceEntry *entry = &m_entries[m_currentEntry];
327  do
328  {
329  for (uint32_t i = 0; i < entry->packetSize / m_maxPacketSize; i++)
330  {
332  }
333 
334  uint16_t sizetosend = entry->packetSize % m_maxPacketSize;
335  SendPacket (sizetosend);
336 
337  m_currentEntry++;
338  m_currentEntry %= m_entries.size ();
339  entry = &m_entries[m_currentEntry];
340  }
341  while (entry->timeToSend == 0);
342  m_sendEvent = Simulator::Schedule (MilliSeconds (entry->timeToSend), &UdpTraceClient::Send, this);
343 }
344 
345 } // Namespace ns3
static bool IsMatchingType(const Address &address)
If the Address matches the type.
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 the class in the ns-3 factory.
Definition: object-base.h:38
virtual int Bind6()=0
Allocate a local IPv6 endpoint for this socket.
hold variables of type string
Definition: string.h:18
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:61
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:170
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:223
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:1399
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:825
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:86
uint32_t timeToSend
Time to send the frame.
The base class for all ns3 applications.
Definition: application.h:60
Hold an unsigned integer type.
Definition: uinteger.h:46
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:103
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:127
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:70
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.
Doxygen introspection did not find any typical Config paths.
Definition: seq-ts-header.h:36
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:82
void LoadTrace(std::string filename)
Load a trace file.
virtual int Bind(const Address &address)=0
Allocate a local endpoint for this socket.
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.
Default trace to send.
Entry to send.
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
uint16_t m_maxPacketSize
Maximum packet size to send (including the SeqTsHeader)
an identifier for simulation events.
Definition: event-id.h:46
uint16_t m_peerPort
Remote peer port.
void SetSeq(uint32_t seq)
uint32_t m_currentEntry
Current entry index.
static Ipv4Address ConvertFrom(const Address &address)
virtual void DoDispose(void)
This method is called by Object::Dispose or by the object's destructor, whichever comes first...
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:53
a unique identifier for an interface.
Definition: type-id.h:49
TypeId SetParent(TypeId tid)
Definition: type-id.cc:610
void SetRemote(Ipv4Address ip, uint16_t port)
set the remote address and port
void AddHeader(const Header &header)
Add header to this packet.
Definition: packet.cc:253
static TypeId LookupByName(std::string name)
Definition: type-id.cc:535
static Ipv6Address ConvertFrom(const Address &address)
Convert the Address object into an Ipv6Address ones.