30 #include <ndn-cxx/lp/tags.hpp> 31 #include <ndn-cxx/util/concepts.hpp> 38 static unique_ptr<Policy>
47 m_policy->setLimit(nMaxPackets);
53 if (!m_shouldAdmit || m_policy->getLimit() == 0) {
59 shared_ptr<lp::CachePolicyTag> tag = data.getTag<lp::CachePolicyTag>();
61 lp::CachePolicyType policy = tag->get().getPolicy();
62 if (policy == lp::CachePolicyType::NO_CACHE) {
68 bool isNewEntry =
false;
69 std::tie(it, isNewEntry) = m_table.emplace(data.shared_from_this(), isUnsolicited);
80 m_policy->afterRefresh(it);
83 m_policy->afterInsert(it);
87 std::pair<Cs::const_iterator, Cs::const_iterator>
88 Cs::findPrefixRange(
const Name& prefix)
const 90 auto first = m_table.lower_bound(prefix);
91 auto last = m_table.end();
92 if (prefix.size() > 0) {
93 last = m_table.lower_bound(prefix.getSuccessor());
99 Cs::eraseImpl(
const Name& prefix,
size_t limit)
102 std::tie(i, last) = findPrefixRange(prefix);
105 while (i != last && nErased < limit) {
106 m_policy->beforeErase(i);
107 i = m_table.erase(i);
114 Cs::findImpl(
const Interest& interest)
const 116 if (!m_shouldServe || m_policy->getLimit() == 0) {
117 return m_table.end();
120 const Name& prefix = interest.getName();
121 auto range = findPrefixRange(prefix);
122 auto match = std::find_if(range.first, range.second,
123 [&interest] (
const auto& entry) { return entry.canSatisfy(interest); });
125 if (match == range.second) {
127 return m_table.end();
129 NFD_LOG_DEBUG(
"find " << prefix <<
" matching " << match->getName());
130 m_policy->beforeUse(match);
138 for (
const Entry& entry : m_table) {
146 BOOST_ASSERT(policy !=
nullptr);
147 BOOST_ASSERT(m_policy !=
nullptr);
148 size_t limit = m_policy->getLimit();
149 this->setPolicyImpl(std::move(policy));
150 m_policy->setLimit(limit);
154 Cs::setPolicyImpl(unique_ptr<Policy> policy)
157 m_policy = std::move(policy);
158 m_beforeEvictConnection = m_policy->beforeEvict.connect([
this] (
auto it) { m_table.erase(it); });
160 m_policy->setCs(
this);
161 BOOST_ASSERT(m_policy->getCs() ==
this);
167 if (m_shouldAdmit == shouldAdmit) {
171 NFD_LOG_INFO((shouldAdmit ?
"Enabling" :
"Disabling") <<
" Data admittance");
177 if (m_shouldServe == shouldServe) {
181 NFD_LOG_INFO((shouldServe ?
"Enabling" :
"Disabling") <<
" Data serving");
Table::const_iterator const_iterator
static unique_ptr< Policy > makeDefaultPolicy()
bool isUnsolicited() const
return whether the stored Data is unsolicited
void updateFreshUntil()
recalculate when the entry would become non-fresh, relative to current time
void enableAdmit(bool shouldAdmit)
set CS_ENABLE_ADMIT flag
static unique_ptr< Policy > create(const std::string &policyName)
bool shouldServe() const
get CS_ENABLE_SERVE flag
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
void insert(const Data &data, bool isUnsolicited=false)
inserts a Data packet
bool shouldAdmit() const
get CS_ENABLE_ADMIT flag
void enableServe(bool shouldServe)
set CS_ENABLE_SERVE flag
void setPolicy(unique_ptr< Policy > policy)
change replacement policy
void clearUnsolicited()
clear 'unsolicited' flag
#define NFD_LOG_INIT(name)
Cs(size_t nMaxPackets=10)