blob: 0c7349f3273e764cdf6ea44ed16b0308bacec0d3 [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 Afanasyevc23a3e32012-08-28 00:16:23 -070026#include "ns3/simulator.h"
Alexander Afanasyev048ae422012-08-17 17:33:02 -070027
28#include "ns3/assert.h"
29
30using namespace std;
Alexander Afanasyev38ba9b22012-08-21 13:32:43 -070031using namespace boost;
Alexander Afanasyev048ae422012-08-17 17:33:02 -070032
Alexander Afanasyev0ffa7162012-08-21 22:39:22 -070033NS_LOG_COMPONENT_DEFINE ("ndn.PitQueue");
34
Alexander Afanasyev048ae422012-08-17 17:33:02 -070035namespace ns3 {
36namespace ndn {
37
Alexander Afanasyev98c16e02012-08-28 00:02:02 -070038const double MIN_WEIGHT = 0.01;
39
Alexander Afanasyevec1e3952012-08-20 13:48:15 -070040PitQueue::PitQueue ()
Alexander Afanasyev372cbab2012-08-22 09:43:53 -070041 // : m_maxQueueSize (20)
42 : m_lastQueue (m_queues.end ())
Alexander Afanasyev048ae422012-08-17 17:33:02 -070043{
44}
Alexander Afanasyevec1e3952012-08-20 13:48:15 -070045
Alexander Afanasyev372cbab2012-08-22 09:43:53 -070046// void
47// PitQueue::SetMaxQueueSize (uint32_t size)
48// {
49// m_maxQueueSize = size;
50// }
Alexander Afanasyevec1e3952012-08-20 13:48:15 -070051
Alexander Afanasyev372cbab2012-08-22 09:43:53 -070052// uint32_t
53// PitQueue::GetMaxQueueSize () const
54// {
55// return m_maxQueueSize;
56// }
Alexander Afanasyev048ae422012-08-17 17:33:02 -070057
58
59bool
60PitQueue::Enqueue (Ptr<Face> inFace,
Alexander Afanasyev98c16e02012-08-28 00:02:02 -070061 Ptr<pit::Entry> pitEntry,
62 double updatedWeight)
Alexander Afanasyev048ae422012-08-17 17:33:02 -070063{
Alexander Afanasyev98c16e02012-08-28 00:02:02 -070064 if (updatedWeight < MIN_WEIGHT) updatedWeight = MIN_WEIGHT;
65
Alexander Afanasyeved449cc2012-08-21 11:10:33 -070066 PerInFaceQueue::iterator queue = m_queues.find (inFace);
67 if (queue == m_queues.end ())
68 {
Alexander Afanasyev98c16e02012-08-28 00:02:02 -070069 pair<PerInFaceQueue::iterator, bool> itemPair =
70 m_queues.insert (make_pair (inFace, boost::make_shared< WeightedQueue > (make_tuple (Queue (), updatedWeight, 0))));
Alexander Afanasyev0ffa7162012-08-21 22:39:22 -070071 m_lastQueue = m_queues.end (); // for some reason end() iterator is invalidated when new item is inserted
Alexander Afanasyeved449cc2012-08-21 11:10:33 -070072 NS_ASSERT (itemPair.second == true);
73
74 queue = itemPair.first;
75 }
Alexander Afanasyevff033952012-08-23 15:45:52 -070076
Alexander Afanasyev98c16e02012-08-28 00:02:02 -070077 if ((inFace->GetLimits ().GetMaxLimit () == 0 && queue->second->get<0> ().size () > 100) ||
78 (inFace->GetLimits ().GetMaxLimit () != 0 && queue->second->get<0> ().size () >= 0.5 * inFace->GetLimits ().GetMaxLimit ()))
Alexander Afanasyevff033952012-08-23 15:45:52 -070079 {
Alexander Afanasyevec1e3952012-08-20 13:48:15 -070080 return false;
Alexander Afanasyevff033952012-08-23 15:45:52 -070081 }
Alexander Afanasyevec1e3952012-08-20 13:48:15 -070082
Alexander Afanasyev98c16e02012-08-28 00:02:02 -070083 Queue::iterator itemIterator = queue->second->get<0> ().insert (queue->second->get<0> ().end (), pitEntry);
Alexander Afanasyev38ba9b22012-08-21 13:32:43 -070084
85 shared_ptr<fw::PitQueueTag> tag = pitEntry->GetFwTag<fw::PitQueueTag> ();
86 if (tag == shared_ptr<fw::PitQueueTag> ())
87 {
88 tag = make_shared<fw::PitQueueTag> ();
89 pitEntry->AddFwTag (tag);
90 }
91 tag->InsertQueue (queue->second, itemIterator);
Alexander Afanasyev98c16e02012-08-28 00:02:02 -070092
93 UpdateWeightedRounds ();
Alexander Afanasyevec1e3952012-08-20 13:48:15 -070094 return true;
Alexander Afanasyev048ae422012-08-17 17:33:02 -070095}
96
Alexander Afanasyev38ba9b22012-08-21 13:32:43 -070097
Alexander Afanasyev048ae422012-08-17 17:33:02 -070098Ptr<pit::Entry>
99PitQueue::Pop ()
100{
Alexander Afanasyevec1e3952012-08-20 13:48:15 -0700101 PerInFaceQueue::iterator queue = m_lastQueue;
102
Alexander Afanasyevc23a3e32012-08-28 00:16:23 -0700103 if (queue != m_queues.end () &&
104 true)
105 {
106 queue ++; // actually implement round robin...
107 }
Alexander Afanasyev98c16e02012-08-28 00:02:02 -0700108
109 while (queue != m_queues.end () && queue->second->get<0> ().size () == 0) // advance iterator
Alexander Afanasyevec1e3952012-08-20 13:48:15 -0700110 {
111 queue ++;
Alexander Afanasyev98c16e02012-08-28 00:02:02 -0700112 m_serviceCounter = 0;
Alexander Afanasyevec1e3952012-08-20 13:48:15 -0700113 }
114
115 if (queue == m_queues.end ())
Alexander Afanasyev0ffa7162012-08-21 22:39:22 -0700116 {
117 queue = m_queues.begin (); // circle to the beginning
Alexander Afanasyev98c16e02012-08-28 00:02:02 -0700118 m_serviceCounter = 0;
Alexander Afanasyev0ffa7162012-08-21 22:39:22 -0700119 }
Alexander Afanasyevec1e3952012-08-20 13:48:15 -0700120
Alexander Afanasyev98c16e02012-08-28 00:02:02 -0700121 while (queue != m_queues.end () && queue->second->get<0> ().size () == 0) // advance iterator
Alexander Afanasyevec1e3952012-08-20 13:48:15 -0700122 {
123 queue ++;
Alexander Afanasyev98c16e02012-08-28 00:02:02 -0700124 m_serviceCounter = 0;
Alexander Afanasyevec1e3952012-08-20 13:48:15 -0700125 }
126
127 if (queue == m_queues.end ()) // e.g., begin () == end ()
Alexander Afanasyev048ae422012-08-17 17:33:02 -0700128 return 0;
129
Alexander Afanasyevc23a3e32012-08-28 00:16:23 -0700130 // if (Simulator::GetContext () == 7)
131 // {
132 // for (PerInFaceQueue::const_iterator somequeue = m_queues.begin ();
133 // somequeue != m_queues.end ();
134 // somequeue ++)
135 // {
136 // if (somequeue == queue) cout << "*";
137 // cout << somequeue->second->get<0> ().size () << " ";
138 // }
139 // cout << endl;
140 // }
141
Alexander Afanasyev98c16e02012-08-28 00:02:02 -0700142 NS_ASSERT_MSG (queue->second->get<0> ().size () != 0, "Logic error");
Alexander Afanasyev048ae422012-08-17 17:33:02 -0700143
Alexander Afanasyev98c16e02012-08-28 00:02:02 -0700144 Ptr<pit::Entry> entry = *queue->second->get<0> ().begin ();
Alexander Afanasyev38ba9b22012-08-21 13:32:43 -0700145 shared_ptr<fw::PitQueueTag> tag = entry->GetFwTag<fw::PitQueueTag> ();
146 NS_ASSERT (tag != shared_ptr<fw::PitQueueTag> ());
Alexander Afanasyev048ae422012-08-17 17:33:02 -0700147
Alexander Afanasyev38ba9b22012-08-21 13:32:43 -0700148#ifdef NS3_LOG_ENABLE
Alexander Afanasyev98c16e02012-08-28 00:02:02 -0700149 size_t queueSize = queue->second->get<0> ().size ();
Alexander Afanasyev38ba9b22012-08-21 13:32:43 -0700150#endif
151 tag->RemoveFromQueue (queue->second);
152#ifdef NS3_LOG_ENABLE
Alexander Afanasyev98c16e02012-08-28 00:02:02 -0700153 NS_ASSERT_MSG (queue->second->get<0> ().size () == queueSize-1, "Queue size should be reduced by one");
Alexander Afanasyev38ba9b22012-08-21 13:32:43 -0700154#endif
155
Alexander Afanasyevec1e3952012-08-20 13:48:15 -0700156 m_lastQueue = queue;
157 return entry;
Alexander Afanasyev048ae422012-08-17 17:33:02 -0700158}
159
160void
161PitQueue::Remove (Ptr<Face> face)
162{
Alexander Afanasyevec1e3952012-08-20 13:48:15 -0700163 if (m_lastQueue->first == face)
Alexander Afanasyev048ae422012-08-17 17:33:02 -0700164 {
Alexander Afanasyevec1e3952012-08-20 13:48:15 -0700165 m_lastQueue++;
Alexander Afanasyev048ae422012-08-17 17:33:02 -0700166 }
Alexander Afanasyevec1e3952012-08-20 13:48:15 -0700167
Alexander Afanasyev38ba9b22012-08-21 13:32:43 -0700168 PerInFaceQueue::iterator queue = m_queues.find (face);
169 if (queue == m_queues.end ())
170 return;
171
Alexander Afanasyev98c16e02012-08-28 00:02:02 -0700172 for (Queue::iterator pitEntry = queue->second->get<0> ().begin ();
173 pitEntry != queue->second->get<0> ().end ();
Alexander Afanasyev38ba9b22012-08-21 13:32:43 -0700174 pitEntry ++)
175 {
176 shared_ptr<fw::PitQueueTag> tag = (*pitEntry)->GetFwTag<fw::PitQueueTag> ();
177 NS_ASSERT (tag != shared_ptr<fw::PitQueueTag> ());
178
Alexander Afanasyev6f101fc2012-08-23 16:34:34 -0700179 tag->RemoveFromQueuesExcept (queue->second);
Alexander Afanasyev38ba9b22012-08-21 13:32:43 -0700180 }
181
Alexander Afanasyev98c16e02012-08-28 00:02:02 -0700182 NS_ASSERT_MSG (queue->second->get<0> ().size () == 0, "Queue size should be 0 by now");
Alexander Afanasyev38ba9b22012-08-21 13:32:43 -0700183 m_queues.erase (queue);
Alexander Afanasyev048ae422012-08-17 17:33:02 -0700184}
185
186void
187PitQueue::Remove (Ptr<pit::Entry> entry)
188{
Alexander Afanasyev38ba9b22012-08-21 13:32:43 -0700189 shared_ptr<fw::PitQueueTag> tag = entry->GetFwTag<fw::PitQueueTag> ();
190 if (tag == shared_ptr<fw::PitQueueTag> ())
191 return;
192
193 tag->RemoveFromAllQueues ();
Alexander Afanasyev048ae422012-08-17 17:33:02 -0700194}
195
Alexander Afanasyev5db92172012-08-21 16:52:07 -0700196bool
197PitQueue::IsEmpty () const
198{
Alexander Afanasyev0ffa7162012-08-21 22:39:22 -0700199 bool isEmpty = true;
Alexander Afanasyev5db92172012-08-21 16:52:07 -0700200
201 for (PerInFaceQueue::const_iterator queue = m_queues.begin ();
202 queue != m_queues.end ();
203 queue ++)
204 {
Alexander Afanasyev98c16e02012-08-28 00:02:02 -0700205 isEmpty &= (queue->second->get<0> ().size () == 0);
Alexander Afanasyev5db92172012-08-21 16:52:07 -0700206 }
207
208 return isEmpty;
209}
210
Alexander Afanasyev98c16e02012-08-28 00:02:02 -0700211bool
212PitQueue::IsEmpty (Ptr<Face> inFace) const
Alexander Afanasyev38ba9b22012-08-21 13:32:43 -0700213{
Alexander Afanasyev98c16e02012-08-28 00:02:02 -0700214 PerInFaceQueue::const_iterator queue = m_queues.find (inFace);
215 if (queue == m_queues.end ())
216 return true;
217
218 return queue->second->get<0> ().size () == 0;
219}
220
221
222void
223PitQueue::UpdateWeightedRounds ()
224{
225 double minWeight = 100.0;
226 for (PerInFaceQueue::const_iterator queue = m_queues.begin ();
227 queue != m_queues.end ();
228 queue ++)
229 {
230 if (queue->second->get<1> () < minWeight)
231 minWeight = queue->second->get<1> ();
232 }
233
234 for (PerInFaceQueue::const_iterator queue = m_queues.begin ();
235 queue != m_queues.end ();
236 queue ++)
237 {
238 queue->second->get<2> () = static_cast<uint32_t>((queue->second->get<1> () + 0.5) / minWeight);
239 }
240}
241
242
243////////////////////////////////////////////////////////////////////////////////
244
245void
246fw::PitQueueTag::InsertQueue (boost::shared_ptr<PitQueue::WeightedQueue> queue, PitQueue::Queue::iterator iterator)
247{
248 // std::cerr << "size before: " << m_items.size () << " item: " << (*iterator)->GetPrefix ();
Alexander Afanasyev38ba9b22012-08-21 13:32:43 -0700249 pair<MapOfItems::iterator, bool> item = m_items.insert (make_pair (queue, iterator));
Alexander Afanasyev98c16e02012-08-28 00:02:02 -0700250 // std::cerr << " and after: " << m_items.size () << std::endl;
251 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 -0700252}
253
254void
255fw::PitQueueTag::RemoveFromAllQueues ()
256{
257 for (MapOfItems::iterator item = m_items.begin ();
258 item != m_items.end ();
259 item ++)
260 {
Alexander Afanasyev98c16e02012-08-28 00:02:02 -0700261 item->first->get<0> ().erase (item->second);
Alexander Afanasyev38ba9b22012-08-21 13:32:43 -0700262 }
263 m_items.clear ();
264}
265
266void
Alexander Afanasyev98c16e02012-08-28 00:02:02 -0700267fw::PitQueueTag::RemoveFromQueue (boost::shared_ptr<PitQueue::WeightedQueue> queue)
Alexander Afanasyev38ba9b22012-08-21 13:32:43 -0700268{
269 MapOfItems::iterator item = m_items.find (queue);
270 if (item == m_items.end ())
271 return;
272
Alexander Afanasyev98c16e02012-08-28 00:02:02 -0700273 item->first->get<0> ().erase (item->second);
Alexander Afanasyev38ba9b22012-08-21 13:32:43 -0700274 m_items.erase (item);
275}
Alexander Afanasyev048ae422012-08-17 17:33:02 -0700276
Alexander Afanasyev6f101fc2012-08-23 16:34:34 -0700277void
Alexander Afanasyev98c16e02012-08-28 00:02:02 -0700278fw::PitQueueTag::RemoveFromQueuesExcept (boost::shared_ptr<PitQueue::WeightedQueue> queue)
Alexander Afanasyev6f101fc2012-08-23 16:34:34 -0700279{
280 for (MapOfItems::iterator item = m_items.begin ();
281 item != m_items.end (); )
282 {
283 if (item->first == queue)
284 {
285 item ++;
286 continue;
287 }
288
Alexander Afanasyev98c16e02012-08-28 00:02:02 -0700289 item->first->get<0> ().erase (item->second);
Alexander Afanasyev6f101fc2012-08-23 16:34:34 -0700290
291 MapOfItems::iterator itemToDelete = item;
292 item ++;
293 m_items.erase (itemToDelete);
294 }
295}
296
Alexander Afanasyev98c16e02012-08-28 00:02:02 -0700297bool
298fw::PitQueueTag::IsLastOneInQueues () const
299{
300 bool lastOne = true;
301
302 for (MapOfItems::const_iterator item = m_items.begin ();
303 item != m_items.end ();
304 item ++)
305 {
306 lastOne &= (item->first->get<0> ().size () == 1);
307 }
308
309 return lastOne;
310}
311
Alexander Afanasyev6f101fc2012-08-23 16:34:34 -0700312
Alexander Afanasyev048ae422012-08-17 17:33:02 -0700313} // namespace ndn
314} // namespace ns3