blob: 34410ba804a8b7664f1b64c89ed80b216f42eafa [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 }
Alexander Afanasyevff033952012-08-23 15:45:52 -070069
70 if ((inFace->GetLimits ().GetMaxLimit () == 0 && queue->second->size () > 100) ||
71 (inFace->GetLimits ().GetMaxLimit () != 0 && queue->second->size () >= 0.5 * inFace->GetLimits ().GetMaxLimit ()))
72 {
Alexander Afanasyevec1e3952012-08-20 13:48:15 -070073 return false;
Alexander Afanasyevff033952012-08-23 15:45:52 -070074 }
Alexander Afanasyevec1e3952012-08-20 13:48:15 -070075
Alexander Afanasyev38ba9b22012-08-21 13:32:43 -070076 Queue::iterator itemIterator = queue->second->insert (queue->second->end (), pitEntry);
77
78 shared_ptr<fw::PitQueueTag> tag = pitEntry->GetFwTag<fw::PitQueueTag> ();
79 if (tag == shared_ptr<fw::PitQueueTag> ())
80 {
81 tag = make_shared<fw::PitQueueTag> ();
82 pitEntry->AddFwTag (tag);
83 }
84 tag->InsertQueue (queue->second, itemIterator);
Alexander Afanasyevec1e3952012-08-20 13:48:15 -070085 return true;
Alexander Afanasyev048ae422012-08-17 17:33:02 -070086}
87
Alexander Afanasyev38ba9b22012-08-21 13:32:43 -070088
Alexander Afanasyev048ae422012-08-17 17:33:02 -070089Ptr<pit::Entry>
90PitQueue::Pop ()
91{
Alexander Afanasyevec1e3952012-08-20 13:48:15 -070092 PerInFaceQueue::iterator queue = m_lastQueue;
93
Alexander Afanasyeved449cc2012-08-21 11:10:33 -070094 while (queue != m_queues.end () && queue->second->size () == 0) // advance iterator
Alexander Afanasyevec1e3952012-08-20 13:48:15 -070095 {
96 queue ++;
97 }
98
99 if (queue == m_queues.end ())
Alexander Afanasyev0ffa7162012-08-21 22:39:22 -0700100 {
101 queue = m_queues.begin (); // circle to the beginning
102 }
Alexander Afanasyevec1e3952012-08-20 13:48:15 -0700103
Alexander Afanasyeved449cc2012-08-21 11:10:33 -0700104 while (queue != m_queues.end () && queue->second->size () == 0) // advance iterator
Alexander Afanasyevec1e3952012-08-20 13:48:15 -0700105 {
106 queue ++;
107 }
108
109 if (queue == m_queues.end ()) // e.g., begin () == end ()
Alexander Afanasyev048ae422012-08-17 17:33:02 -0700110 return 0;
111
Alexander Afanasyeved449cc2012-08-21 11:10:33 -0700112 NS_ASSERT_MSG (queue->second->size () != 0, "Logic error");
Alexander Afanasyev048ae422012-08-17 17:33:02 -0700113
Alexander Afanasyeved449cc2012-08-21 11:10:33 -0700114 Ptr<pit::Entry> entry = *queue->second->begin ();
Alexander Afanasyev38ba9b22012-08-21 13:32:43 -0700115 shared_ptr<fw::PitQueueTag> tag = entry->GetFwTag<fw::PitQueueTag> ();
116 NS_ASSERT (tag != shared_ptr<fw::PitQueueTag> ());
Alexander Afanasyev048ae422012-08-17 17:33:02 -0700117
Alexander Afanasyev38ba9b22012-08-21 13:32:43 -0700118#ifdef NS3_LOG_ENABLE
119 size_t queueSize = queue->second->size ();
120#endif
121 tag->RemoveFromQueue (queue->second);
122#ifdef NS3_LOG_ENABLE
123 NS_ASSERT_MSG (queue->second->size () == queueSize-1, "Queue size should be reduced by one");
124#endif
125
Alexander Afanasyevec1e3952012-08-20 13:48:15 -0700126 m_lastQueue = queue;
127 return entry;
Alexander Afanasyev048ae422012-08-17 17:33:02 -0700128}
129
130void
131PitQueue::Remove (Ptr<Face> face)
132{
Alexander Afanasyevec1e3952012-08-20 13:48:15 -0700133 if (m_lastQueue->first == face)
Alexander Afanasyev048ae422012-08-17 17:33:02 -0700134 {
Alexander Afanasyevec1e3952012-08-20 13:48:15 -0700135 m_lastQueue++;
Alexander Afanasyev048ae422012-08-17 17:33:02 -0700136 }
Alexander Afanasyevec1e3952012-08-20 13:48:15 -0700137
Alexander Afanasyev38ba9b22012-08-21 13:32:43 -0700138 PerInFaceQueue::iterator queue = m_queues.find (face);
139 if (queue == m_queues.end ())
140 return;
141
142 for (Queue::iterator pitEntry = queue->second->begin ();
143 pitEntry != queue->second->end ();
144 pitEntry ++)
145 {
146 shared_ptr<fw::PitQueueTag> tag = (*pitEntry)->GetFwTag<fw::PitQueueTag> ();
147 NS_ASSERT (tag != shared_ptr<fw::PitQueueTag> ());
148
Alexander Afanasyev6f101fc2012-08-23 16:34:34 -0700149 tag->RemoveFromQueuesExcept (queue->second);
Alexander Afanasyev38ba9b22012-08-21 13:32:43 -0700150 }
151
152 NS_ASSERT_MSG (queue->second->size () == 0, "Queue size should be 0 by now");
153 m_queues.erase (queue);
Alexander Afanasyev048ae422012-08-17 17:33:02 -0700154}
155
156void
157PitQueue::Remove (Ptr<pit::Entry> entry)
158{
Alexander Afanasyev38ba9b22012-08-21 13:32:43 -0700159 shared_ptr<fw::PitQueueTag> tag = entry->GetFwTag<fw::PitQueueTag> ();
160 if (tag == shared_ptr<fw::PitQueueTag> ())
161 return;
162
163 tag->RemoveFromAllQueues ();
Alexander Afanasyev048ae422012-08-17 17:33:02 -0700164}
165
Alexander Afanasyev5db92172012-08-21 16:52:07 -0700166bool
167PitQueue::IsEmpty () const
168{
Alexander Afanasyev0ffa7162012-08-21 22:39:22 -0700169 bool isEmpty = true;
Alexander Afanasyev5db92172012-08-21 16:52:07 -0700170
171 for (PerInFaceQueue::const_iterator queue = m_queues.begin ();
172 queue != m_queues.end ();
173 queue ++)
174 {
175 isEmpty &= (queue->second->size () == 0);
176 }
177
178 return isEmpty;
179}
180
Alexander Afanasyev38ba9b22012-08-21 13:32:43 -0700181void
182fw::PitQueueTag::InsertQueue (boost::shared_ptr<PitQueue::Queue> queue, PitQueue::Queue::iterator iterator)
183{
184 pair<MapOfItems::iterator, bool> item = m_items.insert (make_pair (queue, iterator));
185 NS_ASSERT (item.second == true);
186}
187
188void
189fw::PitQueueTag::RemoveFromAllQueues ()
190{
191 for (MapOfItems::iterator item = m_items.begin ();
192 item != m_items.end ();
193 item ++)
194 {
195 item->first->erase (item->second);
196 }
197 m_items.clear ();
198}
199
200void
201fw::PitQueueTag::RemoveFromQueue (boost::shared_ptr<PitQueue::Queue> queue)
202{
203 MapOfItems::iterator item = m_items.find (queue);
204 if (item == m_items.end ())
205 return;
206
207 item->first->erase (item->second);
208 m_items.erase (item);
209}
Alexander Afanasyev048ae422012-08-17 17:33:02 -0700210
Alexander Afanasyev6f101fc2012-08-23 16:34:34 -0700211void
212fw::PitQueueTag::RemoveFromQueuesExcept (boost::shared_ptr<PitQueue::Queue> queue)
213{
214 for (MapOfItems::iterator item = m_items.begin ();
215 item != m_items.end (); )
216 {
217 if (item->first == queue)
218 {
219 item ++;
220 continue;
221 }
222
223 item->first->erase (item->second);
224
225 MapOfItems::iterator itemToDelete = item;
226 item ++;
227 m_items.erase (itemToDelete);
228 }
229}
230
231
Alexander Afanasyev048ae422012-08-17 17:33:02 -0700232} // namespace ndn
233} // namespace ns3