A Discrete-Event Network Simulator
API
udp-trace-client.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2007,2008, 2009 INRIA, UDcast
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: Mohamed Amine Ismail <amine.ismail@sophia.inria.fr>
18 * <amine.ismail@udcast.com>
19 */
20#include "udp-trace-client.h"
21
22#include "seq-ts-header.h"
23
24#include "ns3/boolean.h"
25#include "ns3/inet-socket-address.h"
26#include "ns3/inet6-socket-address.h"
27#include "ns3/ipv4-address.h"
28#include "ns3/log.h"
29#include "ns3/nstime.h"
30#include "ns3/packet.h"
31#include "ns3/simulator.h"
32#include "ns3/socket-factory.h"
33#include "ns3/socket.h"
34#include "ns3/string.h"
35#include "ns3/uinteger.h"
36
37#include <cstdio>
38#include <cstdlib>
39#include <fstream>
40
41namespace ns3
42{
43
44NS_LOG_COMPONENT_DEFINE("UdpTraceClient");
45
46NS_OBJECT_ENSURE_REGISTERED(UdpTraceClient);
47
52 {40, 1542, 'P'},
53 {120, 134, 'B'},
54 {80, 390, 'B'},
55 {240, 765, 'P'},
56 {160, 407, 'B'},
57 {200, 504, 'B'},
58 {360, 903, 'P'},
59 {280, 421, 'B'},
60 {320, 587, 'B'}};
61
64{
65 static TypeId tid =
66 TypeId("ns3::UdpTraceClient")
68 .SetGroupName("Applications")
69 .AddConstructor<UdpTraceClient>()
70 .AddAttribute("RemoteAddress",
71 "The destination Address of the outbound packets",
73 MakeAddressAccessor(&UdpTraceClient::m_peerAddress),
74 MakeAddressChecker())
75 .AddAttribute("RemotePort",
76 "The destination port of the outbound packets",
77 UintegerValue(100),
79 MakeUintegerChecker<uint16_t>())
80 .AddAttribute("MaxPacketSize",
81 "The maximum size of a packet (including the SeqTsHeader, 12 bytes).",
82 UintegerValue(1024),
84 MakeUintegerChecker<uint32_t>())
85 .AddAttribute("TraceFilename",
86 "Name of file to load a trace from. By default, uses a hardcoded trace.",
87 StringValue(""),
90 .AddAttribute("TraceLoop",
91 "Loops through the trace file, starting again once it is over.",
92 BooleanValue(true),
95
96 ;
97 return tid;
98}
99
101{
102 NS_LOG_FUNCTION(this);
103 m_sent = 0;
104 m_socket = nullptr;
106 m_maxPacketSize = 1400;
107}
108
109UdpTraceClient::UdpTraceClient(Ipv4Address ip, uint16_t port, char* traceFile)
110{
111 NS_LOG_FUNCTION(this);
112 m_sent = 0;
113 m_socket = nullptr;
115 m_peerAddress = ip;
117 m_currentEntry = 0;
118 m_maxPacketSize = 1400;
119 if (traceFile != nullptr)
120 {
121 SetTraceFile(traceFile);
122 }
123}
124
126{
127 NS_LOG_FUNCTION(this);
128 m_entries.clear();
129}
130
131void
133{
134 NS_LOG_FUNCTION(this << ip << port);
135 m_entries.clear();
136 m_peerAddress = ip;
138}
139
140void
142{
143 NS_LOG_FUNCTION(this << addr);
144 m_entries.clear();
145 m_peerAddress = addr;
146}
147
148void
149UdpTraceClient::SetTraceFile(std::string traceFile)
150{
151 NS_LOG_FUNCTION(this << traceFile);
152 if (traceFile == "")
153 {
155 }
156 else
157 {
158 LoadTrace(traceFile);
159 }
160}
161
162void
163UdpTraceClient::SetMaxPacketSize(uint16_t maxPacketSize)
164{
165 NS_LOG_FUNCTION(this << maxPacketSize);
166 m_maxPacketSize = maxPacketSize;
167}
168
169uint16_t
171{
172 NS_LOG_FUNCTION(this);
173 return m_maxPacketSize;
174}
175
176void
178{
179 NS_LOG_FUNCTION(this);
181}
182
183void
184UdpTraceClient::LoadTrace(std::string filename)
185{
186 NS_LOG_FUNCTION(this << filename);
187 uint32_t time = 0;
188 uint32_t index = 0;
189 uint32_t oldIndex = 0;
190 uint32_t size = 0;
191 uint32_t prevTime = 0;
192 char frameType;
193 TraceEntry entry;
194 std::ifstream ifTraceFile;
195 ifTraceFile.open(filename, std::ifstream::in);
196 m_entries.clear();
197 if (!ifTraceFile.good())
198 {
200 }
201 while (ifTraceFile.good())
202 {
203 ifTraceFile >> index >> frameType >> time >> size;
204 if (index == oldIndex)
205 {
206 continue;
207 }
208 if (frameType == 'B')
209 {
210 entry.timeToSend = 0;
211 }
212 else
213 {
214 entry.timeToSend = time - prevTime;
215 prevTime = time;
216 }
217 entry.packetSize = size;
218 entry.frameType = frameType;
219 m_entries.push_back(entry);
220 oldIndex = index;
221 }
222 ifTraceFile.close();
223 NS_ASSERT_MSG(prevTime != 0, "A trace file can not contain B frames only.");
224 m_currentEntry = 0;
225}
226
227void
229{
230 NS_LOG_FUNCTION(this);
231 uint32_t prevTime = 0;
232 for (uint32_t i = 0; i < (sizeof(g_defaultEntries) / sizeof(struct TraceEntry)); i++)
233 {
234 struct TraceEntry entry = g_defaultEntries[i];
235 if (entry.frameType == 'B')
236 {
237 entry.timeToSend = 0;
238 }
239 else
240 {
241 uint32_t tmp = entry.timeToSend;
242 entry.timeToSend -= prevTime;
243 prevTime = tmp;
244 }
245 m_entries.push_back(entry);
246 }
247 m_currentEntry = 0;
248}
249
250void
252{
253 NS_LOG_FUNCTION(this);
254
255 if (!m_socket)
256 {
257 TypeId tid = TypeId::LookupByName("ns3::UdpSocketFactory");
260 {
261 if (m_socket->Bind() == -1)
262 {
263 NS_FATAL_ERROR("Failed to bind socket");
264 }
267 }
269 {
270 if (m_socket->Bind6() == -1)
271 {
272 NS_FATAL_ERROR("Failed to bind socket");
273 }
276 }
278 {
279 if (m_socket->Bind() == -1)
280 {
281 NS_FATAL_ERROR("Failed to bind socket");
282 }
284 }
286 {
287 if (m_socket->Bind6() == -1)
288 {
289 NS_FATAL_ERROR("Failed to bind socket");
290 }
292 }
293 else
294 {
295 NS_ASSERT_MSG(false, "Incompatible address type: " << m_peerAddress);
296 }
297 }
301}
302
303void
305{
306 NS_LOG_FUNCTION(this);
308}
309
310void
312{
313 NS_LOG_FUNCTION(this << size);
314 Ptr<Packet> p;
316 if (size > 12)
317 {
318 packetSize = size - 12; // 12 is the size of the SeqTsHeader
319 }
320 else
321 {
322 packetSize = 0;
323 }
324 p = Create<Packet>(packetSize);
325 SeqTsHeader seqTs;
326 seqTs.SetSeq(m_sent);
327 p->AddHeader(seqTs);
328
329 std::stringstream addressString;
331 {
332 addressString << Ipv4Address::ConvertFrom(m_peerAddress);
333 }
335 {
336 addressString << Ipv6Address::ConvertFrom(m_peerAddress);
337 }
338 else
339 {
340 addressString << m_peerAddress;
341 }
342
343 if ((m_socket->Send(p)) >= 0)
344 {
345 ++m_sent;
346 NS_LOG_INFO("Sent " << size << " bytes to " << addressString.str());
347 }
348 else
349 {
350 NS_LOG_INFO("Error while sending " << size << " bytes to " << addressString.str());
351 }
352}
353
354void
356{
357 NS_LOG_FUNCTION(this);
358
360
361 bool cycled = false;
362 Ptr<Packet> p;
363 struct TraceEntry* entry = &m_entries[m_currentEntry];
364 do
365 {
366 for (uint32_t i = 0; i < entry->packetSize / m_maxPacketSize; i++)
367 {
369 }
370
371 uint16_t sizetosend = entry->packetSize % m_maxPacketSize;
372 SendPacket(sizetosend);
373
375 if (m_currentEntry >= m_entries.size())
376 {
377 m_currentEntry = 0;
378 cycled = true;
379 }
380 entry = &m_entries[m_currentEntry];
381 } while (entry->timeToSend == 0);
382
383 if (!cycled || m_traceLoop)
384 {
387 }
388}
389
390void
392{
393 m_traceLoop = traceLoop;
394}
395
396} // Namespace ns3
a polymophic address class
Definition: address.h:92
AttributeValue implementation for Address.
The base class for all ns3 applications.
Definition: application.h:61
void DoDispose() override
Destructor implementation.
Definition: application.cc:85
Ptr< Node > GetNode() const
Definition: application.cc:107
AttributeValue implementation for Boolean.
Definition: boolean.h:37
An identifier for simulation events.
Definition: event-id.h:55
bool IsExpired() const
This method is syntactic sugar for the ns3::Simulator::IsExpired method.
Definition: event-id.cc:69
An Inet6 address class.
static bool IsMatchingType(const Address &addr)
If the address match.
an Inet address class
static bool IsMatchingType(const Address &address)
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:43
static Ipv4Address ConvertFrom(const Address &address)
static bool IsMatchingType(const Address &address)
static Ipv6Address ConvertFrom(const Address &address)
Convert the Address object into an Ipv6Address ones.
static bool IsMatchingType(const Address &address)
If the Address matches the type.
void AddHeader(const Header &header)
Add header to this packet.
Definition: packet.cc:268
Packet header to carry sequence number and timestamp.
Definition: seq-ts-header.h:45
void SetSeq(uint32_t seq)
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:568
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:276
virtual int Send(Ptr< Packet > p, uint32_t flags)=0
Send data (or dummy data) to the remote host.
virtual bool SetAllowBroadcast(bool allowBroadcast)=0
Configure whether broadcast datagram transmissions are allowed.
virtual int Bind6()=0
Allocate a local IPv6 endpoint for this socket.
virtual int Connect(const Address &address)=0
Initiate a connection to a remote host.
void SetRecvCallback(Callback< void, Ptr< Socket > > receivedData)
Notify application when new data is available to be read.
Definition: socket.cc:126
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:72
virtual int Bind(const Address &address)=0
Allocate a local endpoint for this socket.
Hold variables of type string.
Definition: string.h:42
a unique identifier for an interface.
Definition: type-id.h:60
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition: type-id.cc:839
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:935
Hold an unsigned integer type.
Definition: uinteger.h:45
uint16_t port
Definition: dsdv-manet.cc:45
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:66
#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:86
Ptr< const AttributeAccessor > MakeBooleanAccessor(T1 a1)
Definition: boolean.h:86
Ptr< const AttributeChecker > MakeBooleanChecker()
Definition: boolean.cc:124
Ptr< const AttributeChecker > MakeStringChecker()
Definition: string.cc:30
Ptr< const AttributeAccessor > MakeStringAccessor(T1 a1)
Definition: string.h:43
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Definition: uinteger.h:46
Callback< R, Args... > MakeNullCallback()
Definition: callback.h:734
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:160
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:275
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1338
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1350
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Entry to send.
uint32_t timeToSend
Time to send the frame.
uint32_t packetSize
Size of the frame.
char frameType
Frame type (I, P or B)
Default trace to send.
EventId m_sendEvent
Event to send the next packet.
void StopApplication() override
Application specific shutdown code.
static TypeId GetTypeId()
Get the type ID.
void Send()
Send a packet.
uint16_t m_peerPort
Remote peer port.
void SetTraceLoop(bool traceLoop)
Set the trace loop flag.
uint32_t m_sent
Counter for sent packets.
void SetRemote(Address ip, uint16_t port)
set the remote address and port
Address m_peerAddress
Remote peer address.
uint32_t m_currentEntry
Current entry index.
void SetTraceFile(std::string filename)
Set the trace file to be used by the application.
void LoadTrace(std::string filename)
Load a trace file.
void SetMaxPacketSize(uint16_t maxPacketSize)
Set the maximum packet size.
void StartApplication() override
Application specific startup code.
void DoDispose() override
Destructor implementation.
std::vector< struct TraceEntry > m_entries
Entries in the trace to send.
void SendPacket(uint32_t size)
Send a packet of a given size.
bool m_traceLoop
Loop through the trace file.
uint16_t m_maxPacketSize
Maximum packet size to send (including the SeqTsHeader)
static struct TraceEntry g_defaultEntries[]
Default trace to send.
uint16_t GetMaxPacketSize()
Return the maximum packet size.
void LoadDefaultTrace()
Load the default trace.
Ptr< Socket > m_socket
Socket.
Time prevTime
static const uint32_t packetSize
Packet size generated at the AP.