Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 88a0d81 | 2017-08-19 21:31:42 -0400 | [diff] [blame] | 2 | /* |
Junxiao Shi | ebfe4a2 | 2018-04-01 23:53:40 +0000 | [diff] [blame] | 3 | * Copyright (c) 2013-2018 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #ifndef NDN_META_INFO_HPP |
| 23 | #define NDN_META_INFO_HPP |
| 24 | |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 25 | #include "encoding/block.hpp" |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 26 | #include "encoding/encoding-buffer.hpp" |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 27 | #include "name-component.hpp" |
Davide Pesavento | 88a0d81 | 2017-08-19 21:31:42 -0400 | [diff] [blame] | 28 | #include "util/time.hpp" |
| 29 | |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 30 | #include <list> |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 31 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 32 | namespace ndn { |
| 33 | |
Eric Newberry | b555b00 | 2017-05-17 00:30:44 -0700 | [diff] [blame] | 34 | const time::milliseconds DEFAULT_FRESHNESS_PERIOD = time::milliseconds::zero(); |
| 35 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 36 | /** |
Davide Pesavento | 1fd0024 | 2018-05-20 00:11:01 -0400 | [diff] [blame] | 37 | * A MetaInfo holds the meta info which is signed inside the data packet. |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 38 | * |
| 39 | * The class allows experimentation with application-defined meta information blocks, |
| 40 | * which slightly violates NDN-TLV specification. When using the application-defined |
| 41 | * meta information blocks be aware that this may result in packet drop (NFD and |
| 42 | * previous versions of ndn-cxx will gracefully accept such packet). |
| 43 | * |
| 44 | * The following definition of MetaInfo block is assumed in this implementation (compared |
| 45 | * to the NDN-TLV spec, definition extended to allow optional AppMetaInfo TLV blocks): |
| 46 | * |
| 47 | * MetaInfo ::= META-INFO-TYPE TLV-LENGTH |
| 48 | * ContentType? |
| 49 | * FreshnessPeriod? |
| 50 | * FinalBlockId? |
| 51 | * AppMetaInfo* |
| 52 | * |
| 53 | * AppMetaInfo ::= any TLV block with type in the restricted application range [128, 252] |
| 54 | * |
| 55 | * Note that AppMetaInfo blocks are application-defined and must have TLV type from |
| 56 | * the restricted application range [128, 252]. |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 57 | */ |
Alexander Afanasyev | 2a7f720 | 2014-04-23 14:25:29 -0700 | [diff] [blame] | 58 | class MetaInfo |
| 59 | { |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 60 | public: |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 61 | class Error : public tlv::Error |
| 62 | { |
| 63 | public: |
| 64 | explicit |
| 65 | Error(const std::string& what) |
| 66 | : tlv::Error(what) |
| 67 | { |
| 68 | } |
| 69 | }; |
| 70 | |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 71 | MetaInfo(); |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 72 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 73 | /** |
| 74 | * @brief Create from wire encoding |
| 75 | */ |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 76 | explicit |
| 77 | MetaInfo(const Block& block); |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 78 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 79 | template<encoding::Tag TAG> |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 80 | size_t |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 81 | wireEncode(EncodingImpl<TAG>& encoder) const; |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 82 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 83 | const Block& |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 84 | wireEncode() const; |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 85 | |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 86 | void |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 87 | wireDecode(const Block& wire); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 88 | |
Junxiao Shi | a464b92 | 2014-11-12 21:13:06 -0700 | [diff] [blame] | 89 | public: // getter/setter |
Junxiao Shi | ebfe4a2 | 2018-04-01 23:53:40 +0000 | [diff] [blame] | 90 | /** @brief return ContentType |
| 91 | * |
| 92 | * If ContentType element is omitted, returns \c tlv::ContentType_Blob. |
| 93 | */ |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 94 | uint32_t |
Junxiao Shi | ebfe4a2 | 2018-04-01 23:53:40 +0000 | [diff] [blame] | 95 | getType() const |
| 96 | { |
| 97 | return m_type; |
| 98 | } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 99 | |
Junxiao Shi | a464b92 | 2014-11-12 21:13:06 -0700 | [diff] [blame] | 100 | /** @brief set ContentType |
Junxiao Shi | ebfe4a2 | 2018-04-01 23:53:40 +0000 | [diff] [blame] | 101 | * @param type a number defined in \c tlv::ContentTypeValue |
Junxiao Shi | a464b92 | 2014-11-12 21:13:06 -0700 | [diff] [blame] | 102 | */ |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 103 | MetaInfo& |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 104 | setType(uint32_t type); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 105 | |
Junxiao Shi | ebfe4a2 | 2018-04-01 23:53:40 +0000 | [diff] [blame] | 106 | /** @brief return FreshnessPeriod |
| 107 | * |
| 108 | * If FreshnessPeriod element is omitted, returns \c DEFAULT_FRESHNESS_PERIOD. |
| 109 | */ |
| 110 | time::milliseconds |
| 111 | getFreshnessPeriod() const |
| 112 | { |
| 113 | return m_freshnessPeriod; |
| 114 | } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 115 | |
Eric Newberry | b555b00 | 2017-05-17 00:30:44 -0700 | [diff] [blame] | 116 | /** @brief set FreshnessPeriod |
Junxiao Shi | ebfe4a2 | 2018-04-01 23:53:40 +0000 | [diff] [blame] | 117 | * @throw std::invalid_argument specified FreshnessPeriod is negative |
Eric Newberry | b555b00 | 2017-05-17 00:30:44 -0700 | [diff] [blame] | 118 | */ |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 119 | MetaInfo& |
Eric Newberry | b555b00 | 2017-05-17 00:30:44 -0700 | [diff] [blame] | 120 | setFreshnessPeriod(time::milliseconds freshnessPeriod); |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 121 | |
Junxiao Shi | ebfe4a2 | 2018-04-01 23:53:40 +0000 | [diff] [blame] | 122 | /** @brief return FinalBlockId |
| 123 | */ |
| 124 | const optional<name::Component>& |
| 125 | getFinalBlock() const |
| 126 | { |
| 127 | return m_finalBlockId; |
| 128 | } |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 129 | |
Junxiao Shi | ebfe4a2 | 2018-04-01 23:53:40 +0000 | [diff] [blame] | 130 | /** @brief set FinalBlockId |
| 131 | */ |
| 132 | MetaInfo& |
| 133 | setFinalBlock(optional<name::Component> finalBlockId); |
| 134 | |
| 135 | /** @brief return FinalBlockId |
| 136 | * @deprecated use @c getFinalBlock |
| 137 | * |
| 138 | * If FinalBlockId element is omitted, returns a default-constructed @c name::Component. |
| 139 | * This is indistinguishable from having an empty GenericNameComponent as FinalBlockId. |
| 140 | */ |
Davide Pesavento | 1fd0024 | 2018-05-20 00:11:01 -0400 | [diff] [blame] | 141 | [[deprecated("use getFinalBlock")]] |
Junxiao Shi | ebfe4a2 | 2018-04-01 23:53:40 +0000 | [diff] [blame] | 142 | name::Component |
| 143 | getFinalBlockId() const |
| 144 | { |
| 145 | return getFinalBlock().value_or(name::Component()); |
| 146 | } |
| 147 | |
| 148 | /** @brief set FinalBlockId |
| 149 | * @deprecated use @c setFinalBlock |
| 150 | * |
| 151 | * Passing a default-constructed @c name::Component removes FinalBlockId element. |
| 152 | * This API does not support adding an empty GenericNameComponent as FinalBlockId. |
| 153 | */ |
Davide Pesavento | 1fd0024 | 2018-05-20 00:11:01 -0400 | [diff] [blame] | 154 | [[deprecated("use setFinalBlock")]] |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 155 | MetaInfo& |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 156 | setFinalBlockId(const name::Component& finalBlockId); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 157 | |
Junxiao Shi | a464b92 | 2014-11-12 21:13:06 -0700 | [diff] [blame] | 158 | public: // app-defined MetaInfo items |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 159 | /** |
| 160 | * @brief Get all app-defined MetaInfo items |
| 161 | * |
| 162 | * @note Warning: Experimental API, which may change or disappear in the future |
| 163 | * |
Junxiao Shi | ebfe4a2 | 2018-04-01 23:53:40 +0000 | [diff] [blame] | 164 | * @note If MetaInfo is decoded from wire and setType, setFreshnessPeriod, or setFinalBlock |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 165 | * is called before *AppMetaInfo, all app-defined blocks will be lost |
| 166 | */ |
| 167 | const std::list<Block>& |
| 168 | getAppMetaInfo() const; |
| 169 | |
| 170 | /** |
| 171 | * @brief Set app-defined MetaInfo items |
| 172 | * |
| 173 | * This method will replace all existing app-defined MetaInfo items, if they existed. |
| 174 | * |
| 175 | * @throw Error if some block in @p info has type not in the application range |
| 176 | * (http://named-data.net/doc/ndn-tlv/types.html) |
| 177 | * |
| 178 | * @note Warning: Experimental API, which may change or disappear in the future |
| 179 | * |
Junxiao Shi | ebfe4a2 | 2018-04-01 23:53:40 +0000 | [diff] [blame] | 180 | * @note If MetaInfo is decoded from wire and setType, setFreshnessPeriod, or setFinalBlock |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 181 | * is called before *AppMetaInfo, all app-defined blocks will be lost |
| 182 | */ |
| 183 | MetaInfo& |
| 184 | setAppMetaInfo(const std::list<Block>& info); |
| 185 | |
| 186 | /** |
| 187 | * @brief Add an app-defined MetaInfo item |
| 188 | * |
| 189 | * @throw Error if @p block has type not in the application range |
| 190 | * (http://named-data.net/doc/ndn-tlv/types.html) |
| 191 | * |
| 192 | * @note Warning: Experimental API, which may change or disappear in the future |
| 193 | * |
Junxiao Shi | ebfe4a2 | 2018-04-01 23:53:40 +0000 | [diff] [blame] | 194 | * @note If MetaInfo is decoded from wire and setType, setFreshnessPeriod, or setFinalBlock |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 195 | * is called before *AppMetaInfo, all app-defined blocks will be lost |
| 196 | */ |
| 197 | MetaInfo& |
| 198 | addAppMetaInfo(const Block& block); |
| 199 | |
| 200 | /** |
| 201 | * @brief Remove a first app-defined MetaInfo item with type @p tlvType |
| 202 | * |
| 203 | * @return true if an item was deleted |
| 204 | * |
| 205 | * @note Warning: Experimental API, which may change or disappear in the future |
| 206 | * |
Junxiao Shi | ebfe4a2 | 2018-04-01 23:53:40 +0000 | [diff] [blame] | 207 | * @note If MetaInfo is decoded from wire and setType, setFreshnessPeriod, or setFinalBlock |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 208 | * is called before *AppMetaInfo, all app-defined blocks will be lost |
| 209 | */ |
| 210 | bool |
| 211 | removeAppMetaInfo(uint32_t tlvType); |
| 212 | |
| 213 | /** |
| 214 | * @brief Find a first app-defined MetaInfo item of type @p tlvType |
| 215 | * |
| 216 | * @return NULL if an item is not found, otherwise const pointer to the item |
| 217 | * |
| 218 | * @throw Error if @p tlvType is not in the application range |
| 219 | * (http://named-data.net/doc/ndn-tlv/types.html) |
| 220 | * |
| 221 | * @note Warning: Experimental API, which may change or disappear in the future |
| 222 | * |
Junxiao Shi | ebfe4a2 | 2018-04-01 23:53:40 +0000 | [diff] [blame] | 223 | * @note If MetaInfo is decoded from wire and setType, setFreshnessPeriod, or setFinalBlock |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 224 | * is called before *AppMetaInfo, all app-defined blocks will be lost |
| 225 | */ |
| 226 | const Block* |
| 227 | findAppMetaInfo(uint32_t tlvType) const; |
| 228 | |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 229 | public: // EqualityComparable concept |
| 230 | bool |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 231 | operator==(const MetaInfo& other) const; |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 232 | |
| 233 | bool |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 234 | operator!=(const MetaInfo& other) const; |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 235 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 236 | private: |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 237 | uint32_t m_type; |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 238 | time::milliseconds m_freshnessPeriod; |
Junxiao Shi | ebfe4a2 | 2018-04-01 23:53:40 +0000 | [diff] [blame] | 239 | optional<name::Component> m_finalBlockId; |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 240 | std::list<Block> m_appMetaInfo; |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 241 | |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 242 | mutable Block m_wire; |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 243 | }; |
| 244 | |
Davide Pesavento | 88a0d81 | 2017-08-19 21:31:42 -0400 | [diff] [blame] | 245 | NDN_CXX_DECLARE_WIRE_ENCODE_INSTANTIATIONS(MetaInfo); |
| 246 | |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 247 | std::ostream& |
| 248 | operator<<(std::ostream& os, const MetaInfo& info); |
| 249 | |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 250 | inline bool |
| 251 | MetaInfo::operator==(const MetaInfo& other) const |
Alexander Afanasyev | 4ff3c91 | 2014-01-03 15:25:02 -0800 | [diff] [blame] | 252 | { |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 253 | return wireEncode() == other.wireEncode(); |
| 254 | } |
Alexander Afanasyev | 4ff3c91 | 2014-01-03 15:25:02 -0800 | [diff] [blame] | 255 | |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 256 | inline bool |
| 257 | MetaInfo::operator!=(const MetaInfo& other) const |
| 258 | { |
| 259 | return !(*this == other); |
Alexander Afanasyev | 4ff3c91 | 2014-01-03 15:25:02 -0800 | [diff] [blame] | 260 | } |
| 261 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 262 | } // namespace ndn |
| 263 | |
| 264 | #endif // NDN_META_INFO_HPP |