Alexander Afanasyev | 048ae42 | 2012-08-17 17:33:02 -0700 | [diff] [blame] | 1 | /* -*- 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 Afanasyev | 0ffa716 | 2012-08-21 22:39:22 -0700 | [diff] [blame] | 25 | #include "ns3/log.h" |
Alexander Afanasyev | 048ae42 | 2012-08-17 17:33:02 -0700 | [diff] [blame] | 26 | |
| 27 | #include "ns3/assert.h" |
| 28 | |
| 29 | using namespace std; |
Alexander Afanasyev | 38ba9b2 | 2012-08-21 13:32:43 -0700 | [diff] [blame] | 30 | using namespace boost; |
Alexander Afanasyev | 048ae42 | 2012-08-17 17:33:02 -0700 | [diff] [blame] | 31 | |
Alexander Afanasyev | 0ffa716 | 2012-08-21 22:39:22 -0700 | [diff] [blame] | 32 | NS_LOG_COMPONENT_DEFINE ("ndn.PitQueue"); |
| 33 | |
Alexander Afanasyev | 048ae42 | 2012-08-17 17:33:02 -0700 | [diff] [blame] | 34 | namespace ns3 { |
| 35 | namespace ndn { |
| 36 | |
Alexander Afanasyev | ec1e395 | 2012-08-20 13:48:15 -0700 | [diff] [blame] | 37 | PitQueue::PitQueue () |
Alexander Afanasyev | 372cbab | 2012-08-22 09:43:53 -0700 | [diff] [blame] | 38 | // : m_maxQueueSize (20) |
| 39 | : m_lastQueue (m_queues.end ()) |
Alexander Afanasyev | 048ae42 | 2012-08-17 17:33:02 -0700 | [diff] [blame] | 40 | { |
| 41 | } |
Alexander Afanasyev | ec1e395 | 2012-08-20 13:48:15 -0700 | [diff] [blame] | 42 | |
Alexander Afanasyev | 372cbab | 2012-08-22 09:43:53 -0700 | [diff] [blame] | 43 | // void |
| 44 | // PitQueue::SetMaxQueueSize (uint32_t size) |
| 45 | // { |
| 46 | // m_maxQueueSize = size; |
| 47 | // } |
Alexander Afanasyev | ec1e395 | 2012-08-20 13:48:15 -0700 | [diff] [blame] | 48 | |
Alexander Afanasyev | 372cbab | 2012-08-22 09:43:53 -0700 | [diff] [blame] | 49 | // uint32_t |
| 50 | // PitQueue::GetMaxQueueSize () const |
| 51 | // { |
| 52 | // return m_maxQueueSize; |
| 53 | // } |
Alexander Afanasyev | 048ae42 | 2012-08-17 17:33:02 -0700 | [diff] [blame] | 54 | |
| 55 | |
| 56 | bool |
| 57 | PitQueue::Enqueue (Ptr<Face> inFace, |
| 58 | Ptr<pit::Entry> pitEntry) |
| 59 | { |
Alexander Afanasyev | ed449cc | 2012-08-21 11:10:33 -0700 | [diff] [blame] | 60 | 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 Afanasyev | 0ffa716 | 2012-08-21 22:39:22 -0700 | [diff] [blame] | 64 | m_lastQueue = m_queues.end (); // for some reason end() iterator is invalidated when new item is inserted |
Alexander Afanasyev | ed449cc | 2012-08-21 11:10:33 -0700 | [diff] [blame] | 65 | NS_ASSERT (itemPair.second == true); |
| 66 | |
| 67 | queue = itemPair.first; |
| 68 | } |
Alexander Afanasyev | ff03395 | 2012-08-23 15:45:52 -0700 | [diff] [blame] | 69 | |
| 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 Afanasyev | ec1e395 | 2012-08-20 13:48:15 -0700 | [diff] [blame] | 73 | return false; |
Alexander Afanasyev | ff03395 | 2012-08-23 15:45:52 -0700 | [diff] [blame] | 74 | } |
Alexander Afanasyev | ec1e395 | 2012-08-20 13:48:15 -0700 | [diff] [blame] | 75 | |
Alexander Afanasyev | 38ba9b2 | 2012-08-21 13:32:43 -0700 | [diff] [blame] | 76 | 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 Afanasyev | ec1e395 | 2012-08-20 13:48:15 -0700 | [diff] [blame] | 85 | return true; |
Alexander Afanasyev | 048ae42 | 2012-08-17 17:33:02 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Alexander Afanasyev | 38ba9b2 | 2012-08-21 13:32:43 -0700 | [diff] [blame] | 88 | |
Alexander Afanasyev | 048ae42 | 2012-08-17 17:33:02 -0700 | [diff] [blame] | 89 | Ptr<pit::Entry> |
| 90 | PitQueue::Pop () |
| 91 | { |
Alexander Afanasyev | ec1e395 | 2012-08-20 13:48:15 -0700 | [diff] [blame] | 92 | PerInFaceQueue::iterator queue = m_lastQueue; |
| 93 | |
Alexander Afanasyev | ed449cc | 2012-08-21 11:10:33 -0700 | [diff] [blame] | 94 | while (queue != m_queues.end () && queue->second->size () == 0) // advance iterator |
Alexander Afanasyev | ec1e395 | 2012-08-20 13:48:15 -0700 | [diff] [blame] | 95 | { |
| 96 | queue ++; |
| 97 | } |
| 98 | |
| 99 | if (queue == m_queues.end ()) |
Alexander Afanasyev | 0ffa716 | 2012-08-21 22:39:22 -0700 | [diff] [blame] | 100 | { |
| 101 | queue = m_queues.begin (); // circle to the beginning |
| 102 | } |
Alexander Afanasyev | ec1e395 | 2012-08-20 13:48:15 -0700 | [diff] [blame] | 103 | |
Alexander Afanasyev | ed449cc | 2012-08-21 11:10:33 -0700 | [diff] [blame] | 104 | while (queue != m_queues.end () && queue->second->size () == 0) // advance iterator |
Alexander Afanasyev | ec1e395 | 2012-08-20 13:48:15 -0700 | [diff] [blame] | 105 | { |
| 106 | queue ++; |
| 107 | } |
| 108 | |
| 109 | if (queue == m_queues.end ()) // e.g., begin () == end () |
Alexander Afanasyev | 048ae42 | 2012-08-17 17:33:02 -0700 | [diff] [blame] | 110 | return 0; |
| 111 | |
Alexander Afanasyev | ed449cc | 2012-08-21 11:10:33 -0700 | [diff] [blame] | 112 | NS_ASSERT_MSG (queue->second->size () != 0, "Logic error"); |
Alexander Afanasyev | 048ae42 | 2012-08-17 17:33:02 -0700 | [diff] [blame] | 113 | |
Alexander Afanasyev | ed449cc | 2012-08-21 11:10:33 -0700 | [diff] [blame] | 114 | Ptr<pit::Entry> entry = *queue->second->begin (); |
Alexander Afanasyev | 38ba9b2 | 2012-08-21 13:32:43 -0700 | [diff] [blame] | 115 | shared_ptr<fw::PitQueueTag> tag = entry->GetFwTag<fw::PitQueueTag> (); |
| 116 | NS_ASSERT (tag != shared_ptr<fw::PitQueueTag> ()); |
Alexander Afanasyev | 048ae42 | 2012-08-17 17:33:02 -0700 | [diff] [blame] | 117 | |
Alexander Afanasyev | 38ba9b2 | 2012-08-21 13:32:43 -0700 | [diff] [blame] | 118 | #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 Afanasyev | ec1e395 | 2012-08-20 13:48:15 -0700 | [diff] [blame] | 126 | m_lastQueue = queue; |
| 127 | return entry; |
Alexander Afanasyev | 048ae42 | 2012-08-17 17:33:02 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | void |
| 131 | PitQueue::Remove (Ptr<Face> face) |
| 132 | { |
Alexander Afanasyev | ec1e395 | 2012-08-20 13:48:15 -0700 | [diff] [blame] | 133 | if (m_lastQueue->first == face) |
Alexander Afanasyev | 048ae42 | 2012-08-17 17:33:02 -0700 | [diff] [blame] | 134 | { |
Alexander Afanasyev | ec1e395 | 2012-08-20 13:48:15 -0700 | [diff] [blame] | 135 | m_lastQueue++; |
Alexander Afanasyev | 048ae42 | 2012-08-17 17:33:02 -0700 | [diff] [blame] | 136 | } |
Alexander Afanasyev | ec1e395 | 2012-08-20 13:48:15 -0700 | [diff] [blame] | 137 | |
Alexander Afanasyev | 38ba9b2 | 2012-08-21 13:32:43 -0700 | [diff] [blame] | 138 | 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 Afanasyev | 6f101fc | 2012-08-23 16:34:34 -0700 | [diff] [blame^] | 149 | tag->RemoveFromQueuesExcept (queue->second); |
Alexander Afanasyev | 38ba9b2 | 2012-08-21 13:32:43 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | NS_ASSERT_MSG (queue->second->size () == 0, "Queue size should be 0 by now"); |
| 153 | m_queues.erase (queue); |
Alexander Afanasyev | 048ae42 | 2012-08-17 17:33:02 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | void |
| 157 | PitQueue::Remove (Ptr<pit::Entry> entry) |
| 158 | { |
Alexander Afanasyev | 38ba9b2 | 2012-08-21 13:32:43 -0700 | [diff] [blame] | 159 | shared_ptr<fw::PitQueueTag> tag = entry->GetFwTag<fw::PitQueueTag> (); |
| 160 | if (tag == shared_ptr<fw::PitQueueTag> ()) |
| 161 | return; |
| 162 | |
| 163 | tag->RemoveFromAllQueues (); |
Alexander Afanasyev | 048ae42 | 2012-08-17 17:33:02 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Alexander Afanasyev | 5db9217 | 2012-08-21 16:52:07 -0700 | [diff] [blame] | 166 | bool |
| 167 | PitQueue::IsEmpty () const |
| 168 | { |
Alexander Afanasyev | 0ffa716 | 2012-08-21 22:39:22 -0700 | [diff] [blame] | 169 | bool isEmpty = true; |
Alexander Afanasyev | 5db9217 | 2012-08-21 16:52:07 -0700 | [diff] [blame] | 170 | |
| 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 Afanasyev | 38ba9b2 | 2012-08-21 13:32:43 -0700 | [diff] [blame] | 181 | void |
| 182 | fw::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 | |
| 188 | void |
| 189 | fw::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 | |
| 200 | void |
| 201 | fw::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 Afanasyev | 048ae42 | 2012-08-17 17:33:02 -0700 | [diff] [blame] | 210 | |
Alexander Afanasyev | 6f101fc | 2012-08-23 16:34:34 -0700 | [diff] [blame^] | 211 | void |
| 212 | fw::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 Afanasyev | 048ae42 | 2012-08-17 17:33:02 -0700 | [diff] [blame] | 232 | } // namespace ndn |
| 233 | } // namespace ns3 |