pit-entry.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2024, 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.
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/>.
24  */
25 
26 #include "pit-entry.hpp"
27 
28 #include <algorithm>
29 
30 namespace nfd::pit {
31 
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 
62 Entry::Entry(const Interest& interest)
63  : m_interest(interest.shared_from_this())
64 {
65 }
66 
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 &&
75  m_interest->getCanBePrefix() == interest.getCanBePrefix() &&
76  m_interest->getMustBeFresh() == interest.getMustBeFresh();
78 }
79 
80 InRecordCollection::iterator
81 Entry::findInRecord(const Face& face) noexcept
82 {
83  return std::find_if(m_inRecords.begin(), m_inRecords.end(),
84  [&face] (const InRecord& inRecord) { return &inRecord.getFace() == &face; });
85 }
86 
87 InRecordCollection::iterator
88 Entry::insertOrUpdateInRecord(Face& face, const Interest& interest)
89 {
90  BOOST_ASSERT(this->canMatch(interest));
91 
92  auto it = findInRecord(face);
93  if (it == m_inRecords.end()) {
94  m_inRecords.emplace_front(face);
95  it = m_inRecords.begin();
96  }
97 
98  it->update(interest);
99  return it;
100 }
101 
102 OutRecordCollection::iterator
103 Entry::findOutRecord(const Face& face) noexcept
104 {
105  return std::find_if(m_outRecords.begin(), m_outRecords.end(),
106  [&face] (const OutRecord& outRecord) { return &outRecord.getFace() == &face; });
107 }
108 
109 OutRecordCollection::iterator
110 Entry::insertOrUpdateOutRecord(Face& face, const Interest& interest)
111 {
112  BOOST_ASSERT(this->canMatch(interest));
113 
114  auto it = findOutRecord(face);
115  if (it == m_outRecords.end()) {
116  m_outRecords.emplace_front(face);
117  it = m_outRecords.begin();
118  }
119 
120  it->update(interest);
121  return it;
122 }
123 
124 void
126 {
127  auto it = std::find_if(m_outRecords.begin(), m_outRecords.end(),
128  [&face] (const OutRecord& outRecord) { return &outRecord.getFace() == &face; });
129  if (it != m_outRecords.end()) {
130  m_outRecords.erase(it);
131  }
132 }
133 
134 } // namespace nfd::pit
Generalization of a network interface.
Definition: face.hpp:118
bool canMatch(const Interest &interest, size_t nEqualNameComps=0) const
Definition: pit-entry.cpp:68
Entry(const Interest &interest)
Definition: pit-entry.cpp:62
OutRecordCollection::iterator findOutRecord(const Face &face) noexcept
Get the out-record for face.
Definition: pit-entry.cpp:103
OutRecordCollection::iterator insertOrUpdateOutRecord(Face &face, const Interest &interest)
Insert or update an out-record.
Definition: pit-entry.cpp:110
InRecordCollection::iterator findInRecord(const Face &face) noexcept
Get the in-record for face.
Definition: pit-entry.cpp:81
InRecordCollection::iterator insertOrUpdateInRecord(Face &face, const Interest &interest)
Insert or update an in-record.
Definition: pit-entry.cpp:88
void deleteOutRecord(const Face &face)
Delete the out-record for face if it exists.
Definition: pit-entry.cpp:125
void update(const Interest &interest)
Updates lastNonce, lastRenewed, expiry fields.
Definition: pit-entry.cpp:33
Contains information about an Interest from an incoming face.
Definition: pit-entry.hpp:107
void update(const Interest &interest)
Definition: pit-entry.cpp:45
Contains information about an Interest toward an outgoing face.
Definition: pit-entry.hpp:129
bool setIncomingNack(const lp::Nack &nack)
Sets a Nack received from getFace().
Definition: pit-entry.cpp:52
Definition: fib.hpp:40