blob: 27b8d6792b15bbfafb1e0d94b528f2361cdc2407 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Jeff Thompson5cae5e52013-07-10 19:41:20 -07002/**
Alexander Afanasyev74633892015-02-08 18:08:46 -08003 * Copyright (c) 2013-2015 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:
36 explicit
37 Error(const std::string& what)
Junxiao Shic2b8d242014-11-04 08:35:29 -070038 : tlv::Error(what)
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070039 {
40 }
41 };
Junxiao Shiaf8eeea2014-03-31 20:10:56 -070042
Junxiao Shibc5030d2014-09-01 11:53:12 -070043 enum Type {
44 /** \brief indicates KeyLocator is empty (internal use only)
45 */
46 KeyLocator_None = 65535,
47 /** \brief indicates KeyLocator contains a Name
48 */
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080049 KeyLocator_Name = 0,
Junxiao Shibc5030d2014-09-01 11:53:12 -070050 /** \brief indicates KeyLocator contains a KeyDigest
51 */
52 KeyLocator_KeyDigest = 1,
53 /** \brief indicates KeyLocator contains an unknown element
54 */
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080055 KeyLocator_Unknown = 255
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -080056 };
Alexander Afanasyevfa13f8e2014-01-03 15:19:07 -080057
Junxiao Shibc5030d2014-09-01 11:53:12 -070058public: // constructors
59 /** \brief construct an empty KeyLocator
60 */
61 KeyLocator();
Jeff Thompson5cae5e52013-07-10 19:41:20 -070062
Junxiao Shibc5030d2014-09-01 11:53:12 -070063 /** \brief construct from wire encoding
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070064 */
65 explicit
Junxiao Shibc5030d2014-09-01 11:53:12 -070066 KeyLocator(const Block& wire);
Alexander Afanasyevfa13f8e2014-01-03 15:19:07 -080067
Junxiao Shibc5030d2014-09-01 11:53:12 -070068 /** \brief construct from Name
69 * \note implicit conversion is permitted
70 */
71 KeyLocator(const Name& name);
Junxiao Shiaf8eeea2014-03-31 20:10:56 -070072
Junxiao Shibc5030d2014-09-01 11:53:12 -070073public: // encode and decode
74 /** \brief prepend wire encoding
75 * \param block EncodingBuffer or Estimator
76 */
Alexander Afanasyev74633892015-02-08 18:08:46 -080077 template<encoding::Tag TAG>
Alexander Afanasyev809805d2014-02-17 17:20:33 -080078 size_t
Alexander Afanasyev74633892015-02-08 18:08:46 -080079 wireEncode(EncodingImpl<TAG>& block) const;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080080
Junxiao Shibc5030d2014-09-01 11:53:12 -070081 /** \return wire encoding
82 */
Junxiao Shiaf8eeea2014-03-31 20:10:56 -070083 const Block&
Alexander Afanasyev809805d2014-02-17 17:20:33 -080084 wireEncode() const;
Junxiao Shiaf8eeea2014-03-31 20:10:56 -070085
Junxiao Shibc5030d2014-09-01 11:53:12 -070086 /** \brief decode from wire encoding
87 * \throw Error outer TLV type is not KeyLocator
88 * \note No error is thrown for unrecognized inner TLV, but type becomes KeyLocator_Unknown.
89 */
Alexander Afanasyev809805d2014-02-17 17:20:33 -080090 void
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070091 wireDecode(const Block& wire);
Junxiao Shiaf8eeea2014-03-31 20:10:56 -070092
Junxiao Shibc5030d2014-09-01 11:53:12 -070093public: // attributes
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070094 bool
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -080095 empty() const
96 {
Alexander Afanasyev809805d2014-02-17 17:20:33 -080097 return m_type == KeyLocator_None;
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -080098 }
Junxiao Shiaf8eeea2014-03-31 20:10:56 -070099
Junxiao Shibc5030d2014-09-01 11:53:12 -0700100 Type
101 getType() const
102 {
103 return m_type;
104 }
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700105
Junxiao Shibc5030d2014-09-01 11:53:12 -0700106 /** \brief clear KeyLocator
107 * \details type becomes KeyLocator_None
108 * \return self
109 */
110 KeyLocator&
111 clear();
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700112
Junxiao Shibc5030d2014-09-01 11:53:12 -0700113 /** \brief get Name element
114 * \throw Error if type is not KeyLocator_Name
115 */
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700116 const Name&
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800117 getName() const;
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800118
Junxiao Shibc5030d2014-09-01 11:53:12 -0700119 /** \brief set Name element
120 * \details type becomes KeyLocator_Name
121 * \return self
122 */
123 KeyLocator&
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700124 setName(const Name& name);
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700125
Junxiao Shibc5030d2014-09-01 11:53:12 -0700126 /** \brief get KeyDigest element
127 * \throw Error if type is not KeyLocator_KeyDigest
128 */
129 const Block&
130 getKeyDigest() const;
131
132 /** \brief set KeyDigest element
133 * \details type becomes KeyLocator_KeyDigest
134 * \throw Error if Block type is not KeyDigest
135 * \return self
136 */
137 KeyLocator&
138 setKeyDigest(const Block& keyDigest);
139
140 /** \brief set KeyDigest value
141 * \details type becomes KeyLocator_KeyDigest
142 * \return self
143 */
144 KeyLocator&
145 setKeyDigest(const ConstBufferPtr& keyDigest);
146
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700147public: // EqualityComparable concept
148 bool
149 operator==(const KeyLocator& other) const;
150
151 bool
Junxiao Shibc5030d2014-09-01 11:53:12 -0700152 operator!=(const KeyLocator& other) const
153 {
154 return !this->operator==(other);
155 }
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700156
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700157private:
Junxiao Shibc5030d2014-09-01 11:53:12 -0700158 Type m_type;
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800159 Name m_name;
Junxiao Shibc5030d2014-09-01 11:53:12 -0700160 Block m_keyDigest;
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700161
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800162 mutable Block m_wire;
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700163};
Alexander Afanasyevfadc97d2014-01-03 13:22:10 -0800164
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800165} // namespace ndn
166
Junxiao Shibc5030d2014-09-01 11:53:12 -0700167#endif // NDN_KEY_LOCATOR_HPP