pib.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2022 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 
24 #include "ndn-cxx/util/logger.hpp"
25 
26 namespace ndn {
27 namespace security {
28 namespace pib {
29 
31 
32 Pib::Pib(const std::string& locator, shared_ptr<PibImpl> impl)
33  : m_locator(locator)
34  , m_impl(std::move(impl))
35  , m_identities(m_impl)
36 {
37  BOOST_ASSERT(m_impl != nullptr);
38 }
39 
40 Pib::~Pib() = default;
41 
42 std::string
43 Pib::getTpmLocator() const
44 {
45  return m_impl->getTpmLocator();
46 }
47 
48 void
49 Pib::setTpmLocator(const std::string& tpmLocator)
50 {
51  if (tpmLocator == m_impl->getTpmLocator()) {
52  return;
53  }
54 
55  NDN_LOG_DEBUG("Resetting TPM locator to " << tpmLocator);
56  reset();
57  m_impl->setTpmLocator(tpmLocator);
58 }
59 
60 void
61 Pib::reset()
62 {
63  m_impl->setTpmLocator("");
64  m_impl->clearIdentities();
65  m_defaultIdentity = {};
66  m_identities.reset();
67 }
68 
70 Pib::addIdentity(const Name& identityName)
71 {
72  BOOST_ASSERT(m_identities.isConsistent());
73  return m_identities.add(identityName);
74 }
75 
76 void
77 Pib::removeIdentity(const Name& identityName)
78 {
79  BOOST_ASSERT(m_identities.isConsistent());
80 
81  if (m_defaultIdentity && m_defaultIdentity.getName() == identityName) {
82  NDN_LOG_DEBUG("Removing default identity " << identityName);
83  m_defaultIdentity = {};
84  }
85  m_identities.remove(identityName);
86 }
87 
88 Identity
89 Pib::getIdentity(const Name& identity) const
90 {
91  BOOST_ASSERT(m_identities.isConsistent());
92  return m_identities.get(identity);
93 }
94 
95 const IdentityContainer&
96 Pib::getIdentities() const
97 {
98  BOOST_ASSERT(m_identities.isConsistent());
99  return m_identities;
100 }
101 
102 Identity
103 Pib::setDefaultIdentity(const Name& identityName)
104 {
105  BOOST_ASSERT(m_identities.isConsistent());
106 
107  m_defaultIdentity = m_identities.add(identityName);
108  m_impl->setDefaultIdentity(identityName);
109  NDN_LOG_DEBUG("Default identity set to " << identityName);
110 
111  BOOST_ASSERT(m_defaultIdentity);
112  return m_defaultIdentity;
113 }
114 
115 Identity
116 Pib::getDefaultIdentity() const
117 {
118  BOOST_ASSERT(m_identities.isConsistent());
119 
120  if (!m_defaultIdentity) {
121  m_defaultIdentity = m_identities.get(m_impl->getDefaultIdentity());
122  NDN_LOG_DEBUG("Caching default identity " << m_defaultIdentity.getName());
123  }
124 
125  BOOST_ASSERT(m_defaultIdentity);
126  BOOST_ASSERT(m_defaultIdentity.getName() == m_impl->getDefaultIdentity());
127  return m_defaultIdentity;
128 }
129 
130 } // namespace pib
131 } // namespace security
132 } // namespace ndn
Represents an absolute name.
Definition: name.hpp:44
Container of identities of a PIB.
Frontend handle for an identity in the PIB.
Definition: identity.hpp:50
#define NDN_LOG_DEBUG(expression)
Log at DEBUG level.
Definition: logger.hpp:254
#define NDN_LOG_INIT(name)
Define a non-member log module.
Definition: logger.hpp:163
Definition: data.cpp:25