Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 4b3fc86 | 2014-06-19 14:57:57 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014, 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 |
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 | |
| 28 | namespace nfd { |
| 29 | namespace cs { |
| 30 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame^] | 31 | Entry::Entry(const Name& name) |
| 32 | : m_queryName(name) |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 33 | { |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame^] | 34 | BOOST_ASSERT(this->isQuery()); |
| 35 | } |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 36 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame^] | 37 | Entry::Entry(shared_ptr<const Data> data, bool isUnsolicited) |
| 38 | : queueType(Cs::QUEUE_NONE) |
| 39 | , m_data(data) |
| 40 | , m_isUnsolicited(isUnsolicited) |
| 41 | { |
| 42 | BOOST_ASSERT(!this->isQuery()); |
| 43 | } |
| 44 | |
| 45 | bool |
| 46 | Entry::isQuery() const |
| 47 | { |
| 48 | return m_data == nullptr; |
| 49 | } |
| 50 | |
| 51 | shared_ptr<const Data> |
| 52 | Entry::getData() const |
| 53 | { |
| 54 | BOOST_ASSERT(!this->isQuery()); |
| 55 | return m_data; |
| 56 | } |
| 57 | |
| 58 | const Name& |
| 59 | Entry::getFullName() const |
| 60 | { |
| 61 | BOOST_ASSERT(!this->isQuery()); |
| 62 | return m_data->getFullName(); |
| 63 | } |
| 64 | |
| 65 | bool |
| 66 | Entry::canStale() const |
| 67 | { |
| 68 | BOOST_ASSERT(!this->isQuery()); |
| 69 | return m_data->getFreshnessPeriod() >= time::milliseconds::zero(); |
| 70 | } |
| 71 | |
| 72 | bool |
| 73 | Entry::isStale() const |
| 74 | { |
| 75 | BOOST_ASSERT(!this->isQuery()); |
| 76 | return time::steady_clock::now() > m_staleAt; |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | void |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame^] | 80 | Entry::refresh() |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 81 | { |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame^] | 82 | BOOST_ASSERT(!this->isQuery()); |
| 83 | if (this->canStale()) { |
| 84 | m_staleAt = time::steady_clock::now() + time::milliseconds(m_data->getFreshnessPeriod()); |
| 85 | } |
| 86 | else { |
| 87 | m_staleAt = time::steady_clock::TimePoint::max(); |
| 88 | } |
| 89 | } |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 90 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame^] | 91 | bool |
| 92 | Entry::isUnsolicited() const |
| 93 | { |
| 94 | BOOST_ASSERT(!this->isQuery()); |
| 95 | return m_isUnsolicited; |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 96 | } |
| 97 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 98 | void |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame^] | 99 | Entry::unsetUnsolicited() |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 100 | { |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame^] | 101 | BOOST_ASSERT(!this->isQuery()); |
| 102 | m_isUnsolicited = false; |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 103 | } |
| 104 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame^] | 105 | bool |
| 106 | Entry::canSatisfy(const Interest& interest) const |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 107 | { |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame^] | 108 | BOOST_ASSERT(!this->isQuery()); |
| 109 | if (!interest.matchesData(*m_data)) { |
| 110 | return false; |
| 111 | } |
| 112 | |
| 113 | if (interest.getMustBeFresh() == static_cast<int>(true) && this->isStale()) { |
| 114 | return false; |
| 115 | } |
| 116 | |
| 117 | return true; |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 118 | } |
| 119 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame^] | 120 | bool |
| 121 | Entry::operator<(const Entry& other) const |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 122 | { |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame^] | 123 | if (this->isQuery()) { |
| 124 | if (other.isQuery()) { |
| 125 | return m_queryName < other.m_queryName; |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 126 | } |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame^] | 127 | else { |
| 128 | return m_queryName < other.m_queryName; |
| 129 | } |
| 130 | } |
| 131 | else { |
| 132 | if (other.isQuery()) { |
| 133 | return this->getFullName() < other.m_queryName; |
| 134 | } |
| 135 | else { |
| 136 | return this->getFullName() < other.getFullName(); |
| 137 | } |
| 138 | } |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | } // namespace cs |
| 142 | } // namespace nfd |