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 | 0ffa716 | 2012-08-21 22:39:22 -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 | |
| 43 | void |
| 44 | PitQueue::SetMaxQueueSize (uint32_t size) |
Alexander Afanasyev | 048ae42 | 2012-08-17 17:33:02 -0700 | [diff] [blame] | 45 | { |
Alexander Afanasyev | ec1e395 | 2012-08-20 13:48:15 -0700 | [diff] [blame] | 46 | m_maxQueueSize = size; |
| 47 | } |
| 48 | |
| 49 | uint32_t |
| 50 | PitQueue::GetMaxQueueSize () const |
| 51 | { |
| 52 | return m_maxQueueSize; |
Alexander Afanasyev | 048ae42 | 2012-08-17 17:33:02 -0700 | [diff] [blame] | 53 | } |
| 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 | } |
| 69 | |
| 70 | if (queue->second->size () >= m_maxQueueSize) |
Alexander Afanasyev | ec1e395 | 2012-08-20 13:48:15 -0700 | [diff] [blame] | 71 | return false; |
| 72 | |
Alexander Afanasyev | 38ba9b2 | 2012-08-21 13:32:43 -0700 | [diff] [blame] | 73 | 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 Afanasyev | ec1e395 | 2012-08-20 13:48:15 -0700 | [diff] [blame] | 82 | return true; |
Alexander Afanasyev | 048ae42 | 2012-08-17 17:33:02 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Alexander Afanasyev | 38ba9b2 | 2012-08-21 13:32:43 -0700 | [diff] [blame] | 85 | |
Alexander Afanasyev | 048ae42 | 2012-08-17 17:33:02 -0700 | [diff] [blame] | 86 | Ptr<pit::Entry> |
| 87 | PitQueue::Pop () |
| 88 | { |
Alexander Afanasyev | ec1e395 | 2012-08-20 13:48:15 -0700 | [diff] [blame] | 89 | PerInFaceQueue::iterator queue = m_lastQueue; |
| 90 | |
Alexander Afanasyev | ed449cc | 2012-08-21 11:10:33 -0700 | [diff] [blame] | 91 | while (queue != m_queues.end () && queue->second->size () == 0) // advance iterator |
Alexander Afanasyev | ec1e395 | 2012-08-20 13:48:15 -0700 | [diff] [blame] | 92 | { |
| 93 | queue ++; |
| 94 | } |
| 95 | |
| 96 | if (queue == m_queues.end ()) |
Alexander Afanasyev | 0ffa716 | 2012-08-21 22:39:22 -0700 | [diff] [blame^] | 97 | { |
| 98 | queue = m_queues.begin (); // circle to the beginning |
| 99 | } |
Alexander Afanasyev | ec1e395 | 2012-08-20 13:48:15 -0700 | [diff] [blame] | 100 | |
Alexander Afanasyev | ed449cc | 2012-08-21 11:10:33 -0700 | [diff] [blame] | 101 | while (queue != m_queues.end () && queue->second->size () == 0) // advance iterator |
Alexander Afanasyev | ec1e395 | 2012-08-20 13:48:15 -0700 | [diff] [blame] | 102 | { |
| 103 | queue ++; |
| 104 | } |
| 105 | |
| 106 | if (queue == m_queues.end ()) // e.g., begin () == end () |
Alexander Afanasyev | 048ae42 | 2012-08-17 17:33:02 -0700 | [diff] [blame] | 107 | return 0; |
| 108 | |
Alexander Afanasyev | ed449cc | 2012-08-21 11:10:33 -0700 | [diff] [blame] | 109 | NS_ASSERT_MSG (queue->second->size () != 0, "Logic error"); |
Alexander Afanasyev | 048ae42 | 2012-08-17 17:33:02 -0700 | [diff] [blame] | 110 | |
Alexander Afanasyev | ed449cc | 2012-08-21 11:10:33 -0700 | [diff] [blame] | 111 | Ptr<pit::Entry> entry = *queue->second->begin (); |
Alexander Afanasyev | 38ba9b2 | 2012-08-21 13:32:43 -0700 | [diff] [blame] | 112 | shared_ptr<fw::PitQueueTag> tag = entry->GetFwTag<fw::PitQueueTag> (); |
| 113 | NS_ASSERT (tag != shared_ptr<fw::PitQueueTag> ()); |
Alexander Afanasyev | 048ae42 | 2012-08-17 17:33:02 -0700 | [diff] [blame] | 114 | |
Alexander Afanasyev | 38ba9b2 | 2012-08-21 13:32:43 -0700 | [diff] [blame] | 115 | #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 Afanasyev | ec1e395 | 2012-08-20 13:48:15 -0700 | [diff] [blame] | 123 | m_lastQueue = queue; |
| 124 | return entry; |
Alexander Afanasyev | 048ae42 | 2012-08-17 17:33:02 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | void |
| 128 | PitQueue::Remove (Ptr<Face> face) |
| 129 | { |
Alexander Afanasyev | ec1e395 | 2012-08-20 13:48:15 -0700 | [diff] [blame] | 130 | if (m_lastQueue->first == face) |
Alexander Afanasyev | 048ae42 | 2012-08-17 17:33:02 -0700 | [diff] [blame] | 131 | { |
Alexander Afanasyev | ec1e395 | 2012-08-20 13:48:15 -0700 | [diff] [blame] | 132 | m_lastQueue++; |
Alexander Afanasyev | 048ae42 | 2012-08-17 17:33:02 -0700 | [diff] [blame] | 133 | } |
Alexander Afanasyev | ec1e395 | 2012-08-20 13:48:15 -0700 | [diff] [blame] | 134 | |
Alexander Afanasyev | 38ba9b2 | 2012-08-21 13:32:43 -0700 | [diff] [blame] | 135 | 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 Afanasyev | 048ae42 | 2012-08-17 17:33:02 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | void |
| 154 | PitQueue::Remove (Ptr<pit::Entry> entry) |
| 155 | { |
Alexander Afanasyev | 38ba9b2 | 2012-08-21 13:32:43 -0700 | [diff] [blame] | 156 | shared_ptr<fw::PitQueueTag> tag = entry->GetFwTag<fw::PitQueueTag> (); |
| 157 | if (tag == shared_ptr<fw::PitQueueTag> ()) |
| 158 | return; |
| 159 | |
| 160 | tag->RemoveFromAllQueues (); |
Alexander Afanasyev | 048ae42 | 2012-08-17 17:33:02 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Alexander Afanasyev | 5db9217 | 2012-08-21 16:52:07 -0700 | [diff] [blame] | 163 | bool |
| 164 | PitQueue::IsEmpty () const |
| 165 | { |
Alexander Afanasyev | 0ffa716 | 2012-08-21 22:39:22 -0700 | [diff] [blame^] | 166 | bool isEmpty = true; |
Alexander Afanasyev | 5db9217 | 2012-08-21 16:52:07 -0700 | [diff] [blame] | 167 | |
| 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 Afanasyev | 38ba9b2 | 2012-08-21 13:32:43 -0700 | [diff] [blame] | 178 | void |
| 179 | fw::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 | |
| 185 | void |
| 186 | fw::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 | |
| 197 | void |
| 198 | fw::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 Afanasyev | 048ae42 | 2012-08-17 17:33:02 -0700 | [diff] [blame] | 207 | |
| 208 | } // namespace ndn |
| 209 | } // namespace ns3 |