blob: 3239ac3f9cdc2dad40e55c42591cb5070c09dd8e [file] [log] [blame]
Yingdi Yub8f8b342015-04-27 11:06:42 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi5759be32017-10-15 00:00:52 +00002/*
Yingdi Yu6ee2d362015-07-16 21:48:05 -07003 * Copyright (c) 2013-2017 Regents of the University of California.
Yingdi Yub8f8b342015-04-27 11:06:42 -07004 *
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 "identity.hpp"
Yingdi Yucbe72b02015-11-25 17:35:37 -080023#include "detail/identity-impl.hpp"
Yingdi Yub8f8b342015-04-27 11:06:42 -070024
25namespace ndn {
26namespace security {
Yingdi Yu6ee2d362015-07-16 21:48:05 -070027namespace pib {
Yingdi Yub8f8b342015-04-27 11:06:42 -070028
Yingdi Yucbe72b02015-11-25 17:35:37 -080029Identity::Identity() = default;
Yingdi Yub8f8b342015-04-27 11:06:42 -070030
Yingdi Yucbe72b02015-11-25 17:35:37 -080031Identity::Identity(weak_ptr<detail::IdentityImpl> impl)
32 : m_impl(impl)
Yingdi Yub8f8b342015-04-27 11:06:42 -070033{
Yingdi Yub8f8b342015-04-27 11:06:42 -070034}
35
36const Name&
37Identity::getName() const
38{
Yingdi Yucbe72b02015-11-25 17:35:37 -080039 return lock()->getName();
Yingdi Yub8f8b342015-04-27 11:06:42 -070040}
41
42Key
Yingdi Yufe4733a2015-10-22 14:24:12 -070043Identity::addKey(const uint8_t* key, size_t keyLen, const Name& keyName) const
Yingdi Yub8f8b342015-04-27 11:06:42 -070044{
Yingdi Yucbe72b02015-11-25 17:35:37 -080045 return lock()->addKey(key, keyLen, keyName);
Yingdi Yub8f8b342015-04-27 11:06:42 -070046}
47
48void
Yingdi Yufe4733a2015-10-22 14:24:12 -070049Identity::removeKey(const Name& keyName) const
Yingdi Yub8f8b342015-04-27 11:06:42 -070050{
Yingdi Yucbe72b02015-11-25 17:35:37 -080051 return lock()->removeKey(keyName);
Yingdi Yub8f8b342015-04-27 11:06:42 -070052}
53
54Key
Yingdi Yu6ee2d362015-07-16 21:48:05 -070055Identity::getKey(const Name& keyName) const
Yingdi Yub8f8b342015-04-27 11:06:42 -070056{
Yingdi Yucbe72b02015-11-25 17:35:37 -080057 return lock()->getKey(keyName);
Yingdi Yub8f8b342015-04-27 11:06:42 -070058}
59
60const KeyContainer&
Yingdi Yuc8209892015-06-19 17:47:56 -070061Identity::getKeys() const
Yingdi Yub8f8b342015-04-27 11:06:42 -070062{
Yingdi Yucbe72b02015-11-25 17:35:37 -080063 return lock()->getKeys();
Yingdi Yub8f8b342015-04-27 11:06:42 -070064}
65
Yingdi Yucbe72b02015-11-25 17:35:37 -080066const Key&
Yingdi Yufe4733a2015-10-22 14:24:12 -070067Identity::setDefaultKey(const Name& keyName) const
Yingdi Yub8f8b342015-04-27 11:06:42 -070068{
Yingdi Yucbe72b02015-11-25 17:35:37 -080069 return lock()->setDefaultKey(keyName);
Yingdi Yub8f8b342015-04-27 11:06:42 -070070}
71
Yingdi Yucbe72b02015-11-25 17:35:37 -080072const Key&
Yingdi Yufe4733a2015-10-22 14:24:12 -070073Identity::setDefaultKey(const uint8_t* key, size_t keyLen, const Name& keyName) const
Yingdi Yub8f8b342015-04-27 11:06:42 -070074{
Yingdi Yucbe72b02015-11-25 17:35:37 -080075 return lock()->setDefaultKey(key, keyLen, keyName);
Yingdi Yub8f8b342015-04-27 11:06:42 -070076}
77
Yingdi Yucbe72b02015-11-25 17:35:37 -080078const Key&
Yingdi Yuc8209892015-06-19 17:47:56 -070079Identity::getDefaultKey() const
Yingdi Yub8f8b342015-04-27 11:06:42 -070080{
Yingdi Yucbe72b02015-11-25 17:35:37 -080081 return lock()->getDefaultKey();
Yingdi Yub8f8b342015-04-27 11:06:42 -070082}
83
84Identity::operator bool() const
85{
Davide Pesaventobdcedf42017-10-15 14:56:28 -040086 return !m_impl.expired();
Yingdi Yub8f8b342015-04-27 11:06:42 -070087}
88
Yingdi Yucbe72b02015-11-25 17:35:37 -080089shared_ptr<detail::IdentityImpl>
90Identity::lock() const
Yingdi Yub8f8b342015-04-27 11:06:42 -070091{
Yingdi Yucbe72b02015-11-25 17:35:37 -080092 auto impl = m_impl.lock();
93
94 if (impl == nullptr)
95 BOOST_THROW_EXCEPTION(std::domain_error("Invalid Identity instance"));
96
97 return impl;
Yingdi Yub8f8b342015-04-27 11:06:42 -070098}
99
Junxiao Shi5759be32017-10-15 00:00:52 +0000100bool
101operator!=(const Identity& lhs, const Identity& rhs)
102{
103 return lhs.m_impl.owner_before(rhs.m_impl) || rhs.m_impl.owner_before(lhs.m_impl);
104}
105
106std::ostream&
107operator<<(std::ostream& os, const Identity& id)
108{
109 if (id) {
110 os << id.getName();
111 }
112 else {
113 os << "(empty)";
114 }
115 return os;
116}
117
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700118} // namespace pib
Yingdi Yub8f8b342015-04-27 11:06:42 -0700119} // namespace security
120} // namespace ndn