blob: 470e850e1d16f8de3ff2b63a0798e9301029b73d [file] [log] [blame]
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi14b39182019-04-29 19:19:18 +00002/*
3 * Copyright (c) 2014-2019, Regents of the University of California,
Junxiao Shi5640ec82015-01-07 21:51:19 -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.
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080010 *
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070011 * 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/>.
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080024 */
25
26#include "cs-entry.hpp"
27
28namespace nfd {
29namespace cs {
30
Junxiao Shi5e4a02f2019-07-15 11:59:18 +000031Entry::Entry(shared_ptr<const Data> data, bool isUnsolicited)
32 : m_data(std::move(data))
33 , m_isUnsolicited(isUnsolicited)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080034{
Junxiao Shi5e4a02f2019-07-15 11:59:18 +000035 updateFreshUntil();
Junxiao Shia9388182014-12-13 23:16:09 -070036}
37
38bool
Junxiao Shi5e4a02f2019-07-15 11:59:18 +000039Entry::isFresh() const
Junxiao Shia9388182014-12-13 23:16:09 -070040{
Junxiao Shi5e4a02f2019-07-15 11:59:18 +000041 return m_freshUntil >= time::steady_clock::now();
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080042}
43
44void
Junxiao Shi5e4a02f2019-07-15 11:59:18 +000045Entry::updateFreshUntil()
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080046{
Junxiao Shi5e4a02f2019-07-15 11:59:18 +000047 m_freshUntil = time::steady_clock::now() + m_data->getFreshnessPeriod();
Junxiao Shia9388182014-12-13 23:16:09 -070048}
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080049
Junxiao Shia9388182014-12-13 23:16:09 -070050bool
Junxiao Shia9388182014-12-13 23:16:09 -070051Entry::canSatisfy(const Interest& interest) const
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080052{
Junxiao Shia9388182014-12-13 23:16:09 -070053 if (!interest.matchesData(*m_data)) {
54 return false;
55 }
56
Junxiao Shi5e4a02f2019-07-15 11:59:18 +000057 if (interest.getMustBeFresh() && !this->isFresh()) {
Junxiao Shia9388182014-12-13 23:16:09 -070058 return false;
59 }
60
61 return true;
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080062}
63
Junxiao Shi5e4a02f2019-07-15 11:59:18 +000064static int
65compareQueryWithData(const Name& queryName, const Data& data)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080066{
Junxiao Shi5e4a02f2019-07-15 11:59:18 +000067 bool queryIsFullName = !queryName.empty() && queryName[-1].isImplicitSha256Digest();
68
69 int cmp = queryIsFullName ?
70 queryName.compare(0, queryName.size() - 1, data.getName()) :
71 queryName.compare(data.getName());
72
73 if (cmp != 0) { // Name without digest differs
74 return cmp;
75 }
76
77 if (queryIsFullName) { // Name without digest equals, compare digest
78 return queryName[-1].compare(data.getFullName()[-1]);
79 }
80 else { // queryName is a proper prefix of Data fullName
81 return -1;
82 }
83}
84
85static int
86compareDataWithData(const Data& lhs, const Data& rhs)
87{
88 int cmp = lhs.getName().compare(rhs.getName());
89 if (cmp != 0) {
90 return cmp;
91 }
92
93 return lhs.getFullName()[-1].compare(rhs.getFullName()[-1]);
94}
95
96bool
97operator<(const Entry& entry, const Name& queryName)
98{
99 return compareQueryWithData(queryName, entry.getData()) > 0;
100}
101
102bool
103operator<(const Name& queryName, const Entry& entry)
104{
105 return compareQueryWithData(queryName, entry.getData()) < 0;
106}
107
108bool
109operator<(const Entry& lhs, const Entry& rhs)
110{
111 return compareDataWithData(lhs.getData(), rhs.getData()) < 0;
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800112}
113
114} // namespace cs
115} // namespace nfd