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 | |
Junxiao Shi | 11bd9c2 | 2014-03-13 20:44:13 -0700 | [diff] [blame] | 46 | bool |
| 47 | Entry::hasLocalInRecord() const |
| 48 | { |
Junxiao Shi | b2bcbcd | 2014-11-08 09:30:28 -0700 | [diff] [blame] | 49 | return std::any_of(m_inRecords.begin(), m_inRecords.end(), |
| 50 | [] (const InRecord& inRecord) { return inRecord.getFace()->isLocal(); }); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | bool |
Junxiao Shi | 57f0f31 | 2014-03-16 11:52:20 -0700 | [diff] [blame] | 54 | Entry::canForwardTo(const Face& face) const |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 55 | { |
Junxiao Shi | b2bcbcd | 2014-11-08 09:30:28 -0700 | [diff] [blame] | 56 | time::steady_clock::TimePoint now = time::steady_clock::now(); |
| 57 | |
| 58 | bool hasUnexpiredOutRecord = std::any_of(m_outRecords.begin(), m_outRecords.end(), |
| 59 | [&face, &now] (const OutRecord& outRecord) { |
| 60 | return outRecord.getFace().get() == &face && outRecord.getExpiry() >= now; |
| 61 | }); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 62 | if (hasUnexpiredOutRecord) { |
| 63 | return false; |
| 64 | } |
Junxiao Shi | 11bd9c2 | 2014-03-13 20:44:13 -0700 | [diff] [blame] | 65 | |
Junxiao Shi | b2bcbcd | 2014-11-08 09:30:28 -0700 | [diff] [blame] | 66 | bool hasUnexpiredOtherInRecord = std::any_of(m_inRecords.begin(), m_inRecords.end(), |
| 67 | [&face, &now] (const InRecord& inRecord) { |
| 68 | return inRecord.getFace().get() != &face && inRecord.getExpiry() >= now; |
| 69 | }); |
Junxiao Shi | 57f0f31 | 2014-03-16 11:52:20 -0700 | [diff] [blame] | 70 | if (!hasUnexpiredOtherInRecord) { |
| 71 | return false; |
| 72 | } |
| 73 | |
| 74 | return !this->violatesScope(face); |
| 75 | } |
| 76 | |
| 77 | bool |
| 78 | Entry::violatesScope(const Face& face) const |
| 79 | { |
| 80 | // /localhost scope |
| 81 | bool isViolatingLocalhost = !face.isLocal() && |
| 82 | LOCALHOST_NAME.isPrefixOf(this->getName()); |
| 83 | if (isViolatingLocalhost) { |
| 84 | return true; |
| 85 | } |
| 86 | |
| 87 | // /localhop scope |
| 88 | bool isViolatingLocalhop = !face.isLocal() && |
| 89 | LOCALHOP_NAME.isPrefixOf(this->getName()) && |
| 90 | !this->hasLocalInRecord(); |
| 91 | if (isViolatingLocalhop) { |
| 92 | return true; |
| 93 | } |
| 94 | |
| 95 | return false; |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Junxiao Shi | a110f26 | 2014-10-12 12:35:20 -0700 | [diff] [blame] | 98 | int |
| 99 | Entry::findNonce(uint32_t nonce, const Face& face) const |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 100 | { |
Junxiao Shi | a110f26 | 2014-10-12 12:35:20 -0700 | [diff] [blame] | 101 | // TODO should we ignore expired in/out records? |
| 102 | |
| 103 | int dnw = DUPLICATE_NONCE_NONE; |
| 104 | |
Junxiao Shi | b2bcbcd | 2014-11-08 09:30:28 -0700 | [diff] [blame] | 105 | for (const InRecord& inRecord : m_inRecords) { |
| 106 | if (inRecord.getLastNonce() == nonce) { |
| 107 | if (inRecord.getFace().get() == &face) { |
Junxiao Shi | a110f26 | 2014-10-12 12:35:20 -0700 | [diff] [blame] | 108 | dnw |= DUPLICATE_NONCE_IN_SAME; |
| 109 | } |
| 110 | else { |
| 111 | dnw |= DUPLICATE_NONCE_IN_OTHER; |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
Junxiao Shi | b2bcbcd | 2014-11-08 09:30:28 -0700 | [diff] [blame] | 116 | for (const OutRecord& outRecord : m_outRecords) { |
| 117 | if (outRecord.getLastNonce() == nonce) { |
| 118 | if (outRecord.getFace().get() == &face) { |
Junxiao Shi | a110f26 | 2014-10-12 12:35:20 -0700 | [diff] [blame] | 119 | dnw |= DUPLICATE_NONCE_OUT_SAME; |
| 120 | } |
| 121 | else { |
| 122 | dnw |= DUPLICATE_NONCE_OUT_OTHER; |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | return dnw; |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 130 | InRecordCollection::iterator |
| 131 | Entry::insertOrUpdateInRecord(shared_ptr<Face> face, const Interest& interest) |
| 132 | { |
Junxiao Shi | b2bcbcd | 2014-11-08 09:30:28 -0700 | [diff] [blame] | 133 | auto it = std::find_if(m_inRecords.begin(), m_inRecords.end(), |
| 134 | [&face] (const InRecord& inRecord) { return inRecord.getFace() == face; }); |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 135 | if (it == m_inRecords.end()) { |
Junxiao Shi | b2bcbcd | 2014-11-08 09:30:28 -0700 | [diff] [blame] | 136 | m_inRecords.emplace_front(face); |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 137 | it = m_inRecords.begin(); |
| 138 | } |
Junxiao Shi | 11bd9c2 | 2014-03-13 20:44:13 -0700 | [diff] [blame] | 139 | |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 140 | it->update(interest); |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 141 | return it; |
| 142 | } |
| 143 | |
Junxiao Shi | 66f91f8 | 2014-05-10 17:28:58 -0700 | [diff] [blame] | 144 | InRecordCollection::const_iterator |
Junxiao Shi | b2bcbcd | 2014-11-08 09:30:28 -0700 | [diff] [blame] | 145 | Entry::getInRecord(const Face& face) const |
Junxiao Shi | 66f91f8 | 2014-05-10 17:28:58 -0700 | [diff] [blame] | 146 | { |
| 147 | return std::find_if(m_inRecords.begin(), m_inRecords.end(), |
Junxiao Shi | b2bcbcd | 2014-11-08 09:30:28 -0700 | [diff] [blame] | 148 | [&face] (const InRecord& inRecord) { return inRecord.getFace().get() == &face; }); |
Junxiao Shi | 66f91f8 | 2014-05-10 17:28:58 -0700 | [diff] [blame] | 149 | } |
| 150 | |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 151 | void |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 152 | Entry::deleteInRecord(const Face& face) |
| 153 | { |
| 154 | auto it = std::find_if(m_inRecords.begin(), m_inRecords.end(), |
| 155 | [&face] (const InRecord& inRecord) { return inRecord.getFace().get() == &face; }); |
| 156 | if (it != m_inRecords.end()) { |
| 157 | m_inRecords.erase(it); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | void |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 162 | Entry::deleteInRecords() |
| 163 | { |
| 164 | m_inRecords.clear(); |
| 165 | } |
| 166 | |
| 167 | OutRecordCollection::iterator |
| 168 | Entry::insertOrUpdateOutRecord(shared_ptr<Face> face, const Interest& interest) |
| 169 | { |
Junxiao Shi | b2bcbcd | 2014-11-08 09:30:28 -0700 | [diff] [blame] | 170 | auto it = std::find_if(m_outRecords.begin(), m_outRecords.end(), |
| 171 | [&face] (const OutRecord& outRecord) { return outRecord.getFace() == face; }); |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 172 | if (it == m_outRecords.end()) { |
Junxiao Shi | b2bcbcd | 2014-11-08 09:30:28 -0700 | [diff] [blame] | 173 | m_outRecords.emplace_front(face); |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 174 | it = m_outRecords.begin(); |
| 175 | } |
Junxiao Shi | 11bd9c2 | 2014-03-13 20:44:13 -0700 | [diff] [blame] | 176 | |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 177 | it->update(interest); |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 178 | return it; |
| 179 | } |
| 180 | |
Junxiao Shi | 5e5e445 | 2015-09-24 16:56:52 -0700 | [diff] [blame] | 181 | OutRecordCollection::iterator |
| 182 | Entry::getOutRecord(const Face& face) |
Junxiao Shi | 66f91f8 | 2014-05-10 17:28:58 -0700 | [diff] [blame] | 183 | { |
| 184 | return std::find_if(m_outRecords.begin(), m_outRecords.end(), |
Junxiao Shi | b2bcbcd | 2014-11-08 09:30:28 -0700 | [diff] [blame] | 185 | [&face] (const OutRecord& outRecord) { return outRecord.getFace().get() == &face; }); |
Junxiao Shi | 66f91f8 | 2014-05-10 17:28:58 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 188 | void |
Junxiao Shi | b2bcbcd | 2014-11-08 09:30:28 -0700 | [diff] [blame] | 189 | Entry::deleteOutRecord(const Face& face) |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 190 | { |
Junxiao Shi | b2bcbcd | 2014-11-08 09:30:28 -0700 | [diff] [blame] | 191 | auto it = std::find_if(m_outRecords.begin(), m_outRecords.end(), |
| 192 | [&face] (const OutRecord& outRecord) { return outRecord.getFace().get() == &face; }); |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 193 | if (it != m_outRecords.end()) { |
| 194 | m_outRecords.erase(it); |
| 195 | } |
| 196 | } |
| 197 | |
Junxiao Shi | 57f0f31 | 2014-03-16 11:52:20 -0700 | [diff] [blame] | 198 | bool |
| 199 | Entry::hasUnexpiredOutRecords() const |
| 200 | { |
Junxiao Shi | b2bcbcd | 2014-11-08 09:30:28 -0700 | [diff] [blame] | 201 | time::steady_clock::TimePoint now = time::steady_clock::now(); |
| 202 | |
| 203 | return std::any_of(m_outRecords.begin(), m_outRecords.end(), |
| 204 | [&now] (const OutRecord& outRecord) { return outRecord.getExpiry() >= now; }); |
Junxiao Shi | 57f0f31 | 2014-03-16 11:52:20 -0700 | [diff] [blame] | 205 | } |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 206 | |
| 207 | } // namespace pit |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 208 | } // namespace nfd |