Yingdi Yu | 77627ab | 2015-07-21 16:13:49 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Yingdi Yu | 0a312e5 | 2015-07-22 13:14:53 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014-2015, Regents of the University of California. |
Yingdi Yu | 77627ab | 2015-07-21 16:13:49 -0700 | [diff] [blame] | 4 | * |
Yingdi Yu | 0a312e5 | 2015-07-22 13:14:53 -0700 | [diff] [blame] | 5 | * This file is part of ndn-tools (Named Data Networking Essential Tools). |
| 6 | * See AUTHORS.md for complete list of ndn-tools authors and contributors. |
Yingdi Yu | 77627ab | 2015-07-21 16:13:49 -0700 | [diff] [blame] | 7 | * |
Yingdi Yu | 0a312e5 | 2015-07-22 13:14:53 -0700 | [diff] [blame] | 8 | * ndn-tools is free software: you can redistribute it and/or modify it under the terms |
| 9 | * of the GNU General Public License as published by the Free Software Foundation, |
| 10 | * either version 3 of the License, or (at your option) any later version. |
Yingdi Yu | 77627ab | 2015-07-21 16:13:49 -0700 | [diff] [blame] | 11 | * |
Yingdi Yu | 0a312e5 | 2015-07-22 13:14:53 -0700 | [diff] [blame] | 12 | * ndn-tools is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 13 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 | * PURPOSE. See the GNU General Public License for more details. |
Yingdi Yu | 77627ab | 2015-07-21 16:13:49 -0700 | [diff] [blame] | 15 | * |
Yingdi Yu | 0a312e5 | 2015-07-22 13:14:53 -0700 | [diff] [blame] | 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * ndn-tools, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
Yingdi Yu | 77627ab | 2015-07-21 16:13:49 -0700 | [diff] [blame] | 18 | * |
Yingdi Yu | 0a312e5 | 2015-07-22 13:14:53 -0700 | [diff] [blame] | 19 | * @author Yingdi Yu <yingdi@cs.ucla.edu> |
Yingdi Yu | 77627ab | 2015-07-21 16:13:49 -0700 | [diff] [blame] | 20 | */ |
| 21 | |
Yingdi Yu | 0a312e5 | 2015-07-22 13:14:53 -0700 | [diff] [blame] | 22 | #ifndef NDN_TOOLS_PIB_KEY_CACHE_HPP |
| 23 | #define NDN_TOOLS_PIB_KEY_CACHE_HPP |
Yingdi Yu | 77627ab | 2015-07-21 16:13:49 -0700 | [diff] [blame] | 24 | |
| 25 | #include <ndn-cxx/name.hpp> |
| 26 | #include <ndn-cxx/util/time.hpp> |
| 27 | #include <ndn-cxx/security/public-key.hpp> |
| 28 | |
| 29 | #include <stack> |
| 30 | |
| 31 | #include <boost/multi_index_container.hpp> |
| 32 | #include <boost/multi_index/member.hpp> |
| 33 | #include <boost/multi_index/hashed_index.hpp> |
| 34 | #include <boost/multi_index/sequenced_index.hpp> |
| 35 | #include <boost/multi_index/key_extractors.hpp> |
| 36 | |
| 37 | namespace ndn { |
| 38 | namespace pib { |
| 39 | |
| 40 | struct KeyCacheEntry |
| 41 | { |
| 42 | KeyCacheEntry(const Name& name, shared_ptr<PublicKey> key); |
| 43 | |
| 44 | Name name; |
| 45 | shared_ptr<PublicKey> key; |
| 46 | }; |
| 47 | |
| 48 | class byName; |
| 49 | class byUsedTime; |
| 50 | |
| 51 | typedef boost::multi_index::multi_index_container< |
| 52 | KeyCacheEntry, |
| 53 | boost::multi_index::indexed_by< |
| 54 | boost::multi_index::hashed_unique< |
| 55 | boost::multi_index::tag<byName>, |
| 56 | boost::multi_index::member<KeyCacheEntry, Name, &KeyCacheEntry::name>, |
| 57 | std::hash<Name> |
| 58 | >, |
| 59 | |
| 60 | boost::multi_index::sequenced< |
| 61 | boost::multi_index::tag<byUsedTime> |
| 62 | > |
| 63 | > |
| 64 | > KeyContainer; |
| 65 | |
| 66 | class KeyCache : noncopyable |
| 67 | { |
| 68 | public: |
| 69 | explicit |
| 70 | KeyCache(size_t capacity = getDefaultCapacity()); |
| 71 | |
| 72 | void |
| 73 | insert(const Name& name, shared_ptr<PublicKey> key); |
| 74 | |
| 75 | shared_ptr<PublicKey> |
| 76 | find(const Name& name) const; |
| 77 | |
| 78 | void |
| 79 | erase(const Name& name); |
| 80 | |
| 81 | size_t |
| 82 | size() const; |
| 83 | |
| 84 | private: |
| 85 | static size_t |
| 86 | getDefaultCapacity() |
| 87 | { |
| 88 | return 100; |
| 89 | } |
| 90 | |
| 91 | void |
| 92 | evictKey(); |
| 93 | |
| 94 | void |
| 95 | adjustLru(KeyContainer::iterator it) const; |
| 96 | |
| 97 | void |
| 98 | freeEntry(KeyContainer::iterator it); |
| 99 | |
| 100 | private: |
| 101 | size_t m_capacity; |
| 102 | mutable KeyContainer m_keys; |
| 103 | }; |
| 104 | |
| 105 | } // namespace pib |
| 106 | } // namespace ndn |
| 107 | |
Yingdi Yu | 0a312e5 | 2015-07-22 13:14:53 -0700 | [diff] [blame] | 108 | #endif // NDN_TOOLS_PIB_KEY_CACHE_HPP |