Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame^] | 2 | /* |
| 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 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. |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 20 | */ |
| 21 | |
Jeff Thompson | 56ec9e2 | 2013-08-02 11:34:07 -0700 | [diff] [blame] | 22 | #ifndef NDN_DATA_HPP |
Jeff Thompson | a0d18c9 | 2013-08-06 13:55:32 -0700 | [diff] [blame] | 23 | #define NDN_DATA_HPP |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 24 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 25 | #include "meta-info.hpp" |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame^] | 26 | #include "name.hpp" |
| 27 | #include "signature.hpp" |
Alexander Afanasyev | a3887ae | 2014-12-29 16:11:57 -0800 | [diff] [blame] | 28 | #include "tag-host.hpp" |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame^] | 29 | #include "encoding/block.hpp" |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 30 | |
| 31 | namespace ndn { |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 32 | |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame^] | 33 | /** @brief Represents a Data packet |
Junxiao Shi | c2b8d24 | 2014-11-04 08:35:29 -0700 | [diff] [blame] | 34 | */ |
Alexander Afanasyev | a3887ae | 2014-12-29 16:11:57 -0800 | [diff] [blame] | 35 | class Data : public TagHost, public enable_shared_from_this<Data> |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 36 | { |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 37 | public: |
Junxiao Shi | c2b8d24 | 2014-11-04 08:35:29 -0700 | [diff] [blame] | 38 | class Error : public tlv::Error |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 39 | { |
| 40 | public: |
| 41 | explicit |
| 42 | Error(const std::string& what) |
Junxiao Shi | c2b8d24 | 2014-11-04 08:35:29 -0700 | [diff] [blame] | 43 | : tlv::Error(what) |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 44 | { |
| 45 | } |
| 46 | }; |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 47 | |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame^] | 48 | /** @brief Create a new Data with the given name and empty Content |
| 49 | * @warning In certain contexts that use Data::shared_from_this(), Data must be created |
| 50 | * using `make_shared`. Otherwise, .shared_from_this() will trigger undefined behavior. |
Jeff Thompson | 20af073 | 2013-09-12 17:01:45 -0700 | [diff] [blame] | 51 | */ |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame^] | 52 | Data(const Name& name = Name()); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 53 | |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame^] | 54 | /** @brief Create from wire encoding |
| 55 | * @warning In certain contexts that use Data::shared_from_this(), Data must be created |
| 56 | * using `make_shared`. Otherwise, .shared_from_this() will trigger undefined behavior. |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 57 | */ |
| 58 | explicit |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 59 | Data(const Block& wire); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 60 | |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame^] | 61 | /** @brief Fast encoding or block size estimation |
| 62 | * @param encoder EncodingEstimator or EncodingBuffer instance |
| 63 | * @param wantUnsignedPortionOnly If true, only prepends Name, MetaInfo, Content, and |
| 64 | * SignatureInfo to @p encoder, but omit SignatureValue and outmost Type-Length of Data |
| 65 | * element. This is intended to be used with wireEncode(encoder, signatureValue). |
| 66 | * @throw Error SignatureBits are not provided and wantUnsignedPortionOnly is false. |
Jeff Thompson | b7aefa00 | 2013-09-16 18:22:00 -0700 | [diff] [blame] | 67 | */ |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 68 | template<encoding::Tag TAG> |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 69 | size_t |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 70 | wireEncode(EncodingImpl<TAG>& encoder, bool wantUnsignedPortionOnly = false) const; |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 71 | |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame^] | 72 | /** @brief Finalize Data packet encoding with the specified SignatureValue |
| 73 | * @param encoder EncodingBuffer containing Name, MetaInfo, Content, and SignatureInfo, but |
| 74 | * without SignatureValue or outmost Type-Length of Data element |
| 75 | * @param signatureValue SignatureValue element |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 76 | * |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame^] | 77 | * This method is intended to be used in concert with Data::wireEncode(encoder, true) |
| 78 | * @code |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 79 | * Data data; |
| 80 | * ... |
| 81 | * EncodingBuffer encoder; |
| 82 | * data.wireEncode(encoder, true); |
| 83 | * ... |
| 84 | * Block signatureValue = <sign_over_unsigned_portion>(encoder.buf(), encoder.size()); |
| 85 | * data.wireEncode(encoder, signatureValue) |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame^] | 86 | * @endcode |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 87 | */ |
| 88 | const Block& |
| 89 | wireEncode(EncodingBuffer& encoder, const Block& signatureValue) const; |
| 90 | |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame^] | 91 | /** @brief Encode to a wire format |
| 92 | */ |
| 93 | const Block& |
| 94 | wireEncode() const; |
| 95 | |
| 96 | /** @brief Decode from the wire format |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 97 | */ |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 98 | void |
| 99 | wireDecode(const Block& wire); |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 100 | |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame^] | 101 | /** @brief Check if already has wire |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 102 | */ |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 103 | bool |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame^] | 104 | hasWire() const |
| 105 | { |
| 106 | return m_wire.hasWire(); |
| 107 | } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 108 | |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame^] | 109 | /** @brief Get full name including implicit digest |
| 110 | * @pre hasWire() == true; i.e. wireEncode() must have been called |
| 111 | * @throw Error Data has no wire encoding |
Alexander Afanasyev | 3b70310 | 2014-06-13 17:01:14 -0700 | [diff] [blame] | 112 | */ |
| 113 | const Name& |
| 114 | getFullName() const; |
| 115 | |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame^] | 116 | public: // Data fields |
| 117 | /** @brief Get name |
| 118 | */ |
| 119 | const Name& |
| 120 | getName() const |
| 121 | { |
| 122 | return m_name; |
| 123 | } |
| 124 | |
| 125 | /** @brief Set name |
| 126 | * @return a reference to this Data, to allow chaining |
| 127 | */ |
| 128 | Data& |
| 129 | setName(const Name& name); |
| 130 | |
| 131 | /** @brief Get MetaInfo |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 132 | */ |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 133 | const MetaInfo& |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame^] | 134 | getMetaInfo() const |
| 135 | { |
| 136 | return m_metaInfo; |
| 137 | } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 138 | |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame^] | 139 | /** @brief Set MetaInfo |
| 140 | * @return a reference to this Data, to allow chaining |
Jeff Thompson | 0cd8c4a | 2013-09-13 17:46:40 -0700 | [diff] [blame] | 141 | */ |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 142 | Data& |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 143 | setMetaInfo(const MetaInfo& metaInfo); |
Jeff Thompson | 46bd45f | 2013-08-08 16:46:41 -0700 | [diff] [blame] | 144 | |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame^] | 145 | /** @brief Get Content |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 146 | * |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame^] | 147 | * The Content value is accessible through value()/value_size() or value_begin()/value_end() |
| 148 | * methods of the Block class. |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 149 | */ |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 150 | const Block& |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 151 | getContent() const; |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 152 | |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame^] | 153 | /** @brief Set Content from a block |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 154 | * |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame^] | 155 | * If block's TLV-TYPE is Content, it will be used directly as Data's Content element. |
| 156 | * If block's TLV-TYPE is not Content, it will be nested into a Content element. |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 157 | * |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame^] | 158 | * @return a reference to this Data, to allow chaining |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 159 | */ |
| 160 | Data& |
| 161 | setContent(const Block& block); |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 162 | |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame^] | 163 | /** @brief Copy Content value from raw buffer |
| 164 | * @param value pointer to the first octet of the value |
| 165 | * @param valueSize size of the raw buffer |
| 166 | * @return a reference to this Data, to allow chaining |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 167 | */ |
| 168 | Data& |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame^] | 169 | setContent(const uint8_t* value, size_t valueSize); |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 170 | |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame^] | 171 | /** @brief Set Content from wire buffer |
| 172 | * @param value Content value, which does not need to be a TLV element |
| 173 | * @return a reference to this Data, to allow chaining |
| 174 | */ |
| 175 | Data& |
| 176 | setContent(const ConstBufferPtr& value); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 177 | |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame^] | 178 | /** @brief Get Signature |
| 179 | */ |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 180 | const Signature& |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame^] | 181 | getSignature() const |
| 182 | { |
| 183 | return m_signature; |
| 184 | } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 185 | |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame^] | 186 | /** @brief Set Signature |
| 187 | * @return a reference to this Data, to allow chaining |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 188 | */ |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 189 | Data& |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 190 | setSignature(const Signature& signature); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 191 | |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame^] | 192 | /** @brief Set SignatureValue |
| 193 | * @return a reference to this Data, to allow chaining |
| 194 | */ |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 195 | Data& |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 196 | setSignatureValue(const Block& value); |
Yingdi Yu | a4e5767 | 2014-02-06 11:16:17 -0800 | [diff] [blame] | 197 | |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame^] | 198 | public: // MetaInfo fields |
| 199 | uint32_t |
| 200 | getContentType() const |
| 201 | { |
| 202 | return m_metaInfo.getType(); |
| 203 | } |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 204 | |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame^] | 205 | Data& |
| 206 | setContentType(uint32_t type); |
| 207 | |
| 208 | const time::milliseconds& |
| 209 | getFreshnessPeriod() const |
| 210 | { |
| 211 | return m_metaInfo.getFreshnessPeriod(); |
| 212 | } |
| 213 | |
| 214 | Data& |
| 215 | setFreshnessPeriod(const time::milliseconds& freshnessPeriod); |
| 216 | |
| 217 | const name::Component& |
| 218 | getFinalBlockId() const |
| 219 | { |
| 220 | return m_metaInfo.getFinalBlockId(); |
| 221 | } |
| 222 | |
| 223 | Data& |
| 224 | setFinalBlockId(const name::Component& finalBlockId); |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 225 | |
Spyridon Mastorakis | 3b54e85 | 2015-04-07 08:03:25 -0700 | [diff] [blame] | 226 | protected: |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame^] | 227 | /** @brief Clear wire encoding and cached FullName |
| 228 | * @note This does not clear the SignatureValue. |
Jeff Thompson | b7aefa00 | 2013-09-16 18:22:00 -0700 | [diff] [blame] | 229 | */ |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 230 | void |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame^] | 231 | resetWire(); |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 232 | |
| 233 | private: |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 234 | Name m_name; |
| 235 | MetaInfo m_metaInfo; |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame^] | 236 | Block m_content; |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 237 | Signature m_signature; |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 238 | |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 239 | mutable Block m_wire; |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame^] | 240 | mutable Name m_fullName; ///< cached FullName computed from m_wire |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 241 | }; |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 242 | |
Alexander Afanasyev | a0c5f83 | 2014-06-19 13:27:56 -0700 | [diff] [blame] | 243 | std::ostream& |
| 244 | operator<<(std::ostream& os, const Data& data); |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 245 | |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame^] | 246 | bool |
| 247 | operator==(const Data& lhs, const Data& rhs); |
| 248 | |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 249 | inline bool |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame^] | 250 | operator!=(const Data& lhs, const Data& rhs) |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 251 | { |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame^] | 252 | return !(lhs == rhs); |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 253 | } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 254 | |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 255 | } // namespace ndn |
| 256 | |
Junxiao Shi | 81206d5 | 2017-07-23 12:43:22 +0000 | [diff] [blame^] | 257 | #endif // NDN_DATA_HPP |