wifi: reuse Simulator::Now in WifiMacQueue

This commit is contained in:
Gabriel Ferreira
2021-07-24 22:39:44 -03:00
committed by Stefano Avallone
parent bbd1d576bc
commit 9617bd2e4d
2 changed files with 46 additions and 23 deletions

View File

@@ -93,12 +93,12 @@ WifiMacQueue::GetMaxDelay (void) const
}
bool
WifiMacQueue::TtlExceeded (ConstIterator &it)
WifiMacQueue::TtlExceeded (ConstIterator &it, const Time& now)
{
if (Simulator::Now () > (*it)->GetTimeStamp () + m_maxDelay)
if (now > (*it)->GetTimeStamp () + m_maxDelay)
{
NS_LOG_DEBUG ("Removing packet that stayed in the queue for too long (" <<
Simulator::Now () - (*it)->GetTimeStamp () << ")");
now - (*it)->GetTimeStamp () << ")");
m_traceExpired (*it);
auto curr = it++;
DoRemove (curr);
@@ -138,13 +138,14 @@ WifiMacQueue::Insert (ConstIterator pos, Ptr<WifiMacQueueItem> item)
// the queue is full; scan the list in the attempt to remove stale packets
ConstIterator it = begin ();
const Time now = Simulator::Now ();
while (it != end ())
{
if (it == pos && TtlExceeded (it))
if (it == pos && TtlExceeded (it, now))
{
return DoEnqueue (it, item);
}
if (TtlExceeded (it))
if (TtlExceeded (it, now))
{
return DoEnqueue (pos, item);
}
@@ -174,9 +175,10 @@ Ptr<WifiMacQueueItem>
WifiMacQueue::Dequeue (void)
{
NS_LOG_FUNCTION (this);
const Time now = Simulator::Now ();
for (ConstIterator it = begin (); it != end (); )
{
if (!TtlExceeded (it))
if (!TtlExceeded (it, now))
{
return DoDequeue (it);
}
@@ -242,9 +244,10 @@ WifiMacQueue::Dequeue (ConstIterator pos)
{
NS_LOG_FUNCTION (this);
const Time now = Simulator::Now ();
if (!m_expiredPacketsPresent)
{
if (TtlExceeded (pos))
if (TtlExceeded (pos, now))
{
NS_LOG_DEBUG ("Packet lifetime expired");
return 0;
@@ -261,13 +264,13 @@ WifiMacQueue::Dequeue (ConstIterator pos)
// reset the flag signaling the presence of expired packets before returning
m_expiredPacketsPresent = false;
if (TtlExceeded (it))
if (TtlExceeded (it, now))
{
return 0;
}
return DoDequeue (it);
}
else if (!TtlExceeded (it))
else if (!TtlExceeded (it, now))
{
it++;
}
@@ -280,11 +283,12 @@ Ptr<const WifiMacQueueItem>
WifiMacQueue::Peek (void) const
{
NS_LOG_FUNCTION (this);
const Time now = Simulator::Now ();
for (auto it = begin (); it != end (); it++)
{
// skip packets that stayed in the queue for too long. They will be
// actually removed from the queue by the next call to a non-const method
if (Simulator::Now () <= (*it)->GetTimeStamp () + m_maxDelay)
if (now <= (*it)->GetTimeStamp () + m_maxDelay)
{
return DoPeek (it);
}
@@ -300,11 +304,12 @@ WifiMacQueue::PeekByAddress (Mac48Address dest, ConstIterator pos) const
{
NS_LOG_FUNCTION (this << dest);
ConstIterator it = (pos != EMPTY ? pos : begin ());
const Time now = Simulator::Now ();
while (it != end ())
{
// skip packets that stayed in the queue for too long. They will be
// actually removed from the queue by the next call to a non-const method
if (Simulator::Now () <= (*it)->GetTimeStamp () + m_maxDelay)
if (now <= (*it)->GetTimeStamp () + m_maxDelay)
{
if (((*it)->GetHeader ().IsData () || (*it)->GetHeader ().IsQosData ())
&& (*it)->GetDestinationAddress () == dest)
@@ -328,11 +333,12 @@ WifiMacQueue::PeekByTid (uint8_t tid, ConstIterator pos) const
{
NS_LOG_FUNCTION (this << +tid);
ConstIterator it = (pos != EMPTY ? pos : begin ());
const Time now = Simulator::Now ();
while (it != end ())
{
// skip packets that stayed in the queue for too long. They will be
// actually removed from the queue by the next call to a non-const method
if (Simulator::Now () <= (*it)->GetTimeStamp () + m_maxDelay)
if (now <= (*it)->GetTimeStamp () + m_maxDelay)
{
if ((*it)->GetHeader ().IsQosData () && (*it)->GetHeader ().GetQosTid () == tid)
{
@@ -355,11 +361,12 @@ WifiMacQueue::PeekByTidAndAddress (uint8_t tid, Mac48Address dest, ConstIterator
{
NS_LOG_FUNCTION (this << +tid << dest);
ConstIterator it = (pos != EMPTY ? pos : begin ());
const Time now = Simulator::Now ();
while (it != end ())
{
// skip packets that stayed in the queue for too long. They will be
// actually removed from the queue by the next call to a non-const method
if (Simulator::Now () <= (*it)->GetTimeStamp () + m_maxDelay)
if (now <= (*it)->GetTimeStamp () + m_maxDelay)
{
if ((*it)->GetHeader ().IsQosData () && (*it)->GetDestinationAddress () == dest
&& (*it)->GetHeader ().GetQosTid () == tid)
@@ -383,11 +390,12 @@ WifiMacQueue::PeekFirstAvailable (const Ptr<QosBlockedDestinations> blockedPacke
{
NS_LOG_FUNCTION (this);
ConstIterator it = (pos != EMPTY ? pos : begin ());
const Time now = Simulator::Now ();
while (it != end ())
{
// skip packets that stayed in the queue for too long. They will be
// actually removed from the queue by the next call to a non-const method
if (Simulator::Now () <= (*it)->GetTimeStamp () + m_maxDelay)
if (now <= (*it)->GetTimeStamp () + m_maxDelay)
{
if (!(*it)->GetHeader ().IsQosData () || !blockedPackets
|| !blockedPackets->IsBlocked ((*it)->GetHeader ().GetAddr1 (), (*it)->GetHeader ().GetQosTid ()))
@@ -411,9 +419,10 @@ WifiMacQueue::Remove (void)
{
NS_LOG_FUNCTION (this);
const Time now = Simulator::Now ();
for (ConstIterator it = begin (); it != end (); )
{
if (!TtlExceeded (it))
if (!TtlExceeded (it, now))
{
return DoRemove (it);
}
@@ -426,9 +435,11 @@ bool
WifiMacQueue::Remove (Ptr<const Packet> packet)
{
NS_LOG_FUNCTION (this << packet);
const Time now = Simulator::Now ();
for (ConstIterator it = begin (); it != end (); )
{
if (!TtlExceeded (it))
if (!TtlExceeded (it, now))
{
if ((*it)->GetPacket () == packet)
{
@@ -455,6 +466,8 @@ WifiMacQueue::Remove (ConstIterator pos, bool removeExpired)
return pos;
}
const Time now = Simulator::Now ();
// remove stale items queued before the given position
ConstIterator it = begin ();
while (it != end ())
@@ -468,7 +481,7 @@ WifiMacQueue::Remove (ConstIterator pos, bool removeExpired)
DoRemove (curr);
return pos;
}
else if (!TtlExceeded (it))
else if (!TtlExceeded (it, now))
{
it++;
}
@@ -483,10 +496,11 @@ WifiMacQueue::GetNPacketsByAddress (Mac48Address dest)
NS_LOG_FUNCTION (this << dest);
uint32_t nPackets = 0;
const Time now = Simulator::Now ();
for (ConstIterator it = begin (); it != end (); )
{
if (!TtlExceeded (it))
if (!TtlExceeded (it, now))
{
if ((*it)->GetHeader ().IsData () && (*it)->GetDestinationAddress () == dest)
{
@@ -505,9 +519,11 @@ WifiMacQueue::GetNPacketsByTidAndAddress (uint8_t tid, Mac48Address dest)
{
NS_LOG_FUNCTION (this << dest);
uint32_t nPackets = 0;
const Time now = Simulator::Now ();
for (ConstIterator it = begin (); it != end (); )
{
if (!TtlExceeded (it))
if (!TtlExceeded (it, now))
{
if ((*it)->GetHeader ().IsQosData () && (*it)->GetDestinationAddress () == dest
&& (*it)->GetHeader ().GetQosTid () == tid)
@@ -526,9 +542,11 @@ bool
WifiMacQueue::IsEmpty (void)
{
NS_LOG_FUNCTION (this);
const Time now = Simulator::Now ();
for (ConstIterator it = begin (); it != end (); )
{
if (!TtlExceeded (it))
if (!TtlExceeded (it, now))
{
NS_LOG_DEBUG ("returns false");
return false;
@@ -542,10 +560,12 @@ uint32_t
WifiMacQueue::GetNPackets (void)
{
NS_LOG_FUNCTION (this);
const Time now = Simulator::Now ();
// remove packets that stayed in the queue for too long
for (ConstIterator it = begin (); it != end (); )
{
if (!TtlExceeded (it))
if (!TtlExceeded (it, now))
{
it++;
}
@@ -557,10 +577,12 @@ uint32_t
WifiMacQueue::GetNBytes (void)
{
NS_LOG_FUNCTION (this);
const Time now = Simulator::Now ();
// remove packets that stayed in the queue for too long
for (ConstIterator it = begin (); it != end (); )
{
if (!TtlExceeded (it))
if (!TtlExceeded (it, now))
{
it++;
}

View File

@@ -337,9 +337,10 @@ private:
* point to the item that followed the erased one.
*
* \param it an iterator pointing to the item
* \param now a copy of Simulator::Now()
* \return true if the item is removed, false otherwise
*/
bool TtlExceeded (ConstIterator &it);
bool TtlExceeded (ConstIterator &it, const Time& now);
/**
* Wrapper for the DoEnqueue method provided by the base class that additionally
* sets the iterator field of the item and updates internal statistics, if