From 467d8a4a4bd06ca2e6bce3ba773ed37b231d7f60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Deronne?= Date: Thu, 6 Jun 2024 22:56:25 +0200 Subject: [PATCH] applications: UdpTraceClient reload traces when remote address is changed This avoids to clear the trace entries without repopulating them with the currently selected trace file --- src/applications/model/udp-trace-client.cc | 49 ++++++++++++---------- src/applications/model/udp-trace-client.h | 6 +-- 2 files changed, 30 insertions(+), 25 deletions(-) diff --git a/src/applications/model/udp-trace-client.cc b/src/applications/model/udp-trace-client.cc index 6f0506153..49b5c223b 100644 --- a/src/applications/model/udp-trace-client.cc +++ b/src/applications/model/udp-trace-client.cc @@ -150,12 +150,12 @@ UdpTraceClient::SetRemote(const Address& addr) NS_LOG_FUNCTION(this << addr); if (!addr.IsInvalid()) { - m_entries.clear(); m_peer = addr; if (m_peerPort) { SetPort(*m_peerPort); } + LoadTrace(); } } @@ -203,14 +203,8 @@ void UdpTraceClient::SetTraceFile(const std::string& traceFile) { NS_LOG_FUNCTION(this << traceFile); - if (traceFile.empty()) - { - LoadDefaultTrace(); - } - else - { - LoadTrace(traceFile); - } + m_traceFile = traceFile; + LoadTrace(); } void @@ -228,30 +222,42 @@ UdpTraceClient::GetMaxPacketSize() } void -UdpTraceClient::LoadTrace(const std::string& filename) +UdpTraceClient::LoadTrace() { - NS_LOG_FUNCTION(this << filename); - uint32_t time = 0; - uint32_t index = 0; - uint32_t oldIndex = 0; - uint32_t size = 0; - uint32_t prevTime = 0; - char frameType; - TraceEntry entry; - std::ifstream ifTraceFile; - ifTraceFile.open(filename, std::ifstream::in); + NS_LOG_FUNCTION(this); m_entries.clear(); + m_currentEntry = 0; + + if (m_traceFile.empty()) + { + LoadDefaultTrace(); + return; + } + + std::ifstream ifTraceFile; + ifTraceFile.open(m_traceFile, std::ifstream::in); if (!ifTraceFile.good()) { LoadDefaultTrace(); + return; } + + uint32_t oldIndex = 0; + uint32_t prevTime = 0; while (ifTraceFile.good()) { + uint32_t index = 0; + char frameType; + uint32_t time = 0; + uint32_t size = 0; ifTraceFile >> index >> frameType >> time >> size; + if (index == oldIndex) { continue; } + + TraceEntry entry{}; if (frameType == 'B') { entry.timeToSend = 0; @@ -266,9 +272,9 @@ UdpTraceClient::LoadTrace(const std::string& filename) m_entries.push_back(entry); oldIndex = index; } + ifTraceFile.close(); NS_ASSERT_MSG(prevTime != 0, "A trace file can not contain B frames only."); - m_currentEntry = 0; } void @@ -291,7 +297,6 @@ UdpTraceClient::LoadDefaultTrace() } m_entries.push_back(entry); } - m_currentEntry = 0; } void diff --git a/src/applications/model/udp-trace-client.h b/src/applications/model/udp-trace-client.h index 9e2b2b2d9..59f0a711c 100644 --- a/src/applications/model/udp-trace-client.h +++ b/src/applications/model/udp-trace-client.h @@ -128,10 +128,9 @@ class UdpTraceClient : public Application Address GetRemote() const; /** - * \brief Load a trace file - * \param filename the trace file path + * \brief Load current trace file */ - void LoadTrace(const std::string& filename); + void LoadTrace(); /** * \brief Load the default trace @@ -174,6 +173,7 @@ class UdpTraceClient : public Application static TraceEntry g_defaultEntries[]; //!< Default trace to send uint16_t m_maxPacketSize; //!< Maximum packet size to send (including the SeqTsHeader) bool m_traceLoop; //!< Loop through the trace file + std::string m_traceFile; //!< The location of the trace file }; } // namespace ns3