blob: bcc4d8d58251752b53e6f7ffd8cdb91edbb82bc7 [file] [log] [blame]
Yingdi Yu77627ab2015-07-21 16:13:49 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Yingdi Yu0a312e52015-07-22 13:14:53 -07003 * Copyright (c) 2014-2015, Regents of the University of California.
Yingdi Yu77627ab2015-07-21 16:13:49 -07004 *
Yingdi Yu0a312e52015-07-22 13:14:53 -07005 * 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 Yu77627ab2015-07-21 16:13:49 -07007 *
Yingdi Yu0a312e52015-07-22 13:14:53 -07008 * 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 Yu77627ab2015-07-21 16:13:49 -070011 *
Yingdi Yu0a312e52015-07-22 13:14:53 -070012 * 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 Yu77627ab2015-07-21 16:13:49 -070015 *
Yingdi Yu0a312e52015-07-22 13:14:53 -070016 * 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 Yu77627ab2015-07-21 16:13:49 -070018 *
Yingdi Yu0a312e52015-07-22 13:14:53 -070019 * @author Yingdi Yu <yingdi@cs.ucla.edu>
Yingdi Yu77627ab2015-07-21 16:13:49 -070020 */
21
Yingdi Yu0a312e52015-07-22 13:14:53 -070022#ifndef NDN_TOOLS_PIB_KEY_CACHE_HPP
23#define NDN_TOOLS_PIB_KEY_CACHE_HPP
Yingdi Yu77627ab2015-07-21 16:13:49 -070024
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
37namespace ndn {
38namespace pib {
39
40struct KeyCacheEntry
41{
42 KeyCacheEntry(const Name& name, shared_ptr<PublicKey> key);
43
44 Name name;
45 shared_ptr<PublicKey> key;
46};
47
48class byName;
49class byUsedTime;
50
51typedef 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
66class KeyCache : noncopyable
67{
68public:
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
84private:
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
100private:
101 size_t m_capacity;
102 mutable KeyContainer m_keys;
103};
104
105} // namespace pib
106} // namespace ndn
107
Yingdi Yu0a312e52015-07-22 13:14:53 -0700108#endif // NDN_TOOLS_PIB_KEY_CACHE_HPP