NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
cs.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2018, 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.hpp"
27 #include "core/algorithm.hpp"
28 #include "core/asserts.hpp"
29 #include "core/logger.hpp"
30 #include <ndn-cxx/lp/tags.hpp>
31 
32 namespace nfd {
33 namespace cs {
34 
36 
37 NFD_LOG_INIT("ContentStore");
38 
39 unique_ptr<Policy>
41 {
42  const std::string DEFAULT_POLICY = "priority_fifo";
43  return Policy::create(DEFAULT_POLICY);
44 }
45 
46 Cs::Cs(size_t nMaxPackets)
47  : m_shouldAdmit(true)
48  , m_shouldServe(true)
49 {
50  this->setPolicyImpl(makeDefaultPolicy());
51  m_policy->setLimit(nMaxPackets);
52 }
53 
54 void
55 Cs::insert(const Data& data, bool isUnsolicited)
56 {
57  if (!m_shouldAdmit || m_policy->getLimit() == 0) {
58  return;
59  }
60  NFD_LOG_DEBUG("insert " << data.getName());
61 
62  // recognize CachePolicy
63  shared_ptr<lp::CachePolicyTag> tag = data.getTag<lp::CachePolicyTag>();
64  if (tag != nullptr) {
65  lp::CachePolicyType policy = tag->get().getPolicy();
66  if (policy == lp::CachePolicyType::NO_CACHE) {
67  return;
68  }
69  }
70 
71  iterator it;
72  bool isNewEntry = false;
73  std::tie(it, isNewEntry) = m_table.emplace(data.shared_from_this(), isUnsolicited);
74  EntryImpl& entry = const_cast<EntryImpl&>(*it);
75 
76  entry.updateStaleTime();
77 
78  if (!isNewEntry) { // existing entry
79  // XXX This doesn't forbid unsolicited Data from refreshing a solicited entry.
80  if (entry.isUnsolicited() && !isUnsolicited) {
81  entry.unsetUnsolicited();
82  }
83 
84  m_policy->afterRefresh(it);
85  }
86  else {
87  m_policy->afterInsert(it);
88  }
89 }
90 
91 void
92 Cs::find(const Interest& interest,
93  const HitCallback& hitCallback,
94  const MissCallback& missCallback) const
95 {
96  BOOST_ASSERT(static_cast<bool>(hitCallback));
97  BOOST_ASSERT(static_cast<bool>(missCallback));
98 
99  if (!m_shouldServe || m_policy->getLimit() == 0) {
100  missCallback(interest);
101  return;
102  }
103  const Name& prefix = interest.getName();
104  bool isRightmost = interest.getChildSelector() == 1;
105  NFD_LOG_DEBUG("find " << prefix << (isRightmost ? " R" : " L"));
106 
107  iterator first = m_table.lower_bound(prefix);
108  iterator last = m_table.end();
109  if (prefix.size() > 0) {
110  last = m_table.lower_bound(prefix.getSuccessor());
111  }
112 
113  iterator match = last;
114  if (isRightmost) {
115  match = this->findRightmost(interest, first, last);
116  }
117  else {
118  match = this->findLeftmost(interest, first, last);
119  }
120 
121  if (match == last) {
122  NFD_LOG_DEBUG(" no-match");
123  missCallback(interest);
124  return;
125  }
126  NFD_LOG_DEBUG(" matching " << match->getName());
127  m_policy->beforeUse(match);
128  hitCallback(interest, match->getData());
129 }
130 
131 iterator
132 Cs::findLeftmost(const Interest& interest, iterator first, iterator last) const
133 {
134  return std::find_if(first, last, bind(&cs::EntryImpl::canSatisfy, _1, interest));
135 }
136 
137 iterator
138 Cs::findRightmost(const Interest& interest, iterator first, iterator last) const
139 {
140  // Each loop visits a sub-namespace under a prefix one component longer than Interest Name.
141  // If there is a match in that sub-namespace, the leftmost match is returned;
142  // otherwise, loop continues.
143 
144  size_t interestNameLength = interest.getName().size();
145  for (iterator right = last; right != first;) {
146  iterator prev = std::prev(right);
147 
148  // special case: [first,prev] have exact Names
149  if (prev->getName().size() == interestNameLength) {
150  NFD_LOG_TRACE(" find-among-exact " << prev->getName());
151  iterator matchExact = this->findRightmostAmongExact(interest, first, right);
152  return matchExact == right ? last : matchExact;
153  }
154 
155  Name prefix = prev->getName().getPrefix(interestNameLength + 1);
156  iterator left = m_table.lower_bound(prefix);
157 
158  // normal case: [left,right) are under one-component-longer prefix
159  NFD_LOG_TRACE(" find-under-prefix " << prefix);
160  iterator match = this->findLeftmost(interest, left, right);
161  if (match != right) {
162  return match;
163  }
164  right = left;
165  }
166  return last;
167 }
168 
169 iterator
170 Cs::findRightmostAmongExact(const Interest& interest, iterator first, iterator last) const
171 {
172  return find_last_if(first, last, bind(&EntryImpl::canSatisfy, _1, interest));
173 }
174 
175 void
176 Cs::dump()
177 {
178  NFD_LOG_DEBUG("dump table");
179  for (const EntryImpl& entry : m_table) {
180  NFD_LOG_TRACE(entry.getFullName());
181  }
182 }
183 
184 void
185 Cs::setPolicy(unique_ptr<Policy> policy)
186 {
187  BOOST_ASSERT(policy != nullptr);
188  BOOST_ASSERT(m_policy != nullptr);
189  size_t limit = m_policy->getLimit();
190  this->setPolicyImpl(std::move(policy));
191  m_policy->setLimit(limit);
192 }
193 
194 void
195 Cs::setPolicyImpl(unique_ptr<Policy> policy)
196 {
197  NFD_LOG_DEBUG("set-policy " << policy->getName());
198  m_policy = std::move(policy);
199  m_beforeEvictConnection = m_policy->beforeEvict.connect([this] (iterator it) {
200  m_table.erase(it);
201  });
202 
203  m_policy->setCs(this);
204  BOOST_ASSERT(m_policy->getCs() == this);
205 }
206 
207 void
208 Cs::enableAdmit(bool shouldAdmit)
209 {
210  if (m_shouldAdmit == shouldAdmit) {
211  return;
212  }
213  m_shouldAdmit = shouldAdmit;
214  NFD_LOG_INFO((shouldAdmit ? "Enabling" : "Disabling") << " Data admittance");
215 }
216 
217 void
218 Cs::enableServe(bool shouldServe)
219 {
220  if (m_shouldServe == shouldServe) {
221  return;
222  }
223  m_shouldServe = shouldServe;
224  NFD_LOG_INFO((shouldServe ? "Enabling" : "Disabling") << " Data serving");
225 }
226 
227 } // namespace cs
228 } // namespace nfd
shared_ptr< T > getTag() const
get a tag item
Definition: tag-host.hpp:67
const Name & getName() const
Get name.
Definition: data.hpp:121
int getChildSelector() const
Definition: interest.hpp:304
bool isUnsolicited() const
Definition: cs-entry.hpp:73
implements the ContentStore
static unique_ptr< Policy > create(const std::string &policyName)
Definition: cs-policy.cpp:45
Name getSuccessor() const
Get the successor of a name.
Definition: name.cpp:247
bool shouldAdmit() const
get CS_ENABLE_ADMIT flag
Definition: cs.hpp:131
It find_last_if(It first, It last, Pred p)
Definition: algorithm.hpp:49
represents an Interest packet
Definition: interest.hpp:42
#define NFD_LOG_DEBUG(expression)
Definition: logger.hpp:55
bool shouldServe() const
get CS_ENABLE_SERVE flag
Definition: cs.hpp:146
void enableAdmit(bool shouldAdmit)
set CS_ENABLE_ADMIT flag
Definition: cs.cpp:208
#define NFD_LOG_INFO(expression)
Definition: logger.hpp:56
an Entry in ContentStore implementation
provides a tag type for simple types
Definition: tag.hpp:58
Table::const_iterator iterator
Definition: cs-internal.hpp:41
#define NFD_LOG_TRACE(expression)
Definition: logger.hpp:54
void insert(const Data &data, bool isUnsolicited=false)
inserts a Data packet
Definition: cs.cpp:55
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:40
boost::transform_iterator< EntryFromEntryImpl, iterator, const Entry & > const_iterator
ContentStore iterator (public API)
Definition: cs.hpp:171
void setPolicy(unique_ptr< Policy > policy)
change replacement policy
Definition: cs.cpp:185
Cs(size_t nMaxPackets=10)
Definition: cs.cpp:46
unique_ptr< Policy > makeDefaultPolicy()
Definition: cs.cpp:40
CachePolicyType
indicates the cache policy applied to a Data packet
Represents an absolute name.
Definition: name.hpp:42
void find(const Interest &interest, const HitCallback &hitCallback, const MissCallback &missCallback) const
finds the best matching Data packet
Definition: cs.cpp:92
std::function< void(const Interest &, const Data &data)> HitCallback
Definition: cs.hpp:73
std::function< void(const Interest &)> MissCallback
Definition: cs.hpp:74
NFD_ASSERT_FORWARD_ITERATOR(Cs::const_iterator)
size_t size() const
Get number of components.
Definition: name.hpp:154
void updateStaleTime()
refreshes stale time relative to current time
Definition: cs-entry.cpp:48
const T & get() const
Definition: tag.hpp:86
#define NFD_LOG_INIT(name)
Definition: logger.hpp:34
Represents a Data packet.
Definition: data.hpp:35
bool canSatisfy(const Interest &interest) const
determines whether Interest can be satisified by the stored Data
Definition: cs-entry.cpp:55
void enableServe(bool shouldServe)
set CS_ENABLE_SERVE flag
Definition: cs.cpp:218
const Name & getName() const
Definition: interest.hpp:139