Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 14b3918 | 2019-04-29 19:19:18 +0000 | [diff] [blame] | 2 | /* |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2022, Regents of the University of California, |
Junxiao Shi | 5640ec8 | 2015-01-07 21:51:19 -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. |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 10 | * |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 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/>. |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 24 | */ |
| 25 | |
| 26 | #include "cs-entry.hpp" |
| 27 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 28 | namespace nfd::cs { |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 29 | |
Junxiao Shi | 5e4a02f | 2019-07-15 11:59:18 +0000 | [diff] [blame] | 30 | Entry::Entry(shared_ptr<const Data> data, bool isUnsolicited) |
| 31 | : m_data(std::move(data)) |
| 32 | , m_isUnsolicited(isUnsolicited) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 33 | { |
Junxiao Shi | 5e4a02f | 2019-07-15 11:59:18 +0000 | [diff] [blame] | 34 | updateFreshUntil(); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | bool |
Junxiao Shi | 5e4a02f | 2019-07-15 11:59:18 +0000 | [diff] [blame] | 38 | Entry::isFresh() const |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 39 | { |
Junxiao Shi | 5e4a02f | 2019-07-15 11:59:18 +0000 | [diff] [blame] | 40 | return m_freshUntil >= time::steady_clock::now(); |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | void |
Junxiao Shi | 5e4a02f | 2019-07-15 11:59:18 +0000 | [diff] [blame] | 44 | Entry::updateFreshUntil() |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 45 | { |
Junxiao Shi | 5e4a02f | 2019-07-15 11:59:18 +0000 | [diff] [blame] | 46 | m_freshUntil = time::steady_clock::now() + m_data->getFreshnessPeriod(); |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 47 | } |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 48 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 49 | bool |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 50 | Entry::canSatisfy(const Interest& interest) const |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 51 | { |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 52 | if (!interest.matchesData(*m_data)) { |
| 53 | return false; |
| 54 | } |
| 55 | |
Junxiao Shi | 5e4a02f | 2019-07-15 11:59:18 +0000 | [diff] [blame] | 56 | if (interest.getMustBeFresh() && !this->isFresh()) { |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 57 | return false; |
| 58 | } |
| 59 | |
| 60 | return true; |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 61 | } |
| 62 | |
Junxiao Shi | 5e4a02f | 2019-07-15 11:59:18 +0000 | [diff] [blame] | 63 | static int |
| 64 | compareQueryWithData(const Name& queryName, const Data& data) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 65 | { |
Junxiao Shi | 5e4a02f | 2019-07-15 11:59:18 +0000 | [diff] [blame] | 66 | bool queryIsFullName = !queryName.empty() && queryName[-1].isImplicitSha256Digest(); |
| 67 | |
| 68 | int cmp = queryIsFullName ? |
| 69 | queryName.compare(0, queryName.size() - 1, data.getName()) : |
| 70 | queryName.compare(data.getName()); |
| 71 | |
| 72 | if (cmp != 0) { // Name without digest differs |
| 73 | return cmp; |
| 74 | } |
| 75 | |
| 76 | if (queryIsFullName) { // Name without digest equals, compare digest |
| 77 | return queryName[-1].compare(data.getFullName()[-1]); |
| 78 | } |
| 79 | else { // queryName is a proper prefix of Data fullName |
| 80 | return -1; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | static int |
| 85 | compareDataWithData(const Data& lhs, const Data& rhs) |
| 86 | { |
| 87 | int cmp = lhs.getName().compare(rhs.getName()); |
| 88 | if (cmp != 0) { |
| 89 | return cmp; |
| 90 | } |
| 91 | |
| 92 | return lhs.getFullName()[-1].compare(rhs.getFullName()[-1]); |
| 93 | } |
| 94 | |
| 95 | bool |
| 96 | operator<(const Entry& entry, const Name& queryName) |
| 97 | { |
| 98 | return compareQueryWithData(queryName, entry.getData()) > 0; |
| 99 | } |
| 100 | |
| 101 | bool |
| 102 | operator<(const Name& queryName, const Entry& entry) |
| 103 | { |
| 104 | return compareQueryWithData(queryName, entry.getData()) < 0; |
| 105 | } |
| 106 | |
| 107 | bool |
| 108 | operator<(const Entry& lhs, const Entry& rhs) |
| 109 | { |
| 110 | return compareDataWithData(lhs.getData(), rhs.getData()) < 0; |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 111 | } |
| 112 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 113 | } // namespace nfd::cs |