NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
cs-entry.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2019, 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.
10  *
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/>.
24  */
25 
26 #include "cs-entry.hpp"
27 
28 namespace nfd {
29 namespace cs {
30 
31 Entry::Entry(shared_ptr<const Data> data, bool isUnsolicited)
32  : m_data(std::move(data))
33  , m_isUnsolicited(isUnsolicited)
34 {
36 }
37 
38 bool
40 {
41  return m_freshUntil >= time::steady_clock::now();
42 }
43 
44 void
46 {
47  m_freshUntil = time::steady_clock::now() + m_data->getFreshnessPeriod();
48 }
49 
50 bool
51 Entry::canSatisfy(const Interest& interest) const
52 {
53  if (!interest.matchesData(*m_data)) {
54  return false;
55  }
56 
57  if (interest.getMustBeFresh() && !this->isFresh()) {
58  return false;
59  }
60 
61  return true;
62 }
63 
64 static int
65 compareQueryWithData(const Name& queryName, const Data& data)
66 {
67  bool queryIsFullName = !queryName.empty() && queryName[-1].isImplicitSha256Digest();
68 
69  int cmp = queryIsFullName ?
70  queryName.compare(0, queryName.size() - 1, data.getName()) :
71  queryName.compare(data.getName());
72 
73  if (cmp != 0) { // Name without digest differs
74  return cmp;
75  }
76 
77  if (queryIsFullName) { // Name without digest equals, compare digest
78  return queryName[-1].compare(data.getFullName()[-1]);
79  }
80  else { // queryName is a proper prefix of Data fullName
81  return -1;
82  }
83 }
84 
85 static int
86 compareDataWithData(const Data& lhs, const Data& rhs)
87 {
88  int cmp = lhs.getName().compare(rhs.getName());
89  if (cmp != 0) {
90  return cmp;
91  }
92 
93  return lhs.getFullName()[-1].compare(rhs.getFullName()[-1]);
94 }
95 
96 bool
97 operator<(const Entry& entry, const Name& queryName)
98 {
99  return compareQueryWithData(queryName, entry.getData()) > 0;
100 }
101 
102 bool
103 operator<(const Name& queryName, const Entry& entry)
104 {
105  return compareQueryWithData(queryName, entry.getData()) < 0;
106 }
107 
108 bool
109 operator<(const Entry& lhs, const Entry& rhs)
110 {
111  return compareDataWithData(lhs.getData(), rhs.getData()) < 0;
112 }
113 
114 } // namespace cs
115 } // namespace nfd
bool getMustBeFresh() const noexcept
Check whether the MustBeFresh element is present.
Definition: interest.hpp:205
bool operator<(const Entry &entry, const Name &queryName)
Definition: cs-entry.cpp:97
static time_point now() noexcept
Definition: time.cpp:80
STL namespace.
static int compareQueryWithData(const Name &queryName, const Data &data)
Definition: cs-entry.cpp:65
Represents an Interest packet.
Definition: interest.hpp:48
bool isFresh() const
check if the stored Data is fresh now
Definition: cs-entry.cpp:39
int compare(const Name &other) const
Compare this to the other Name using NDN canonical ordering.
Definition: name.hpp:629
NDN_CXX_NODISCARD bool empty() const
Checks if the name is empty, i.e.
Definition: name.hpp:143
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:39
a ContentStore entry
Definition: cs-entry.hpp:36
bool matchesData(const Data &data) const
Check if Interest can be satisfied by data.
Definition: interest.cpp:325
const Name & getName() const noexcept
Get name.
Definition: data.hpp:127
Represents an absolute name.
Definition: name.hpp:41
const Name & getFullName() const
Get full name including implicit digest.
Definition: data.cpp:207
const Data & getData() const
return the stored Data
Definition: cs-entry.hpp:42
size_t size() const
Returns the number of components.
Definition: name.hpp:151
Entry(shared_ptr< const Data > data, bool isUnsolicited)
Definition: cs-entry.cpp:31
Represents a Data packet.
Definition: data.hpp:37
static int compareDataWithData(const Data &lhs, const Data &rhs)
Definition: cs-entry.cpp:86
bool canSatisfy(const Interest &interest) const
determine whether Interest can be satisified by the stored Data
Definition: cs-entry.cpp:51
void updateFreshUntil()
recalculate when the entry would become non-fresh, relative to current time
Definition: cs-entry.cpp:45