blob: fcf3360b6b1dfb3529cb8c74fe64ac9bd64f82f0 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento88a0d812017-08-19 21:31:42 -04002/*
Junxiao Shi68b53852018-07-25 13:56:38 -06003 * Copyright (c) 2013-2018 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * 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.
Jeff Thompson5cae5e52013-07-10 19:41:20 -070020 */
21
Jeff Thompson71b2f872013-12-17 12:03:17 -080022#ifndef NDN_KEY_LOCATOR_HPP
23#define NDN_KEY_LOCATOR_HPP
Jeff Thompson5cae5e52013-07-10 19:41:20 -070024
Alexander Afanasyev15f67312014-07-22 15:11:09 -070025#include "encoding/encoding-buffer.hpp"
Jeff Thompson2e6269c2013-08-22 10:36:00 -070026#include "name.hpp"
Jeff Thompson5cae5e52013-07-10 19:41:20 -070027
28namespace ndn {
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -080029
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070030class KeyLocator
31{
Jeff Thompson5cae5e52013-07-10 19:41:20 -070032public:
Junxiao Shic2b8d242014-11-04 08:35:29 -070033 class Error : public tlv::Error
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070034 {
35 public:
Junxiao Shi68b53852018-07-25 13:56:38 -060036 using tlv::Error::Error;
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070037 };
Junxiao Shiaf8eeea2014-03-31 20:10:56 -070038
Junxiao Shibc5030d2014-09-01 11:53:12 -070039 enum Type {
40 /** \brief indicates KeyLocator is empty (internal use only)
41 */
42 KeyLocator_None = 65535,
43 /** \brief indicates KeyLocator contains a Name
44 */
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080045 KeyLocator_Name = 0,
Junxiao Shibc5030d2014-09-01 11:53:12 -070046 /** \brief indicates KeyLocator contains a KeyDigest
47 */
48 KeyLocator_KeyDigest = 1,
49 /** \brief indicates KeyLocator contains an unknown element
50 */
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080051 KeyLocator_Unknown = 255
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -080052 };
Alexander Afanasyevfa13f8e2014-01-03 15:19:07 -080053
Junxiao Shibc5030d2014-09-01 11:53:12 -070054public: // constructors
55 /** \brief construct an empty KeyLocator
56 */
57 KeyLocator();
Jeff Thompson5cae5e52013-07-10 19:41:20 -070058
Junxiao Shibc5030d2014-09-01 11:53:12 -070059 /** \brief construct from wire encoding
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070060 */
61 explicit
Junxiao Shibc5030d2014-09-01 11:53:12 -070062 KeyLocator(const Block& wire);
Alexander Afanasyevfa13f8e2014-01-03 15:19:07 -080063
Junxiao Shibc5030d2014-09-01 11:53:12 -070064 /** \brief construct from Name
65 * \note implicit conversion is permitted
66 */
67 KeyLocator(const Name& name);
Junxiao Shiaf8eeea2014-03-31 20:10:56 -070068
Junxiao Shibc5030d2014-09-01 11:53:12 -070069public: // encode and decode
70 /** \brief prepend wire encoding
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -070071 * \param encoder EncodingBuffer or Estimator
Junxiao Shibc5030d2014-09-01 11:53:12 -070072 */
Alexander Afanasyev74633892015-02-08 18:08:46 -080073 template<encoding::Tag TAG>
Alexander Afanasyev809805d2014-02-17 17:20:33 -080074 size_t
Alexander Afanasyevd5c48e02015-06-24 11:58:14 -070075 wireEncode(EncodingImpl<TAG>& encoder) const;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080076
Junxiao Shibc5030d2014-09-01 11:53:12 -070077 /** \return wire encoding
78 */
Junxiao Shiaf8eeea2014-03-31 20:10:56 -070079 const Block&
Alexander Afanasyev809805d2014-02-17 17:20:33 -080080 wireEncode() const;
Junxiao Shiaf8eeea2014-03-31 20:10:56 -070081
Junxiao Shibc5030d2014-09-01 11:53:12 -070082 /** \brief decode from wire encoding
83 * \throw Error outer TLV type is not KeyLocator
84 * \note No error is thrown for unrecognized inner TLV, but type becomes KeyLocator_Unknown.
85 */
Alexander Afanasyev809805d2014-02-17 17:20:33 -080086 void
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070087 wireDecode(const Block& wire);
Junxiao Shiaf8eeea2014-03-31 20:10:56 -070088
Junxiao Shibc5030d2014-09-01 11:53:12 -070089public: // attributes
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070090 bool
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -080091 empty() const
92 {
Alexander Afanasyev809805d2014-02-17 17:20:33 -080093 return m_type == KeyLocator_None;
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -080094 }
Junxiao Shiaf8eeea2014-03-31 20:10:56 -070095
Junxiao Shibc5030d2014-09-01 11:53:12 -070096 Type
97 getType() const
98 {
99 return m_type;
100 }
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700101
Junxiao Shibc5030d2014-09-01 11:53:12 -0700102 /** \brief clear KeyLocator
103 * \details type becomes KeyLocator_None
104 * \return self
105 */
106 KeyLocator&
107 clear();
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700108
Junxiao Shibc5030d2014-09-01 11:53:12 -0700109 /** \brief get Name element
110 * \throw Error if type is not KeyLocator_Name
111 */
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700112 const Name&
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800113 getName() const;
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800114
Junxiao Shibc5030d2014-09-01 11:53:12 -0700115 /** \brief set Name element
116 * \details type becomes KeyLocator_Name
117 * \return self
118 */
119 KeyLocator&
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700120 setName(const Name& name);
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700121
Junxiao Shibc5030d2014-09-01 11:53:12 -0700122 /** \brief get KeyDigest element
123 * \throw Error if type is not KeyLocator_KeyDigest
124 */
125 const Block&
126 getKeyDigest() const;
127
128 /** \brief set KeyDigest element
129 * \details type becomes KeyLocator_KeyDigest
130 * \throw Error if Block type is not KeyDigest
131 * \return self
132 */
133 KeyLocator&
134 setKeyDigest(const Block& keyDigest);
135
136 /** \brief set KeyDigest value
137 * \details type becomes KeyLocator_KeyDigest
138 * \return self
139 */
140 KeyLocator&
141 setKeyDigest(const ConstBufferPtr& keyDigest);
142
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700143public: // EqualityComparable concept
144 bool
145 operator==(const KeyLocator& other) const;
146
147 bool
Junxiao Shibc5030d2014-09-01 11:53:12 -0700148 operator!=(const KeyLocator& other) const
149 {
150 return !this->operator==(other);
151 }
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700152
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700153private:
Junxiao Shibc5030d2014-09-01 11:53:12 -0700154 Type m_type;
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800155 Name m_name;
Junxiao Shibc5030d2014-09-01 11:53:12 -0700156 Block m_keyDigest;
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700157
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800158 mutable Block m_wire;
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700159};
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800160
Davide Pesavento88a0d812017-08-19 21:31:42 -0400161NDN_CXX_DECLARE_WIRE_ENCODE_INSTANTIATIONS(KeyLocator);
162
Alexander Afanasyev5f1820e2017-01-04 18:12:42 -0800163std::ostream&
164operator<<(std::ostream& os, const KeyLocator& keyLocator);
165
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800166} // namespace ndn
167
Junxiao Shibc5030d2014-09-01 11:53:12 -0700168#endif // NDN_KEY_LOCATOR_HPP