src/node/drop-tail-queue.cc
author Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
Thu, 03 Jan 2008 11:39:45 +0100
changeset 2249 3a1da26d61dc
parent 2232 9abd038ee588
child 2250 18f432098389
permissions -rw-r--r--
replace ComponentManager::Create and ClassId with InterfaceId::CreateObjest and InterfaceId

/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
 * Copyright (c) 2007 University of Washington
 * All rights reserved.
 *
 * 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 "drop-tail-queue.h"

NS_LOG_COMPONENT_DEFINE ("DropTailQueue");

namespace ns3 {

NS_OBJECT_ENSURE_REGISTERED (DropTailQueue);

InterfaceId DropTailQueue::iid (void) 
{
  static InterfaceId iid = InterfaceId ("DropTailQueue")
    .SetParent<Queue> ()
    .AddConstructor<DropTailQueue> ();
  return iid;
}

DropTailQueue::DropTailQueue () :
  Queue (),
  m_packets (),
  m_maxPackets(DTQ_NPACKETS_MAX_DEFAULT)
{
  NS_LOG_FUNCTION;
}

DropTailQueue::~DropTailQueue ()
{
  NS_LOG_FUNCTION;
}

void 
DropTailQueue::SetMaxPackets (uint32_t npackets)
{
  NS_LOG_FUNCTION;
  NS_LOG_PARAMS (this << npackets);
  m_maxPackets = npackets;
}

uint32_t 
DropTailQueue::GetMaxPackets (void)
{
  NS_LOG_FUNCTION;
  NS_LOG_LOGIC ("returns " << m_maxPackets);
  return m_maxPackets;
}

bool 
DropTailQueue::DoEnqueue (Ptr<Packet> p)
{
  NS_LOG_FUNCTION;
  NS_LOG_PARAMS (this << p);

  if (m_packets.size () >= m_maxPackets)
    {
      NS_LOG_LOGIC ("Queue full -- droppping pkt");
      Drop (p);
      return false;
    }

  m_packets.push(p);
  return true;
}

Ptr<Packet>
DropTailQueue::DoDequeue (void)
{
  NS_LOG_FUNCTION;
  NS_LOG_PARAMS (this);

  if (m_packets.empty()) 
    {
      NS_LOG_LOGIC ("Queue empty");
      return false;
    }

  Ptr<Packet> p = m_packets.front ();
  m_packets.pop ();

  NS_LOG_LOGIC ("Popped " << p);

  return p;
}

Ptr<Packet>
DropTailQueue::DoPeek (void) const
{
  NS_LOG_FUNCTION;
  NS_LOG_PARAMS (this);

  if (m_packets.empty()) 
    {
      NS_LOG_LOGIC ("Queue empty");
      return false;
    }

  Ptr<Packet> p = m_packets.front ();

  return p;
}

} // namespace ns3


#ifdef RUN_SELF_TESTS

#include "ns3/test.h"

namespace ns3 {

class DropTailQueueTest: public Test {
public:
  virtual bool RunTests (void);
  DropTailQueueTest ();
};


DropTailQueueTest::DropTailQueueTest ()
  : Test ("DropTailQueue") {}


bool
DropTailQueueTest::RunTests (void)
{
  bool result = true;

  DropTailQueue queue;
  queue.SetMaxPackets (3);
  
  Ptr<Packet> p1, p2, p3, p4;
  p1 = Create<Packet> ();
  p2 = Create<Packet> ();
  p3 = Create<Packet> ();
  p4 = Create<Packet> ();

  NS_TEST_ASSERT_EQUAL (queue.GetNPackets (), 0);
  queue.Enqueue (p1);
  NS_TEST_ASSERT_EQUAL (queue.GetNPackets (), 1);
  queue.Enqueue (p2);
  NS_TEST_ASSERT_EQUAL (queue.GetNPackets (), 2);
  queue.Enqueue (p3);
  NS_TEST_ASSERT_EQUAL (queue.GetNPackets (), 3);
  queue.Enqueue (p4); // will be dropped
  NS_TEST_ASSERT_EQUAL (queue.GetNPackets (), 3);

  Ptr<Packet> p;

  p = queue.Dequeue ();
  NS_TEST_ASSERT (p != 0);
  NS_TEST_ASSERT_EQUAL (queue.GetNPackets (), 2);
  NS_TEST_ASSERT_EQUAL (p->GetUid (), p1->GetUid ());

  p = queue.Dequeue ();
  NS_TEST_ASSERT (p != 0);
  NS_TEST_ASSERT_EQUAL (queue.GetNPackets (), 1);
  NS_TEST_ASSERT_EQUAL (p->GetUid (), p2->GetUid ());

  p = queue.Dequeue ();
  NS_TEST_ASSERT (p != 0);
  NS_TEST_ASSERT_EQUAL (queue.GetNPackets (), 0);
  NS_TEST_ASSERT_EQUAL (p->GetUid (), p3->GetUid ());

  p = queue.Dequeue ();
  NS_TEST_ASSERT (p == 0);

  return result;
}


static DropTailQueueTest gDropTailQueueTest;

}; // namespace ns3

#endif /* RUN_SELF_TESTS */