blob: 6b24eb35106fc52441aecb52ec497a2b8d6b1952 [file] [log] [blame]
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventoa3148082018-04-12 18:21:54 -04002/*
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -04003 * Copyright (c) 2014-2022, Regents of the University of California,
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -05004 * 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-policy.hpp"
27#include "cs.hpp"
Davide Pesavento2cae8ca2019-04-18 20:48:05 -040028#include "common/logger.hpp"
Davide Pesaventoa3148082018-04-12 18:21:54 -040029
Junxiao Shi7ee00fb2016-12-04 21:23:00 +000030#include <boost/range/adaptor/map.hpp>
31#include <boost/range/algorithm/copy.hpp>
Junxiao Shi2af9ca32016-08-25 21:51:02 +000032
Davide Pesaventoa3148082018-04-12 18:21:54 -040033NFD_LOG_INIT(CsPolicy);
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050034
35namespace nfd {
36namespace cs {
37
Junxiao Shi52555b02016-11-25 21:39:06 +000038Policy::Registry&
39Policy::getRegistry()
40{
41 static Registry registry;
42 return registry;
43}
44
45unique_ptr<Policy>
Junxiao Shi7ee00fb2016-12-04 21:23:00 +000046Policy::create(const std::string& policyName)
Junxiao Shi52555b02016-11-25 21:39:06 +000047{
48 Registry& registry = getRegistry();
Junxiao Shi7ee00fb2016-12-04 21:23:00 +000049 auto i = registry.find(policyName);
Junxiao Shi52555b02016-11-25 21:39:06 +000050 return i == registry.end() ? nullptr : i->second();
51}
52
Junxiao Shi7ee00fb2016-12-04 21:23:00 +000053std::set<std::string>
54Policy::getPolicyNames()
55{
56 std::set<std::string> policyNames;
57 boost::copy(getRegistry() | boost::adaptors::map_keys,
58 std::inserter(policyNames, policyNames.end()));
59 return policyNames;
60}
61
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040062Policy::Policy(std::string_view policyName)
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050063 : m_policyName(policyName)
64{
65}
66
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050067void
68Policy::setLimit(size_t nMaxEntries)
69{
Junxiao Shi2af9ca32016-08-25 21:51:02 +000070 NFD_LOG_INFO("setLimit " << nMaxEntries);
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050071 m_limit = nMaxEntries;
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050072 this->evictEntries();
73}
74
75void
Junxiao Shi07f2e2f2019-07-22 09:10:06 -060076Policy::afterInsert(EntryRef i)
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050077{
78 BOOST_ASSERT(m_cs != nullptr);
79 this->doAfterInsert(i);
80}
81
82void
Junxiao Shi07f2e2f2019-07-22 09:10:06 -060083Policy::afterRefresh(EntryRef i)
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050084{
85 BOOST_ASSERT(m_cs != nullptr);
86 this->doAfterRefresh(i);
87}
88
89void
Junxiao Shi07f2e2f2019-07-22 09:10:06 -060090Policy::beforeErase(EntryRef i)
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050091{
92 BOOST_ASSERT(m_cs != nullptr);
93 this->doBeforeErase(i);
94}
95
96void
Junxiao Shi07f2e2f2019-07-22 09:10:06 -060097Policy::beforeUse(EntryRef i)
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050098{
99 BOOST_ASSERT(m_cs != nullptr);
100 this->doBeforeUse(i);
101}
102
103} // namespace cs
104} // namespace nfd