blob: df366c9862599f18821f52e910f423e39e5c688a [file] [log] [blame]
Yingdi Yub8f8b342015-04-27 11:06:42 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2015 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#include "pib.hpp"
23#include "pib-impl.hpp"
24
25namespace ndn {
26namespace security {
27
28Pib::Pib(const std::string scheme, const std::string& location, shared_ptr<PibImpl> impl)
29 : m_scheme(scheme)
30 , m_location(location)
Yingdi Yuc8209892015-06-19 17:47:56 -070031 , m_hasDefaultIdentity(false)
32 , m_needRefreshIdentities(true)
Yingdi Yub8f8b342015-04-27 11:06:42 -070033 , m_impl(impl)
34{
35}
36
37Pib::~Pib()
38{
39}
40
41std::string
42Pib::getPibLocator() const
43{
44 return m_scheme + ":" + m_location;
45}
46
47void
48Pib::setTpmLocator(const std::string& tpmLocator)
49{
50 m_impl->setTpmLocator(tpmLocator);
51}
52
53std::string
54Pib::getTpmLocator() const
55{
56 return m_impl->getTpmLocator();
57}
58
59Identity
60Pib::addIdentity(const Name& identity)
61{
Yingdi Yuc8209892015-06-19 17:47:56 -070062 if (!m_needRefreshIdentities && m_identities.find(identity) == m_identities.end()) {
63 // if we have already loaded all the identities, but the new identity is not one of them
64 // the IdentityContainer should be refreshed
65 m_needRefreshIdentities = true;
66 }
Yingdi Yub8f8b342015-04-27 11:06:42 -070067 return Identity(identity, m_impl, true);
68}
69
70void
71Pib::removeIdentity(const Name& identity)
72{
Yingdi Yuc8209892015-06-19 17:47:56 -070073 if (m_hasDefaultIdentity && m_defaultIdentity.getName() == identity)
74 m_hasDefaultIdentity = false;
75
Yingdi Yub8f8b342015-04-27 11:06:42 -070076 m_impl->removeIdentity(identity);
Yingdi Yuc8209892015-06-19 17:47:56 -070077 m_needRefreshIdentities = true;
Yingdi Yub8f8b342015-04-27 11:06:42 -070078}
79
80Identity
Yingdi Yuc8209892015-06-19 17:47:56 -070081Pib::getIdentity(const Name& identity) const
Yingdi Yub8f8b342015-04-27 11:06:42 -070082{
83 return Identity(identity, m_impl, false);
84}
85
Yingdi Yuc8209892015-06-19 17:47:56 -070086const IdentityContainer&
Yingdi Yub8f8b342015-04-27 11:06:42 -070087Pib::getIdentities() const
88{
Yingdi Yuc8209892015-06-19 17:47:56 -070089 if (m_needRefreshIdentities) {
Alexander Afanasyev66ca2032015-12-04 13:17:02 -080090 m_identities = IdentityContainer(m_impl->getIdentities(), m_impl);
Yingdi Yuc8209892015-06-19 17:47:56 -070091 m_needRefreshIdentities = false;
92 }
93
94 return m_identities;
Yingdi Yub8f8b342015-04-27 11:06:42 -070095}
96
Yingdi Yuc8209892015-06-19 17:47:56 -070097Identity&
Yingdi Yub8f8b342015-04-27 11:06:42 -070098Pib::setDefaultIdentity(const Name& identityName)
99{
Yingdi Yuc8209892015-06-19 17:47:56 -0700100 m_defaultIdentity = addIdentity(identityName);
101 m_hasDefaultIdentity = true;
102
Yingdi Yub8f8b342015-04-27 11:06:42 -0700103 m_impl->setDefaultIdentity(identityName);
Yingdi Yuc8209892015-06-19 17:47:56 -0700104 return m_defaultIdentity;
Yingdi Yub8f8b342015-04-27 11:06:42 -0700105}
106
Yingdi Yuc8209892015-06-19 17:47:56 -0700107Identity&
108Pib::getDefaultIdentity() const
Yingdi Yub8f8b342015-04-27 11:06:42 -0700109{
Yingdi Yuc8209892015-06-19 17:47:56 -0700110 if (!m_hasDefaultIdentity) {
Alexander Afanasyev66ca2032015-12-04 13:17:02 -0800111 m_defaultIdentity = Identity(m_impl->getDefaultIdentity(), m_impl, false);
Yingdi Yuc8209892015-06-19 17:47:56 -0700112 m_hasDefaultIdentity = true;
113 }
114
115 return m_defaultIdentity;
Yingdi Yub8f8b342015-04-27 11:06:42 -0700116}
117
118
119} // namespace security
120} // namespace ndn