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); |
| 130 | |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 131 | private: |
Jeff Thompson | b7aefa00 | 2013-09-16 18:22:00 -0700 | [diff] [blame] | 132 | /** |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 133 | * @brief Clear the wire encoding. |
Jeff Thompson | b7aefa00 | 2013-09-16 18:22:00 -0700 | [diff] [blame] | 134 | */ |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 135 | inline void |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 136 | onChanged(); |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 137 | |
| 138 | private: |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 139 | Name name_; |
Jeff Thompson | fec716d | 2013-09-11 13:54:36 -0700 | [diff] [blame] | 140 | MetaInfo metaInfo_; |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 141 | mutable Block content_; |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 142 | Signature signature_; |
| 143 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 144 | mutable Block wire_; |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 145 | }; |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 146 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 147 | inline |
| 148 | Data::Data() |
Alexander Afanasyev | ec3cbad | 2013-12-30 17:06:45 -0800 | [diff] [blame] | 149 | : content_(Tlv::Content) // empty content |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 150 | { |
| 151 | } |
| 152 | |
| 153 | inline |
| 154 | Data::Data(const Name& name) |
| 155 | : name_(name) |
| 156 | { |
| 157 | } |
| 158 | |
| 159 | inline |
| 160 | Data::~Data() |
| 161 | { |
| 162 | } |
| 163 | |
| 164 | inline const Name& |
| 165 | Data::getName() const |
| 166 | { |
| 167 | return name_; |
| 168 | } |
| 169 | |
| 170 | inline void |
| 171 | Data::setName(const Name& name) |
| 172 | { |
| 173 | onChanged(); |
| 174 | name_ = name; |
| 175 | } |
| 176 | |
| 177 | inline const MetaInfo& |
| 178 | Data::getMetaInfo() const |
| 179 | { |
| 180 | return metaInfo_; |
| 181 | } |
| 182 | |
| 183 | inline void |
| 184 | Data::setMetaInfo(const MetaInfo& metaInfo) |
| 185 | { |
| 186 | onChanged(); |
| 187 | metaInfo_ = metaInfo; |
| 188 | } |
| 189 | |
| 190 | inline uint32_t |
| 191 | Data::getContentType() const |
| 192 | { |
| 193 | return metaInfo_.getType(); |
| 194 | } |
| 195 | |
| 196 | inline void |
| 197 | Data::setContentType(uint32_t type) |
| 198 | { |
| 199 | onChanged(); |
| 200 | metaInfo_.setType(type); |
| 201 | } |
| 202 | |
| 203 | inline Milliseconds |
| 204 | Data::getFreshnessPeriod() const |
| 205 | { |
| 206 | return metaInfo_.getFreshnessPeriod(); |
| 207 | } |
| 208 | |
| 209 | inline void |
| 210 | Data::setFreshnessPeriod(Milliseconds freshnessPeriod) |
| 211 | { |
| 212 | onChanged(); |
| 213 | metaInfo_.setFreshnessPeriod(freshnessPeriod); |
| 214 | } |
| 215 | |
| 216 | inline const Block& |
| 217 | Data::getContent() const |
| 218 | { |
Alexander Afanasyev | 196b9aa | 2014-01-31 17:19:16 -0800 | [diff] [blame^] | 219 | if (content_.empty()) |
| 220 | content_ = dataBlock(Tlv::Content, reinterpret_cast<const uint8_t*>(0), 0); |
| 221 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 222 | if (!content_.hasWire()) |
| 223 | content_.encode(); |
| 224 | return content_; |
| 225 | } |
| 226 | |
| 227 | inline void |
| 228 | Data::setContent(const uint8_t* content, size_t contentLength) |
| 229 | { |
| 230 | onChanged(); |
| 231 | |
Alexander Afanasyev | 277f469 | 2014-01-03 15:29:55 -0800 | [diff] [blame] | 232 | content_ = dataBlock(Tlv::Content, content, contentLength); |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | inline void |
| 236 | Data::setContent(const ConstBufferPtr &contentValue) |
| 237 | { |
| 238 | onChanged(); |
| 239 | |
| 240 | content_ = Block(Tlv::Content, contentValue); // not real a wire encoding yet |
| 241 | } |
| 242 | |
| 243 | inline void |
| 244 | Data::setContent(const Block& content) |
Alexander Afanasyev | e0c02f5 | 2013-12-28 20:44:25 -0800 | [diff] [blame] | 245 | { |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 246 | onChanged(); |
| 247 | |
Alexander Afanasyev | e0c02f5 | 2013-12-28 20:44:25 -0800 | [diff] [blame] | 248 | if (content.type() == Tlv::Content) |
| 249 | content_ = content; |
| 250 | else { |
| 251 | content_ = Block(Tlv::Content, content); |
| 252 | } |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | inline const Signature& |
| 256 | Data::getSignature() const |
| 257 | { |
| 258 | return signature_; |
| 259 | } |
| 260 | |
Alexander Afanasyev | ad39b18 | 2014-01-03 15:38:58 -0800 | [diff] [blame] | 261 | inline void |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 262 | Data::setSignature(const Signature& signature) |
| 263 | { |
| 264 | onChanged(); |
| 265 | signature_ = signature; |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 266 | } |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 267 | |
Alexander Afanasyev | ad39b18 | 2014-01-03 15:38:58 -0800 | [diff] [blame] | 268 | inline void |
| 269 | Data::setSignatureValue(const Block &value) |
| 270 | { |
| 271 | onChanged(); |
| 272 | signature_.setValue(value); |
| 273 | } |
| 274 | |
| 275 | |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 276 | inline void |
| 277 | Data::onChanged() |
| 278 | { |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 279 | // The values have changed, so the wire format is invalidated |
| 280 | |
| 281 | // !!!Note!!! Signature is not invalidated and it is responsibility of |
| 282 | // the application to do proper re-signing if necessary |
| 283 | |
Alexander Afanasyev | ad39b18 | 2014-01-03 15:38:58 -0800 | [diff] [blame] | 284 | wire_.reset(); |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 285 | } |
| 286 | |
Alexander Afanasyev | 4ff3c91 | 2014-01-03 15:25:02 -0800 | [diff] [blame] | 287 | std::ostream& |
| 288 | operator << (std::ostream &os, const Data &data); |
| 289 | |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 290 | } // namespace ndn |
| 291 | |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 292 | #endif |