/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ /* * Copyright (c) 2007 University of Washington * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation; * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "ns3/log.h" #include "ns3/enum.h" #include "ns3/uinteger.h" #include "reorder-queue.h" namespace ns3 { NS_LOG_COMPONENT_DEFINE ("ReorderQueue"); NS_OBJECT_ENSURE_REGISTERED (ReorderQueue); TypeId ReorderQueue::GetTypeId (void) { static TypeId tid = TypeId ("ns3::ReorderQueue") .SetParent () .SetGroupName("Network") .AddConstructor () .AddAttribute ("Mode", "Whether to use bytes (see MaxBytes) or packets (see MaxPackets) as the maximum queue size metric.", EnumValue (QUEUE_MODE_PACKETS), MakeEnumAccessor (&ReorderQueue::SetMode), MakeEnumChecker (QUEUE_MODE_BYTES, "QUEUE_MODE_BYTES", QUEUE_MODE_PACKETS, "QUEUE_MODE_PACKETS")) .AddAttribute ("MaxPackets", "The maximum number of packets accepted by this ReorderQueue.", UintegerValue (100), MakeUintegerAccessor (&ReorderQueue::m_maxPackets), MakeUintegerChecker ()) .AddAttribute ("MaxBytes", "The maximum number of bytes accepted by this ReorderQueue.", UintegerValue (100 * 65535), MakeUintegerAccessor (&ReorderQueue::m_maxBytes), MakeUintegerChecker ()) .AddAttribute ("ReorderDepth", "The number of packets that will bypass a held packet", UintegerValue (3), MakeUintegerAccessor (&ReorderQueue::m_reorderDepth), MakeUintegerChecker ()) .AddAttribute ("InSequenceLength", "The number of packets until a reordering event", UintegerValue (10), MakeUintegerAccessor (&ReorderQueue::m_inSequenceLength), MakeUintegerChecker ()) ; return tid; } ReorderQueue::ReorderQueue () : Queue (), m_packets (), m_bytesInQueue (0), m_holdCount (0), m_inSequenceCount (0), m_held (0) { NS_LOG_FUNCTION (this); } ReorderQueue::~ReorderQueue () { NS_LOG_FUNCTION (this); } void ReorderQueue::SetMode (ReorderQueue::QueueMode mode) { NS_LOG_FUNCTION (this << mode); m_mode = mode; } ReorderQueue::QueueMode ReorderQueue::GetMode (void) { NS_LOG_FUNCTION (this); return m_mode; } bool ReorderQueue::DoEnqueue (Ptr p) { NS_LOG_FUNCTION (this << p); if (m_mode == QUEUE_MODE_PACKETS && (m_packets.size () >= m_maxPackets)) { NS_LOG_LOGIC ("Queue full (at max packets) -- droppping pkt"); Drop (p); return false; } if (m_mode == QUEUE_MODE_BYTES && (m_bytesInQueue + p->GetSize () >= m_maxBytes)) { NS_LOG_LOGIC ("Queue full (packet would exceed max bytes) -- droppping pkt"); Drop (p); return false; } m_bytesInQueue += p->GetSize (); m_packets.push (p); NS_LOG_LOGIC ("Number packets " << m_packets.size ()); NS_LOG_LOGIC ("Number bytes " << m_bytesInQueue); return true; } Ptr ReorderQueue::DoDequeue (void) { NS_LOG_FUNCTION (this); if (m_packets.empty ()) { NS_LOG_LOGIC ("Queue empty"); return 0; } Ptr p; if (m_holdCount == m_reorderDepth && m_held) { p = m_held; m_held = 0; m_bytesInQueue -= p->GetSize (); NS_LOG_LOGIC ("Released " << p->GetUid ()); m_inSequenceCount = 0; m_holdCount = 0; } else { if (m_inSequenceCount == m_inSequenceLength) { m_held = m_packets.front (); NS_LOG_LOGIC ("Hold " << m_held->GetUid ()); m_packets.pop (); m_inSequenceCount = 0; m_holdCount = 0; if (m_packets.empty ()) { // We need to release this packet since no others to reorder with NS_LOG_LOGIC ("Release " << m_held->GetUid () << " anyway"); p = m_held; m_held = 0; m_bytesInQueue -= p->GetSize (); return p; } } p = m_packets.front (); m_packets.pop (); m_bytesInQueue -= p->GetSize (); NS_LOG_LOGIC ("Popped " << p); if (m_held) { m_holdCount++; } else { m_inSequenceCount++; } } NS_LOG_LOGIC ("Number packets " << m_packets.size ()); NS_LOG_LOGIC ("Number bytes " << m_bytesInQueue); return p; } Ptr ReorderQueue::DoPeek (void) const { NS_LOG_FUNCTION (this); if (m_packets.empty ()) { NS_LOG_LOGIC ("Queue empty"); return 0; } Ptr p = m_packets.front (); NS_LOG_LOGIC ("Number packets " << m_packets.size ()); NS_LOG_LOGIC ("Number bytes " << m_bytesInQueue); return p; } } // namespace ns3