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. |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
Jeff Thompson | 56ec9e2 | 2013-08-02 11:34:07 -0700 | [diff] [blame] | 7 | #ifndef NDN_DATA_HPP |
Jeff Thompson | a0d18c9 | 2013-08-06 13:55:32 -0700 | [diff] [blame] | 8 | #define NDN_DATA_HPP |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 9 | |
Jeff Thompson | 46bd45f | 2013-08-08 16:46:41 -0700 | [diff] [blame] | 10 | #include "common.hpp" |
Jeff Thompson | 5341219 | 2013-08-06 13:35:50 -0700 | [diff] [blame] | 11 | #include "name.hpp" |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 12 | #include "encoding/block.hpp" |
Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 13 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 14 | #include "signature.hpp" |
| 15 | #include "meta-info.hpp" |
| 16 | #include "key-locator.hpp" |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 17 | #include "management/nfd-local-control-header.hpp" |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 18 | |
| 19 | namespace ndn { |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -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 | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 24 | class Error : public std::runtime_error |
| 25 | { |
| 26 | public: |
| 27 | explicit |
| 28 | Error(const std::string& what) |
| 29 | : std::runtime_error(what) |
| 30 | { |
| 31 | } |
| 32 | }; |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 33 | |
Jeff Thompson | 20af073 | 2013-09-12 17:01:45 -0700 | [diff] [blame] | 34 | /** |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 35 | * @brief Create an empty Data object |
Jeff Thompson | 20af073 | 2013-09-12 17:01:45 -0700 | [diff] [blame] | 36 | */ |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 37 | Data(); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 38 | |
Jeff Thompson | 20af073 | 2013-09-12 17:01:45 -0700 | [diff] [blame] | 39 | /** |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 40 | * @brief Create a new Data object with the given name |
Jeff Thompson | 20af073 | 2013-09-12 17:01:45 -0700 | [diff] [blame] | 41 | * @param name A reference to the name which is copied. |
| 42 | */ |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 43 | Data(const Name& name); |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 44 | |
| 45 | /** |
| 46 | * @brief Create a new Data object from wire encoding |
| 47 | */ |
| 48 | explicit |
| 49 | Data(const Block& wire) |
| 50 | { |
| 51 | wireDecode(wire); |
| 52 | } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 53 | |
Jeff Thompson | 25bfdca | 2013-10-16 17:05:41 -0700 | [diff] [blame] | 54 | /** |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 55 | * @brief The destructor |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 56 | */ |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 57 | ~Data(); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 58 | |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 59 | /** |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 60 | * @brief Fast encoding or block size estimation |
Jeff Thompson | b7aefa00 | 2013-09-16 18:22:00 -0700 | [diff] [blame] | 61 | */ |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 62 | template<bool T> |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 63 | size_t |
| 64 | wireEncode(EncodingImpl<T>& block, bool unsignedPortion = false) const; |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 65 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 66 | /** |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 67 | * @brief Encode to a wire format |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 68 | */ |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 69 | const Block& |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 70 | wireEncode() const; |
| 71 | |
| 72 | /** |
| 73 | * @brief Decode from the wire format |
| 74 | */ |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 75 | void |
| 76 | wireDecode(const Block& wire); |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 77 | |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 78 | /** |
| 79 | * @brief Check if already has wire |
| 80 | */ |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 81 | bool |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 82 | hasWire() const; |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 83 | |
| 84 | //////////////////////////////////////////////////////////////////// |
| 85 | |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 86 | const Name& |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 87 | getName() const; |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 88 | |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 89 | /** |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 90 | * @brief Set name to a copy of the given Name. |
| 91 | * |
Jeff Thompson | 0cd8c4a | 2013-09-13 17:46:40 -0700 | [diff] [blame] | 92 | * @param name The Name which is copied. |
Jeff Thompson | 6d59197 | 2013-10-17 11:16:32 -0700 | [diff] [blame] | 93 | * @return This Data so that you can chain calls to update values. |
Jeff Thompson | 0cd8c4a | 2013-09-13 17:46:40 -0700 | [diff] [blame] | 94 | */ |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 95 | void |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 96 | setName(const Name& name); |
| 97 | |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 98 | // |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 99 | |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 100 | const MetaInfo& |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 101 | getMetaInfo() const; |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 102 | |
Jeff Thompson | 0cd8c4a | 2013-09-13 17:46:40 -0700 | [diff] [blame] | 103 | /** |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 104 | * @brief Set metaInfo to a copy of the given MetaInfo. |
Jeff Thompson | 0cd8c4a | 2013-09-13 17:46:40 -0700 | [diff] [blame] | 105 | * @param metaInfo The MetaInfo which is copied. |
Jeff Thompson | 6d59197 | 2013-10-17 11:16:32 -0700 | [diff] [blame] | 106 | * @return This Data so that you can chain calls to update values. |
Jeff Thompson | 0cd8c4a | 2013-09-13 17:46:40 -0700 | [diff] [blame] | 107 | */ |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 108 | void |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 109 | setMetaInfo(const MetaInfo& metaInfo); |
Jeff Thompson | 46bd45f | 2013-08-08 16:46:41 -0700 | [diff] [blame] | 110 | |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 111 | // |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 112 | |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 113 | /////////////////////////////////////////////////////////////// |
| 114 | /////////////////////////////////////////////////////////////// |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 115 | /////////////////////////////////////////////////////////////// |
| 116 | // MetaInfo proxy methods |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 117 | |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 118 | uint32_t |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 119 | getContentType() const; |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 120 | |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 121 | void |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 122 | setContentType(uint32_t type); |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 123 | |
| 124 | // |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 125 | |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 126 | const time::milliseconds& |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 127 | getFreshnessPeriod() const; |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 128 | |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 129 | void |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 130 | setFreshnessPeriod(const time::milliseconds& freshnessPeriod); |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 131 | |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 132 | // |
| 133 | |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 134 | const name::Component& |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 135 | getFinalBlockId() const; |
| 136 | |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 137 | void |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 138 | setFinalBlockId(const name::Component& finalBlockId); |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 139 | |
| 140 | // |
| 141 | /////////////////////////////////////////////////////////////// |
| 142 | /////////////////////////////////////////////////////////////// |
| 143 | /////////////////////////////////////////////////////////////// |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 144 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 145 | /** |
| 146 | * @brief Get content Block |
| 147 | * |
| 148 | * To access content value, one can use value()/value_size() or |
| 149 | * value_begin()/value_end() methods of the Block class |
| 150 | */ |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 151 | const Block& |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 152 | getContent() const; |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 153 | |
Jeff Thompson | 0899c0f | 2013-09-12 12:15:31 -0700 | [diff] [blame] | 154 | /** |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 155 | * @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] | 156 | * @param content A vector whose contents are copied. |
Jeff Thompson | 6d59197 | 2013-10-17 11:16:32 -0700 | [diff] [blame] | 157 | * @return This Data so that you can chain calls to update values. |
Jeff Thompson | 0899c0f | 2013-09-12 12:15:31 -0700 | [diff] [blame] | 158 | */ |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 159 | void |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 160 | setContent(const uint8_t* content, size_t contentLength); |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 161 | |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 162 | void |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 163 | setContent(const Block& content); |
| 164 | |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 165 | void |
| 166 | setContent(const ConstBufferPtr& contentValue); |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 167 | |
| 168 | // |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 169 | |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 170 | const Signature& |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 171 | getSignature() const; |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 172 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 173 | /** |
| 174 | * @brief Set the signature to a copy of the given signature. |
| 175 | * @param signature The signature object which is cloned. |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 176 | */ |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 177 | void |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 178 | setSignature(const Signature& signature); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 179 | |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 180 | void |
| 181 | setSignatureValue(const Block& value); |
Yingdi Yu | a4e5767 | 2014-02-06 11:16:17 -0800 | [diff] [blame] | 182 | |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 183 | /////////////////////////////////////////////////////////////// |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 184 | |
| 185 | nfd::LocalControlHeader& |
| 186 | getLocalControlHeader(); |
| 187 | |
| 188 | const nfd::LocalControlHeader& |
| 189 | getLocalControlHeader() const; |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 190 | |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 191 | uint64_t |
Yingdi Yu | a4e5767 | 2014-02-06 11:16:17 -0800 | [diff] [blame] | 192 | getIncomingFaceId() const; |
| 193 | |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 194 | void |
Yingdi Yu | a4e5767 | 2014-02-06 11:16:17 -0800 | [diff] [blame] | 195 | setIncomingFaceId(uint64_t incomingFaceId); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 196 | |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 197 | public: // EqualityComparable concept |
| 198 | bool |
| 199 | operator==(const Data& other) const; |
| 200 | |
| 201 | bool |
| 202 | operator!=(const Data& other) const; |
| 203 | |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 204 | private: |
Jeff Thompson | b7aefa00 | 2013-09-16 18:22:00 -0700 | [diff] [blame] | 205 | /** |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 206 | * @brief Clear the wire encoding. |
Jeff Thompson | b7aefa00 | 2013-09-16 18:22:00 -0700 | [diff] [blame] | 207 | */ |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 208 | void |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 209 | onChanged(); |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 210 | |
| 211 | private: |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 212 | Name m_name; |
| 213 | MetaInfo m_metaInfo; |
| 214 | mutable Block m_content; |
| 215 | Signature m_signature; |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 216 | |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 217 | mutable Block m_wire; |
Yingdi Yu | a4e5767 | 2014-02-06 11:16:17 -0800 | [diff] [blame] | 218 | |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 219 | nfd::LocalControlHeader m_localControlHeader; |
| 220 | friend class nfd::LocalControlHeader; |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 221 | }; |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 222 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 223 | inline |
| 224 | Data::Data() |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 225 | : m_content(Tlv::Content) // empty content |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 226 | { |
| 227 | } |
| 228 | |
| 229 | inline |
| 230 | Data::Data(const Name& name) |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 231 | : m_name(name) |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 232 | { |
| 233 | } |
| 234 | |
| 235 | inline |
| 236 | Data::~Data() |
| 237 | { |
| 238 | } |
| 239 | |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 240 | template<bool T> |
| 241 | inline size_t |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 242 | Data::wireEncode(EncodingImpl<T>& block, bool unsignedPortion/* = false*/) const |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 243 | { |
| 244 | size_t total_len = 0; |
| 245 | |
| 246 | // Data ::= DATA-TLV TLV-LENGTH |
| 247 | // Name |
| 248 | // MetaInfo |
| 249 | // Content |
| 250 | // Signature |
| 251 | |
| 252 | // (reverse encoding) |
| 253 | |
| 254 | if (!unsignedPortion && !m_signature) |
| 255 | { |
| 256 | throw Error("Requested wire format, but data packet has not been signed yet"); |
| 257 | } |
| 258 | |
| 259 | if (!unsignedPortion) |
| 260 | { |
| 261 | // SignatureValue |
| 262 | total_len += prependBlock(block, m_signature.getValue()); |
| 263 | } |
| 264 | |
| 265 | // SignatureInfo |
| 266 | total_len += prependBlock(block, m_signature.getInfo()); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 267 | |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 268 | // Content |
| 269 | total_len += prependBlock(block, getContent()); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 270 | |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 271 | // MetaInfo |
| 272 | total_len += getMetaInfo().wireEncode(block); |
| 273 | |
| 274 | // Name |
| 275 | total_len += getName().wireEncode(block); |
| 276 | |
| 277 | if (!unsignedPortion) |
| 278 | { |
| 279 | total_len += block.prependVarNumber (total_len); |
| 280 | total_len += block.prependVarNumber (Tlv::Data); |
| 281 | } |
| 282 | return total_len; |
| 283 | } |
| 284 | |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 285 | inline const Block& |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 286 | Data::wireEncode() const |
| 287 | { |
| 288 | if (m_wire.hasWire()) |
| 289 | return m_wire; |
| 290 | |
| 291 | EncodingEstimator estimator; |
| 292 | size_t estimatedSize = wireEncode(estimator); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 293 | |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 294 | EncodingBuffer buffer(estimatedSize, 0); |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 295 | wireEncode(buffer); |
| 296 | |
| 297 | const_cast<Data*>(this)->wireDecode(buffer.block()); |
| 298 | return m_wire; |
| 299 | } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 300 | |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 301 | /** |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 302 | * Decode the input using a particular wire format and update this Data. |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 303 | * @param input The input byte array to be decoded. |
| 304 | */ |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 305 | inline void |
| 306 | Data::wireDecode(const Block& wire) |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 307 | { |
| 308 | m_wire = wire; |
| 309 | m_wire.parse(); |
| 310 | |
| 311 | // Data ::= DATA-TLV TLV-LENGTH |
| 312 | // Name |
| 313 | // MetaInfo |
| 314 | // Content |
| 315 | // Signature |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 316 | |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 317 | // Name |
| 318 | m_name.wireDecode(m_wire.get(Tlv::Name)); |
| 319 | |
| 320 | // MetaInfo |
| 321 | m_metaInfo.wireDecode(m_wire.get(Tlv::MetaInfo)); |
| 322 | |
| 323 | // Content |
| 324 | m_content = m_wire.get(Tlv::Content); |
| 325 | |
| 326 | /////////////// |
| 327 | // Signature // |
| 328 | /////////////// |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 329 | |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 330 | // SignatureInfo |
| 331 | m_signature.setInfo(m_wire.get(Tlv::SignatureInfo)); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 332 | |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 333 | // SignatureValue |
| 334 | Block::element_const_iterator val = m_wire.find(Tlv::SignatureValue); |
| 335 | if (val != m_wire.elements_end()) |
| 336 | m_signature.setValue(*val); |
| 337 | } |
| 338 | |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 339 | inline bool |
| 340 | Data::hasWire() const |
| 341 | { |
| 342 | return m_wire.hasWire(); |
| 343 | } |
| 344 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 345 | inline const Name& |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 346 | Data::getName() const |
| 347 | { |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 348 | return m_name; |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 349 | } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 350 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 351 | inline void |
| 352 | Data::setName(const Name& name) |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 353 | { |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 354 | onChanged(); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 355 | m_name = name; |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 356 | } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 357 | |
| 358 | inline const MetaInfo& |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 359 | Data::getMetaInfo() const |
| 360 | { |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 361 | return m_metaInfo; |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 362 | } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 363 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 364 | inline void |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 365 | Data::setMetaInfo(const MetaInfo& metaInfo) |
| 366 | { |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 367 | onChanged(); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 368 | m_metaInfo = metaInfo; |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 369 | } |
| 370 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 371 | inline uint32_t |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 372 | Data::getContentType() const |
| 373 | { |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 374 | return m_metaInfo.getType(); |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 375 | } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 376 | |
| 377 | inline void |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 378 | Data::setContentType(uint32_t type) |
| 379 | { |
| 380 | onChanged(); |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 381 | m_metaInfo.setType(type); |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 382 | } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 383 | |
| 384 | inline const time::milliseconds& |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 385 | Data::getFreshnessPeriod() const |
| 386 | { |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 387 | return m_metaInfo.getFreshnessPeriod(); |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 388 | } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 389 | |
| 390 | inline void |
| 391 | Data::setFreshnessPeriod(const time::milliseconds& freshnessPeriod) |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 392 | { |
| 393 | onChanged(); |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 394 | m_metaInfo.setFreshnessPeriod(freshnessPeriod); |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 395 | } |
| 396 | |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 397 | inline const name::Component& |
| 398 | Data::getFinalBlockId() const |
| 399 | { |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 400 | return m_metaInfo.getFinalBlockId(); |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | inline void |
| 404 | Data::setFinalBlockId(const name::Component& finalBlockId) |
| 405 | { |
| 406 | onChanged(); |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 407 | m_metaInfo.setFinalBlockId(finalBlockId); |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 408 | } |
| 409 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 410 | inline const Block& |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 411 | Data::getContent() const |
| 412 | { |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 413 | if (m_content.empty()) |
| 414 | m_content = dataBlock(Tlv::Content, reinterpret_cast<const uint8_t*>(0), 0); |
Alexander Afanasyev | 196b9aa | 2014-01-31 17:19:16 -0800 | [diff] [blame] | 415 | |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 416 | if (!m_content.hasWire()) |
| 417 | m_content.encode(); |
| 418 | return m_content; |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | inline void |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 422 | Data::setContent(const uint8_t* content, size_t contentLength) |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 423 | { |
| 424 | onChanged(); |
| 425 | |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 426 | m_content = dataBlock(Tlv::Content, content, contentLength); |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | inline void |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 430 | Data::setContent(const ConstBufferPtr& contentValue) |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 431 | { |
| 432 | onChanged(); |
| 433 | |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 434 | m_content = Block(Tlv::Content, contentValue); // not real a wire encoding yet |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 435 | } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 436 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 437 | inline void |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 438 | Data::setContent(const Block& content) |
Alexander Afanasyev | e0c02f5 | 2013-12-28 20:44:25 -0800 | [diff] [blame] | 439 | { |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 440 | onChanged(); |
| 441 | |
Alexander Afanasyev | e0c02f5 | 2013-12-28 20:44:25 -0800 | [diff] [blame] | 442 | if (content.type() == Tlv::Content) |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 443 | m_content = content; |
Alexander Afanasyev | e0c02f5 | 2013-12-28 20:44:25 -0800 | [diff] [blame] | 444 | else { |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 445 | m_content = Block(Tlv::Content, content); |
Alexander Afanasyev | e0c02f5 | 2013-12-28 20:44:25 -0800 | [diff] [blame] | 446 | } |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | inline const Signature& |
| 450 | Data::getSignature() const |
| 451 | { |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 452 | return m_signature; |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 453 | } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 454 | |
Alexander Afanasyev | ad39b18 | 2014-01-03 15:38:58 -0800 | [diff] [blame] | 455 | inline void |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 456 | Data::setSignature(const Signature& signature) |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 457 | { |
| 458 | onChanged(); |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 459 | m_signature = signature; |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 460 | } |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 461 | |
Alexander Afanasyev | ad39b18 | 2014-01-03 15:38:58 -0800 | [diff] [blame] | 462 | inline void |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 463 | Data::setSignatureValue(const Block& value) |
Alexander Afanasyev | ad39b18 | 2014-01-03 15:38:58 -0800 | [diff] [blame] | 464 | { |
| 465 | onChanged(); |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 466 | m_signature.setValue(value); |
Alexander Afanasyev | ad39b18 | 2014-01-03 15:38:58 -0800 | [diff] [blame] | 467 | } |
| 468 | |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 469 | // |
| 470 | |
| 471 | inline nfd::LocalControlHeader& |
| 472 | Data::getLocalControlHeader() |
| 473 | { |
| 474 | return m_localControlHeader; |
| 475 | } |
| 476 | |
| 477 | inline const nfd::LocalControlHeader& |
| 478 | Data::getLocalControlHeader() const |
| 479 | { |
| 480 | return m_localControlHeader; |
| 481 | } |
| 482 | |
Yingdi Yu | a4e5767 | 2014-02-06 11:16:17 -0800 | [diff] [blame] | 483 | inline uint64_t |
| 484 | Data::getIncomingFaceId() const |
| 485 | { |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 486 | return getLocalControlHeader().getIncomingFaceId(); |
Yingdi Yu | a4e5767 | 2014-02-06 11:16:17 -0800 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | inline void |
| 490 | Data::setIncomingFaceId(uint64_t incomingFaceId) |
| 491 | { |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 492 | getLocalControlHeader().setIncomingFaceId(incomingFaceId); |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 493 | // ! do not reset Data's wire ! |
Yingdi Yu | a4e5767 | 2014-02-06 11:16:17 -0800 | [diff] [blame] | 494 | } |
Alexander Afanasyev | ad39b18 | 2014-01-03 15:38:58 -0800 | [diff] [blame] | 495 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 496 | inline void |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 497 | Data::onChanged() |
| 498 | { |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 499 | // The values have changed, so the wire format is invalidated |
| 500 | |
| 501 | // !!!Note!!! Signature is not invalidated and it is responsibility of |
| 502 | // the application to do proper re-signing if necessary |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 503 | |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 504 | m_wire.reset(); |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 505 | } |
| 506 | |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 507 | inline bool |
| 508 | Data::operator==(const Data& other) const |
| 509 | { |
| 510 | return getName() == other.getName() && |
| 511 | getMetaInfo() == other.getMetaInfo() && |
| 512 | getContent() == other.getContent() && |
| 513 | getSignature() == other.getSignature(); |
| 514 | } |
| 515 | |
| 516 | inline bool |
| 517 | Data::operator!=(const Data& other) const |
| 518 | { |
| 519 | return !(*this == other); |
| 520 | } |
| 521 | |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 522 | inline std::ostream& |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 523 | operator<<(std::ostream& os, const Data& data) |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 524 | { |
| 525 | os << "Name: " << data.getName() << "\n"; |
| 526 | os << "MetaInfo: " << data.getMetaInfo() << "\n"; |
| 527 | os << "Content: (size: " << data.getContent().value_size() << ")\n"; |
| 528 | os << "Signature: (type: " << data.getSignature().getType() << |
| 529 | ", value_length: "<< data.getSignature().getValue().value_size() << ")"; |
| 530 | os << std::endl; |
| 531 | |
| 532 | return os; |
| 533 | } |
Alexander Afanasyev | 4ff3c91 | 2014-01-03 15:25:02 -0800 | [diff] [blame] | 534 | |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 535 | } // namespace ndn |
| 536 | |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 537 | #endif |