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