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 | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 2 | /** |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame^] | 3 | * Copyright (c) 2013-2015 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 | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #ifndef NDN_MANAGEMENT_NFD_LOCAL_CONTROL_HEADER_HPP |
| 23 | #define NDN_MANAGEMENT_NFD_LOCAL_CONTROL_HEADER_HPP |
| 24 | |
| 25 | #include "../encoding/encoding-buffer.hpp" |
| 26 | #include "../encoding/tlv-nfd.hpp" |
Alexander Afanasyev | 01065fb | 2014-10-02 13:01:46 -0700 | [diff] [blame] | 27 | #include "../encoding/block-helpers.hpp" |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 28 | |
| 29 | namespace ndn { |
| 30 | namespace nfd { |
| 31 | |
Alexander Afanasyev | 4671bf7 | 2014-05-19 09:01:37 -0400 | [diff] [blame] | 32 | /** |
| 33 | * @ingroup management |
| 34 | * @brief Class to handle work with LocalControlHeader |
| 35 | * @sa http://redmine.named-data.net/projects/nfd/wiki/LocalControlHeader |
| 36 | */ |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 37 | class LocalControlHeader |
| 38 | { |
| 39 | public: |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 40 | class Error : public std::runtime_error |
| 41 | { |
| 42 | public: |
| 43 | explicit |
| 44 | Error(const std::string& what) |
| 45 | : std::runtime_error(what) |
| 46 | { |
| 47 | } |
| 48 | }; |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 49 | |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 50 | LocalControlHeader() |
| 51 | : m_incomingFaceId(INVALID_FACE_ID) |
| 52 | , m_nextHopFaceId(INVALID_FACE_ID) |
| 53 | { |
| 54 | } |
| 55 | |
| 56 | /** |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 57 | * @brief Create from wire encoding |
| 58 | * |
| 59 | * @sa wireDecode |
| 60 | */ |
| 61 | explicit |
| 62 | LocalControlHeader(const Block& wire, |
| 63 | bool encodeIncomingFaceId = true, bool encodeNextHopFaceId = true) |
| 64 | { |
| 65 | wireDecode(wire, encodeIncomingFaceId, encodeNextHopFaceId); |
| 66 | } |
| 67 | |
| 68 | /** |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 69 | * @brief Create wire encoding with options LocalControlHeader and the supplied item |
| 70 | * |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 71 | * The caller is responsible of checking whether LocalControlHeader contains |
| 72 | * any information. |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 73 | * |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 74 | * !It is an error to call this method if neither IncomingFaceId nor NextHopFaceId is |
| 75 | * set, or neither of them is enabled. |
| 76 | * |
| 77 | * @throws LocalControlHeader::Error when empty LocalControlHeader be produced |
| 78 | * |
| 79 | * @returns Block, containing LocalControlHeader. Top-level length field of the |
| 80 | * returned LocalControlHeader includes payload length, but the memory |
| 81 | * block is independent of the payload's wire buffer. It is expected |
| 82 | * that both LocalControlHeader's and payload's wire will be send out |
| 83 | * together within a single send call. |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 84 | * |
| 85 | * @see http://redmine.named-data.net/projects/nfd/wiki/LocalControlHeader |
| 86 | */ |
| 87 | template<class U> |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 88 | inline Block |
| 89 | wireEncode(const U& payload, |
| 90 | bool encodeIncomingFaceId, bool encodeNextHopFaceId) const; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 91 | |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 92 | /** |
| 93 | * @brief Decode from the wire format and set LocalControlHeader on the supplied item |
| 94 | * |
| 95 | * The supplied wire MUST contain LocalControlHeader. Determination whether the optional |
| 96 | * LocalControlHeader should be done before calling this method. |
| 97 | */ |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 98 | inline void |
Alexander Afanasyev | 37bb190 | 2014-02-19 23:57:23 -0800 | [diff] [blame] | 99 | wireDecode(const Block& wire, |
| 100 | bool encodeIncomingFaceId = true, bool encodeNextHopFaceId = true); |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 101 | |
| 102 | inline static const Block& |
| 103 | getPayload(const Block& wire); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 104 | |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 105 | /////////////////////////////////////////////////////////////////////////////// |
| 106 | /////////////////////////////////////////////////////////////////////////////// |
| 107 | /////////////////////////////////////////////////////////////////////////////// |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 108 | // Getters/setters |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 109 | |
| 110 | bool |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 111 | empty(bool encodeIncomingFaceId, bool encodeNextHopFaceId) const |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 112 | { |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 113 | return !((encodeIncomingFaceId && hasIncomingFaceId()) || |
| 114 | (encodeNextHopFaceId && hasNextHopFaceId())); |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 115 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 116 | |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 117 | // |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 118 | |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 119 | bool |
| 120 | hasIncomingFaceId() const |
| 121 | { |
| 122 | return m_incomingFaceId != INVALID_FACE_ID; |
| 123 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 124 | |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 125 | uint64_t |
| 126 | getIncomingFaceId() const |
| 127 | { |
| 128 | return m_incomingFaceId; |
| 129 | } |
| 130 | |
| 131 | void |
| 132 | setIncomingFaceId(uint64_t incomingFaceId) |
| 133 | { |
| 134 | m_incomingFaceId = incomingFaceId; |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | // |
| 138 | |
| 139 | bool |
| 140 | hasNextHopFaceId() const |
| 141 | { |
| 142 | return m_nextHopFaceId != INVALID_FACE_ID; |
| 143 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 144 | |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 145 | uint64_t |
| 146 | getNextHopFaceId() const |
| 147 | { |
| 148 | return m_nextHopFaceId; |
| 149 | } |
| 150 | |
| 151 | void |
| 152 | setNextHopFaceId(uint64_t nextHopFaceId) |
| 153 | { |
| 154 | m_nextHopFaceId = nextHopFaceId; |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | private: |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame^] | 158 | template<encoding::Tag TAG> |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 159 | inline size_t |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame^] | 160 | wireEncode(EncodingImpl<TAG>& block, size_t payloadSize, |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 161 | bool encodeIncomingFaceId, bool encodeNextHopFaceId) const; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 162 | |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 163 | private: |
| 164 | uint64_t m_incomingFaceId; |
| 165 | uint64_t m_nextHopFaceId; |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 166 | }; |
| 167 | |
| 168 | |
| 169 | /** |
| 170 | * @brief Fast encoding or block size estimation |
| 171 | */ |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame^] | 172 | template<encoding::Tag TAG> |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 173 | inline size_t |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame^] | 174 | LocalControlHeader::wireEncode(EncodingImpl<TAG>& block, size_t payloadSize, |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 175 | bool encodeIncomingFaceId, bool encodeNextHopFaceId) const |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 176 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 177 | size_t totalLength = payloadSize; |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 178 | |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 179 | if (encodeIncomingFaceId && hasIncomingFaceId()) |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 180 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 181 | totalLength += prependNonNegativeIntegerBlock(block, |
| 182 | tlv::nfd::IncomingFaceId, getIncomingFaceId()); |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 183 | } |
| 184 | |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 185 | if (encodeNextHopFaceId && hasNextHopFaceId()) |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 186 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 187 | totalLength += prependNonNegativeIntegerBlock(block, |
| 188 | tlv::nfd::NextHopFaceId, getNextHopFaceId()); |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 189 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 190 | |
| 191 | totalLength += block.prependVarNumber(totalLength); |
| 192 | totalLength += block.prependVarNumber(tlv::nfd::LocalControlHeader); |
| 193 | return totalLength; |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | template<class U> |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 197 | inline Block |
| 198 | LocalControlHeader::wireEncode(const U& payload, |
| 199 | bool encodeIncomingFaceId, bool encodeNextHopFaceId) const |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 200 | { |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 201 | /// @todo should this be BOOST_ASSERT instead? This is kind of unnecessary overhead |
| 202 | if (empty(encodeIncomingFaceId, encodeNextHopFaceId)) |
| 203 | throw Error("Requested wire for LocalControlHeader, but none of the fields are set or enabled"); |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 204 | |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 205 | EncodingEstimator estimator; |
| 206 | size_t length = wireEncode(estimator, payload.wireEncode().size(), |
| 207 | encodeIncomingFaceId, encodeNextHopFaceId); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 208 | |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 209 | EncodingBuffer buffer(length); |
| 210 | wireEncode(buffer, payload.wireEncode().size(), |
| 211 | encodeIncomingFaceId, encodeNextHopFaceId); |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 212 | |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 213 | return buffer.block(false); |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 214 | } |
| 215 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 216 | inline void |
Alexander Afanasyev | 37bb190 | 2014-02-19 23:57:23 -0800 | [diff] [blame] | 217 | LocalControlHeader::wireDecode(const Block& wire, |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 218 | bool encodeIncomingFaceId/* = true*/, |
| 219 | bool encodeNextHopFaceId/* = true*/) |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 220 | { |
| 221 | BOOST_ASSERT(wire.type() == tlv::nfd::LocalControlHeader); |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 222 | wire.parse(); |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 223 | |
| 224 | m_incomingFaceId = INVALID_FACE_ID; |
| 225 | m_nextHopFaceId = INVALID_FACE_ID; |
| 226 | |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 227 | for (Block::element_const_iterator i = wire.elements_begin(); |
| 228 | i != wire.elements_end(); |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 229 | ++i) |
| 230 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 231 | switch (i->type()) |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 232 | { |
| 233 | case tlv::nfd::IncomingFaceId: |
Alexander Afanasyev | 37bb190 | 2014-02-19 23:57:23 -0800 | [diff] [blame] | 234 | if (encodeIncomingFaceId) |
| 235 | m_incomingFaceId = readNonNegativeInteger(*i); |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 236 | break; |
| 237 | case tlv::nfd::NextHopFaceId: |
Alexander Afanasyev | 37bb190 | 2014-02-19 23:57:23 -0800 | [diff] [blame] | 238 | if (encodeNextHopFaceId) |
| 239 | m_nextHopFaceId = readNonNegativeInteger(*i); |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 240 | break; |
| 241 | default: |
| 242 | // ignore all unsupported |
| 243 | break; |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | inline const Block& |
| 249 | LocalControlHeader::getPayload(const Block& wire) |
| 250 | { |
| 251 | if (wire.type() == tlv::nfd::LocalControlHeader) |
| 252 | { |
| 253 | wire.parse(); |
| 254 | if (wire.elements_size() < 1) |
| 255 | return wire; // don't throw an error, but don't continue processing |
| 256 | |
| 257 | return wire.elements()[wire.elements().size()-1]; |
| 258 | } |
| 259 | else |
| 260 | { |
| 261 | return wire; |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | |
| 266 | } // namespace nfd |
| 267 | } // namespace ndn |
| 268 | |
| 269 | #endif // NDN_MANAGEMENT_NFD_LOCAL_CONTROL_HEADER_HPP |