A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
udp-trace-client.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2007,2008, 2009 INRIA, UDcast
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Mohamed Amine Ismail <amine.ismail@sophia.inria.fr>
7 * <amine.ismail@udcast.com>
8 */
9#include "udp-trace-client.h"
10
11#include "seq-ts-header.h"
12
13#include "ns3/address-utils.h"
14#include "ns3/boolean.h"
15#include "ns3/log.h"
16#include "ns3/nstime.h"
17#include "ns3/packet.h"
18#include "ns3/simulator.h"
19#include "ns3/socket-factory.h"
20#include "ns3/socket.h"
21#include "ns3/string.h"
22#include "ns3/uinteger.h"
23
24#include <cstdio>
25#include <cstdlib>
26#include <fstream>
27
28namespace ns3
29{
30
31NS_LOG_COMPONENT_DEFINE("UdpTraceClient");
32
34
35/**
36 * @brief Default trace to send
37 */
39 {0, 534, 'I'},
40 {40, 1542, 'P'},
41 {120, 134, 'B'},
42 {80, 390, 'B'},
43 {240, 765, 'P'},
44 {160, 407, 'B'},
45 {200, 504, 'B'},
46 {360, 903, 'P'},
47 {280, 421, 'B'},
48 {320, 587, 'B'},
49};
50
53{
54 static TypeId tid =
55 TypeId("ns3::UdpTraceClient")
57 .SetGroupName("Applications")
58 .AddConstructor<UdpTraceClient>()
59 // NS_DEPRECATED_3_44
60 .AddAttribute(
61 "RemoteAddress",
62 "The destination Address of the outbound packets",
65 // this is needed to indicate which version of the function overload to use
66 static_cast<void (UdpTraceClient::*)(const Address&)>(
70 "Replaced by Remote in ns-3.44.")
71 // NS_DEPRECATED_3_44
72 .AddAttribute(
73 "RemotePort",
74 "The destination port of the outbound packets",
77 // this is needed to indicate which version of the function overload to use
78 static_cast<void (UdpTraceClient::*)(const Address&)>(
83 "Replaced by Remote in ns-3.44.")
84 .AddAttribute("MaxPacketSize",
85 "The maximum size of a packet (including the SeqTsHeader, 12 bytes).",
86 UintegerValue(1024),
89 .AddAttribute("TraceFilename",
90 "Name of file to load a trace from. By default, uses a hardcoded trace.",
91 StringValue(""),
94 .AddAttribute("TraceLoop",
95 "Loops through the trace file, starting again once it is over.",
96 BooleanValue(true),
99
100 ;
101 return tid;
102}
103
105 : SourceApplication(false)
106{
107 NS_LOG_FUNCTION(this);
108 m_protocolTid = TypeId::LookupByName("ns3::UdpSocketFactory");
109}
110
115
116void
118{
119 NS_LOG_FUNCTION(this << ip << port);
120 SetRemote(ip);
121 SetPort(port);
122}
123
124void
126{
127 NS_LOG_FUNCTION(this << addr);
128 if (!addr.IsInvalid())
129 {
130 m_peer = addr;
131 if (m_peerPort)
132 {
134 }
135 LoadTrace();
136 }
137}
138
141{
142 return m_peer;
143}
144
145void
147{
148 NS_LOG_FUNCTION(this << port);
149 if (m_peer.IsInvalid())
150 {
151 // save for later
153 return;
154 }
156 {
158 }
159}
160
161uint16_t
163{
164 if (m_peer.IsInvalid())
165 {
167 }
169 {
171 }
173 {
175 }
177}
178
179void
180UdpTraceClient::SetTraceFile(const std::string& traceFile)
181{
182 NS_LOG_FUNCTION(this << traceFile);
183 m_traceFile = traceFile;
184 LoadTrace();
185}
186
187void
188UdpTraceClient::SetMaxPacketSize(uint16_t maxPacketSize)
189{
190 NS_LOG_FUNCTION(this << maxPacketSize);
191 m_maxPacketSize = maxPacketSize;
192}
193
194uint16_t
199
200void
202{
203 NS_LOG_FUNCTION(this);
204 m_entries.clear();
205 m_currentEntry = 0;
206
207 if (m_traceFile.empty())
208 {
210 return;
211 }
212
213 std::ifstream ifTraceFile;
214 ifTraceFile.open(m_traceFile, std::ifstream::in);
215 if (!ifTraceFile.good())
216 {
218 return;
219 }
220
221 uint32_t oldIndex = 0;
222 uint32_t prevTime = 0;
223 while (ifTraceFile.good())
224 {
225 uint32_t index = 0;
226 char frameType;
227 uint32_t time = 0;
228 uint32_t size = 0;
229 ifTraceFile >> index >> frameType >> time >> size;
230
231 if (index == oldIndex)
232 {
233 continue;
234 }
235
236 TraceEntry entry{};
237 if (frameType == 'B')
238 {
239 entry.timeToSend = 0;
240 }
241 else
242 {
243 entry.timeToSend = time - prevTime;
244 prevTime = time;
245 }
246 entry.packetSize = size;
247 entry.frameType = frameType;
248 m_entries.push_back(entry);
249 oldIndex = index;
250 }
251
252 ifTraceFile.close();
253 NS_ASSERT_MSG(prevTime != 0, "A trace file can not contain B frames only.");
254}
255
256void
258{
259 NS_LOG_FUNCTION(this);
260 uint32_t prevTime = 0;
261 for (uint32_t i = 0; i < (sizeof(g_defaultEntries) / sizeof(TraceEntry)); i++)
262 {
263 TraceEntry entry = g_defaultEntries[i];
264 if (entry.frameType == 'B')
265 {
266 entry.timeToSend = 0;
267 }
268 else
269 {
270 uint32_t tmp = entry.timeToSend;
271 entry.timeToSend -= prevTime;
272 prevTime = tmp;
273 }
274 m_entries.push_back(entry);
275 }
276}
277
278void
285
286void
292
293void
295{
296 NS_LOG_FUNCTION(this << size);
298 if (size > 12)
299 {
300 packetSize = size - 12; // 12 is the size of the SeqTsHeader
301 }
302 else
303 {
304 packetSize = 0;
305 }
306 auto p = Create<Packet>(packetSize);
307 SeqTsHeader seqTs;
308 seqTs.SetSeq(m_sent);
309 p->AddHeader(seqTs);
310
311 std::stringstream addressString{};
312#ifdef NS3_LOG_ENABLE
314 {
315 addressString << InetSocketAddress::ConvertFrom(m_peer).GetIpv4() << ":"
317 }
319 {
320 addressString << Inet6SocketAddress::ConvertFrom(m_peer).GetIpv6() << ":"
322 }
323#endif
324
325 m_txTrace(p);
326 if ((m_socket->Send(p)) >= 0)
327 {
328 ++m_sent;
329 NS_LOG_INFO("Sent " << size << " bytes to " << addressString.str());
330 }
331 else
332 {
333 NS_LOG_INFO("Error while sending " << size << " bytes to " << addressString.str());
334 }
335}
336
337void
339{
340 NS_LOG_FUNCTION(this);
341
342 NS_ASSERT(m_sendEvent.IsExpired());
343
344 bool cycled = false;
345 Ptr<Packet> p;
347 do
348 {
349 for (uint32_t i = 0; i < entry->packetSize / m_maxPacketSize; i++)
350 {
352 }
353
354 auto sizetosend = entry->packetSize % m_maxPacketSize;
355 SendPacket(sizetosend);
356
358 if (m_currentEntry >= m_entries.size())
359 {
360 m_currentEntry = 0;
361 cycled = true;
362 }
363 entry = &m_entries[m_currentEntry];
364 } while (entry->timeToSend == 0);
365
366 if (!cycled || m_traceLoop)
367 {
370 }
371}
372
373void
375{
376 m_traceLoop = traceLoop;
377}
378
379} // Namespace ns3
a polymophic address class
Definition address.h:90
bool IsInvalid() const
Definition address.cc:60
AttributeValue implementation for Address.
Definition address.h:275
AttributeValue implementation for Boolean.
Definition boolean.h:26
static Inet6SocketAddress ConvertFrom(const Address &addr)
Convert the address to a InetSocketAddress.
uint16_t GetPort() const
Get the port.
static bool IsMatchingType(const Address &addr)
If the address match.
Ipv6Address GetIpv6() const
Get the IPv6 address.
static bool IsMatchingType(const Address &address)
Ipv4Address GetIpv4() const
static InetSocketAddress ConvertFrom(const Address &address)
Returns an InetSocketAddress which corresponds to the input Address.
static bool IsMatchingType(const Address &address)
static bool IsMatchingType(const Address &address)
If the Address matches the type.
Smart pointer class similar to boost::intrusive_ptr.
Definition ptr.h:67
Packet header to carry sequence number and timestamp.
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:561
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:274
static EventId ScheduleNow(FUNC f, Ts &&... args)
Schedule an event to expire Now.
Definition simulator.h:595
TypeId m_protocolTid
Protocol TypeId value.
SourceApplication(bool allowPacketSocket=true)
Constructor.
Ptr< Socket > m_socket
Socket.
TracedCallback< Ptr< const Packet > > m_txTrace
Traced Callback: transmitted packets.
Address m_peer
Peer address.
Hold variables of type string.
Definition string.h:45
a unique identifier for an interface.
Definition type-id.h:49
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition type-id.cc:872
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1001
@ DEPRECATED
Attribute or trace source is deprecated; user is warned.
Definition type-id.h:65
A trace based streamer.
EventId m_sendEvent
Event to send the next packet.
void SetTraceFile(const std::string &filename)
Set the trace file to be used by the application.
Address GetRemote() const
Get the remote address (temporary function until deprecated attributes are removed).
static TypeId GetTypeId()
Get the type ID.
void Send()
Send a packet.
std::optional< uint16_t > m_peerPort
Remote peer port (deprecated) // NS_DEPRECATED_3_44.
void SetTraceLoop(bool traceLoop)
Set the trace loop flag.
uint32_t m_sent
Counter for sent packets.
uint16_t GetMaxPacketSize() const
Return the maximum packet size.
std::vector< TraceEntry > m_entries
Entries in the trace to send.
uint32_t m_currentEntry
Current entry index.
void SetMaxPacketSize(uint16_t maxPacketSize)
Set the maximum packet size.
void LoadTrace()
Load current trace file.
void SetPort(uint16_t port)
Set the remote port (temporary function until deprecated attributes are removed).
static constexpr uint16_t DEFAULT_PORT
default port
void SetRemote(const Address &ip, uint16_t port)
set the remote address and port
void CancelEvents() override
Cancel all pending events.
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).
std::string m_traceFile
The location of the trace file.
static TraceEntry g_defaultEntries[]
Default trace to send.
uint16_t GetPort() const
Get the remote port (temporary function until deprecated attributes are removed).
void LoadDefaultTrace()
Load the default trace.
void DoStartApplication() override
Application specific startup code for child subclasses.
Hold an unsigned integer type.
Definition uinteger.h:34
uint16_t port
Definition dsdv-manet.cc:33
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition assert.h:55
#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:75
Ptr< const AttributeChecker > MakeAddressChecker()
Definition address.cc:169
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:275
Ptr< const AttributeChecker > MakeBooleanChecker()
Definition boolean.cc:114
Ptr< const AttributeAccessor > MakeBooleanAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Definition boolean.h:70
Ptr< const AttributeChecker > MakeStringChecker()
Definition string.cc:19
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:46
Ptr< const AttributeChecker > MakeUintegerChecker()
Definition uinteger.h:85
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:35
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#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:264
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition object-base.h:35
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:439
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition nstime.h:1381
Address ConvertToSocketAddress(const Address &address, uint16_t port)
Convert IPv4/IPv6 address with port to a socket address.
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).
Time prevTime
static const uint32_t packetSize
Packet size generated at the AP.