blob: 3423775d64436e598d37de731db6a2710b442ac9 [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 Afanasyev98c16e02012-08-28 00:02:02 -070037const double MIN_WEIGHT = 0.01;
38
Alexander Afanasyevec1e3952012-08-20 13:48:15 -070039PitQueue::PitQueue ()
Alexander Afanasyev372cbab2012-08-22 09:43:53 -070040 // : m_maxQueueSize (20)
41 : m_lastQueue (m_queues.end ())
Alexander Afanasyev048ae422012-08-17 17:33:02 -070042{
43}
Alexander Afanasyevec1e3952012-08-20 13:48:15 -070044
Alexander Afanasyev372cbab2012-08-22 09:43:53 -070045// void
46// PitQueue::SetMaxQueueSize (uint32_t size)
47// {
48// m_maxQueueSize = size;
49// }
Alexander Afanasyevec1e3952012-08-20 13:48:15 -070050
Alexander Afanasyev372cbab2012-08-22 09:43:53 -070051// uint32_t
52// PitQueue::GetMaxQueueSize () const
53// {
54// return m_maxQueueSize;
55// }
Alexander Afanasyev048ae422012-08-17 17:33:02 -070056
57
58bool
59PitQueue::Enqueue (Ptr<Face> inFace,
Alexander Afanasyev98c16e02012-08-28 00:02:02 -070060 Ptr<pit::Entry> pitEntry,
61 double updatedWeight)
Alexander Afanasyev048ae422012-08-17 17:33:02 -070062{
Alexander Afanasyev98c16e02012-08-28 00:02:02 -070063 if (updatedWeight < MIN_WEIGHT) updatedWeight = MIN_WEIGHT;
64
Alexander Afanasyeved449cc2012-08-21 11:10:33 -070065 PerInFaceQueue::iterator queue = m_queues.find (inFace);
66 if (queue == m_queues.end ())
67 {
Alexander Afanasyev98c16e02012-08-28 00:02:02 -070068 pair<PerInFaceQueue::iterator, bool> itemPair =
69 m_queues.insert (make_pair (inFace, boost::make_shared< WeightedQueue > (make_tuple (Queue (), updatedWeight, 0))));
Alexander Afanasyev0ffa7162012-08-21 22:39:22 -070070 m_lastQueue = m_queues.end (); // for some reason end() iterator is invalidated when new item is inserted
Alexander Afanasyeved449cc2012-08-21 11:10:33 -070071 NS_ASSERT (itemPair.second == true);
72
73 queue = itemPair.first;
74 }
Alexander Afanasyevff033952012-08-23 15:45:52 -070075
Alexander Afanasyev98c16e02012-08-28 00:02:02 -070076 if ((inFace->GetLimits ().GetMaxLimit () == 0 && queue->second->get<0> ().size () > 100) ||
77 (inFace->GetLimits ().GetMaxLimit () != 0 && queue->second->get<0> ().size () >= 0.5 * inFace->GetLimits ().GetMaxLimit ()))
Alexander Afanasyevff033952012-08-23 15:45:52 -070078 {
Alexander Afanasyevec1e3952012-08-20 13:48:15 -070079 return false;
Alexander Afanasyevff033952012-08-23 15:45:52 -070080 }
Alexander Afanasyevec1e3952012-08-20 13:48:15 -070081
Alexander Afanasyev98c16e02012-08-28 00:02:02 -070082 Queue::iterator itemIterator = queue->second->get<0> ().insert (queue->second->get<0> ().end (), pitEntry);
Alexander Afanasyev38ba9b22012-08-21 13:32:43 -070083
84 shared_ptr<fw::PitQueueTag> tag = pitEntry->GetFwTag<fw::PitQueueTag> ();
85 if (tag == shared_ptr<fw::PitQueueTag> ())
86 {
87 tag = make_shared<fw::PitQueueTag> ();
88 pitEntry->AddFwTag (tag);
89 }
90 tag->InsertQueue (queue->second, itemIterator);
Alexander Afanasyev98c16e02012-08-28 00:02:02 -070091
92 UpdateWeightedRounds ();
Alexander Afanasyevec1e3952012-08-20 13:48:15 -070093 return true;
Alexander Afanasyev048ae422012-08-17 17:33:02 -070094}
95
Alexander Afanasyev38ba9b22012-08-21 13:32:43 -070096
Alexander Afanasyev048ae422012-08-17 17:33:02 -070097Ptr<pit::Entry>
98PitQueue::Pop ()
99{
Alexander Afanasyevec1e3952012-08-20 13:48:15 -0700100 PerInFaceQueue::iterator queue = m_lastQueue;
101
Alexander Afanasyev98c16e02012-08-28 00:02:02 -0700102 // if (queue != m_queues.end () &&
103 // true)
104 // {
105 // queue ++; // actually implement round robin...
106 // }
107
108 while (queue != m_queues.end () && queue->second->get<0> ().size () == 0) // advance iterator
Alexander Afanasyevec1e3952012-08-20 13:48:15 -0700109 {
110 queue ++;
Alexander Afanasyev98c16e02012-08-28 00:02:02 -0700111 m_serviceCounter = 0;
Alexander Afanasyevec1e3952012-08-20 13:48:15 -0700112 }
113
114 if (queue == m_queues.end ())
Alexander Afanasyev0ffa7162012-08-21 22:39:22 -0700115 {
116 queue = m_queues.begin (); // circle to the beginning
Alexander Afanasyev98c16e02012-08-28 00:02:02 -0700117 m_serviceCounter = 0;
Alexander Afanasyev0ffa7162012-08-21 22:39:22 -0700118 }
Alexander Afanasyevec1e3952012-08-20 13:48:15 -0700119
Alexander Afanasyev98c16e02012-08-28 00:02:02 -0700120 while (queue != m_queues.end () && queue->second->get<0> ().size () == 0) // advance iterator
Alexander Afanasyevec1e3952012-08-20 13:48:15 -0700121 {
122 queue ++;
Alexander Afanasyev98c16e02012-08-28 00:02:02 -0700123 m_serviceCounter = 0;
Alexander Afanasyevec1e3952012-08-20 13:48:15 -0700124 }
125
126 if (queue == m_queues.end ()) // e.g., begin () == end ()
Alexander Afanasyev048ae422012-08-17 17:33:02 -0700127 return 0;
128
Alexander Afanasyev98c16e02012-08-28 00:02:02 -0700129 NS_ASSERT_MSG (queue->second->get<0> ().size () != 0, "Logic error");
Alexander Afanasyev048ae422012-08-17 17:33:02 -0700130
Alexander Afanasyev98c16e02012-08-28 00:02:02 -0700131 Ptr<pit::Entry> entry = *queue->second->get<0> ().begin ();
Alexander Afanasyev38ba9b22012-08-21 13:32:43 -0700132 shared_ptr<fw::PitQueueTag> tag = entry->GetFwTag<fw::PitQueueTag> ();
133 NS_ASSERT (tag != shared_ptr<fw::PitQueueTag> ());
Alexander Afanasyev048ae422012-08-17 17:33:02 -0700134
Alexander Afanasyev38ba9b22012-08-21 13:32:43 -0700135#ifdef NS3_LOG_ENABLE
Alexander Afanasyev98c16e02012-08-28 00:02:02 -0700136 size_t queueSize = queue->second->get<0> ().size ();
Alexander Afanasyev38ba9b22012-08-21 13:32:43 -0700137#endif
138 tag->RemoveFromQueue (queue->second);
139#ifdef NS3_LOG_ENABLE
Alexander Afanasyev98c16e02012-08-28 00:02:02 -0700140 NS_ASSERT_MSG (queue->second->get<0> ().size () == queueSize-1, "Queue size should be reduced by one");
Alexander Afanasyev38ba9b22012-08-21 13:32:43 -0700141#endif
142
Alexander Afanasyevec1e3952012-08-20 13:48:15 -0700143 m_lastQueue = queue;
144 return entry;
Alexander Afanasyev048ae422012-08-17 17:33:02 -0700145}
146
147void
148PitQueue::Remove (Ptr<Face> face)
149{
Alexander Afanasyevec1e3952012-08-20 13:48:15 -0700150 if (m_lastQueue->first == face)
Alexander Afanasyev048ae422012-08-17 17:33:02 -0700151 {
Alexander Afanasyevec1e3952012-08-20 13:48:15 -0700152 m_lastQueue++;
Alexander Afanasyev048ae422012-08-17 17:33:02 -0700153 }
Alexander Afanasyevec1e3952012-08-20 13:48:15 -0700154
Alexander Afanasyev38ba9b22012-08-21 13:32:43 -0700155 PerInFaceQueue::iterator queue = m_queues.find (face);
156 if (queue == m_queues.end ())
157 return;
158
Alexander Afanasyev98c16e02012-08-28 00:02:02 -0700159 for (Queue::iterator pitEntry = queue->second->get<0> ().begin ();
160 pitEntry != queue->second->get<0> ().end ();
Alexander Afanasyev38ba9b22012-08-21 13:32:43 -0700161 pitEntry ++)
162 {
163 shared_ptr<fw::PitQueueTag> tag = (*pitEntry)->GetFwTag<fw::PitQueueTag> ();
164 NS_ASSERT (tag != shared_ptr<fw::PitQueueTag> ());
165
Alexander Afanasyev6f101fc2012-08-23 16:34:34 -0700166 tag->RemoveFromQueuesExcept (queue->second);
Alexander Afanasyev38ba9b22012-08-21 13:32:43 -0700167 }
168
Alexander Afanasyev98c16e02012-08-28 00:02:02 -0700169 NS_ASSERT_MSG (queue->second->get<0> ().size () == 0, "Queue size should be 0 by now");
Alexander Afanasyev38ba9b22012-08-21 13:32:43 -0700170 m_queues.erase (queue);
Alexander Afanasyev048ae422012-08-17 17:33:02 -0700171}
172
173void
174PitQueue::Remove (Ptr<pit::Entry> entry)
175{
Alexander Afanasyev38ba9b22012-08-21 13:32:43 -0700176 shared_ptr<fw::PitQueueTag> tag = entry->GetFwTag<fw::PitQueueTag> ();
177 if (tag == shared_ptr<fw::PitQueueTag> ())
178 return;
179
180 tag->RemoveFromAllQueues ();
Alexander Afanasyev048ae422012-08-17 17:33:02 -0700181}
182
Alexander Afanasyev5db92172012-08-21 16:52:07 -0700183bool
184PitQueue::IsEmpty () const
185{
Alexander Afanasyev0ffa7162012-08-21 22:39:22 -0700186 bool isEmpty = true;
Alexander Afanasyev5db92172012-08-21 16:52:07 -0700187
188 for (PerInFaceQueue::const_iterator queue = m_queues.begin ();
189 queue != m_queues.end ();
190 queue ++)
191 {
Alexander Afanasyev98c16e02012-08-28 00:02:02 -0700192 isEmpty &= (queue->second->get<0> ().size () == 0);
Alexander Afanasyev5db92172012-08-21 16:52:07 -0700193 }
194
195 return isEmpty;
196}
197
Alexander Afanasyev98c16e02012-08-28 00:02:02 -0700198bool
199PitQueue::IsEmpty (Ptr<Face> inFace) const
Alexander Afanasyev38ba9b22012-08-21 13:32:43 -0700200{
Alexander Afanasyev98c16e02012-08-28 00:02:02 -0700201 PerInFaceQueue::const_iterator queue = m_queues.find (inFace);
202 if (queue == m_queues.end ())
203 return true;
204
205 return queue->second->get<0> ().size () == 0;
206}
207
208
209void
210PitQueue::UpdateWeightedRounds ()
211{
212 double minWeight = 100.0;
213 for (PerInFaceQueue::const_iterator queue = m_queues.begin ();
214 queue != m_queues.end ();
215 queue ++)
216 {
217 if (queue->second->get<1> () < minWeight)
218 minWeight = queue->second->get<1> ();
219 }
220
221 for (PerInFaceQueue::const_iterator queue = m_queues.begin ();
222 queue != m_queues.end ();
223 queue ++)
224 {
225 queue->second->get<2> () = static_cast<uint32_t>((queue->second->get<1> () + 0.5) / minWeight);
226 }
227}
228
229
230////////////////////////////////////////////////////////////////////////////////
231
232void
233fw::PitQueueTag::InsertQueue (boost::shared_ptr<PitQueue::WeightedQueue> queue, PitQueue::Queue::iterator iterator)
234{
235 // std::cerr << "size before: " << m_items.size () << " item: " << (*iterator)->GetPrefix ();
Alexander Afanasyev38ba9b22012-08-21 13:32:43 -0700236 pair<MapOfItems::iterator, bool> item = m_items.insert (make_pair (queue, iterator));
Alexander Afanasyev98c16e02012-08-28 00:02:02 -0700237 // std::cerr << " and after: " << m_items.size () << std::endl;
238 NS_ASSERT_MSG (item.second == true, "Should be a new tag for PIT entry, but something is wrong");
Alexander Afanasyev38ba9b22012-08-21 13:32:43 -0700239}
240
241void
242fw::PitQueueTag::RemoveFromAllQueues ()
243{
244 for (MapOfItems::iterator item = m_items.begin ();
245 item != m_items.end ();
246 item ++)
247 {
Alexander Afanasyev98c16e02012-08-28 00:02:02 -0700248 item->first->get<0> ().erase (item->second);
Alexander Afanasyev38ba9b22012-08-21 13:32:43 -0700249 }
250 m_items.clear ();
251}
252
253void
Alexander Afanasyev98c16e02012-08-28 00:02:02 -0700254fw::PitQueueTag::RemoveFromQueue (boost::shared_ptr<PitQueue::WeightedQueue> queue)
Alexander Afanasyev38ba9b22012-08-21 13:32:43 -0700255{
256 MapOfItems::iterator item = m_items.find (queue);
257 if (item == m_items.end ())
258 return;
259
Alexander Afanasyev98c16e02012-08-28 00:02:02 -0700260 item->first->get<0> ().erase (item->second);
Alexander Afanasyev38ba9b22012-08-21 13:32:43 -0700261 m_items.erase (item);
262}
Alexander Afanasyev048ae422012-08-17 17:33:02 -0700263
Alexander Afanasyev6f101fc2012-08-23 16:34:34 -0700264void
Alexander Afanasyev98c16e02012-08-28 00:02:02 -0700265fw::PitQueueTag::RemoveFromQueuesExcept (boost::shared_ptr<PitQueue::WeightedQueue> queue)
Alexander Afanasyev6f101fc2012-08-23 16:34:34 -0700266{
267 for (MapOfItems::iterator item = m_items.begin ();
268 item != m_items.end (); )
269 {
270 if (item->first == queue)
271 {
272 item ++;
273 continue;
274 }
275
Alexander Afanasyev98c16e02012-08-28 00:02:02 -0700276 item->first->get<0> ().erase (item->second);
Alexander Afanasyev6f101fc2012-08-23 16:34:34 -0700277
278 MapOfItems::iterator itemToDelete = item;
279 item ++;
280 m_items.erase (itemToDelete);
281 }
282}
283
Alexander Afanasyev98c16e02012-08-28 00:02:02 -0700284bool
285fw::PitQueueTag::IsLastOneInQueues () const
286{
287 bool lastOne = true;
288
289 for (MapOfItems::const_iterator item = m_items.begin ();
290 item != m_items.end ();
291 item ++)
292 {
293 lastOne &= (item->first->get<0> ().size () == 1);
294 }
295
296 return lastOne;
297}
298
Alexander Afanasyev6f101fc2012-08-23 16:34:34 -0700299
Alexander Afanasyev048ae422012-08-17 17:33:02 -0700300} // namespace ndn
301} // namespace ns3