blob: 584dda3c0b42863088f7ff4b7498225f59d9a239 [file] [log] [blame]
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi5640ec82015-01-07 21:51:19 -07003 * 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 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 Shia9388182014-12-13 23:16:09 -070031Entry::Entry(const Name& name)
32 : m_queryName(name)
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080033{
Junxiao Shia9388182014-12-13 23:16:09 -070034 BOOST_ASSERT(this->isQuery());
35}
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080036
Junxiao Shia9388182014-12-13 23:16:09 -070037Entry::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
45bool
46Entry::isQuery() const
47{
48 return m_data == nullptr;
49}
50
51shared_ptr<const Data>
52Entry::getData() const
53{
54 BOOST_ASSERT(!this->isQuery());
55 return m_data;
56}
57
58const Name&
Junxiao Shi5640ec82015-01-07 21:51:19 -070059Entry::getName() const
60{
61 BOOST_ASSERT(!this->isQuery());
62 return m_data->getName();
63}
64
65const Name&
Junxiao Shia9388182014-12-13 23:16:09 -070066Entry::getFullName() const
67{
68 BOOST_ASSERT(!this->isQuery());
69 return m_data->getFullName();
70}
71
72bool
73Entry::canStale() const
74{
75 BOOST_ASSERT(!this->isQuery());
76 return m_data->getFreshnessPeriod() >= time::milliseconds::zero();
77}
78
79bool
80Entry::isStale() const
81{
82 BOOST_ASSERT(!this->isQuery());
83 return time::steady_clock::now() > m_staleAt;
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080084}
85
86void
Junxiao Shia9388182014-12-13 23:16:09 -070087Entry::refresh()
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080088{
Junxiao Shia9388182014-12-13 23:16:09 -070089 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 Moiseenko76cf77a2014-03-05 14:35:51 -080097
Junxiao Shia9388182014-12-13 23:16:09 -070098bool
99Entry::isUnsolicited() const
100{
101 BOOST_ASSERT(!this->isQuery());
102 return m_isUnsolicited;
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800103}
104
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800105void
Junxiao Shia9388182014-12-13 23:16:09 -0700106Entry::unsetUnsolicited()
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800107{
Junxiao Shia9388182014-12-13 23:16:09 -0700108 BOOST_ASSERT(!this->isQuery());
109 m_isUnsolicited = false;
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800110}
111
Junxiao Shia9388182014-12-13 23:16:09 -0700112bool
113Entry::canSatisfy(const Interest& interest) const
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800114{
Junxiao Shia9388182014-12-13 23:16:09 -0700115 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 Moiseenko76cf77a2014-03-05 14:35:51 -0800125}
126
Junxiao Shia9388182014-12-13 23:16:09 -0700127bool
128Entry::operator<(const Entry& other) const
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800129{
Junxiao Shia9388182014-12-13 23:16:09 -0700130 if (this->isQuery()) {
131 if (other.isQuery()) {
132 return m_queryName < other.m_queryName;
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800133 }
Junxiao Shia9388182014-12-13 23:16:09 -0700134 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 Moiseenko76cf77a2014-03-05 14:35:51 -0800146}
147
148} // namespace cs
149} // namespace nfd