Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2013-2016 Regents of the University of California. |
| 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 |
| 32 | * \tparam C the concrete class; it must provide .wireReset() method |
| 33 | to clear wire encoding when a field changes |
| 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() |
| 50 | : m_faceId(0) |
| 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 | |
| 58 | uint64_t |
| 59 | getFaceId() const |
| 60 | { |
| 61 | return m_faceId; |
| 62 | } |
| 63 | |
| 64 | C& |
| 65 | setFaceId(uint64_t faceId) |
| 66 | { |
| 67 | wireReset(); |
| 68 | m_faceId = faceId; |
| 69 | return static_cast<C&>(*this); |
| 70 | } |
| 71 | |
| 72 | const std::string& |
| 73 | getRemoteUri() const |
| 74 | { |
| 75 | return m_remoteUri; |
| 76 | } |
| 77 | |
| 78 | C& |
| 79 | setRemoteUri(const std::string& remoteUri) |
| 80 | { |
| 81 | wireReset(); |
| 82 | m_remoteUri = remoteUri; |
| 83 | return static_cast<C&>(*this); |
| 84 | } |
| 85 | |
| 86 | const std::string& |
| 87 | getLocalUri() const |
| 88 | { |
| 89 | return m_localUri; |
| 90 | } |
| 91 | |
| 92 | C& |
| 93 | setLocalUri(const std::string& localUri) |
| 94 | { |
| 95 | wireReset(); |
| 96 | m_localUri = localUri; |
| 97 | return static_cast<C&>(*this); |
| 98 | } |
| 99 | |
| 100 | FaceScope |
| 101 | getFaceScope() const |
| 102 | { |
| 103 | return m_faceScope; |
| 104 | } |
| 105 | |
| 106 | C& |
| 107 | setFaceScope(FaceScope faceScope) |
| 108 | { |
| 109 | wireReset(); |
| 110 | m_faceScope = faceScope; |
| 111 | return static_cast<C&>(*this); |
| 112 | } |
| 113 | |
| 114 | FacePersistency |
| 115 | getFacePersistency() const |
| 116 | { |
| 117 | return m_facePersistency; |
| 118 | } |
| 119 | |
| 120 | C& |
| 121 | setFacePersistency(FacePersistency facePersistency) |
| 122 | { |
| 123 | wireReset(); |
| 124 | m_facePersistency = facePersistency; |
| 125 | return static_cast<C&>(*this); |
| 126 | } |
| 127 | |
| 128 | LinkType |
| 129 | getLinkType() const |
| 130 | { |
| 131 | return m_linkType; |
| 132 | } |
| 133 | |
| 134 | C& |
| 135 | setLinkType(LinkType linkType) |
| 136 | { |
| 137 | wireReset(); |
| 138 | m_linkType = linkType; |
| 139 | return static_cast<C&>(*this); |
| 140 | } |
| 141 | |
Eric Newberry | 1aa3e1e | 2016-09-28 20:27:45 -0700 | [diff] [blame^] | 142 | uint64_t |
| 143 | getFlags() const |
| 144 | { |
| 145 | return m_flags; |
| 146 | } |
| 147 | |
| 148 | C& |
| 149 | setFlags(uint64_t flags) |
| 150 | { |
| 151 | wireReset(); |
| 152 | m_flags = flags; |
| 153 | return static_cast<C&>(*this); |
| 154 | } |
| 155 | |
| 156 | bool |
| 157 | getFlagBit(size_t bit) const |
| 158 | { |
| 159 | if (bit >= 64) { |
| 160 | BOOST_THROW_EXCEPTION(std::out_of_range("bit must be within range [0, 64)")); |
| 161 | } |
| 162 | |
| 163 | return m_flags & (1 << bit); |
| 164 | } |
| 165 | |
| 166 | C& |
| 167 | setFlagBit(size_t bit, bool value) |
| 168 | { |
| 169 | if (bit >= 64) { |
| 170 | BOOST_THROW_EXCEPTION(std::out_of_range("bit must be within range [0, 64)")); |
| 171 | } |
| 172 | |
| 173 | wireReset(); |
| 174 | |
| 175 | if (value) { |
| 176 | m_flags |= (1 << bit); |
| 177 | } |
| 178 | else { |
| 179 | m_flags &= ~(1 << bit); |
| 180 | } |
| 181 | |
| 182 | return static_cast<C&>(*this); |
| 183 | } |
| 184 | |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 185 | protected: |
| 186 | virtual void |
| 187 | wireReset() const = 0; |
| 188 | |
| 189 | protected: |
| 190 | uint64_t m_faceId; |
| 191 | std::string m_remoteUri; |
| 192 | std::string m_localUri; |
| 193 | FaceScope m_faceScope; |
| 194 | FacePersistency m_facePersistency; |
| 195 | LinkType m_linkType; |
Eric Newberry | 1aa3e1e | 2016-09-28 20:27:45 -0700 | [diff] [blame^] | 196 | uint64_t m_flags; |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 197 | }; |
| 198 | |
| 199 | } // namespace nfd |
| 200 | } // namespace ndn |
| 201 | |
| 202 | #endif // NDN_MGMT_NFD_FACE_TRAITS_HPP |