Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 2 | /** |
Jeff Thompson | 7687dc0 | 2013-09-13 11:54:07 -0700 | [diff] [blame] | 3 | * Copyright (C) 2013 Regents of the University of California. |
| 4 | * @author: Jeff Thompson <jefft0@remap.ucla.edu> |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 5 | * See COPYING for copyright and distribution information. |
| 6 | */ |
| 7 | |
Jeff Thompson | 56ec9e2 | 2013-08-02 11:34:07 -0700 | [diff] [blame] | 8 | #ifndef NDN_DATA_HPP |
Jeff Thompson | a0d18c9 | 2013-08-06 13:55:32 -0700 | [diff] [blame] | 9 | #define NDN_DATA_HPP |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 10 | |
Jeff Thompson | 46bd45f | 2013-08-08 16:46:41 -0700 | [diff] [blame] | 11 | #include "common.hpp" |
Jeff Thompson | 5341219 | 2013-08-06 13:35:50 -0700 | [diff] [blame] | 12 | #include "name.hpp" |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 13 | #include "encoding/block.hpp" |
Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 14 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 15 | #include "signature.hpp" |
| 16 | #include "meta-info.hpp" |
| 17 | #include "key-locator.hpp" |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 18 | |
| 19 | namespace ndn { |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 20 | |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 21 | class Data : public enable_shared_from_this<Data> |
| 22 | { |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 23 | public: |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 24 | struct Error : public std::runtime_error { Error(const std::string &what) : std::runtime_error(what) {} }; |
| 25 | |
Jeff Thompson | 20af073 | 2013-09-12 17:01:45 -0700 | [diff] [blame] | 26 | /** |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 27 | * @brief Create an empty Data object |
Jeff Thompson | 20af073 | 2013-09-12 17:01:45 -0700 | [diff] [blame] | 28 | */ |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 29 | inline |
| 30 | Data(); |
| 31 | |
Jeff Thompson | 20af073 | 2013-09-12 17:01:45 -0700 | [diff] [blame] | 32 | /** |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 33 | * @brief Create a new Data object with the given name |
Jeff Thompson | 20af073 | 2013-09-12 17:01:45 -0700 | [diff] [blame] | 34 | * @param name A reference to the name which is copied. |
| 35 | */ |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 36 | inline |
| 37 | Data(const Name& name); |
Jeff Thompson | 25bfdca | 2013-10-16 17:05:41 -0700 | [diff] [blame] | 38 | |
| 39 | /** |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame^] | 40 | * @brief The destructor |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 41 | */ |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame^] | 42 | inline |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 43 | ~Data(); |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 44 | |
| 45 | /** |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame^] | 46 | * @brief Fast encoding or block size estimation |
Jeff Thompson | b7aefa00 | 2013-09-16 18:22:00 -0700 | [diff] [blame] | 47 | */ |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame^] | 48 | template<bool T> |
| 49 | inline size_t |
| 50 | wireEncode(EncodingImpl<T> &block, bool unsignedPortion = false) const; |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 51 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 52 | /** |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame^] | 53 | * @brief Encode to a wire format |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 54 | */ |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame^] | 55 | inline const Block& |
| 56 | wireEncode() const; |
| 57 | |
| 58 | /** |
| 59 | * @brief Decode from the wire format |
| 60 | */ |
| 61 | inline void |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 62 | wireDecode(const Block &wire); |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 63 | |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame^] | 64 | //////////////////////////////////////////////////////////////////// |
| 65 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 66 | inline const Name& |
| 67 | getName() const; |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 68 | |
| 69 | /** |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 70 | * @brief Set name to a copy of the given Name. |
| 71 | * |
Jeff Thompson | 0cd8c4a | 2013-09-13 17:46:40 -0700 | [diff] [blame] | 72 | * @param name The Name which is copied. |
Jeff Thompson | 6d59197 | 2013-10-17 11:16:32 -0700 | [diff] [blame] | 73 | * @return This Data so that you can chain calls to update values. |
Jeff Thompson | 0cd8c4a | 2013-09-13 17:46:40 -0700 | [diff] [blame] | 74 | */ |
Alexander Afanasyev | a61757b | 2014-01-03 15:09:29 -0800 | [diff] [blame] | 75 | inline void |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 76 | setName(const Name& name); |
| 77 | |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame^] | 78 | // |
| 79 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 80 | inline const MetaInfo& |
| 81 | getMetaInfo() const; |
Jeff Thompson | 46bd45f | 2013-08-08 16:46:41 -0700 | [diff] [blame] | 82 | |
Jeff Thompson | 0cd8c4a | 2013-09-13 17:46:40 -0700 | [diff] [blame] | 83 | /** |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 84 | * @brief Set metaInfo to a copy of the given MetaInfo. |
Jeff Thompson | 0cd8c4a | 2013-09-13 17:46:40 -0700 | [diff] [blame] | 85 | * @param metaInfo The MetaInfo which is copied. |
Jeff Thompson | 6d59197 | 2013-10-17 11:16:32 -0700 | [diff] [blame] | 86 | * @return This Data so that you can chain calls to update values. |
Jeff Thompson | 0cd8c4a | 2013-09-13 17:46:40 -0700 | [diff] [blame] | 87 | */ |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 88 | inline void |
| 89 | setMetaInfo(const MetaInfo& metaInfo); |
Jeff Thompson | 46bd45f | 2013-08-08 16:46:41 -0700 | [diff] [blame] | 90 | |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame^] | 91 | // |
| 92 | |
| 93 | /////////////////////////////////////////////////////////////// |
| 94 | /////////////////////////////////////////////////////////////// |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 95 | /////////////////////////////////////////////////////////////// |
| 96 | // MetaInfo proxy methods |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame^] | 97 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 98 | inline uint32_t |
| 99 | getContentType() const; |
| 100 | |
| 101 | inline void |
| 102 | setContentType(uint32_t type); |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame^] | 103 | |
| 104 | // |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 105 | |
| 106 | inline Milliseconds |
| 107 | getFreshnessPeriod() const; |
| 108 | |
| 109 | inline void |
| 110 | setFreshnessPeriod(Milliseconds freshnessPeriod); |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 111 | |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame^] | 112 | // |
| 113 | |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 114 | inline const name::Component& |
| 115 | getFinalBlockId() const; |
| 116 | |
| 117 | inline void |
| 118 | setFinalBlockId(const name::Component& finalBlockId); |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame^] | 119 | |
| 120 | // |
| 121 | /////////////////////////////////////////////////////////////// |
| 122 | /////////////////////////////////////////////////////////////// |
| 123 | /////////////////////////////////////////////////////////////// |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 124 | |
| 125 | /** |
| 126 | * @brief Get content Block |
| 127 | * |
| 128 | * To access content value, one can use value()/value_size() or |
| 129 | * value_begin()/value_end() methods of the Block class |
| 130 | */ |
| 131 | inline const Block& |
| 132 | getContent() const; |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 133 | |
Jeff Thompson | 0899c0f | 2013-09-12 12:15:31 -0700 | [diff] [blame] | 134 | /** |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 135 | * @brief Set the content to a copy of the data in the vector. |
Jeff Thompson | 0899c0f | 2013-09-12 12:15:31 -0700 | [diff] [blame] | 136 | * @param content A vector whose contents are copied. |
Jeff Thompson | 6d59197 | 2013-10-17 11:16:32 -0700 | [diff] [blame] | 137 | * @return This Data so that you can chain calls to update values. |
Jeff Thompson | 0899c0f | 2013-09-12 12:15:31 -0700 | [diff] [blame] | 138 | */ |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 139 | inline void |
| 140 | setContent(const uint8_t* content, size_t contentLength); |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 141 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 142 | inline void |
| 143 | setContent(const Block& content); |
| 144 | |
| 145 | inline void |
| 146 | setContent(const ConstBufferPtr &contentValue); |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame^] | 147 | |
| 148 | // |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 149 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 150 | inline const Signature& |
| 151 | getSignature() const; |
| 152 | |
| 153 | /** |
| 154 | * @brief Set the signature to a copy of the given signature. |
| 155 | * @param signature The signature object which is cloned. |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 156 | */ |
Alexander Afanasyev | ad39b18 | 2014-01-03 15:38:58 -0800 | [diff] [blame] | 157 | inline void |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 158 | setSignature(const Signature& signature); |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame^] | 159 | |
Alexander Afanasyev | ad39b18 | 2014-01-03 15:38:58 -0800 | [diff] [blame] | 160 | inline void |
| 161 | setSignatureValue(const Block &value); |
Yingdi Yu | a4e5767 | 2014-02-06 11:16:17 -0800 | [diff] [blame] | 162 | |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame^] | 163 | /////////////////////////////////////////////////////////////// |
| 164 | |
Yingdi Yu | a4e5767 | 2014-02-06 11:16:17 -0800 | [diff] [blame] | 165 | inline uint64_t |
| 166 | getIncomingFaceId() const; |
| 167 | |
| 168 | inline void |
| 169 | setIncomingFaceId(uint64_t incomingFaceId); |
Alexander Afanasyev | ad39b18 | 2014-01-03 15:38:58 -0800 | [diff] [blame] | 170 | |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 171 | private: |
Jeff Thompson | b7aefa00 | 2013-09-16 18:22:00 -0700 | [diff] [blame] | 172 | /** |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 173 | * @brief Clear the wire encoding. |
Jeff Thompson | b7aefa00 | 2013-09-16 18:22:00 -0700 | [diff] [blame] | 174 | */ |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 175 | inline void |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 176 | onChanged(); |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 177 | |
| 178 | private: |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame^] | 179 | Name m_name; |
| 180 | MetaInfo m_metaInfo; |
| 181 | mutable Block m_content; |
| 182 | Signature m_signature; |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 183 | |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame^] | 184 | mutable Block m_wire; |
Yingdi Yu | a4e5767 | 2014-02-06 11:16:17 -0800 | [diff] [blame] | 185 | |
| 186 | uint64_t m_incomingFaceId; |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 187 | }; |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 188 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 189 | inline |
| 190 | Data::Data() |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame^] | 191 | : m_content(Tlv::Content) // empty content |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 192 | { |
| 193 | } |
| 194 | |
| 195 | inline |
| 196 | Data::Data(const Name& name) |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame^] | 197 | : m_name(name) |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 198 | { |
| 199 | } |
| 200 | |
| 201 | inline |
| 202 | Data::~Data() |
| 203 | { |
| 204 | } |
| 205 | |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame^] | 206 | template<bool T> |
| 207 | inline size_t |
| 208 | Data::wireEncode(EncodingImpl<T> &block, bool unsignedPortion/* = false*/) const |
| 209 | { |
| 210 | size_t total_len = 0; |
| 211 | |
| 212 | // Data ::= DATA-TLV TLV-LENGTH |
| 213 | // Name |
| 214 | // MetaInfo |
| 215 | // Content |
| 216 | // Signature |
| 217 | |
| 218 | // (reverse encoding) |
| 219 | |
| 220 | if (!unsignedPortion && !m_signature) |
| 221 | { |
| 222 | throw Error("Requested wire format, but data packet has not been signed yet"); |
| 223 | } |
| 224 | |
| 225 | if (!unsignedPortion) |
| 226 | { |
| 227 | // SignatureValue |
| 228 | total_len += prependBlock(block, m_signature.getValue()); |
| 229 | } |
| 230 | |
| 231 | // SignatureInfo |
| 232 | total_len += prependBlock(block, m_signature.getInfo()); |
| 233 | |
| 234 | // Content |
| 235 | total_len += prependBlock(block, getContent()); |
| 236 | |
| 237 | // MetaInfo |
| 238 | total_len += getMetaInfo().wireEncode(block); |
| 239 | |
| 240 | // Name |
| 241 | total_len += getName().wireEncode(block); |
| 242 | |
| 243 | if (!unsignedPortion) |
| 244 | { |
| 245 | total_len += block.prependVarNumber (total_len); |
| 246 | total_len += block.prependVarNumber (Tlv::Data); |
| 247 | } |
| 248 | return total_len; |
| 249 | } |
| 250 | |
| 251 | inline const Block & |
| 252 | Data::wireEncode() const |
| 253 | { |
| 254 | if (m_wire.hasWire()) |
| 255 | return m_wire; |
| 256 | |
| 257 | EncodingEstimator estimator; |
| 258 | size_t estimatedSize = wireEncode(estimator); |
| 259 | |
| 260 | EncodingBuffer buffer(estimatedSize, 0); |
| 261 | wireEncode(buffer); |
| 262 | |
| 263 | const_cast<Data*>(this)->wireDecode(buffer.block()); |
| 264 | return m_wire; |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * Decode the input using a particular wire format and update this Data. |
| 269 | * @param input The input byte array to be decoded. |
| 270 | */ |
| 271 | void |
| 272 | Data::wireDecode(const Block &wire) |
| 273 | { |
| 274 | m_wire = wire; |
| 275 | m_wire.parse(); |
| 276 | |
| 277 | // Data ::= DATA-TLV TLV-LENGTH |
| 278 | // Name |
| 279 | // MetaInfo |
| 280 | // Content |
| 281 | // Signature |
| 282 | |
| 283 | // Name |
| 284 | m_name.wireDecode(m_wire.get(Tlv::Name)); |
| 285 | |
| 286 | // MetaInfo |
| 287 | m_metaInfo.wireDecode(m_wire.get(Tlv::MetaInfo)); |
| 288 | |
| 289 | // Content |
| 290 | m_content = m_wire.get(Tlv::Content); |
| 291 | |
| 292 | /////////////// |
| 293 | // Signature // |
| 294 | /////////////// |
| 295 | |
| 296 | // SignatureInfo |
| 297 | m_signature.setInfo(m_wire.get(Tlv::SignatureInfo)); |
| 298 | |
| 299 | // SignatureValue |
| 300 | Block::element_const_iterator val = m_wire.find(Tlv::SignatureValue); |
| 301 | if (val != m_wire.elements_end()) |
| 302 | m_signature.setValue(*val); |
| 303 | } |
| 304 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 305 | inline const Name& |
| 306 | Data::getName() const |
| 307 | { |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame^] | 308 | return m_name; |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | inline void |
| 312 | Data::setName(const Name& name) |
| 313 | { |
| 314 | onChanged(); |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame^] | 315 | m_name = name; |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | inline const MetaInfo& |
| 319 | Data::getMetaInfo() const |
| 320 | { |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame^] | 321 | return m_metaInfo; |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | inline void |
| 325 | Data::setMetaInfo(const MetaInfo& metaInfo) |
| 326 | { |
| 327 | onChanged(); |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame^] | 328 | m_metaInfo = metaInfo; |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | inline uint32_t |
| 332 | Data::getContentType() const |
| 333 | { |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame^] | 334 | return m_metaInfo.getType(); |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | inline void |
| 338 | Data::setContentType(uint32_t type) |
| 339 | { |
| 340 | onChanged(); |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame^] | 341 | m_metaInfo.setType(type); |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | inline Milliseconds |
| 345 | Data::getFreshnessPeriod() const |
| 346 | { |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame^] | 347 | return m_metaInfo.getFreshnessPeriod(); |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | inline void |
| 351 | Data::setFreshnessPeriod(Milliseconds freshnessPeriod) |
| 352 | { |
| 353 | onChanged(); |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame^] | 354 | m_metaInfo.setFreshnessPeriod(freshnessPeriod); |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 355 | } |
| 356 | |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 357 | inline const name::Component& |
| 358 | Data::getFinalBlockId() const |
| 359 | { |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame^] | 360 | return m_metaInfo.getFinalBlockId(); |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | inline void |
| 364 | Data::setFinalBlockId(const name::Component& finalBlockId) |
| 365 | { |
| 366 | onChanged(); |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame^] | 367 | m_metaInfo.setFinalBlockId(finalBlockId); |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 368 | } |
| 369 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 370 | inline const Block& |
| 371 | Data::getContent() const |
| 372 | { |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame^] | 373 | if (m_content.empty()) |
| 374 | m_content = dataBlock(Tlv::Content, reinterpret_cast<const uint8_t*>(0), 0); |
Alexander Afanasyev | 196b9aa | 2014-01-31 17:19:16 -0800 | [diff] [blame] | 375 | |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame^] | 376 | if (!m_content.hasWire()) |
| 377 | m_content.encode(); |
| 378 | return m_content; |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | inline void |
| 382 | Data::setContent(const uint8_t* content, size_t contentLength) |
| 383 | { |
| 384 | onChanged(); |
| 385 | |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame^] | 386 | m_content = dataBlock(Tlv::Content, content, contentLength); |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | inline void |
| 390 | Data::setContent(const ConstBufferPtr &contentValue) |
| 391 | { |
| 392 | onChanged(); |
| 393 | |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame^] | 394 | m_content = Block(Tlv::Content, contentValue); // not real a wire encoding yet |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | inline void |
| 398 | Data::setContent(const Block& content) |
Alexander Afanasyev | e0c02f5 | 2013-12-28 20:44:25 -0800 | [diff] [blame] | 399 | { |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 400 | onChanged(); |
| 401 | |
Alexander Afanasyev | e0c02f5 | 2013-12-28 20:44:25 -0800 | [diff] [blame] | 402 | if (content.type() == Tlv::Content) |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame^] | 403 | m_content = content; |
Alexander Afanasyev | e0c02f5 | 2013-12-28 20:44:25 -0800 | [diff] [blame] | 404 | else { |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame^] | 405 | m_content = Block(Tlv::Content, content); |
Alexander Afanasyev | e0c02f5 | 2013-12-28 20:44:25 -0800 | [diff] [blame] | 406 | } |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | inline const Signature& |
| 410 | Data::getSignature() const |
| 411 | { |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame^] | 412 | return m_signature; |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 413 | } |
| 414 | |
Alexander Afanasyev | ad39b18 | 2014-01-03 15:38:58 -0800 | [diff] [blame] | 415 | inline void |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 416 | Data::setSignature(const Signature& signature) |
| 417 | { |
| 418 | onChanged(); |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame^] | 419 | m_signature = signature; |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 420 | } |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 421 | |
Alexander Afanasyev | ad39b18 | 2014-01-03 15:38:58 -0800 | [diff] [blame] | 422 | inline void |
| 423 | Data::setSignatureValue(const Block &value) |
| 424 | { |
| 425 | onChanged(); |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame^] | 426 | m_signature.setValue(value); |
Alexander Afanasyev | ad39b18 | 2014-01-03 15:38:58 -0800 | [diff] [blame] | 427 | } |
| 428 | |
Yingdi Yu | a4e5767 | 2014-02-06 11:16:17 -0800 | [diff] [blame] | 429 | inline uint64_t |
| 430 | Data::getIncomingFaceId() const |
| 431 | { |
| 432 | return m_incomingFaceId; |
| 433 | } |
| 434 | |
| 435 | inline void |
| 436 | Data::setIncomingFaceId(uint64_t incomingFaceId) |
| 437 | { |
| 438 | m_incomingFaceId = incomingFaceId; |
| 439 | } |
Alexander Afanasyev | ad39b18 | 2014-01-03 15:38:58 -0800 | [diff] [blame] | 440 | |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 441 | inline void |
| 442 | Data::onChanged() |
| 443 | { |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 444 | // The values have changed, so the wire format is invalidated |
| 445 | |
| 446 | // !!!Note!!! Signature is not invalidated and it is responsibility of |
| 447 | // the application to do proper re-signing if necessary |
| 448 | |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame^] | 449 | m_wire.reset(); |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 450 | } |
| 451 | |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame^] | 452 | inline std::ostream& |
| 453 | operator << (std::ostream &os, const Data &data) |
| 454 | { |
| 455 | os << "Name: " << data.getName() << "\n"; |
| 456 | os << "MetaInfo: " << data.getMetaInfo() << "\n"; |
| 457 | os << "Content: (size: " << data.getContent().value_size() << ")\n"; |
| 458 | os << "Signature: (type: " << data.getSignature().getType() << |
| 459 | ", value_length: "<< data.getSignature().getValue().value_size() << ")"; |
| 460 | os << std::endl; |
| 461 | |
| 462 | return os; |
| 463 | } |
Alexander Afanasyev | 4ff3c91 | 2014-01-03 15:25:02 -0800 | [diff] [blame] | 464 | |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 465 | } // namespace ndn |
| 466 | |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 467 | #endif |