Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Davide Pesavento | 7f20d6e | 2017-01-16 14:43:58 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 6 | * |
| 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 20 | */ |
| 21 | |
| 22 | #ifndef NDN_MGMT_NFD_FACE_TRAITS_HPP |
| 23 | #define NDN_MGMT_NFD_FACE_TRAITS_HPP |
| 24 | |
| 25 | #include "../../encoding/tlv-nfd.hpp" |
| 26 | |
| 27 | namespace ndn { |
| 28 | namespace nfd { |
| 29 | |
| 30 | /** \ingroup management |
| 31 | * \brief providers getters and setters of face information fields |
Davide Pesavento | 7f20d6e | 2017-01-16 14:43:58 -0500 | [diff] [blame] | 32 | * \tparam C the concrete class; it must provide a wireReset() member |
| 33 | * function to clear the wire encoding when a field changes |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 34 | */ |
| 35 | template<class C> |
| 36 | class FaceTraits |
| 37 | { |
| 38 | public: |
| 39 | class Error : public tlv::Error |
| 40 | { |
| 41 | public: |
| 42 | explicit |
| 43 | Error(const std::string& what) |
| 44 | : tlv::Error(what) |
| 45 | { |
| 46 | } |
| 47 | }; |
| 48 | |
| 49 | FaceTraits() |
Davide Pesavento | f8503d2 | 2017-02-17 01:19:10 -0500 | [diff] [blame] | 50 | : m_faceId(INVALID_FACE_ID) |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 51 | , m_faceScope(FACE_SCOPE_NON_LOCAL) |
| 52 | , m_facePersistency(FACE_PERSISTENCY_PERSISTENT) |
| 53 | , m_linkType(LINK_TYPE_POINT_TO_POINT) |
Eric Newberry | 1aa3e1e | 2016-09-28 20:27:45 -0700 | [diff] [blame] | 54 | , m_flags(0x0) |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 55 | { |
| 56 | } |
| 57 | |
Davide Pesavento | 7f20d6e | 2017-01-16 14:43:58 -0500 | [diff] [blame] | 58 | virtual |
| 59 | ~FaceTraits() = default; |
| 60 | |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 61 | uint64_t |
| 62 | getFaceId() const |
| 63 | { |
| 64 | return m_faceId; |
| 65 | } |
| 66 | |
| 67 | C& |
| 68 | setFaceId(uint64_t faceId) |
| 69 | { |
| 70 | wireReset(); |
| 71 | m_faceId = faceId; |
| 72 | return static_cast<C&>(*this); |
| 73 | } |
| 74 | |
| 75 | const std::string& |
| 76 | getRemoteUri() const |
| 77 | { |
| 78 | return m_remoteUri; |
| 79 | } |
| 80 | |
| 81 | C& |
| 82 | setRemoteUri(const std::string& remoteUri) |
| 83 | { |
| 84 | wireReset(); |
| 85 | m_remoteUri = remoteUri; |
| 86 | return static_cast<C&>(*this); |
| 87 | } |
| 88 | |
| 89 | const std::string& |
| 90 | getLocalUri() const |
| 91 | { |
| 92 | return m_localUri; |
| 93 | } |
| 94 | |
| 95 | C& |
| 96 | setLocalUri(const std::string& localUri) |
| 97 | { |
| 98 | wireReset(); |
| 99 | m_localUri = localUri; |
| 100 | return static_cast<C&>(*this); |
| 101 | } |
| 102 | |
| 103 | FaceScope |
| 104 | getFaceScope() const |
| 105 | { |
| 106 | return m_faceScope; |
| 107 | } |
| 108 | |
| 109 | C& |
| 110 | setFaceScope(FaceScope faceScope) |
| 111 | { |
| 112 | wireReset(); |
| 113 | m_faceScope = faceScope; |
| 114 | return static_cast<C&>(*this); |
| 115 | } |
| 116 | |
| 117 | FacePersistency |
| 118 | getFacePersistency() const |
| 119 | { |
| 120 | return m_facePersistency; |
| 121 | } |
| 122 | |
| 123 | C& |
| 124 | setFacePersistency(FacePersistency facePersistency) |
| 125 | { |
| 126 | wireReset(); |
| 127 | m_facePersistency = facePersistency; |
| 128 | return static_cast<C&>(*this); |
| 129 | } |
| 130 | |
| 131 | LinkType |
| 132 | getLinkType() const |
| 133 | { |
| 134 | return m_linkType; |
| 135 | } |
| 136 | |
| 137 | C& |
| 138 | setLinkType(LinkType linkType) |
| 139 | { |
| 140 | wireReset(); |
| 141 | m_linkType = linkType; |
| 142 | return static_cast<C&>(*this); |
| 143 | } |
| 144 | |
Eric Newberry | 1aa3e1e | 2016-09-28 20:27:45 -0700 | [diff] [blame] | 145 | uint64_t |
| 146 | getFlags() const |
| 147 | { |
| 148 | return m_flags; |
| 149 | } |
| 150 | |
| 151 | C& |
| 152 | setFlags(uint64_t flags) |
| 153 | { |
| 154 | wireReset(); |
| 155 | m_flags = flags; |
| 156 | return static_cast<C&>(*this); |
| 157 | } |
| 158 | |
| 159 | bool |
| 160 | getFlagBit(size_t bit) const |
| 161 | { |
| 162 | if (bit >= 64) { |
| 163 | BOOST_THROW_EXCEPTION(std::out_of_range("bit must be within range [0, 64)")); |
| 164 | } |
| 165 | |
| 166 | return m_flags & (1 << bit); |
| 167 | } |
| 168 | |
| 169 | C& |
| 170 | setFlagBit(size_t bit, bool value) |
| 171 | { |
| 172 | if (bit >= 64) { |
| 173 | BOOST_THROW_EXCEPTION(std::out_of_range("bit must be within range [0, 64)")); |
| 174 | } |
| 175 | |
| 176 | wireReset(); |
| 177 | |
| 178 | if (value) { |
| 179 | m_flags |= (1 << bit); |
| 180 | } |
| 181 | else { |
| 182 | m_flags &= ~(1 << bit); |
| 183 | } |
| 184 | |
| 185 | return static_cast<C&>(*this); |
| 186 | } |
| 187 | |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 188 | protected: |
| 189 | virtual void |
| 190 | wireReset() const = 0; |
| 191 | |
| 192 | protected: |
| 193 | uint64_t m_faceId; |
| 194 | std::string m_remoteUri; |
| 195 | std::string m_localUri; |
| 196 | FaceScope m_faceScope; |
| 197 | FacePersistency m_facePersistency; |
| 198 | LinkType m_linkType; |
Eric Newberry | 1aa3e1e | 2016-09-28 20:27:45 -0700 | [diff] [blame] | 199 | uint64_t m_flags; |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 200 | }; |
| 201 | |
| 202 | } // namespace nfd |
| 203 | } // namespace ndn |
| 204 | |
| 205 | #endif // NDN_MGMT_NFD_FACE_TRAITS_HPP |