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 | * |
| 64 | * This is virtual so that a subclass can override to validate the name. |
| 65 | * |
Jeff Thompson | 0cd8c4a | 2013-09-13 17:46:40 -0700 | [diff] [blame] | 66 | * @param name The Name which is copied. |
Jeff Thompson | 6d59197 | 2013-10-17 11:16:32 -0700 | [diff] [blame] | 67 | * @return This Data so that you can chain calls to update values. |
Jeff Thompson | 0cd8c4a | 2013-09-13 17:46:40 -0700 | [diff] [blame] | 68 | */ |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 69 | inline virtual void |
| 70 | setName(const Name& name); |
| 71 | |
| 72 | inline const MetaInfo& |
| 73 | getMetaInfo() const; |
Jeff Thompson | 46bd45f | 2013-08-08 16:46:41 -0700 | [diff] [blame] | 74 | |
Jeff Thompson | 0cd8c4a | 2013-09-13 17:46:40 -0700 | [diff] [blame] | 75 | /** |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 76 | * @brief Set metaInfo to a copy of the given MetaInfo. |
Jeff Thompson | 0cd8c4a | 2013-09-13 17:46:40 -0700 | [diff] [blame] | 77 | * @param metaInfo The MetaInfo which is copied. |
Jeff Thompson | 6d59197 | 2013-10-17 11:16:32 -0700 | [diff] [blame] | 78 | * @return This Data so that you can chain calls to update values. |
Jeff Thompson | 0cd8c4a | 2013-09-13 17:46:40 -0700 | [diff] [blame] | 79 | */ |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 80 | inline void |
| 81 | setMetaInfo(const MetaInfo& metaInfo); |
Jeff Thompson | 46bd45f | 2013-08-08 16:46:41 -0700 | [diff] [blame] | 82 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 83 | /////////////////////////////////////////////////////////////// |
| 84 | // MetaInfo proxy methods |
| 85 | inline uint32_t |
| 86 | getContentType() const; |
| 87 | |
| 88 | inline void |
| 89 | setContentType(uint32_t type); |
| 90 | |
| 91 | inline Milliseconds |
| 92 | getFreshnessPeriod() const; |
| 93 | |
| 94 | inline void |
| 95 | setFreshnessPeriod(Milliseconds freshnessPeriod); |
| 96 | |
| 97 | /** |
| 98 | * @brief Get content Block |
| 99 | * |
| 100 | * To access content value, one can use value()/value_size() or |
| 101 | * value_begin()/value_end() methods of the Block class |
| 102 | */ |
| 103 | inline const Block& |
| 104 | getContent() const; |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 105 | |
Jeff Thompson | 0899c0f | 2013-09-12 12:15:31 -0700 | [diff] [blame] | 106 | /** |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 107 | * @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] | 108 | * @param content A vector whose contents are copied. |
Jeff Thompson | 6d59197 | 2013-10-17 11:16:32 -0700 | [diff] [blame] | 109 | * @return This Data so that you can chain calls to update values. |
Jeff Thompson | 0899c0f | 2013-09-12 12:15:31 -0700 | [diff] [blame] | 110 | */ |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 111 | inline void |
| 112 | setContent(const uint8_t* content, size_t contentLength); |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 113 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 114 | inline void |
| 115 | setContent(const Block& content); |
| 116 | |
| 117 | inline void |
| 118 | setContent(const ConstBufferPtr &contentValue); |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 119 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 120 | inline const Signature& |
| 121 | getSignature() const; |
| 122 | |
| 123 | /** |
| 124 | * @brief Set the signature to a copy of the given signature. |
| 125 | * @param signature The signature object which is cloned. |
| 126 | * @return This Data so that you can chain calls to update values. |
| 127 | */ |
| 128 | inline Data& |
| 129 | setSignature(const Signature& signature); |
Jeff Thompson | c2b7b14 | 2013-09-12 15:29:04 -0700 | [diff] [blame] | 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() |
| 149 | { |
| 150 | } |
| 151 | |
| 152 | inline |
| 153 | Data::Data(const Name& name) |
| 154 | : name_(name) |
| 155 | { |
| 156 | } |
| 157 | |
| 158 | inline |
| 159 | Data::~Data() |
| 160 | { |
| 161 | } |
| 162 | |
| 163 | inline const Name& |
| 164 | Data::getName() const |
| 165 | { |
| 166 | return name_; |
| 167 | } |
| 168 | |
| 169 | inline void |
| 170 | Data::setName(const Name& name) |
| 171 | { |
| 172 | onChanged(); |
| 173 | name_ = name; |
| 174 | } |
| 175 | |
| 176 | inline const MetaInfo& |
| 177 | Data::getMetaInfo() const |
| 178 | { |
| 179 | return metaInfo_; |
| 180 | } |
| 181 | |
| 182 | inline void |
| 183 | Data::setMetaInfo(const MetaInfo& metaInfo) |
| 184 | { |
| 185 | onChanged(); |
| 186 | metaInfo_ = metaInfo; |
| 187 | } |
| 188 | |
| 189 | inline uint32_t |
| 190 | Data::getContentType() const |
| 191 | { |
| 192 | return metaInfo_.getType(); |
| 193 | } |
| 194 | |
| 195 | inline void |
| 196 | Data::setContentType(uint32_t type) |
| 197 | { |
| 198 | onChanged(); |
| 199 | metaInfo_.setType(type); |
| 200 | } |
| 201 | |
| 202 | inline Milliseconds |
| 203 | Data::getFreshnessPeriod() const |
| 204 | { |
| 205 | return metaInfo_.getFreshnessPeriod(); |
| 206 | } |
| 207 | |
| 208 | inline void |
| 209 | Data::setFreshnessPeriod(Milliseconds freshnessPeriod) |
| 210 | { |
| 211 | onChanged(); |
| 212 | metaInfo_.setFreshnessPeriod(freshnessPeriod); |
| 213 | } |
| 214 | |
| 215 | inline const Block& |
| 216 | Data::getContent() const |
| 217 | { |
| 218 | if (!content_.hasWire()) |
| 219 | content_.encode(); |
| 220 | return content_; |
| 221 | } |
| 222 | |
| 223 | inline void |
| 224 | Data::setContent(const uint8_t* content, size_t contentLength) |
| 225 | { |
| 226 | onChanged(); |
| 227 | |
| 228 | OBufferStream os; |
| 229 | Tlv::writeVarNumber(os, Tlv::Content); |
| 230 | Tlv::writeVarNumber(os, contentLength); |
| 231 | os.write(reinterpret_cast<const char *>(content), contentLength); |
| 232 | |
| 233 | content_ = Block(os.buf()); |
| 234 | } |
| 235 | |
| 236 | inline void |
| 237 | Data::setContent(const ConstBufferPtr &contentValue) |
| 238 | { |
| 239 | onChanged(); |
| 240 | |
| 241 | content_ = Block(Tlv::Content, contentValue); // not real a wire encoding yet |
| 242 | } |
| 243 | |
| 244 | inline void |
| 245 | Data::setContent(const Block& content) |
| 246 | { |
| 247 | onChanged(); |
| 248 | |
| 249 | content_ = content; |
| 250 | } |
| 251 | |
| 252 | inline const Signature& |
| 253 | Data::getSignature() const |
| 254 | { |
| 255 | return signature_; |
| 256 | } |
| 257 | |
| 258 | inline Data& |
| 259 | Data::setSignature(const Signature& signature) |
| 260 | { |
| 261 | onChanged(); |
| 262 | signature_ = signature; |
| 263 | |
| 264 | return *this; |
| 265 | } |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 266 | |
| 267 | inline void |
| 268 | Data::onChanged() |
| 269 | { |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 270 | // The values have changed, so the wire format is invalidated |
| 271 | |
| 272 | // !!!Note!!! Signature is not invalidated and it is responsibility of |
| 273 | // the application to do proper re-signing if necessary |
| 274 | |
| 275 | wire_ = Block(); |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 276 | } |
| 277 | |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 278 | } // namespace ndn |
| 279 | |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 280 | #endif |