Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 2 | /** |
Eric Newberry | b555b00 | 2017-05-17 00:30:44 -0700 | [diff] [blame^] | 3 | * Copyright (c) 2013-2017 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 | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 25 | #include "common.hpp" |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 26 | #include "encoding/block.hpp" |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 27 | #include "encoding/encoding-buffer.hpp" |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 28 | #include "util/time.hpp" |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 29 | #include "name-component.hpp" |
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 | /** |
| 37 | * An 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 |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 90 | uint32_t |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 91 | getType() const; |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 92 | |
Junxiao Shi | a464b92 | 2014-11-12 21:13:06 -0700 | [diff] [blame] | 93 | /** @brief set ContentType |
| 94 | * @param type a code defined in tlv::ContentTypeValue |
| 95 | */ |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 96 | MetaInfo& |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 97 | setType(uint32_t type); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 98 | |
| 99 | const time::milliseconds& |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 100 | getFreshnessPeriod() const; |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 101 | |
Eric Newberry | b555b00 | 2017-05-17 00:30:44 -0700 | [diff] [blame^] | 102 | /** @brief set FreshnessPeriod |
| 103 | * @throw std::invalid_argument specified FreshnessPeriod is < 0 |
| 104 | */ |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 105 | MetaInfo& |
Eric Newberry | b555b00 | 2017-05-17 00:30:44 -0700 | [diff] [blame^] | 106 | setFreshnessPeriod(time::milliseconds freshnessPeriod); |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 107 | |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 108 | const name::Component& |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 109 | getFinalBlockId() const; |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 110 | |
| 111 | MetaInfo& |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 112 | setFinalBlockId(const name::Component& finalBlockId); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 113 | |
Junxiao Shi | a464b92 | 2014-11-12 21:13:06 -0700 | [diff] [blame] | 114 | public: // app-defined MetaInfo items |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 115 | /** |
| 116 | * @brief Get all app-defined MetaInfo items |
| 117 | * |
| 118 | * @note Warning: Experimental API, which may change or disappear in the future |
| 119 | * |
| 120 | * @note If MetaInfo is decoded from wire and setType, setFreshnessPeriod, or setFinalBlockId |
| 121 | * is called before *AppMetaInfo, all app-defined blocks will be lost |
| 122 | */ |
| 123 | const std::list<Block>& |
| 124 | getAppMetaInfo() const; |
| 125 | |
| 126 | /** |
| 127 | * @brief Set app-defined MetaInfo items |
| 128 | * |
| 129 | * This method will replace all existing app-defined MetaInfo items, if they existed. |
| 130 | * |
| 131 | * @throw Error if some block in @p info has type not in the application range |
| 132 | * (http://named-data.net/doc/ndn-tlv/types.html) |
| 133 | * |
| 134 | * @note Warning: Experimental API, which may change or disappear in the future |
| 135 | * |
| 136 | * @note If MetaInfo is decoded from wire and setType, setFreshnessPeriod, or setFinalBlockId |
| 137 | * is called before *AppMetaInfo, all app-defined blocks will be lost |
| 138 | */ |
| 139 | MetaInfo& |
| 140 | setAppMetaInfo(const std::list<Block>& info); |
| 141 | |
| 142 | /** |
| 143 | * @brief Add an app-defined MetaInfo item |
| 144 | * |
| 145 | * @throw Error if @p block has type not in the application range |
| 146 | * (http://named-data.net/doc/ndn-tlv/types.html) |
| 147 | * |
| 148 | * @note Warning: Experimental API, which may change or disappear in the future |
| 149 | * |
| 150 | * @note If MetaInfo is decoded from wire and setType, setFreshnessPeriod, or setFinalBlockId |
| 151 | * is called before *AppMetaInfo, all app-defined blocks will be lost |
| 152 | */ |
| 153 | MetaInfo& |
| 154 | addAppMetaInfo(const Block& block); |
| 155 | |
| 156 | /** |
| 157 | * @brief Remove a first app-defined MetaInfo item with type @p tlvType |
| 158 | * |
| 159 | * @return true if an item was deleted |
| 160 | * |
| 161 | * @note Warning: Experimental API, which may change or disappear in the future |
| 162 | * |
| 163 | * @note If MetaInfo is decoded from wire and setType, setFreshnessPeriod, or setFinalBlockId |
| 164 | * is called before *AppMetaInfo, all app-defined blocks will be lost |
| 165 | */ |
| 166 | bool |
| 167 | removeAppMetaInfo(uint32_t tlvType); |
| 168 | |
| 169 | /** |
| 170 | * @brief Find a first app-defined MetaInfo item of type @p tlvType |
| 171 | * |
| 172 | * @return NULL if an item is not found, otherwise const pointer to the item |
| 173 | * |
| 174 | * @throw Error if @p tlvType is not in the application range |
| 175 | * (http://named-data.net/doc/ndn-tlv/types.html) |
| 176 | * |
| 177 | * @note Warning: Experimental API, which may change or disappear in the future |
| 178 | * |
| 179 | * @note If MetaInfo is decoded from wire and setType, setFreshnessPeriod, or setFinalBlockId |
| 180 | * is called before *AppMetaInfo, all app-defined blocks will be lost |
| 181 | */ |
| 182 | const Block* |
| 183 | findAppMetaInfo(uint32_t tlvType) const; |
| 184 | |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 185 | public: // EqualityComparable concept |
| 186 | bool |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 187 | operator==(const MetaInfo& other) const; |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 188 | |
| 189 | bool |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 190 | operator!=(const MetaInfo& other) const; |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 191 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 192 | private: |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 193 | uint32_t m_type; |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 194 | time::milliseconds m_freshnessPeriod; |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 195 | name::Component m_finalBlockId; |
Shock Jiang | ca7ea70 | 2014-10-02 11:23:25 -0700 | [diff] [blame] | 196 | std::list<Block> m_appMetaInfo; |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 197 | |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 198 | mutable Block m_wire; |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 199 | }; |
| 200 | |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 201 | std::ostream& |
| 202 | operator<<(std::ostream& os, const MetaInfo& info); |
| 203 | |
| 204 | ///////////////////////////////////////////////////////////////////////// |
| 205 | |
| 206 | inline uint32_t |
| 207 | MetaInfo::getType() const |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 208 | { |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 209 | return m_type; |
Alexander Afanasyev | 95b0e34 | 2014-02-12 21:34:44 -0800 | [diff] [blame] | 210 | } |
| 211 | |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 212 | inline const time::milliseconds& |
| 213 | MetaInfo::getFreshnessPeriod() const |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 214 | { |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 215 | return m_freshnessPeriod; |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 216 | } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 217 | |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 218 | inline const name::Component& |
| 219 | MetaInfo::getFinalBlockId() const |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 220 | { |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 221 | return m_finalBlockId; |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 222 | } |
| 223 | |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 224 | inline bool |
| 225 | MetaInfo::operator==(const MetaInfo& other) const |
Alexander Afanasyev | 4ff3c91 | 2014-01-03 15:25:02 -0800 | [diff] [blame] | 226 | { |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 227 | return wireEncode() == other.wireEncode(); |
| 228 | } |
Alexander Afanasyev | 4ff3c91 | 2014-01-03 15:25:02 -0800 | [diff] [blame] | 229 | |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 230 | inline bool |
| 231 | MetaInfo::operator!=(const MetaInfo& other) const |
| 232 | { |
| 233 | return !(*this == other); |
Alexander Afanasyev | 4ff3c91 | 2014-01-03 15:25:02 -0800 | [diff] [blame] | 234 | } |
| 235 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 236 | } // namespace ndn |
| 237 | |
| 238 | #endif // NDN_META_INFO_HPP |