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