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; |
| 29 | |
| 30 | namespace ns3 { |
| 31 | namespace ndn { |
| 32 | |
| 33 | PitQueue::Entry::Entry (Ptr<Face> inFace, Ptr<pit::Entry> pitEntry, double virtualTime) |
| 34 | : m_inFace (inFace) |
| 35 | , m_pitEntry (pitEntry) |
| 36 | , m_virtualTime (virtualTime) |
| 37 | { |
| 38 | } |
| 39 | |
| 40 | bool |
| 41 | PitQueue::Entry::operator < (const Entry &otherEntry) const |
| 42 | { |
| 43 | return this->m_virtualTime < otherEntry.m_virtualTime; |
| 44 | } |
| 45 | |
| 46 | |
| 47 | bool |
| 48 | PitQueue::Enqueue (Ptr<Face> inFace, |
| 49 | Ptr<pit::Entry> pitEntry) |
| 50 | { |
| 51 | return false; |
| 52 | } |
| 53 | |
| 54 | Ptr<pit::Entry> |
| 55 | PitQueue::Pop () |
| 56 | { |
| 57 | if (m_queue.size () == 0) |
| 58 | return 0; |
| 59 | |
| 60 | PendingInterestsQueue::iterator topEntryIterator = m_queue.begin (); |
| 61 | Entry entry = *topEntryIterator; // copy entry |
| 62 | m_queue.erase (topEntryIterator); // remove entry |
| 63 | |
| 64 | DecreasePerFaceCount (entry.m_inFace); |
| 65 | |
| 66 | if (m_queue.size () == 0) |
| 67 | m_lastProcessedVirtualTime = 0; |
| 68 | else |
| 69 | m_lastProcessedVirtualTime = entry.m_virtualTime; |
| 70 | |
| 71 | |
| 72 | return entry.m_pitEntry; |
| 73 | } |
| 74 | |
| 75 | void |
| 76 | PitQueue::DecreasePerFaceCount (Ptr<Face> inFace) |
| 77 | { |
| 78 | PerInFaceMapOfNumberOfIncomingInterests::iterator numberOfEnqueued = m_numberEnqueuedInterests.find (inFace); |
| 79 | NS_ASSERT_MSG (numberOfEnqueued != m_numberEnqueuedInterests.end () && |
| 80 | numberOfEnqueued->second > 0, |
| 81 | "Logic error"); |
| 82 | |
| 83 | numberOfEnqueued->second --; |
| 84 | if (numberOfEnqueued->second == 0) |
| 85 | { |
| 86 | m_numberEnqueuedInterests.erase (numberOfEnqueued); |
| 87 | m_lastVirtualTime.erase (inFace); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | void |
| 92 | PitQueue::Remove (Ptr<Face> face) |
| 93 | { |
| 94 | m_numberEnqueuedInterests.erase (face); |
| 95 | m_lastVirtualTime.erase (face); |
| 96 | |
| 97 | for (PendingInterestsQueue::iterator item = m_queue.begin (); |
| 98 | item != m_queue.end (); |
| 99 | /* manual iterator advancement */) |
| 100 | { |
| 101 | if (item->m_inFace == face) |
| 102 | { |
| 103 | PendingInterestsQueue::iterator toRemove = item; |
| 104 | item ++; |
| 105 | m_queue.erase (toRemove); // hopefully a safe operation |
| 106 | } |
| 107 | else |
| 108 | item ++; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | void |
| 113 | PitQueue::Remove (Ptr<pit::Entry> entry) |
| 114 | { |
| 115 | for (PendingInterestsQueue::iterator item = m_queue.begin (); |
| 116 | item != m_queue.end (); |
| 117 | /* manual iterator advancement */) |
| 118 | { |
| 119 | // compare addresses |
| 120 | if (&(*entry) == &(**item)) |
| 121 | { |
| 122 | PendingInterestsQueue::iterator toRemove = item; |
| 123 | item ++; |
| 124 | |
| 125 | DecreasePerFaceCount (toRemove->m_inFace); |
| 126 | UpdateLastVirtTime (toRemove->m_inFace); |
| 127 | |
| 128 | m_queue.erase (toRemove); // hopefully a safe operation |
| 129 | break; // there should be at most one item for one pit entry |
| 130 | } |
| 131 | else |
| 132 | item ++; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | void |
| 137 | PitQueue::UpdateLastVirtTime (Ptr<Face> inFace) |
| 138 | { |
| 139 | PerInFaceMapOfNumberOfIncomingInterests::iterator numberOfEnqueued = m_numberEnqueuedInterests.find (inFace); |
| 140 | if (numberOfEnqueued->second == 0) |
| 141 | return; |
| 142 | |
| 143 | double lastTime = 0; |
| 144 | for (PendingInterestsQueue::iterator item = m_queue.begin (); |
| 145 | item != m_queue.end (); |
| 146 | item ++) |
| 147 | { |
| 148 | if (inFace == item->m_inFace) |
| 149 | { |
| 150 | if (lastTime < item->m_virtualTime) |
| 151 | lastTime = item->m_virtualTime; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | m_lastVirtualTime [inFace] = lastTime; |
| 156 | } |
| 157 | |
| 158 | |
| 159 | } // namespace ndn |
| 160 | } // namespace ns3 |