src/node/drop-tail-queue.cc
author Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
Thu, 15 Nov 2007 11:41:42 +0000
changeset 1860 cb17b9a8625b
parent 1831 e86e659900f8
child 1870 67b3d2dea3d5
permissions -rw-r--r--
Bug 105: Queue getter methods should be const

/* -*- 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 {

const ClassId DropTailQueue::cid = 
  MakeClassId<DropTailQueue> ("DropTailQueue", Queue::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 (const 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;
}

bool
DropTailQueue::DoDequeue (Packet& p)
{
  NS_LOG_FUNCTION;
  NS_LOG_PARAMS (this << &p);

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

  p = m_packets.front ();
  m_packets.pop ();

  NS_LOG_LOGIC ("Popped " << &p);

  return true;
}

bool
DropTailQueue::DoPeek (Packet& p) const
{
  NS_LOG_FUNCTION;
  NS_LOG_PARAMS (this << &p);

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

  p = m_packets.front ();

  return true;
}

} // 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);
  
  Packet p1, p2, p3, p4;

  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);

  Packet p;

  NS_TEST_ASSERT (queue.Dequeue (p));
  NS_TEST_ASSERT_EQUAL (queue.GetNPackets (), 2);
  NS_TEST_ASSERT_EQUAL (p.GetUid (), p1.GetUid ());

  NS_TEST_ASSERT (queue.Dequeue (p));
  NS_TEST_ASSERT_EQUAL (queue.GetNPackets (), 1);
  NS_TEST_ASSERT_EQUAL (p.GetUid (), p2.GetUid ());

  NS_TEST_ASSERT (queue.Dequeue (p));
  NS_TEST_ASSERT_EQUAL (queue.GetNPackets (), 0);
  NS_TEST_ASSERT_EQUAL (p.GetUid (), p3.GetUid ());

  NS_TEST_ASSERT (!queue.Dequeue (p));

  return result;
}


static DropTailQueueTest gDropTailQueueTest;

}; // namespace ns3

#endif /* RUN_SELF_TESTS */