blob: 75ce818bfb465532649ff9442751c453a19f9a85 [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"
Alexander Afanasyev0ffa7162012-08-21 22:39:22 -070025#include "ns3/log.h"
Alexander Afanasyev048ae422012-08-17 17:33:02 -070026
27#include "ns3/assert.h"
28
29using namespace std;
Alexander Afanasyev38ba9b22012-08-21 13:32:43 -070030using namespace boost;
Alexander Afanasyev048ae422012-08-17 17:33:02 -070031
Alexander Afanasyev0ffa7162012-08-21 22:39:22 -070032NS_LOG_COMPONENT_DEFINE ("ndn.PitQueue");
33
Alexander Afanasyev048ae422012-08-17 17:33:02 -070034namespace ns3 {
35namespace ndn {
36
Alexander Afanasyevec1e3952012-08-20 13:48:15 -070037PitQueue::PitQueue ()
Alexander Afanasyev372cbab2012-08-22 09:43:53 -070038 // : m_maxQueueSize (20)
39 : m_lastQueue (m_queues.end ())
Alexander Afanasyev048ae422012-08-17 17:33:02 -070040{
41}
Alexander Afanasyevec1e3952012-08-20 13:48:15 -070042
Alexander Afanasyev372cbab2012-08-22 09:43:53 -070043// void
44// PitQueue::SetMaxQueueSize (uint32_t size)
45// {
46// m_maxQueueSize = size;
47// }
Alexander Afanasyevec1e3952012-08-20 13:48:15 -070048
Alexander Afanasyev372cbab2012-08-22 09:43:53 -070049// uint32_t
50// PitQueue::GetMaxQueueSize () const
51// {
52// return m_maxQueueSize;
53// }
Alexander Afanasyev048ae422012-08-17 17:33:02 -070054
55
56bool
57PitQueue::Enqueue (Ptr<Face> inFace,
58 Ptr<pit::Entry> pitEntry)
59{
Alexander Afanasyeved449cc2012-08-21 11:10:33 -070060 PerInFaceQueue::iterator queue = m_queues.find (inFace);
61 if (queue == m_queues.end ())
62 {
63 pair<PerInFaceQueue::iterator, bool> itemPair = m_queues.insert (make_pair (inFace, boost::make_shared<Queue> ()));
Alexander Afanasyev0ffa7162012-08-21 22:39:22 -070064 m_lastQueue = m_queues.end (); // for some reason end() iterator is invalidated when new item is inserted
Alexander Afanasyeved449cc2012-08-21 11:10:33 -070065 NS_ASSERT (itemPair.second == true);
66
67 queue = itemPair.first;
68 }
69
Alexander Afanasyev372cbab2012-08-22 09:43:53 -070070 if (queue->second->size () >= inFace->GetLimits ().GetMaxLimit ())
Alexander Afanasyevec1e3952012-08-20 13:48:15 -070071 return false;
72
Alexander Afanasyev38ba9b22012-08-21 13:32:43 -070073 Queue::iterator itemIterator = queue->second->insert (queue->second->end (), pitEntry);
74
75 shared_ptr<fw::PitQueueTag> tag = pitEntry->GetFwTag<fw::PitQueueTag> ();
76 if (tag == shared_ptr<fw::PitQueueTag> ())
77 {
78 tag = make_shared<fw::PitQueueTag> ();
79 pitEntry->AddFwTag (tag);
80 }
81 tag->InsertQueue (queue->second, itemIterator);
Alexander Afanasyevec1e3952012-08-20 13:48:15 -070082 return true;
Alexander Afanasyev048ae422012-08-17 17:33:02 -070083}
84
Alexander Afanasyev38ba9b22012-08-21 13:32:43 -070085
Alexander Afanasyev048ae422012-08-17 17:33:02 -070086Ptr<pit::Entry>
87PitQueue::Pop ()
88{
Alexander Afanasyevec1e3952012-08-20 13:48:15 -070089 PerInFaceQueue::iterator queue = m_lastQueue;
90
Alexander Afanasyeved449cc2012-08-21 11:10:33 -070091 while (queue != m_queues.end () && queue->second->size () == 0) // advance iterator
Alexander Afanasyevec1e3952012-08-20 13:48:15 -070092 {
93 queue ++;
94 }
95
96 if (queue == m_queues.end ())
Alexander Afanasyev0ffa7162012-08-21 22:39:22 -070097 {
98 queue = m_queues.begin (); // circle to the beginning
99 }
Alexander Afanasyevec1e3952012-08-20 13:48:15 -0700100
Alexander Afanasyeved449cc2012-08-21 11:10:33 -0700101 while (queue != m_queues.end () && queue->second->size () == 0) // advance iterator
Alexander Afanasyevec1e3952012-08-20 13:48:15 -0700102 {
103 queue ++;
104 }
105
106 if (queue == m_queues.end ()) // e.g., begin () == end ()
Alexander Afanasyev048ae422012-08-17 17:33:02 -0700107 return 0;
108
Alexander Afanasyeved449cc2012-08-21 11:10:33 -0700109 NS_ASSERT_MSG (queue->second->size () != 0, "Logic error");
Alexander Afanasyev048ae422012-08-17 17:33:02 -0700110
Alexander Afanasyeved449cc2012-08-21 11:10:33 -0700111 Ptr<pit::Entry> entry = *queue->second->begin ();
Alexander Afanasyev38ba9b22012-08-21 13:32:43 -0700112 shared_ptr<fw::PitQueueTag> tag = entry->GetFwTag<fw::PitQueueTag> ();
113 NS_ASSERT (tag != shared_ptr<fw::PitQueueTag> ());
Alexander Afanasyev048ae422012-08-17 17:33:02 -0700114
Alexander Afanasyev38ba9b22012-08-21 13:32:43 -0700115#ifdef NS3_LOG_ENABLE
116 size_t queueSize = queue->second->size ();
117#endif
118 tag->RemoveFromQueue (queue->second);
119#ifdef NS3_LOG_ENABLE
120 NS_ASSERT_MSG (queue->second->size () == queueSize-1, "Queue size should be reduced by one");
121#endif
122
Alexander Afanasyevec1e3952012-08-20 13:48:15 -0700123 m_lastQueue = queue;
124 return entry;
Alexander Afanasyev048ae422012-08-17 17:33:02 -0700125}
126
127void
128PitQueue::Remove (Ptr<Face> face)
129{
Alexander Afanasyevec1e3952012-08-20 13:48:15 -0700130 if (m_lastQueue->first == face)
Alexander Afanasyev048ae422012-08-17 17:33:02 -0700131 {
Alexander Afanasyevec1e3952012-08-20 13:48:15 -0700132 m_lastQueue++;
Alexander Afanasyev048ae422012-08-17 17:33:02 -0700133 }
Alexander Afanasyevec1e3952012-08-20 13:48:15 -0700134
Alexander Afanasyev38ba9b22012-08-21 13:32:43 -0700135 PerInFaceQueue::iterator queue = m_queues.find (face);
136 if (queue == m_queues.end ())
137 return;
138
139 for (Queue::iterator pitEntry = queue->second->begin ();
140 pitEntry != queue->second->end ();
141 pitEntry ++)
142 {
143 shared_ptr<fw::PitQueueTag> tag = (*pitEntry)->GetFwTag<fw::PitQueueTag> ();
144 NS_ASSERT (tag != shared_ptr<fw::PitQueueTag> ());
145
146 tag->RemoveFromQueue (queue->second);
147 }
148
149 NS_ASSERT_MSG (queue->second->size () == 0, "Queue size should be 0 by now");
150 m_queues.erase (queue);
Alexander Afanasyev048ae422012-08-17 17:33:02 -0700151}
152
153void
154PitQueue::Remove (Ptr<pit::Entry> entry)
155{
Alexander Afanasyev38ba9b22012-08-21 13:32:43 -0700156 shared_ptr<fw::PitQueueTag> tag = entry->GetFwTag<fw::PitQueueTag> ();
157 if (tag == shared_ptr<fw::PitQueueTag> ())
158 return;
159
160 tag->RemoveFromAllQueues ();
Alexander Afanasyev048ae422012-08-17 17:33:02 -0700161}
162
Alexander Afanasyev5db92172012-08-21 16:52:07 -0700163bool
164PitQueue::IsEmpty () const
165{
Alexander Afanasyev0ffa7162012-08-21 22:39:22 -0700166 bool isEmpty = true;
Alexander Afanasyev5db92172012-08-21 16:52:07 -0700167
168 for (PerInFaceQueue::const_iterator queue = m_queues.begin ();
169 queue != m_queues.end ();
170 queue ++)
171 {
172 isEmpty &= (queue->second->size () == 0);
173 }
174
175 return isEmpty;
176}
177
Alexander Afanasyev38ba9b22012-08-21 13:32:43 -0700178void
179fw::PitQueueTag::InsertQueue (boost::shared_ptr<PitQueue::Queue> queue, PitQueue::Queue::iterator iterator)
180{
181 pair<MapOfItems::iterator, bool> item = m_items.insert (make_pair (queue, iterator));
182 NS_ASSERT (item.second == true);
183}
184
185void
186fw::PitQueueTag::RemoveFromAllQueues ()
187{
188 for (MapOfItems::iterator item = m_items.begin ();
189 item != m_items.end ();
190 item ++)
191 {
192 item->first->erase (item->second);
193 }
194 m_items.clear ();
195}
196
197void
198fw::PitQueueTag::RemoveFromQueue (boost::shared_ptr<PitQueue::Queue> queue)
199{
200 MapOfItems::iterator item = m_items.find (queue);
201 if (item == m_items.end ())
202 return;
203
204 item->first->erase (item->second);
205 m_items.erase (item);
206}
Alexander Afanasyev048ae422012-08-17 17:33:02 -0700207
208} // namespace ndn
209} // namespace ns3