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