blob: cfbbc82ae423f78dff9bf011e7470786b45a9d6f [file] [log] [blame]
Junxiao Shicbba04c2014-01-26 14:21:22 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Teng Liang6f09ab62018-03-01 20:04:08 -07002/*
Davide Pesavento4d2e7882024-02-15 23:00:21 -05003 * Copyright (c) 2014-2024, Regents of the University of California,
Junxiao Shicde37ad2015-12-24 01:02:05 -07004 * 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 Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
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 Afanasyev28d586a2014-07-10 20:10:54 -070024 */
Junxiao Shicbba04c2014-01-26 14:21:22 -070025
26#include "pit-entry.hpp"
Davide Pesavento50a6af32019-02-21 00:04:40 -050027
Junxiao Shicbba04c2014-01-26 14:21:22 -070028#include <algorithm>
29
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040030namespace nfd::pit {
Junxiao Shicbba04c2014-01-26 14:21:22 -070031
Davide Pesaventob124b8e2024-02-16 16:54:26 -050032void
33FaceRecord::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
44void
45InRecord::update(const Interest& interest)
46{
47 FaceRecord::update(interest);
48 m_interest = interest.shared_from_this();
49}
50
51bool
52OutRecord::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 Shicbba04c2014-01-26 14:21:22 -070062Entry::Entry(const Interest& interest)
Davide Pesavento50a6af32019-02-21 00:04:40 -050063 : m_interest(interest.shared_from_this())
Junxiao Shicbba04c2014-01-26 14:21:22 -070064{
65}
66
Junxiao Shia2742922016-11-17 22:53:23 +000067bool
68Entry::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 Shi25d97282019-05-14 13:44:46 -060075 m_interest->getCanBePrefix() == interest.getCanBePrefix() &&
76 m_interest->getMustBeFresh() == interest.getMustBeFresh();
77 /// \todo #3162 match ForwardingHint field
Junxiao Shia2742922016-11-17 22:53:23 +000078}
79
Junxiao Shi4846f372016-04-05 13:39:30 -070080InRecordCollection::iterator
Davide Pesaventob124b8e2024-02-16 16:54:26 -050081Entry::findInRecord(const Face& face)
Junxiao Shicbba04c2014-01-26 14:21:22 -070082{
Junxiao Shi4846f372016-04-05 13:39:30 -070083 return std::find_if(m_inRecords.begin(), m_inRecords.end(),
Davide Pesavento4d2e7882024-02-15 23:00:21 -050084 [&face] (const InRecord& inRecord) { return &inRecord.getFace() == &face; });
Junxiao Shicbba04c2014-01-26 14:21:22 -070085}
86
Junxiao Shicbba04c2014-01-26 14:21:22 -070087InRecordCollection::iterator
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +000088Entry::insertOrUpdateInRecord(Face& face, const Interest& interest)
Junxiao Shicbba04c2014-01-26 14:21:22 -070089{
Junxiao Shia2742922016-11-17 22:53:23 +000090 BOOST_ASSERT(this->canMatch(interest));
91
Junxiao Shib2bcbcd2014-11-08 09:30:28 -070092 auto it = std::find_if(m_inRecords.begin(), m_inRecords.end(),
Davide Pesavento4d2e7882024-02-15 23:00:21 -050093 [&face] (const InRecord& inRecord) { return &inRecord.getFace() == &face; });
Junxiao Shicbba04c2014-01-26 14:21:22 -070094 if (it == m_inRecords.end()) {
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +000095 m_inRecords.emplace_front(face);
Junxiao Shicbba04c2014-01-26 14:21:22 -070096 it = m_inRecords.begin();
97 }
Junxiao Shi11bd9c22014-03-13 20:44:13 -070098
Junxiao Shicbba04c2014-01-26 14:21:22 -070099 it->update(interest);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700100 return it;
101}
102
Junxiao Shicbba04c2014-01-26 14:21:22 -0700103OutRecordCollection::iterator
Davide Pesaventob124b8e2024-02-16 16:54:26 -0500104Entry::findOutRecord(const Face& face)
Junxiao Shi4846f372016-04-05 13:39:30 -0700105{
106 return std::find_if(m_outRecords.begin(), m_outRecords.end(),
Davide Pesavento4d2e7882024-02-15 23:00:21 -0500107 [&face] (const OutRecord& outRecord) { return &outRecord.getFace() == &face; });
Junxiao Shi4846f372016-04-05 13:39:30 -0700108}
109
110OutRecordCollection::iterator
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000111Entry::insertOrUpdateOutRecord(Face& face, const Interest& interest)
Junxiao Shicbba04c2014-01-26 14:21:22 -0700112{
Junxiao Shia2742922016-11-17 22:53:23 +0000113 BOOST_ASSERT(this->canMatch(interest));
114
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700115 auto it = std::find_if(m_outRecords.begin(), m_outRecords.end(),
Davide Pesavento4d2e7882024-02-15 23:00:21 -0500116 [&face] (const OutRecord& outRecord) { return &outRecord.getFace() == &face; });
Junxiao Shicbba04c2014-01-26 14:21:22 -0700117 if (it == m_outRecords.end()) {
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000118 m_outRecords.emplace_front(face);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700119 it = m_outRecords.begin();
120 }
Junxiao Shi11bd9c22014-03-13 20:44:13 -0700121
Junxiao Shicbba04c2014-01-26 14:21:22 -0700122 it->update(interest);
Junxiao Shicbba04c2014-01-26 14:21:22 -0700123 return it;
124}
125
126void
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000127Entry::deleteOutRecord(const Face& face)
Junxiao Shicbba04c2014-01-26 14:21:22 -0700128{
Junxiao Shib2bcbcd2014-11-08 09:30:28 -0700129 auto it = std::find_if(m_outRecords.begin(), m_outRecords.end(),
Davide Pesavento4d2e7882024-02-15 23:00:21 -0500130 [&face] (const OutRecord& outRecord) { return &outRecord.getFace() == &face; });
Junxiao Shicbba04c2014-01-26 14:21:22 -0700131 if (it != m_outRecords.end()) {
132 m_outRecords.erase(it);
133 }
134}
135
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400136} // namespace nfd::pit