blob: 3486d6d3ee8381f57aa051b38bea4d77ef33184b [file] [log] [blame]
Alexander Afanasyev048ae422012-08-17 17:33:02 -07001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2012 University of California, Los Angeles
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation;
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
19 */
20
21#include "ndn-pit-queue.h"
22
23#include "ns3/ndn-face.h"
24#include "ns3/ndn-pit-entry.h"
25
26#include "ns3/assert.h"
27
28using namespace std;
29
30namespace ns3 {
31namespace ndn {
32
Alexander Afanasyevec1e3952012-08-20 13:48:15 -070033PitQueue::PitQueue ()
34 : m_maxQueueSize (10)
35 , m_lastQueue (m_queues.begin ())
Alexander Afanasyev048ae422012-08-17 17:33:02 -070036{
37}
Alexander Afanasyevec1e3952012-08-20 13:48:15 -070038
39void
40PitQueue::SetMaxQueueSize (uint32_t size)
Alexander Afanasyev048ae422012-08-17 17:33:02 -070041{
Alexander Afanasyevec1e3952012-08-20 13:48:15 -070042 m_maxQueueSize = size;
43}
44
45uint32_t
46PitQueue::GetMaxQueueSize () const
47{
48 return m_maxQueueSize;
Alexander Afanasyev048ae422012-08-17 17:33:02 -070049}
50
51
52bool
53PitQueue::Enqueue (Ptr<Face> inFace,
54 Ptr<pit::Entry> pitEntry)
55{
Alexander Afanasyevec1e3952012-08-20 13:48:15 -070056 Queue &queue = m_queues [inFace]; // either lookup or create
57 if (queue.size () >= m_maxQueueSize)
58 return false;
59
60 queue.push_back (pitEntry);
61 return true;
Alexander Afanasyev048ae422012-08-17 17:33:02 -070062}
63
64Ptr<pit::Entry>
65PitQueue::Pop ()
66{
Alexander Afanasyevec1e3952012-08-20 13:48:15 -070067 PerInFaceQueue::iterator queue = m_lastQueue;
68
69 while (queue != m_queues.end () && queue->second.size () == 0) // advance iterator
70 {
71 queue ++;
72 }
73
74 if (queue == m_queues.end ())
75 queue = m_queues.begin (); // circle to the beginning
76
77 while (queue != m_queues.end () && queue->second.size () == 0) // advance iterator
78 {
79 queue ++;
80 }
81
82 if (queue == m_queues.end ()) // e.g., begin () == end ()
Alexander Afanasyev048ae422012-08-17 17:33:02 -070083 return 0;
84
Alexander Afanasyevec1e3952012-08-20 13:48:15 -070085 NS_ASSERT_MSG (queue->second.size () != 0, "Logic error");
Alexander Afanasyev048ae422012-08-17 17:33:02 -070086
Alexander Afanasyevec1e3952012-08-20 13:48:15 -070087 Ptr<pit::Entry> entry = *queue->second.begin ();
88 queue->second.pop_front ();
Alexander Afanasyev048ae422012-08-17 17:33:02 -070089
Alexander Afanasyevec1e3952012-08-20 13:48:15 -070090 m_lastQueue = queue;
91 return entry;
Alexander Afanasyev048ae422012-08-17 17:33:02 -070092}
93
94void
95PitQueue::Remove (Ptr<Face> face)
96{
Alexander Afanasyevec1e3952012-08-20 13:48:15 -070097 if (m_lastQueue->first == face)
Alexander Afanasyev048ae422012-08-17 17:33:02 -070098 {
Alexander Afanasyevec1e3952012-08-20 13:48:15 -070099 m_lastQueue++;
Alexander Afanasyev048ae422012-08-17 17:33:02 -0700100 }
Alexander Afanasyevec1e3952012-08-20 13:48:15 -0700101
102 m_queues.erase (face);
Alexander Afanasyev048ae422012-08-17 17:33:02 -0700103}
104
105void
106PitQueue::Remove (Ptr<pit::Entry> entry)
107{
Alexander Afanasyev048ae422012-08-17 17:33:02 -0700108}
109
110
111} // namespace ndn
112} // namespace ns3