Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (C) 2014 Named Data Networking Project |
| 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
| 7 | #ifndef NFD_FACE_LOCAL_FACE_HPP |
| 8 | #define NFD_FACE_LOCAL_FACE_HPP |
| 9 | |
| 10 | #include "face.hpp" |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 11 | #include <ndn-cpp-dev/management/nfd-control-parameters.hpp> |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 12 | |
| 13 | namespace nfd { |
| 14 | |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 15 | using ndn::nfd::LocalControlFeature; |
| 16 | using ndn::nfd::LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID; |
| 17 | using ndn::nfd::LOCAL_CONTROL_FEATURE_NEXT_HOP_FACE_ID; |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 18 | |
| 19 | /** \brief represents a face |
| 20 | */ |
| 21 | class LocalFace : public Face |
| 22 | { |
| 23 | public: |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 24 | |
Alexander Afanasyev | a39b90b | 2014-03-05 15:31:00 +0000 | [diff] [blame] | 25 | explicit |
| 26 | LocalFace(const FaceUri& uri); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 27 | |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 28 | /** \brief get whether any LocalControlHeader feature is enabled |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 29 | * |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 30 | * \returns true if any feature is enabled. |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 31 | */ |
| 32 | bool |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 33 | isLocalControlHeaderEnabled() const; |
| 34 | |
| 35 | /** \brief get whether a specific LocalControlHeader feature is enabled |
| 36 | * |
| 37 | * \param feature The feature. |
| 38 | * \returns true if the specified feature is enabled. |
| 39 | */ |
| 40 | bool |
| 41 | isLocalControlHeaderEnabled(LocalControlFeature feature) const; |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 42 | |
| 43 | /** \brief enable or disable a LocalControlHeader feature |
| 44 | * |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 45 | * \param feature The feature. Cannot be LOCAL_CONTROL_FEATURE_ANY |
| 46 | * or LOCAL_CONTROL_FEATURE_MAX |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 47 | */ |
| 48 | void |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 49 | setLocalControlHeaderFeature(LocalControlFeature feature, bool enabled = true); |
| 50 | |
| 51 | public: |
| 52 | |
| 53 | static const size_t LOCAL_CONTROL_FEATURE_MAX = 3; /// upper bound of LocalControlFeature enum |
| 54 | static const size_t LOCAL_CONTROL_FEATURE_ANY = 0; /// any feature |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 55 | |
| 56 | protected: |
| 57 | // statically overridden from Face |
| 58 | |
| 59 | /** \brief Decode block into Interest/Data, considering potential LocalControlHeader |
| 60 | * |
| 61 | * If LocalControlHeader is present, the encoded data is filtered out, based |
| 62 | * on enabled features on the face. |
| 63 | */ |
| 64 | bool |
| 65 | decodeAndDispatchInput(const Block& element); |
| 66 | |
| 67 | // LocalFace-specific methods |
| 68 | |
| 69 | /** \brief Check if LocalControlHeader needs to be included, taking into account |
| 70 | * both set parameters in supplied LocalControlHeader and features |
| 71 | * enabled on the local face. |
| 72 | */ |
| 73 | bool |
| 74 | isEmptyFilteredLocalControlHeader(const ndn::nfd::LocalControlHeader& header) const; |
| 75 | |
| 76 | /** \brief Create LocalControlHeader, considering enabled features |
| 77 | */ |
| 78 | template<class Packet> |
| 79 | Block |
| 80 | filterAndEncodeLocalControlHeader(const Packet& packet); |
| 81 | |
| 82 | private: |
| 83 | std::vector<bool> m_localControlHeaderFeatures; |
| 84 | }; |
| 85 | |
| 86 | inline |
Alexander Afanasyev | a39b90b | 2014-03-05 15:31:00 +0000 | [diff] [blame] | 87 | LocalFace::LocalFace(const FaceUri& uri) |
| 88 | : Face(uri, true) |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 89 | , m_localControlHeaderFeatures(LocalFace::LOCAL_CONTROL_FEATURE_MAX) |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 90 | { |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | inline bool |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 94 | LocalFace::isLocalControlHeaderEnabled() const |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 95 | { |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 96 | return m_localControlHeaderFeatures[LOCAL_CONTROL_FEATURE_ANY]; |
| 97 | } |
| 98 | |
| 99 | inline bool |
| 100 | LocalFace::isLocalControlHeaderEnabled(LocalControlFeature feature) const |
| 101 | { |
| 102 | BOOST_ASSERT(0 < feature && feature < m_localControlHeaderFeatures.size()); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 103 | return m_localControlHeaderFeatures[feature]; |
| 104 | } |
| 105 | |
| 106 | inline void |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 107 | LocalFace::setLocalControlHeaderFeature(LocalControlFeature feature, bool enabled/* = true*/) |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 108 | { |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 109 | BOOST_ASSERT(0 < feature && feature < m_localControlHeaderFeatures.size()); |
| 110 | |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 111 | m_localControlHeaderFeatures[feature] = enabled; |
| 112 | |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 113 | m_localControlHeaderFeatures[LOCAL_CONTROL_FEATURE_ANY] = |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 114 | std::find(m_localControlHeaderFeatures.begin() + 1, |
| 115 | m_localControlHeaderFeatures.end(), true) < |
| 116 | m_localControlHeaderFeatures.end(); |
| 117 | // 'find(..) < .end()' instead of 'find(..) != .end()' due to LLVM Bug 16816 |
| 118 | } |
| 119 | |
| 120 | inline bool |
| 121 | LocalFace::decodeAndDispatchInput(const Block& element) |
| 122 | { |
| 123 | const Block& payload = ndn::nfd::LocalControlHeader::getPayload(element); |
| 124 | |
| 125 | // If received LocalControlHeader, but it is not enabled on the face |
| 126 | if ((&payload != &element) && !this->isLocalControlHeaderEnabled()) |
| 127 | return false; |
| 128 | |
| 129 | if (payload.type() == tlv::Interest) |
| 130 | { |
| 131 | shared_ptr<Interest> i = make_shared<Interest>(); |
| 132 | i->wireDecode(payload); |
| 133 | if (&payload != &element) |
| 134 | { |
| 135 | i->getLocalControlHeader().wireDecode(element, |
| 136 | false, |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 137 | this->isLocalControlHeaderEnabled(LOCAL_CONTROL_FEATURE_NEXT_HOP_FACE_ID)); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | this->onReceiveInterest(*i); |
| 141 | } |
| 142 | else if (payload.type() == tlv::Data) |
| 143 | { |
| 144 | shared_ptr<Data> d = make_shared<Data>(); |
| 145 | d->wireDecode(payload); |
| 146 | |
| 147 | /// \todo Uncomment and correct the following when we have more |
| 148 | /// options in LocalControlHeader that apply for incoming |
| 149 | /// Data packets (if ever) |
| 150 | // if (&payload != &element) |
| 151 | // { |
| 152 | // |
| 153 | // d->getLocalControlHeader().wireDecode(element, |
| 154 | // false, |
| 155 | // false); |
| 156 | // } |
| 157 | |
| 158 | this->onReceiveData(*d); |
| 159 | } |
| 160 | else |
| 161 | return false; |
| 162 | |
| 163 | return true; |
| 164 | } |
| 165 | |
| 166 | inline bool |
| 167 | LocalFace::isEmptyFilteredLocalControlHeader(const ndn::nfd::LocalControlHeader& header) const |
| 168 | { |
| 169 | if (!this->isLocalControlHeaderEnabled()) |
| 170 | return true; |
| 171 | |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 172 | return header.empty(this->isLocalControlHeaderEnabled(LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID), |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 173 | false); |
| 174 | } |
| 175 | |
| 176 | template<class Packet> |
| 177 | inline Block |
| 178 | LocalFace::filterAndEncodeLocalControlHeader(const Packet& packet) |
| 179 | { |
| 180 | return packet.getLocalControlHeader().wireEncode(packet, |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 181 | this->isLocalControlHeaderEnabled(LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID), |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 182 | false); |
| 183 | } |
| 184 | |
| 185 | } // namespace nfd |
| 186 | |
| 187 | #endif // NFD_FACE_LOCAL_FACE_HPP |