cs.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2022, 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 #ifndef NFD_DAEMON_TABLE_CS_HPP
27 #define NFD_DAEMON_TABLE_CS_HPP
28 
29 #include "cs-policy.hpp"
30 
31 namespace nfd {
32 namespace cs {
33 
44 class Cs : noncopyable
45 {
46 public:
47  explicit
48  Cs(size_t nMaxPackets = 10);
49 
52  void
53  insert(const Data& data, bool isUnsolicited = false);
54 
62  template<typename AfterEraseCallback>
63  void
64  erase(const Name& prefix, size_t limit, AfterEraseCallback&& cb)
65  {
66  size_t nErased = eraseImpl(prefix, limit);
67  cb(nErased);
68  }
69 
79  template<typename HitCallback, typename MissCallback>
80  void
81  find(const Interest& interest, HitCallback&& hit, MissCallback&& miss) const
82  {
83  auto match = findImpl(interest);
84  if (match == m_table.end()) {
85  miss(interest);
86  return;
87  }
88  hit(interest, match->getData());
89  }
90 
93  size_t
94  size() const
95  {
96  return m_table.size();
97  }
98 
99 public: // configuration
102  size_t
103  getLimit() const noexcept
104  {
105  return m_policy->getLimit();
106  }
107 
110  void
111  setLimit(size_t nMaxPackets)
112  {
113  return m_policy->setLimit(nMaxPackets);
114  }
115 
118  Policy*
119  getPolicy() const noexcept
120  {
121  return m_policy.get();
122  }
123 
127  void
128  setPolicy(unique_ptr<Policy> policy);
129 
133  bool
134  shouldAdmit() const noexcept
135  {
136  return m_shouldAdmit;
137  }
138 
142  void
143  enableAdmit(bool shouldAdmit) noexcept;
144 
148  bool
149  shouldServe() const noexcept
150  {
151  return m_shouldServe;
152  }
153 
157  void
158  enableServe(bool shouldServe) noexcept;
159 
160 public: // enumeration
161  using const_iterator = Table::const_iterator;
162 
164  begin() const
165  {
166  return m_table.begin();
167  }
168 
170  end() const
171  {
172  return m_table.end();
173  }
174 
175 private:
176  std::pair<const_iterator, const_iterator>
177  findPrefixRange(const Name& prefix) const;
178 
179  size_t
180  eraseImpl(const Name& prefix, size_t limit);
181 
183  findImpl(const Interest& interest) const;
184 
185  void
186  setPolicyImpl(unique_ptr<Policy> policy);
187 
189  void
190  dump();
191 
192 private:
193  Table m_table;
194  unique_ptr<Policy> m_policy;
195  signal::ScopedConnection m_beforeEvictConnection;
196 
197  bool m_shouldAdmit = true;
198  bool m_shouldServe = true;
199 };
200 
201 } // namespace cs
202 
203 using cs::Cs;
204 
205 } // namespace nfd
206 
207 #endif // NFD_DAEMON_TABLE_CS_HPP
Implements the Content Store.
Definition: cs.hpp:45
bool shouldServe() const noexcept
Get CS_ENABLE_SERVE flag.
Definition: cs.hpp:149
Table::const_iterator const_iterator
Definition: cs.hpp:161
void enableAdmit(bool shouldAdmit) noexcept
Set CS_ENABLE_ADMIT flag.
Definition: cs.cpp:160
size_t size() const
Get number of stored packets.
Definition: cs.hpp:94
bool shouldAdmit() const noexcept
Get CS_ENABLE_ADMIT flag.
Definition: cs.hpp:134
void erase(const Name &prefix, size_t limit, AfterEraseCallback &&cb)
Asynchronously erases entries under prefix.
Definition: cs.hpp:64
void setPolicy(unique_ptr< Policy > policy)
Change replacement policy.
Definition: cs.cpp:139
Cs(size_t nMaxPackets=10)
Definition: cs.cpp:42
void insert(const Data &data, bool isUnsolicited=false)
Inserts a Data packet.
Definition: cs.cpp:49
void find(const Interest &interest, HitCallback &&hit, MissCallback &&miss) const
Finds the best matching Data packet.
Definition: cs.hpp:81
void setLimit(size_t nMaxPackets)
Change capacity (in number of packets).
Definition: cs.hpp:111
void enableServe(bool shouldServe) noexcept
Set CS_ENABLE_SERVE flag.
Definition: cs.cpp:170
Policy * getPolicy() const noexcept
Get replacement policy.
Definition: cs.hpp:119
const_iterator end() const
Definition: cs.hpp:170
size_t getLimit() const noexcept
Get capacity (in number of packets).
Definition: cs.hpp:103
const_iterator begin() const
Definition: cs.hpp:164
Represents a CS replacement policy.
Definition: cs-policy.hpp:43
#define NFD_PUBLIC_WITH_TESTS_ELSE_PRIVATE
Definition: common.hpp:41
std::set< Entry, std::less<> > Table
An ordered container of ContentStore entries.
Definition: cs-entry.hpp:117
-status-http-server
Definition: common.hpp:71