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