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