Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame^] | 2 | /* |
| 3 | * Copyright (c) 2013-2018 Regents of the University of California. |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 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 | |
Alexander Afanasyev | 97709c0 | 2016-08-25 19:58:30 -0700 | [diff] [blame] | 22 | #ifndef NDN_SECURITY_PIB_IDENTITY_CONTAINER_HPP |
| 23 | #define NDN_SECURITY_PIB_IDENTITY_CONTAINER_HPP |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 24 | |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 25 | #include "identity.hpp" |
| 26 | |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame^] | 27 | #include <iterator> |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 28 | #include <set> |
| 29 | #include <unordered_map> |
| 30 | |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 31 | namespace ndn { |
| 32 | namespace security { |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 33 | namespace pib { |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 34 | |
| 35 | class PibImpl; |
| 36 | |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 37 | namespace detail { |
| 38 | class IdentityImpl; |
| 39 | } // namespace detail |
| 40 | |
| 41 | /** |
| 42 | * @brief Container of identities of a Pib |
| 43 | * |
| 44 | * The container is used to search/enumerate identities of a Pib. |
| 45 | * The container can be created only by Pib. |
| 46 | */ |
| 47 | class IdentityContainer : noncopyable |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 48 | { |
| 49 | public: |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame^] | 50 | class const_iterator |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 51 | { |
| 52 | public: |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame^] | 53 | using iterator_category = std::forward_iterator_tag; |
| 54 | using value_type = const Identity; |
| 55 | using difference_type = std::ptrdiff_t; |
| 56 | using pointer = value_type*; |
| 57 | using reference = value_type&; |
| 58 | |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 59 | const_iterator(); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 60 | |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 61 | Identity |
| 62 | operator*(); |
| 63 | |
| 64 | const_iterator& |
| 65 | operator++(); |
| 66 | |
| 67 | const_iterator |
| 68 | operator++(int); |
| 69 | |
| 70 | bool |
| 71 | operator==(const const_iterator& other); |
| 72 | |
| 73 | bool |
| 74 | operator!=(const const_iterator& other); |
| 75 | |
| 76 | private: |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 77 | const_iterator(std::set<Name>::const_iterator it, const IdentityContainer& container); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 78 | |
| 79 | private: |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 80 | std::set<Name>::const_iterator m_it; |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 81 | const IdentityContainer* m_container; |
| 82 | |
| 83 | friend class IdentityContainer; |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | typedef const_iterator iterator; |
| 87 | |
| 88 | public: |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 89 | const_iterator |
| 90 | begin() const; |
| 91 | |
| 92 | const_iterator |
| 93 | end() const; |
| 94 | |
| 95 | const_iterator |
| 96 | find(const Name& keyId) const; |
| 97 | |
| 98 | size_t |
| 99 | size() const; |
| 100 | |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 101 | /** |
| 102 | * @brief Add @p identity into the container |
| 103 | */ |
| 104 | Identity |
| 105 | add(const Name& identityName); |
| 106 | |
| 107 | /** |
| 108 | * @brief Remove @p identity from the container |
| 109 | */ |
| 110 | void |
| 111 | remove(const Name& identity); |
| 112 | |
| 113 | /** |
| 114 | * @brief Get @p identity from the container |
| 115 | * @throw Pib::Error @p identity does not exist |
| 116 | */ |
| 117 | Identity |
| 118 | get(const Name& identity) const; |
| 119 | |
| 120 | /** |
| 121 | * @brief Reset state of the container |
| 122 | * |
| 123 | * This method removes all loaded identities and retrieves identity names from the PIB |
| 124 | * implementation. |
| 125 | */ |
| 126 | void |
| 127 | reset(); |
| 128 | |
| 129 | /** |
| 130 | * @brief Check if the container is consistent with the backend storage |
| 131 | * |
| 132 | * @note this method is heavyweight and should be used in debugging mode only. |
| 133 | */ |
| 134 | bool |
| 135 | isConsistent() const; |
| 136 | |
| 137 | NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
| 138 | /** |
| 139 | * @brief Create identity container |
| 140 | * @param impl The PIB backend implementation. |
| 141 | */ |
| 142 | explicit |
| 143 | IdentityContainer(shared_ptr<PibImpl> pibImpl); |
| 144 | |
| 145 | const std::set<Name>& |
| 146 | getIdentityNames() const |
| 147 | { |
| 148 | return m_identityNames; |
| 149 | } |
| 150 | |
| 151 | const std::unordered_map<Name, shared_ptr<detail::IdentityImpl>>& |
| 152 | getLoadedIdentities() const |
| 153 | { |
| 154 | return m_identities; |
| 155 | } |
| 156 | |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 157 | private: |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 158 | std::set<Name> m_identityNames; |
| 159 | /// @brief Cache of loaded detail::IdentityImpl. |
| 160 | mutable std::unordered_map<Name, shared_ptr<detail::IdentityImpl>> m_identities; |
| 161 | |
| 162 | shared_ptr<PibImpl> m_pibImpl; |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 163 | |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 164 | friend class Pib; |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 165 | }; |
| 166 | |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 167 | } // namespace pib |
| 168 | |
| 169 | using pib::IdentityContainer; |
| 170 | |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 171 | } // namespace security |
| 172 | } // namespace ndn |
| 173 | |
Alexander Afanasyev | 97709c0 | 2016-08-25 19:58:30 -0700 | [diff] [blame] | 174 | #endif // NDN_SECURITY_PIB_IDENTITY_CONTAINER_HPP |