identity-container.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 
23 #include "ndn-cxx/security/pib/impl/identity-impl.hpp"
26 #include "ndn-cxx/util/logger.hpp"
27 
28 namespace ndn {
29 namespace security {
30 namespace pib {
31 
32 NDN_LOG_INIT(ndn.security.IdentityContainer);
33 
34 NDN_CXX_ASSERT_FORWARD_ITERATOR(IdentityContainer::const_iterator);
35 
36 IdentityContainer::const_iterator::const_iterator(NameSet::const_iterator it,
37  const IdentityContainer& container) noexcept
38  : m_it(it)
39  , m_container(&container)
40 {
41 }
42 
43 Identity
45 {
46  BOOST_ASSERT(m_container != nullptr);
47  return m_container->get(*m_it);
48 }
49 
50 bool
52 {
53  bool isThisEnd = m_container == nullptr || m_it == m_container->m_identityNames.end();
54  bool isOtherEnd = other.m_container == nullptr || other.m_it == other.m_container->m_identityNames.end();
55  if (isThisEnd)
56  return isOtherEnd;
57  return !isOtherEnd && m_container->m_pib == other.m_container->m_pib && m_it == other.m_it;
58 }
59 
60 IdentityContainer::IdentityContainer(shared_ptr<PibImpl> pibImpl)
61  : m_pib(std::move(pibImpl))
62 {
63  BOOST_ASSERT(m_pib != nullptr);
64  m_identityNames = m_pib->getIdentities();
65 }
66 
68 IdentityContainer::find(const Name& identity) const
69 {
70  return {m_identityNames.find(identity), *this};
71 }
72 
74 IdentityContainer::add(const Name& identityName)
75 {
76  bool didInsert = m_identityNames.insert(identityName).second;
77  if (!didInsert) {
78  // identity already exists
79  return get(identityName);
80  }
81 
82  NDN_LOG_DEBUG("Adding " << identityName);
83  m_pib->addIdentity(identityName);
84  auto ret = m_identities.emplace(identityName,
85  std::make_shared<detail::IdentityImpl>(identityName, m_pib));
86  // consistency check
87  BOOST_ASSERT(ret.second);
88 
89  return Identity(ret.first->second);
90 }
91 
92 void
93 IdentityContainer::remove(const Name& identityName)
94 {
95  if (m_identityNames.erase(identityName) > 0) {
96  NDN_LOG_DEBUG("Removing " << identityName);
97  m_identities.erase(identityName);
98  }
99  else {
100  // consistency check
101  BOOST_ASSERT(m_identities.find(identityName) == m_identities.end());
102  }
103  m_pib->removeIdentity(identityName);
104 }
105 
106 Identity
107 IdentityContainer::get(const Name& identityName) const
108 {
109  auto it = m_identities.find(identityName);
110  if (it != m_identities.end()) {
111  return Identity(it->second);
112  }
113 
114  // check that the identity exists in the backend
115  if (!m_pib->hasIdentity(identityName)) {
116  NDN_THROW(Pib::Error("Identity `" + identityName.toUri() + "` does not exist"));
117  }
118 
119  auto id = std::make_shared<detail::IdentityImpl>(identityName, m_pib);
120  m_identities[identityName] = id;
121  return Identity(id);
122 }
123 
124 void
126 {
127  NDN_LOG_DEBUG("Reloading");
128  m_identities.clear();
129  m_identityNames = m_pib->getIdentities();
130 }
131 
132 bool
134 {
135  return m_identityNames == m_pib->getIdentities();
136 }
137 
138 } // namespace pib
139 } // namespace security
140 } // namespace ndn
Represents an absolute name.
Definition: name.hpp:44
void toUri(std::ostream &os, name::UriFormat format=name::UriFormat::DEFAULT) const
Write URI representation of the name to the output stream.
Definition: name.cpp:349
bool operator==(const const_iterator &other) const
Identity get(const Name &identity) const
Return an identity by name.
const_iterator find(const Name &identity) const
void remove(const Name &identity)
Remove identity from the container.
Identity add(const Name &identity)
Add identity into the container.
bool isConsistent() const
Check if the container is consistent with the backend storage.
void reset()
Reset the state of the container.
Frontend handle for an identity in the PIB.
Definition: identity.hpp:50
Represents a semantic error.
Definition: pib.hpp:57
#define NDN_CXX_ASSERT_FORWARD_ITERATOR(T)
Assert T is a forward iterator.
Definition: concepts.hpp:147
#define NDN_THROW(e)
Definition: exception.hpp:61
#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