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