Yingdi Yu | 77627ab | 2015-07-21 16:13:49 -0700 | [diff] [blame^] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2013-2014 Regents of the University of California. |
| 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 6 | * |
| 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 20 | */ |
| 21 | |
| 22 | #ifndef NDN_PIB_KEY_CACHE_HPP |
| 23 | #define NDN_PIB_KEY_CACHE_HPP |
| 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 | |
| 108 | #endif // NDN_PIB_KEY_CACHE_HPP |