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