blob: be6202cf9bb1e07485cadf988d3e203bbb7c2efb [file] [log] [blame]
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev4b3fc862014-06-19 14:57:57 -07003 * 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 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&
59Entry::getFullName() const
60{
61 BOOST_ASSERT(!this->isQuery());
62 return m_data->getFullName();
63}
64
65bool
66Entry::canStale() const
67{
68 BOOST_ASSERT(!this->isQuery());
69 return m_data->getFreshnessPeriod() >= time::milliseconds::zero();
70}
71
72bool
73Entry::isStale() const
74{
75 BOOST_ASSERT(!this->isQuery());
76 return time::steady_clock::now() > m_staleAt;
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080077}
78
79void
Junxiao Shia9388182014-12-13 23:16:09 -070080Entry::refresh()
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080081{
Junxiao Shia9388182014-12-13 23:16:09 -070082 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 Moiseenko76cf77a2014-03-05 14:35:51 -080090
Junxiao Shia9388182014-12-13 23:16:09 -070091bool
92Entry::isUnsolicited() const
93{
94 BOOST_ASSERT(!this->isQuery());
95 return m_isUnsolicited;
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080096}
97
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080098void
Junxiao Shia9388182014-12-13 23:16:09 -070099Entry::unsetUnsolicited()
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800100{
Junxiao Shia9388182014-12-13 23:16:09 -0700101 BOOST_ASSERT(!this->isQuery());
102 m_isUnsolicited = false;
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800103}
104
Junxiao Shia9388182014-12-13 23:16:09 -0700105bool
106Entry::canSatisfy(const Interest& interest) const
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800107{
Junxiao Shia9388182014-12-13 23:16:09 -0700108 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 Moiseenko76cf77a2014-03-05 14:35:51 -0800118}
119
Junxiao Shia9388182014-12-13 23:16:09 -0700120bool
121Entry::operator<(const Entry& other) const
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800122{
Junxiao Shia9388182014-12-13 23:16:09 -0700123 if (this->isQuery()) {
124 if (other.isQuery()) {
125 return m_queryName < other.m_queryName;
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800126 }
Junxiao Shia9388182014-12-13 23:16:09 -0700127 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 Moiseenko76cf77a2014-03-05 14:35:51 -0800139}
140
141} // namespace cs
142} // namespace nfd