Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Teng Liang | 6f09ab6 | 2018-03-01 20:04:08 -0700 | [diff] [blame] | 2 | /* |
Davide Pesavento | 4d2e788 | 2024-02-15 23:00:21 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2024, Regents of the University of California, |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 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" |
Davide Pesavento | 50a6af3 | 2019-02-21 00:04:40 -0500 | [diff] [blame] | 27 | |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 28 | #include <algorithm> |
| 29 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 30 | namespace nfd::pit { |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 31 | |
Davide Pesavento | b124b8e | 2024-02-16 16:54:26 -0500 | [diff] [blame] | 32 | void |
| 33 | FaceRecord::update(const Interest& interest) |
| 34 | { |
| 35 | m_lastNonce = interest.getNonce(); |
| 36 | m_lastRenewed = time::steady_clock::now(); |
| 37 | |
| 38 | auto lifetime = interest.getInterestLifetime(); |
| 39 | // impose an upper bound on the lifetime to prevent integer overflow when calculating m_expiry |
| 40 | lifetime = std::min<time::milliseconds>(lifetime, 10_days); |
| 41 | m_expiry = m_lastRenewed + lifetime; |
| 42 | } |
| 43 | |
| 44 | void |
| 45 | InRecord::update(const Interest& interest) |
| 46 | { |
| 47 | FaceRecord::update(interest); |
| 48 | m_interest = interest.shared_from_this(); |
| 49 | } |
| 50 | |
| 51 | bool |
| 52 | OutRecord::setIncomingNack(const lp::Nack& nack) |
| 53 | { |
| 54 | if (nack.getInterest().getNonce() != this->getLastNonce()) { |
| 55 | return false; |
| 56 | } |
| 57 | |
| 58 | m_incomingNack = make_unique<lp::NackHeader>(nack.getHeader()); |
| 59 | return true; |
| 60 | } |
| 61 | |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 62 | Entry::Entry(const Interest& interest) |
Davide Pesavento | 50a6af3 | 2019-02-21 00:04:40 -0500 | [diff] [blame] | 63 | : m_interest(interest.shared_from_this()) |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 64 | { |
| 65 | } |
| 66 | |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 67 | bool |
| 68 | Entry::canMatch(const Interest& interest, size_t nEqualNameComps) const |
| 69 | { |
| 70 | BOOST_ASSERT(m_interest->getName().compare(0, nEqualNameComps, |
| 71 | interest.getName(), 0, nEqualNameComps) == 0); |
| 72 | |
| 73 | return m_interest->getName().compare(nEqualNameComps, Name::npos, |
| 74 | interest.getName(), nEqualNameComps) == 0 && |
Junxiao Shi | 25d9728 | 2019-05-14 13:44:46 -0600 | [diff] [blame] | 75 | m_interest->getCanBePrefix() == interest.getCanBePrefix() && |
| 76 | m_interest->getMustBeFresh() == interest.getMustBeFresh(); |
| 77 | /// \todo #3162 match ForwardingHint field |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Junxiao Shi | 4846f37 | 2016-04-05 13:39:30 -0700 | [diff] [blame] | 80 | InRecordCollection::iterator |
Davide Pesavento | 972de80 | 2024-02-16 18:42:55 -0500 | [diff] [blame] | 81 | Entry::findInRecord(const Face& face) noexcept |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 82 | { |
Junxiao Shi | 4846f37 | 2016-04-05 13:39:30 -0700 | [diff] [blame] | 83 | return std::find_if(m_inRecords.begin(), m_inRecords.end(), |
Davide Pesavento | 4d2e788 | 2024-02-15 23:00:21 -0500 | [diff] [blame] | 84 | [&face] (const InRecord& inRecord) { return &inRecord.getFace() == &face; }); |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 87 | InRecordCollection::iterator |
Md Ashiqur Rahman | c88d2d4 | 2019-08-28 20:19:47 +0000 | [diff] [blame] | 88 | Entry::insertOrUpdateInRecord(Face& face, const Interest& interest) |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 89 | { |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 90 | BOOST_ASSERT(this->canMatch(interest)); |
| 91 | |
Davide Pesavento | 972de80 | 2024-02-16 18:42:55 -0500 | [diff] [blame] | 92 | auto it = findInRecord(face); |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 93 | if (it == m_inRecords.end()) { |
Md Ashiqur Rahman | c88d2d4 | 2019-08-28 20:19:47 +0000 | [diff] [blame] | 94 | m_inRecords.emplace_front(face); |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 95 | it = m_inRecords.begin(); |
| 96 | } |
Junxiao Shi | 11bd9c2 | 2014-03-13 20:44:13 -0700 | [diff] [blame] | 97 | |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 98 | it->update(interest); |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 99 | return it; |
| 100 | } |
| 101 | |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 102 | OutRecordCollection::iterator |
Davide Pesavento | 972de80 | 2024-02-16 18:42:55 -0500 | [diff] [blame] | 103 | Entry::findOutRecord(const Face& face) noexcept |
Junxiao Shi | 4846f37 | 2016-04-05 13:39:30 -0700 | [diff] [blame] | 104 | { |
| 105 | return std::find_if(m_outRecords.begin(), m_outRecords.end(), |
Davide Pesavento | 4d2e788 | 2024-02-15 23:00:21 -0500 | [diff] [blame] | 106 | [&face] (const OutRecord& outRecord) { return &outRecord.getFace() == &face; }); |
Junxiao Shi | 4846f37 | 2016-04-05 13:39:30 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | OutRecordCollection::iterator |
Md Ashiqur Rahman | c88d2d4 | 2019-08-28 20:19:47 +0000 | [diff] [blame] | 110 | Entry::insertOrUpdateOutRecord(Face& face, const Interest& interest) |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 111 | { |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 112 | BOOST_ASSERT(this->canMatch(interest)); |
| 113 | |
Davide Pesavento | 972de80 | 2024-02-16 18:42:55 -0500 | [diff] [blame] | 114 | auto it = findOutRecord(face); |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 115 | if (it == m_outRecords.end()) { |
Md Ashiqur Rahman | c88d2d4 | 2019-08-28 20:19:47 +0000 | [diff] [blame] | 116 | m_outRecords.emplace_front(face); |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 117 | it = m_outRecords.begin(); |
| 118 | } |
Junxiao Shi | 11bd9c2 | 2014-03-13 20:44:13 -0700 | [diff] [blame] | 119 | |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 120 | it->update(interest); |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 121 | return it; |
| 122 | } |
| 123 | |
| 124 | void |
Md Ashiqur Rahman | c88d2d4 | 2019-08-28 20:19:47 +0000 | [diff] [blame] | 125 | Entry::deleteOutRecord(const Face& face) |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 126 | { |
Junxiao Shi | b2bcbcd | 2014-11-08 09:30:28 -0700 | [diff] [blame] | 127 | auto it = std::find_if(m_outRecords.begin(), m_outRecords.end(), |
Davide Pesavento | 4d2e788 | 2024-02-15 23:00:21 -0500 | [diff] [blame] | 128 | [&face] (const OutRecord& outRecord) { return &outRecord.getFace() == &face; }); |
Junxiao Shi | cbba04c | 2014-01-26 14:21:22 -0700 | [diff] [blame] | 129 | if (it != m_outRecords.end()) { |
| 130 | m_outRecords.erase(it); |
| 131 | } |
| 132 | } |
| 133 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 134 | } // namespace nfd::pit |