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 | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 40 | * @brief The virtual destructor. |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 41 | */ |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 42 | inline virtual |
| 43 | ~Data(); |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 44 | |
| 45 | /** |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 46 | * @brief Encode this Data for a wire format. |
Jeff Thompson | b7aefa00 | 2013-09-16 18:22:00 -0700 | [diff] [blame] | 47 | * @return The encoded byte array. |
| 48 | */ |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 49 | const Block& |
| 50 | wireEncode() const; |
| 51 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 52 | /** |
| 53 | * @brief Decode the input using a particular wire format and update this Data. |
| 54 | * @param input The input byte array to be decoded. |
| 55 | */ |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 56 | void |
| 57 | wireDecode(const Block &wire); |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 58 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 59 | inline const Name& |
| 60 | getName() const; |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 61 | |
| 62 | /** |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 63 | * @brief Set name to a copy of the given Name. |
| 64 | * |
Jeff Thompson | 0cd8c4a | 2013-09-13 17:46:40 -0700 | [diff] [blame] | 65 | * @param name The Name which is copied. |
Jeff Thompson | 6d59197 | 2013-10-17 11:16:32 -0700 | [diff] [blame] | 66 | * @return This Data so that you can chain calls to update values. |
Jeff Thompson | 0cd8c4a | 2013-09-13 17:46:40 -0700 | [diff] [blame] | 67 | */ |
Alexander Afanasyev | a61757b | 2014-01-03 15:09:29 -0800 | [diff] [blame] | 68 | inline void |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 69 | setName(const Name& name); |
| 70 | |
| 71 | inline const MetaInfo& |
| 72 | getMetaInfo() const; |
Jeff Thompson | 46bd45f | 2013-08-08 16:46:41 -0700 | [diff] [blame] | 73 | |
Jeff Thompson | 0cd8c4a | 2013-09-13 17:46:40 -0700 | [diff] [blame] | 74 | /** |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 75 | * @brief Set metaInfo to a copy of the given MetaInfo. |
Jeff Thompson | 0cd8c4a | 2013-09-13 17:46:40 -0700 | [diff] [blame] | 76 | * @param metaInfo The MetaInfo which is copied. |
Jeff Thompson | 6d59197 | 2013-10-17 11:16:32 -0700 | [diff] [blame] | 77 | * @return This Data so that you can chain calls to update values. |
Jeff Thompson | 0cd8c4a | 2013-09-13 17:46:40 -0700 | [diff] [blame] | 78 | */ |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 79 | inline void |
| 80 | setMetaInfo(const MetaInfo& metaInfo); |
Jeff Thompson | 46bd45f | 2013-08-08 16:46:41 -0700 | [diff] [blame] | 81 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 82 | /////////////////////////////////////////////////////////////// |
| 83 | // MetaInfo proxy methods |
| 84 | inline uint32_t |
| 85 | getContentType() const; |
| 86 | |
| 87 | inline void |
| 88 | setContentType(uint32_t type); |
| 89 | |
| 90 | inline Milliseconds |
| 91 | getFreshnessPeriod() const; |
| 92 | |
| 93 | inline void |
| 94 | setFreshnessPeriod(Milliseconds freshnessPeriod); |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame^] | 95 | |
| 96 | inline const name::Component& |
| 97 | getFinalBlockId() const; |
| 98 | |
| 99 | inline void |
| 100 | setFinalBlockId(const name::Component& finalBlockId); |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 101 | |
| 102 | /** |
| 103 | * @brief Get content Block |
| 104 | * |
| 105 | * To access content value, one can use value()/value_size() or |
| 106 | * value_begin()/value_end() methods of the Block class |
| 107 | */ |
| 108 | inline const Block& |
| 109 | getContent() const; |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 110 | |
Jeff Thompson | 0899c0f | 2013-09-12 12:15:31 -0700 | [diff] [blame] | 111 | /** |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 112 | * @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] | 113 | * @param content A vector whose contents are copied. |
Jeff Thompson | 6d59197 | 2013-10-17 11:16:32 -0700 | [diff] [blame] | 114 | * @return This Data so that you can chain calls to update values. |
Jeff Thompson | 0899c0f | 2013-09-12 12:15:31 -0700 | [diff] [blame] | 115 | */ |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 116 | inline void |
| 117 | setContent(const uint8_t* content, size_t contentLength); |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 118 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 119 | inline void |
| 120 | setContent(const Block& content); |
| 121 | |
| 122 | inline void |
| 123 | setContent(const ConstBufferPtr &contentValue); |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 124 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 125 | inline const Signature& |
| 126 | getSignature() const; |
| 127 | |
| 128 | /** |
| 129 | * @brief Set the signature to a copy of the given signature. |
| 130 | * @param signature The signature object which is cloned. |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 131 | */ |
Alexander Afanasyev | ad39b18 | 2014-01-03 15:38:58 -0800 | [diff] [blame] | 132 | inline void |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 133 | setSignature(const Signature& signature); |
Jeff Thompson | c2b7b14 | 2013-09-12 15:29:04 -0700 | [diff] [blame] | 134 | |
Alexander Afanasyev | ad39b18 | 2014-01-03 15:38:58 -0800 | [diff] [blame] | 135 | inline void |
| 136 | setSignatureValue(const Block &value); |
Yingdi Yu | a4e5767 | 2014-02-06 11:16:17 -0800 | [diff] [blame] | 137 | |
| 138 | inline uint64_t |
| 139 | getIncomingFaceId() const; |
| 140 | |
| 141 | inline void |
| 142 | setIncomingFaceId(uint64_t incomingFaceId); |
Alexander Afanasyev | ad39b18 | 2014-01-03 15:38:58 -0800 | [diff] [blame] | 143 | |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 144 | private: |
Jeff Thompson | b7aefa00 | 2013-09-16 18:22:00 -0700 | [diff] [blame] | 145 | /** |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 146 | * @brief Clear the wire encoding. |
Jeff Thompson | b7aefa00 | 2013-09-16 18:22:00 -0700 | [diff] [blame] | 147 | */ |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 148 | inline void |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 149 | onChanged(); |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 150 | |
| 151 | private: |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 152 | Name name_; |
Jeff Thompson | fec716d | 2013-09-11 13:54:36 -0700 | [diff] [blame] | 153 | MetaInfo metaInfo_; |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 154 | mutable Block content_; |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 155 | Signature signature_; |
| 156 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 157 | mutable Block wire_; |
Yingdi Yu | a4e5767 | 2014-02-06 11:16:17 -0800 | [diff] [blame] | 158 | |
| 159 | uint64_t m_incomingFaceId; |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 160 | }; |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 161 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 162 | inline |
| 163 | Data::Data() |
Alexander Afanasyev | ec3cbad | 2013-12-30 17:06:45 -0800 | [diff] [blame] | 164 | : content_(Tlv::Content) // empty content |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 165 | { |
| 166 | } |
| 167 | |
| 168 | inline |
| 169 | Data::Data(const Name& name) |
| 170 | : name_(name) |
| 171 | { |
| 172 | } |
| 173 | |
| 174 | inline |
| 175 | Data::~Data() |
| 176 | { |
| 177 | } |
| 178 | |
| 179 | inline const Name& |
| 180 | Data::getName() const |
| 181 | { |
| 182 | return name_; |
| 183 | } |
| 184 | |
| 185 | inline void |
| 186 | Data::setName(const Name& name) |
| 187 | { |
| 188 | onChanged(); |
| 189 | name_ = name; |
| 190 | } |
| 191 | |
| 192 | inline const MetaInfo& |
| 193 | Data::getMetaInfo() const |
| 194 | { |
| 195 | return metaInfo_; |
| 196 | } |
| 197 | |
| 198 | inline void |
| 199 | Data::setMetaInfo(const MetaInfo& metaInfo) |
| 200 | { |
| 201 | onChanged(); |
| 202 | metaInfo_ = metaInfo; |
| 203 | } |
| 204 | |
| 205 | inline uint32_t |
| 206 | Data::getContentType() const |
| 207 | { |
| 208 | return metaInfo_.getType(); |
| 209 | } |
| 210 | |
| 211 | inline void |
| 212 | Data::setContentType(uint32_t type) |
| 213 | { |
| 214 | onChanged(); |
| 215 | metaInfo_.setType(type); |
| 216 | } |
| 217 | |
| 218 | inline Milliseconds |
| 219 | Data::getFreshnessPeriod() const |
| 220 | { |
| 221 | return metaInfo_.getFreshnessPeriod(); |
| 222 | } |
| 223 | |
| 224 | inline void |
| 225 | Data::setFreshnessPeriod(Milliseconds freshnessPeriod) |
| 226 | { |
| 227 | onChanged(); |
| 228 | metaInfo_.setFreshnessPeriod(freshnessPeriod); |
| 229 | } |
| 230 | |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame^] | 231 | inline const name::Component& |
| 232 | Data::getFinalBlockId() const |
| 233 | { |
| 234 | return metaInfo_.getFinalBlockId(); |
| 235 | } |
| 236 | |
| 237 | inline void |
| 238 | Data::setFinalBlockId(const name::Component& finalBlockId) |
| 239 | { |
| 240 | onChanged(); |
| 241 | metaInfo_.setFinalBlockId(finalBlockId); |
| 242 | } |
| 243 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 244 | inline const Block& |
| 245 | Data::getContent() const |
| 246 | { |
Alexander Afanasyev | 196b9aa | 2014-01-31 17:19:16 -0800 | [diff] [blame] | 247 | if (content_.empty()) |
| 248 | content_ = dataBlock(Tlv::Content, reinterpret_cast<const uint8_t*>(0), 0); |
| 249 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 250 | if (!content_.hasWire()) |
| 251 | content_.encode(); |
| 252 | return content_; |
| 253 | } |
| 254 | |
| 255 | inline void |
| 256 | Data::setContent(const uint8_t* content, size_t contentLength) |
| 257 | { |
| 258 | onChanged(); |
| 259 | |
Alexander Afanasyev | 277f469 | 2014-01-03 15:29:55 -0800 | [diff] [blame] | 260 | content_ = dataBlock(Tlv::Content, content, contentLength); |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | inline void |
| 264 | Data::setContent(const ConstBufferPtr &contentValue) |
| 265 | { |
| 266 | onChanged(); |
| 267 | |
| 268 | content_ = Block(Tlv::Content, contentValue); // not real a wire encoding yet |
| 269 | } |
| 270 | |
| 271 | inline void |
| 272 | Data::setContent(const Block& content) |
Alexander Afanasyev | e0c02f5 | 2013-12-28 20:44:25 -0800 | [diff] [blame] | 273 | { |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 274 | onChanged(); |
| 275 | |
Alexander Afanasyev | e0c02f5 | 2013-12-28 20:44:25 -0800 | [diff] [blame] | 276 | if (content.type() == Tlv::Content) |
| 277 | content_ = content; |
| 278 | else { |
| 279 | content_ = Block(Tlv::Content, content); |
| 280 | } |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | inline const Signature& |
| 284 | Data::getSignature() const |
| 285 | { |
| 286 | return signature_; |
| 287 | } |
| 288 | |
Alexander Afanasyev | ad39b18 | 2014-01-03 15:38:58 -0800 | [diff] [blame] | 289 | inline void |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 290 | Data::setSignature(const Signature& signature) |
| 291 | { |
| 292 | onChanged(); |
| 293 | signature_ = signature; |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 294 | } |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 295 | |
Alexander Afanasyev | ad39b18 | 2014-01-03 15:38:58 -0800 | [diff] [blame] | 296 | inline void |
| 297 | Data::setSignatureValue(const Block &value) |
| 298 | { |
| 299 | onChanged(); |
| 300 | signature_.setValue(value); |
| 301 | } |
| 302 | |
Yingdi Yu | a4e5767 | 2014-02-06 11:16:17 -0800 | [diff] [blame] | 303 | inline uint64_t |
| 304 | Data::getIncomingFaceId() const |
| 305 | { |
| 306 | return m_incomingFaceId; |
| 307 | } |
| 308 | |
| 309 | inline void |
| 310 | Data::setIncomingFaceId(uint64_t incomingFaceId) |
| 311 | { |
| 312 | m_incomingFaceId = incomingFaceId; |
| 313 | } |
Alexander Afanasyev | ad39b18 | 2014-01-03 15:38:58 -0800 | [diff] [blame] | 314 | |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 315 | inline void |
| 316 | Data::onChanged() |
| 317 | { |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 318 | // The values have changed, so the wire format is invalidated |
| 319 | |
| 320 | // !!!Note!!! Signature is not invalidated and it is responsibility of |
| 321 | // the application to do proper re-signing if necessary |
| 322 | |
Alexander Afanasyev | ad39b18 | 2014-01-03 15:38:58 -0800 | [diff] [blame] | 323 | wire_.reset(); |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 324 | } |
| 325 | |
Alexander Afanasyev | 4ff3c91 | 2014-01-03 15:25:02 -0800 | [diff] [blame] | 326 | std::ostream& |
| 327 | operator << (std::ostream &os, const Data &data); |
| 328 | |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 329 | } // namespace ndn |
| 330 | |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 331 | #endif |