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