Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2022, Regents of the University of California, |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 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-policy.hpp" |
| 27 | #include "cs.hpp" |
Davide Pesavento | 2cae8ca | 2019-04-18 20:48:05 -0400 | [diff] [blame] | 28 | #include "common/logger.hpp" |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 29 | |
Junxiao Shi | 7ee00fb | 2016-12-04 21:23:00 +0000 | [diff] [blame] | 30 | #include <boost/range/adaptor/map.hpp> |
| 31 | #include <boost/range/algorithm/copy.hpp> |
Junxiao Shi | 2af9ca3 | 2016-08-25 21:51:02 +0000 | [diff] [blame] | 32 | |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 33 | NFD_LOG_INIT(CsPolicy); |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 34 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame^] | 35 | namespace nfd::cs { |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 36 | |
Junxiao Shi | 52555b0 | 2016-11-25 21:39:06 +0000 | [diff] [blame] | 37 | Policy::Registry& |
| 38 | Policy::getRegistry() |
| 39 | { |
| 40 | static Registry registry; |
| 41 | return registry; |
| 42 | } |
| 43 | |
| 44 | unique_ptr<Policy> |
Junxiao Shi | 7ee00fb | 2016-12-04 21:23:00 +0000 | [diff] [blame] | 45 | Policy::create(const std::string& policyName) |
Junxiao Shi | 52555b0 | 2016-11-25 21:39:06 +0000 | [diff] [blame] | 46 | { |
| 47 | Registry& registry = getRegistry(); |
Junxiao Shi | 7ee00fb | 2016-12-04 21:23:00 +0000 | [diff] [blame] | 48 | auto i = registry.find(policyName); |
Junxiao Shi | 52555b0 | 2016-11-25 21:39:06 +0000 | [diff] [blame] | 49 | return i == registry.end() ? nullptr : i->second(); |
| 50 | } |
| 51 | |
Junxiao Shi | 7ee00fb | 2016-12-04 21:23:00 +0000 | [diff] [blame] | 52 | std::set<std::string> |
| 53 | Policy::getPolicyNames() |
| 54 | { |
| 55 | std::set<std::string> policyNames; |
| 56 | boost::copy(getRegistry() | boost::adaptors::map_keys, |
| 57 | std::inserter(policyNames, policyNames.end())); |
| 58 | return policyNames; |
| 59 | } |
| 60 | |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 61 | Policy::Policy(std::string_view policyName) |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 62 | : m_policyName(policyName) |
| 63 | { |
| 64 | } |
| 65 | |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 66 | void |
| 67 | Policy::setLimit(size_t nMaxEntries) |
| 68 | { |
Junxiao Shi | 2af9ca3 | 2016-08-25 21:51:02 +0000 | [diff] [blame] | 69 | NFD_LOG_INFO("setLimit " << nMaxEntries); |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 70 | m_limit = nMaxEntries; |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 71 | this->evictEntries(); |
| 72 | } |
| 73 | |
| 74 | void |
Junxiao Shi | 07f2e2f | 2019-07-22 09:10:06 -0600 | [diff] [blame] | 75 | Policy::afterInsert(EntryRef i) |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 76 | { |
| 77 | BOOST_ASSERT(m_cs != nullptr); |
| 78 | this->doAfterInsert(i); |
| 79 | } |
| 80 | |
| 81 | void |
Junxiao Shi | 07f2e2f | 2019-07-22 09:10:06 -0600 | [diff] [blame] | 82 | Policy::afterRefresh(EntryRef i) |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 83 | { |
| 84 | BOOST_ASSERT(m_cs != nullptr); |
| 85 | this->doAfterRefresh(i); |
| 86 | } |
| 87 | |
| 88 | void |
Junxiao Shi | 07f2e2f | 2019-07-22 09:10:06 -0600 | [diff] [blame] | 89 | Policy::beforeErase(EntryRef i) |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 90 | { |
| 91 | BOOST_ASSERT(m_cs != nullptr); |
| 92 | this->doBeforeErase(i); |
| 93 | } |
| 94 | |
| 95 | void |
Junxiao Shi | 07f2e2f | 2019-07-22 09:10:06 -0600 | [diff] [blame] | 96 | Policy::beforeUse(EntryRef i) |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 97 | { |
| 98 | BOOST_ASSERT(m_cs != nullptr); |
| 99 | this->doBeforeUse(i); |
| 100 | } |
| 101 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame^] | 102 | } // namespace nfd::cs |