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" |
| 25 | |
| 26 | #include "ns3/assert.h" |
| 27 | |
| 28 | using namespace std; |
Alexander Afanasyev | 38ba9b2 | 2012-08-21 13:32:43 -0700 | [diff] [blame] | 29 | using namespace boost; |
Alexander Afanasyev | 048ae42 | 2012-08-17 17:33:02 -0700 | [diff] [blame] | 30 | |
| 31 | namespace ns3 { |
| 32 | namespace ndn { |
| 33 | |
Alexander Afanasyev | ec1e395 | 2012-08-20 13:48:15 -0700 | [diff] [blame] | 34 | PitQueue::PitQueue () |
| 35 | : m_maxQueueSize (10) |
| 36 | , m_lastQueue (m_queues.begin ()) |
Alexander Afanasyev | 048ae42 | 2012-08-17 17:33:02 -0700 | [diff] [blame] | 37 | { |
| 38 | } |
Alexander Afanasyev | ec1e395 | 2012-08-20 13:48:15 -0700 | [diff] [blame] | 39 | |
| 40 | void |
| 41 | PitQueue::SetMaxQueueSize (uint32_t size) |
Alexander Afanasyev | 048ae42 | 2012-08-17 17:33:02 -0700 | [diff] [blame] | 42 | { |
Alexander Afanasyev | ec1e395 | 2012-08-20 13:48:15 -0700 | [diff] [blame] | 43 | m_maxQueueSize = size; |
| 44 | } |
| 45 | |
| 46 | uint32_t |
| 47 | PitQueue::GetMaxQueueSize () const |
| 48 | { |
| 49 | return m_maxQueueSize; |
Alexander Afanasyev | 048ae42 | 2012-08-17 17:33:02 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | |
| 53 | bool |
| 54 | PitQueue::Enqueue (Ptr<Face> inFace, |
| 55 | Ptr<pit::Entry> pitEntry) |
| 56 | { |
Alexander Afanasyev | ed449cc | 2012-08-21 11:10:33 -0700 | [diff] [blame] | 57 | PerInFaceQueue::iterator queue = m_queues.find (inFace); |
| 58 | if (queue == m_queues.end ()) |
| 59 | { |
| 60 | pair<PerInFaceQueue::iterator, bool> itemPair = m_queues.insert (make_pair (inFace, boost::make_shared<Queue> ())); |
| 61 | NS_ASSERT (itemPair.second == true); |
| 62 | |
| 63 | queue = itemPair.first; |
| 64 | } |
| 65 | |
| 66 | if (queue->second->size () >= m_maxQueueSize) |
Alexander Afanasyev | ec1e395 | 2012-08-20 13:48:15 -0700 | [diff] [blame] | 67 | return false; |
| 68 | |
Alexander Afanasyev | 38ba9b2 | 2012-08-21 13:32:43 -0700 | [diff] [blame] | 69 | Queue::iterator itemIterator = queue->second->insert (queue->second->end (), pitEntry); |
| 70 | |
| 71 | shared_ptr<fw::PitQueueTag> tag = pitEntry->GetFwTag<fw::PitQueueTag> (); |
| 72 | if (tag == shared_ptr<fw::PitQueueTag> ()) |
| 73 | { |
| 74 | tag = make_shared<fw::PitQueueTag> (); |
| 75 | pitEntry->AddFwTag (tag); |
| 76 | } |
| 77 | tag->InsertQueue (queue->second, itemIterator); |
Alexander Afanasyev | ec1e395 | 2012-08-20 13:48:15 -0700 | [diff] [blame] | 78 | return true; |
Alexander Afanasyev | 048ae42 | 2012-08-17 17:33:02 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Alexander Afanasyev | 38ba9b2 | 2012-08-21 13:32:43 -0700 | [diff] [blame] | 81 | |
Alexander Afanasyev | 048ae42 | 2012-08-17 17:33:02 -0700 | [diff] [blame] | 82 | Ptr<pit::Entry> |
| 83 | PitQueue::Pop () |
| 84 | { |
Alexander Afanasyev | ec1e395 | 2012-08-20 13:48:15 -0700 | [diff] [blame] | 85 | PerInFaceQueue::iterator queue = m_lastQueue; |
| 86 | |
Alexander Afanasyev | ed449cc | 2012-08-21 11:10:33 -0700 | [diff] [blame] | 87 | while (queue != m_queues.end () && queue->second->size () == 0) // advance iterator |
Alexander Afanasyev | ec1e395 | 2012-08-20 13:48:15 -0700 | [diff] [blame] | 88 | { |
| 89 | queue ++; |
| 90 | } |
| 91 | |
| 92 | if (queue == m_queues.end ()) |
| 93 | queue = m_queues.begin (); // circle to the beginning |
| 94 | |
Alexander Afanasyev | ed449cc | 2012-08-21 11:10:33 -0700 | [diff] [blame] | 95 | while (queue != m_queues.end () && queue->second->size () == 0) // advance iterator |
Alexander Afanasyev | ec1e395 | 2012-08-20 13:48:15 -0700 | [diff] [blame] | 96 | { |
| 97 | queue ++; |
| 98 | } |
| 99 | |
| 100 | if (queue == m_queues.end ()) // e.g., begin () == end () |
Alexander Afanasyev | 048ae42 | 2012-08-17 17:33:02 -0700 | [diff] [blame] | 101 | return 0; |
| 102 | |
Alexander Afanasyev | ed449cc | 2012-08-21 11:10:33 -0700 | [diff] [blame] | 103 | NS_ASSERT_MSG (queue->second->size () != 0, "Logic error"); |
Alexander Afanasyev | 048ae42 | 2012-08-17 17:33:02 -0700 | [diff] [blame] | 104 | |
Alexander Afanasyev | ed449cc | 2012-08-21 11:10:33 -0700 | [diff] [blame] | 105 | Ptr<pit::Entry> entry = *queue->second->begin (); |
Alexander Afanasyev | 38ba9b2 | 2012-08-21 13:32:43 -0700 | [diff] [blame] | 106 | shared_ptr<fw::PitQueueTag> tag = entry->GetFwTag<fw::PitQueueTag> (); |
| 107 | NS_ASSERT (tag != shared_ptr<fw::PitQueueTag> ()); |
Alexander Afanasyev | 048ae42 | 2012-08-17 17:33:02 -0700 | [diff] [blame] | 108 | |
Alexander Afanasyev | 38ba9b2 | 2012-08-21 13:32:43 -0700 | [diff] [blame] | 109 | #ifdef NS3_LOG_ENABLE |
| 110 | size_t queueSize = queue->second->size (); |
| 111 | #endif |
| 112 | tag->RemoveFromQueue (queue->second); |
| 113 | #ifdef NS3_LOG_ENABLE |
| 114 | NS_ASSERT_MSG (queue->second->size () == queueSize-1, "Queue size should be reduced by one"); |
| 115 | #endif |
| 116 | |
Alexander Afanasyev | ec1e395 | 2012-08-20 13:48:15 -0700 | [diff] [blame] | 117 | m_lastQueue = queue; |
| 118 | return entry; |
Alexander Afanasyev | 048ae42 | 2012-08-17 17:33:02 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | void |
| 122 | PitQueue::Remove (Ptr<Face> face) |
| 123 | { |
Alexander Afanasyev | ec1e395 | 2012-08-20 13:48:15 -0700 | [diff] [blame] | 124 | if (m_lastQueue->first == face) |
Alexander Afanasyev | 048ae42 | 2012-08-17 17:33:02 -0700 | [diff] [blame] | 125 | { |
Alexander Afanasyev | ec1e395 | 2012-08-20 13:48:15 -0700 | [diff] [blame] | 126 | m_lastQueue++; |
Alexander Afanasyev | 048ae42 | 2012-08-17 17:33:02 -0700 | [diff] [blame] | 127 | } |
Alexander Afanasyev | ec1e395 | 2012-08-20 13:48:15 -0700 | [diff] [blame] | 128 | |
Alexander Afanasyev | 38ba9b2 | 2012-08-21 13:32:43 -0700 | [diff] [blame] | 129 | PerInFaceQueue::iterator queue = m_queues.find (face); |
| 130 | if (queue == m_queues.end ()) |
| 131 | return; |
| 132 | |
| 133 | for (Queue::iterator pitEntry = queue->second->begin (); |
| 134 | pitEntry != queue->second->end (); |
| 135 | pitEntry ++) |
| 136 | { |
| 137 | shared_ptr<fw::PitQueueTag> tag = (*pitEntry)->GetFwTag<fw::PitQueueTag> (); |
| 138 | NS_ASSERT (tag != shared_ptr<fw::PitQueueTag> ()); |
| 139 | |
| 140 | tag->RemoveFromQueue (queue->second); |
| 141 | } |
| 142 | |
| 143 | NS_ASSERT_MSG (queue->second->size () == 0, "Queue size should be 0 by now"); |
| 144 | m_queues.erase (queue); |
Alexander Afanasyev | 048ae42 | 2012-08-17 17:33:02 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | void |
| 148 | PitQueue::Remove (Ptr<pit::Entry> entry) |
| 149 | { |
Alexander Afanasyev | 38ba9b2 | 2012-08-21 13:32:43 -0700 | [diff] [blame] | 150 | shared_ptr<fw::PitQueueTag> tag = entry->GetFwTag<fw::PitQueueTag> (); |
| 151 | if (tag == shared_ptr<fw::PitQueueTag> ()) |
| 152 | return; |
| 153 | |
| 154 | tag->RemoveFromAllQueues (); |
Alexander Afanasyev | 048ae42 | 2012-08-17 17:33:02 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Alexander Afanasyev | 5db9217 | 2012-08-21 16:52:07 -0700 | [diff] [blame] | 157 | bool |
| 158 | PitQueue::IsEmpty () const |
| 159 | { |
| 160 | bool isEmpty = (m_queues.size () == 0); |
| 161 | |
| 162 | for (PerInFaceQueue::const_iterator queue = m_queues.begin (); |
| 163 | queue != m_queues.end (); |
| 164 | queue ++) |
| 165 | { |
| 166 | isEmpty &= (queue->second->size () == 0); |
| 167 | } |
| 168 | |
| 169 | return isEmpty; |
| 170 | } |
| 171 | |
Alexander Afanasyev | 38ba9b2 | 2012-08-21 13:32:43 -0700 | [diff] [blame] | 172 | void |
| 173 | fw::PitQueueTag::InsertQueue (boost::shared_ptr<PitQueue::Queue> queue, PitQueue::Queue::iterator iterator) |
| 174 | { |
| 175 | pair<MapOfItems::iterator, bool> item = m_items.insert (make_pair (queue, iterator)); |
| 176 | NS_ASSERT (item.second == true); |
| 177 | } |
| 178 | |
| 179 | void |
| 180 | fw::PitQueueTag::RemoveFromAllQueues () |
| 181 | { |
| 182 | for (MapOfItems::iterator item = m_items.begin (); |
| 183 | item != m_items.end (); |
| 184 | item ++) |
| 185 | { |
| 186 | item->first->erase (item->second); |
| 187 | } |
| 188 | m_items.clear (); |
| 189 | } |
| 190 | |
| 191 | void |
| 192 | fw::PitQueueTag::RemoveFromQueue (boost::shared_ptr<PitQueue::Queue> queue) |
| 193 | { |
| 194 | MapOfItems::iterator item = m_items.find (queue); |
| 195 | if (item == m_items.end ()) |
| 196 | return; |
| 197 | |
| 198 | item->first->erase (item->second); |
| 199 | m_items.erase (item); |
| 200 | } |
Alexander Afanasyev | 048ae42 | 2012-08-17 17:33:02 -0700 | [diff] [blame] | 201 | |
| 202 | } // namespace ndn |
| 203 | } // namespace ns3 |