blob: b287308ace30c6ca17e99ce17af4c7a7f77a91f1 [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;
Alexander Afanasyev38ba9b22012-08-21 13:32:43 -070029using namespace boost;
Alexander Afanasyev048ae422012-08-17 17:33:02 -070030
31namespace ns3 {
32namespace ndn {
33
Alexander Afanasyevec1e3952012-08-20 13:48:15 -070034PitQueue::PitQueue ()
35 : m_maxQueueSize (10)
36 , m_lastQueue (m_queues.begin ())
Alexander Afanasyev048ae422012-08-17 17:33:02 -070037{
38}
Alexander Afanasyevec1e3952012-08-20 13:48:15 -070039
40void
41PitQueue::SetMaxQueueSize (uint32_t size)
Alexander Afanasyev048ae422012-08-17 17:33:02 -070042{
Alexander Afanasyevec1e3952012-08-20 13:48:15 -070043 m_maxQueueSize = size;
44}
45
46uint32_t
47PitQueue::GetMaxQueueSize () const
48{
49 return m_maxQueueSize;
Alexander Afanasyev048ae422012-08-17 17:33:02 -070050}
51
52
53bool
54PitQueue::Enqueue (Ptr<Face> inFace,
55 Ptr<pit::Entry> pitEntry)
56{
Alexander Afanasyeved449cc2012-08-21 11:10:33 -070057 PerInFaceQueue::iterator queue = m_queues.find (inFace);
58 if (queue == m_queues.end ())
59 {
60 pair<PerInFaceQueue::iterator, bool> itemPair = m_queues.insert (make_pair (inFace, boost::make_shared<Queue> ()));
61 NS_ASSERT (itemPair.second == true);
62
63 queue = itemPair.first;
64 }
65
66 if (queue->second->size () >= m_maxQueueSize)
Alexander Afanasyevec1e3952012-08-20 13:48:15 -070067 return false;
68
Alexander Afanasyev38ba9b22012-08-21 13:32:43 -070069 Queue::iterator itemIterator = queue->second->insert (queue->second->end (), pitEntry);
70
71 shared_ptr<fw::PitQueueTag> tag = pitEntry->GetFwTag<fw::PitQueueTag> ();
72 if (tag == shared_ptr<fw::PitQueueTag> ())
73 {
74 tag = make_shared<fw::PitQueueTag> ();
75 pitEntry->AddFwTag (tag);
76 }
77 tag->InsertQueue (queue->second, itemIterator);
Alexander Afanasyevec1e3952012-08-20 13:48:15 -070078 return true;
Alexander Afanasyev048ae422012-08-17 17:33:02 -070079}
80
Alexander Afanasyev38ba9b22012-08-21 13:32:43 -070081
Alexander Afanasyev048ae422012-08-17 17:33:02 -070082Ptr<pit::Entry>
83PitQueue::Pop ()
84{
Alexander Afanasyevec1e3952012-08-20 13:48:15 -070085 PerInFaceQueue::iterator queue = m_lastQueue;
86
Alexander Afanasyeved449cc2012-08-21 11:10:33 -070087 while (queue != m_queues.end () && queue->second->size () == 0) // advance iterator
Alexander Afanasyevec1e3952012-08-20 13:48:15 -070088 {
89 queue ++;
90 }
91
92 if (queue == m_queues.end ())
93 queue = m_queues.begin (); // circle to the beginning
94
Alexander Afanasyeved449cc2012-08-21 11:10:33 -070095 while (queue != m_queues.end () && queue->second->size () == 0) // advance iterator
Alexander Afanasyevec1e3952012-08-20 13:48:15 -070096 {
97 queue ++;
98 }
99
100 if (queue == m_queues.end ()) // e.g., begin () == end ()
Alexander Afanasyev048ae422012-08-17 17:33:02 -0700101 return 0;
102
Alexander Afanasyeved449cc2012-08-21 11:10:33 -0700103 NS_ASSERT_MSG (queue->second->size () != 0, "Logic error");
Alexander Afanasyev048ae422012-08-17 17:33:02 -0700104
Alexander Afanasyeved449cc2012-08-21 11:10:33 -0700105 Ptr<pit::Entry> entry = *queue->second->begin ();
Alexander Afanasyev38ba9b22012-08-21 13:32:43 -0700106 shared_ptr<fw::PitQueueTag> tag = entry->GetFwTag<fw::PitQueueTag> ();
107 NS_ASSERT (tag != shared_ptr<fw::PitQueueTag> ());
Alexander Afanasyev048ae422012-08-17 17:33:02 -0700108
Alexander Afanasyev38ba9b22012-08-21 13:32:43 -0700109#ifdef NS3_LOG_ENABLE
110 size_t queueSize = queue->second->size ();
111#endif
112 tag->RemoveFromQueue (queue->second);
113#ifdef NS3_LOG_ENABLE
114 NS_ASSERT_MSG (queue->second->size () == queueSize-1, "Queue size should be reduced by one");
115#endif
116
Alexander Afanasyevec1e3952012-08-20 13:48:15 -0700117 m_lastQueue = queue;
118 return entry;
Alexander Afanasyev048ae422012-08-17 17:33:02 -0700119}
120
121void
122PitQueue::Remove (Ptr<Face> face)
123{
Alexander Afanasyevec1e3952012-08-20 13:48:15 -0700124 if (m_lastQueue->first == face)
Alexander Afanasyev048ae422012-08-17 17:33:02 -0700125 {
Alexander Afanasyevec1e3952012-08-20 13:48:15 -0700126 m_lastQueue++;
Alexander Afanasyev048ae422012-08-17 17:33:02 -0700127 }
Alexander Afanasyevec1e3952012-08-20 13:48:15 -0700128
Alexander Afanasyev38ba9b22012-08-21 13:32:43 -0700129 PerInFaceQueue::iterator queue = m_queues.find (face);
130 if (queue == m_queues.end ())
131 return;
132
133 for (Queue::iterator pitEntry = queue->second->begin ();
134 pitEntry != queue->second->end ();
135 pitEntry ++)
136 {
137 shared_ptr<fw::PitQueueTag> tag = (*pitEntry)->GetFwTag<fw::PitQueueTag> ();
138 NS_ASSERT (tag != shared_ptr<fw::PitQueueTag> ());
139
140 tag->RemoveFromQueue (queue->second);
141 }
142
143 NS_ASSERT_MSG (queue->second->size () == 0, "Queue size should be 0 by now");
144 m_queues.erase (queue);
Alexander Afanasyev048ae422012-08-17 17:33:02 -0700145}
146
147void
148PitQueue::Remove (Ptr<pit::Entry> entry)
149{
Alexander Afanasyev38ba9b22012-08-21 13:32:43 -0700150 shared_ptr<fw::PitQueueTag> tag = entry->GetFwTag<fw::PitQueueTag> ();
151 if (tag == shared_ptr<fw::PitQueueTag> ())
152 return;
153
154 tag->RemoveFromAllQueues ();
Alexander Afanasyev048ae422012-08-17 17:33:02 -0700155}
156
Alexander Afanasyev5db92172012-08-21 16:52:07 -0700157bool
158PitQueue::IsEmpty () const
159{
160 bool isEmpty = (m_queues.size () == 0);
161
162 for (PerInFaceQueue::const_iterator queue = m_queues.begin ();
163 queue != m_queues.end ();
164 queue ++)
165 {
166 isEmpty &= (queue->second->size () == 0);
167 }
168
169 return isEmpty;
170}
171
Alexander Afanasyev38ba9b22012-08-21 13:32:43 -0700172void
173fw::PitQueueTag::InsertQueue (boost::shared_ptr<PitQueue::Queue> queue, PitQueue::Queue::iterator iterator)
174{
175 pair<MapOfItems::iterator, bool> item = m_items.insert (make_pair (queue, iterator));
176 NS_ASSERT (item.second == true);
177}
178
179void
180fw::PitQueueTag::RemoveFromAllQueues ()
181{
182 for (MapOfItems::iterator item = m_items.begin ();
183 item != m_items.end ();
184 item ++)
185 {
186 item->first->erase (item->second);
187 }
188 m_items.clear ();
189}
190
191void
192fw::PitQueueTag::RemoveFromQueue (boost::shared_ptr<PitQueue::Queue> queue)
193{
194 MapOfItems::iterator item = m_items.find (queue);
195 if (item == m_items.end ())
196 return;
197
198 item->first->erase (item->second);
199 m_items.erase (item);
200}
Alexander Afanasyev048ae422012-08-17 17:33:02 -0700201
202} // namespace ndn
203} // namespace ns3