Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 28d586a | 2014-07-10 20:10:54 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014, Regents of the University of California, |
| 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
Alexander Afanasyev | 28d586a | 2014-07-10 20:10:54 -0700 | [diff] [blame] | 24 | */ |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 25 | |
| 26 | #include "pit-entry.hpp" |
| 27 | #include <algorithm> |
| 28 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 29 | namespace nfd { |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 30 | namespace pit { |
| 31 | |
Junxiao Shi | 57f0f31 | 2014-03-16 11:52:20 -0700 | [diff] [blame] | 32 | const Name Entry::LOCALHOST_NAME("ndn:/localhost"); |
| 33 | const Name Entry::LOCALHOP_NAME("ndn:/localhop"); |
| 34 | |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 35 | Entry::Entry(const Interest& interest) |
Alexander Afanasyev | 28d586a | 2014-07-10 20:10:54 -0700 | [diff] [blame] | 36 | : m_interest(interest.shared_from_this()) |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 37 | { |
| 38 | } |
| 39 | |
| 40 | const Name& |
| 41 | Entry::getName() const |
| 42 | { |
Alexander Afanasyev | 28d586a | 2014-07-10 20:10:54 -0700 | [diff] [blame] | 43 | return m_interest->getName(); |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | const InRecordCollection& |
| 47 | Entry::getInRecords() const |
| 48 | { |
| 49 | return m_inRecords; |
| 50 | } |
| 51 | |
| 52 | const OutRecordCollection& |
| 53 | Entry::getOutRecords() const |
| 54 | { |
| 55 | return m_outRecords; |
| 56 | } |
| 57 | |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 58 | static inline bool |
Junxiao Shi | 11bd9c2 | 2014-03-13 20:44:13 -0700 | [diff] [blame] | 59 | predicate_InRecord_isLocal(const InRecord& inRecord) |
| 60 | { |
| 61 | return inRecord.getFace()->isLocal(); |
| 62 | } |
| 63 | |
| 64 | bool |
| 65 | Entry::hasLocalInRecord() const |
| 66 | { |
| 67 | InRecordCollection::const_iterator it = std::find_if( |
| 68 | m_inRecords.begin(), m_inRecords.end(), &predicate_InRecord_isLocal); |
| 69 | return it != m_inRecords.end(); |
| 70 | } |
| 71 | |
| 72 | static inline bool |
Junxiao Shi | 57f0f31 | 2014-03-16 11:52:20 -0700 | [diff] [blame] | 73 | predicate_FaceRecord_Face(const FaceRecord& faceRecord, const Face* face) |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 74 | { |
Junxiao Shi | 57f0f31 | 2014-03-16 11:52:20 -0700 | [diff] [blame] | 75 | return faceRecord.getFace().get() == face; |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | static inline bool |
| 79 | predicate_FaceRecord_ne_Face_and_unexpired(const FaceRecord& faceRecord, |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 80 | const Face* face, const time::steady_clock::TimePoint& now) |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 81 | { |
Junxiao Shi | 57f0f31 | 2014-03-16 11:52:20 -0700 | [diff] [blame] | 82 | return faceRecord.getFace().get() != face && faceRecord.getExpiry() >= now; |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | bool |
Junxiao Shi | 57f0f31 | 2014-03-16 11:52:20 -0700 | [diff] [blame] | 86 | Entry::canForwardTo(const Face& face) const |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 87 | { |
| 88 | OutRecordCollection::const_iterator outIt = std::find_if( |
| 89 | m_outRecords.begin(), m_outRecords.end(), |
Junxiao Shi | 57f0f31 | 2014-03-16 11:52:20 -0700 | [diff] [blame] | 90 | bind(&predicate_FaceRecord_Face, _1, &face)); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 91 | bool hasUnexpiredOutRecord = outIt != m_outRecords.end() && |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 92 | outIt->getExpiry() >= time::steady_clock::now(); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 93 | if (hasUnexpiredOutRecord) { |
| 94 | return false; |
| 95 | } |
Junxiao Shi | 11bd9c2 | 2014-03-13 20:44:13 -0700 | [diff] [blame] | 96 | |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 97 | InRecordCollection::const_iterator inIt = std::find_if( |
| 98 | m_inRecords.begin(), m_inRecords.end(), |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 99 | bind(&predicate_FaceRecord_ne_Face_and_unexpired, _1, &face, time::steady_clock::now())); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 100 | bool hasUnexpiredOtherInRecord = inIt != m_inRecords.end(); |
Junxiao Shi | 57f0f31 | 2014-03-16 11:52:20 -0700 | [diff] [blame] | 101 | if (!hasUnexpiredOtherInRecord) { |
| 102 | return false; |
| 103 | } |
| 104 | |
| 105 | return !this->violatesScope(face); |
| 106 | } |
| 107 | |
| 108 | bool |
| 109 | Entry::violatesScope(const Face& face) const |
| 110 | { |
| 111 | // /localhost scope |
| 112 | bool isViolatingLocalhost = !face.isLocal() && |
| 113 | LOCALHOST_NAME.isPrefixOf(this->getName()); |
| 114 | if (isViolatingLocalhost) { |
| 115 | return true; |
| 116 | } |
| 117 | |
| 118 | // /localhop scope |
| 119 | bool isViolatingLocalhop = !face.isLocal() && |
| 120 | LOCALHOP_NAME.isPrefixOf(this->getName()) && |
| 121 | !this->hasLocalInRecord(); |
| 122 | if (isViolatingLocalhop) { |
| 123 | return true; |
| 124 | } |
| 125 | |
| 126 | return false; |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Junxiao Shi | a110f26 | 2014-10-12 12:35:20 -0700 | [diff] [blame^] | 129 | int |
| 130 | Entry::findNonce(uint32_t nonce, const Face& face) const |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 131 | { |
Junxiao Shi | a110f26 | 2014-10-12 12:35:20 -0700 | [diff] [blame^] | 132 | // TODO should we ignore expired in/out records? |
| 133 | |
| 134 | int dnw = DUPLICATE_NONCE_NONE; |
| 135 | |
| 136 | for (InRecordCollection::const_iterator it = m_inRecords.begin(); |
| 137 | it != m_inRecords.end(); ++it) { |
| 138 | if (it->getLastNonce() == nonce) { |
| 139 | if (it->getFace().get() == &face) { |
| 140 | dnw |= DUPLICATE_NONCE_IN_SAME; |
| 141 | } |
| 142 | else { |
| 143 | dnw |= DUPLICATE_NONCE_IN_OTHER; |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | for (OutRecordCollection::const_iterator it = m_outRecords.begin(); |
| 149 | it != m_outRecords.end(); ++it) { |
| 150 | if (it->getLastNonce() == nonce) { |
| 151 | if (it->getFace().get() == &face) { |
| 152 | dnw |= DUPLICATE_NONCE_OUT_SAME; |
| 153 | } |
| 154 | else { |
| 155 | dnw |= DUPLICATE_NONCE_OUT_OTHER; |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | return dnw; |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 163 | InRecordCollection::iterator |
| 164 | Entry::insertOrUpdateInRecord(shared_ptr<Face> face, const Interest& interest) |
| 165 | { |
| 166 | InRecordCollection::iterator it = std::find_if(m_inRecords.begin(), |
Junxiao Shi | 57f0f31 | 2014-03-16 11:52:20 -0700 | [diff] [blame] | 167 | m_inRecords.end(), bind(&predicate_FaceRecord_Face, _1, face.get())); |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 168 | if (it == m_inRecords.end()) { |
| 169 | m_inRecords.push_front(InRecord(face)); |
| 170 | it = m_inRecords.begin(); |
| 171 | } |
Junxiao Shi | 11bd9c2 | 2014-03-13 20:44:13 -0700 | [diff] [blame] | 172 | |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 173 | it->update(interest); |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 174 | return it; |
| 175 | } |
| 176 | |
Junxiao Shi | 66f91f8 | 2014-05-10 17:28:58 -0700 | [diff] [blame] | 177 | InRecordCollection::const_iterator |
| 178 | Entry::getInRecord(shared_ptr<Face> face) const |
| 179 | { |
| 180 | return std::find_if(m_inRecords.begin(), m_inRecords.end(), |
| 181 | bind(&predicate_FaceRecord_Face, _1, face.get())); |
| 182 | } |
| 183 | |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 184 | void |
| 185 | Entry::deleteInRecords() |
| 186 | { |
| 187 | m_inRecords.clear(); |
| 188 | } |
| 189 | |
| 190 | OutRecordCollection::iterator |
| 191 | Entry::insertOrUpdateOutRecord(shared_ptr<Face> face, const Interest& interest) |
| 192 | { |
| 193 | OutRecordCollection::iterator it = std::find_if(m_outRecords.begin(), |
Junxiao Shi | 57f0f31 | 2014-03-16 11:52:20 -0700 | [diff] [blame] | 194 | m_outRecords.end(), bind(&predicate_FaceRecord_Face, _1, face.get())); |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 195 | if (it == m_outRecords.end()) { |
| 196 | m_outRecords.push_front(OutRecord(face)); |
| 197 | it = m_outRecords.begin(); |
| 198 | } |
Junxiao Shi | 11bd9c2 | 2014-03-13 20:44:13 -0700 | [diff] [blame] | 199 | |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 200 | it->update(interest); |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 201 | return it; |
| 202 | } |
| 203 | |
Junxiao Shi | 66f91f8 | 2014-05-10 17:28:58 -0700 | [diff] [blame] | 204 | OutRecordCollection::const_iterator |
| 205 | Entry::getOutRecord(shared_ptr<Face> face) const |
| 206 | { |
| 207 | return std::find_if(m_outRecords.begin(), m_outRecords.end(), |
| 208 | bind(&predicate_FaceRecord_Face, _1, face.get())); |
| 209 | } |
| 210 | |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 211 | void |
| 212 | Entry::deleteOutRecord(shared_ptr<Face> face) |
| 213 | { |
| 214 | OutRecordCollection::iterator it = std::find_if(m_outRecords.begin(), |
Junxiao Shi | 57f0f31 | 2014-03-16 11:52:20 -0700 | [diff] [blame] | 215 | m_outRecords.end(), bind(&predicate_FaceRecord_Face, _1, face.get())); |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 216 | if (it != m_outRecords.end()) { |
| 217 | m_outRecords.erase(it); |
| 218 | } |
| 219 | } |
| 220 | |
Junxiao Shi | 57f0f31 | 2014-03-16 11:52:20 -0700 | [diff] [blame] | 221 | static inline bool |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 222 | predicate_FaceRecord_unexpired(const FaceRecord& faceRecord, const time::steady_clock::TimePoint& now) |
Junxiao Shi | 57f0f31 | 2014-03-16 11:52:20 -0700 | [diff] [blame] | 223 | { |
| 224 | return faceRecord.getExpiry() >= now; |
| 225 | } |
| 226 | |
| 227 | bool |
| 228 | Entry::hasUnexpiredOutRecords() const |
| 229 | { |
| 230 | OutRecordCollection::const_iterator it = std::find_if(m_outRecords.begin(), |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 231 | m_outRecords.end(), bind(&predicate_FaceRecord_unexpired, _1, time::steady_clock::now())); |
Junxiao Shi | 57f0f31 | 2014-03-16 11:52:20 -0700 | [diff] [blame] | 232 | return it != m_outRecords.end(); |
| 233 | } |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 234 | |
| 235 | } // namespace pit |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 236 | } // namespace nfd |