Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | 5640ec8 | 2015-01-07 21:51:19 -0700 | [diff] [blame^] | 3 | * Copyright (c) 2014-2015, 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& |
Junxiao Shi | 5640ec8 | 2015-01-07 21:51:19 -0700 | [diff] [blame^] | 59 | Entry::getName() const |
| 60 | { |
| 61 | BOOST_ASSERT(!this->isQuery()); |
| 62 | return m_data->getName(); |
| 63 | } |
| 64 | |
| 65 | const Name& |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 66 | Entry::getFullName() const |
| 67 | { |
| 68 | BOOST_ASSERT(!this->isQuery()); |
| 69 | return m_data->getFullName(); |
| 70 | } |
| 71 | |
| 72 | bool |
| 73 | Entry::canStale() const |
| 74 | { |
| 75 | BOOST_ASSERT(!this->isQuery()); |
| 76 | return m_data->getFreshnessPeriod() >= time::milliseconds::zero(); |
| 77 | } |
| 78 | |
| 79 | bool |
| 80 | Entry::isStale() const |
| 81 | { |
| 82 | BOOST_ASSERT(!this->isQuery()); |
| 83 | return time::steady_clock::now() > m_staleAt; |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | void |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 87 | Entry::refresh() |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 88 | { |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 89 | BOOST_ASSERT(!this->isQuery()); |
| 90 | if (this->canStale()) { |
| 91 | m_staleAt = time::steady_clock::now() + time::milliseconds(m_data->getFreshnessPeriod()); |
| 92 | } |
| 93 | else { |
| 94 | m_staleAt = time::steady_clock::TimePoint::max(); |
| 95 | } |
| 96 | } |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 97 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 98 | bool |
| 99 | Entry::isUnsolicited() const |
| 100 | { |
| 101 | BOOST_ASSERT(!this->isQuery()); |
| 102 | return m_isUnsolicited; |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 103 | } |
| 104 | |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 105 | void |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 106 | Entry::unsetUnsolicited() |
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 | m_isUnsolicited = false; |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 110 | } |
| 111 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 112 | bool |
| 113 | Entry::canSatisfy(const Interest& interest) const |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 114 | { |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 115 | BOOST_ASSERT(!this->isQuery()); |
| 116 | if (!interest.matchesData(*m_data)) { |
| 117 | return false; |
| 118 | } |
| 119 | |
| 120 | if (interest.getMustBeFresh() == static_cast<int>(true) && this->isStale()) { |
| 121 | return false; |
| 122 | } |
| 123 | |
| 124 | return true; |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 125 | } |
| 126 | |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 127 | bool |
| 128 | Entry::operator<(const Entry& other) const |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 129 | { |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 130 | if (this->isQuery()) { |
| 131 | if (other.isQuery()) { |
| 132 | return m_queryName < other.m_queryName; |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 133 | } |
Junxiao Shi | a938818 | 2014-12-13 23:16:09 -0700 | [diff] [blame] | 134 | else { |
| 135 | return m_queryName < other.m_queryName; |
| 136 | } |
| 137 | } |
| 138 | else { |
| 139 | if (other.isQuery()) { |
| 140 | return this->getFullName() < other.m_queryName; |
| 141 | } |
| 142 | else { |
| 143 | return this->getFullName() < other.getFullName(); |
| 144 | } |
| 145 | } |
Ilya Moiseenko | 76cf77a | 2014-03-05 14:35:51 -0800 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | } // namespace cs |
| 149 | } // namespace nfd |