Junxiao Shi | 05dd444 | 2017-02-06 22:50:07 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 87fc0f8 | 2018-04-11 23:43:51 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | d6ea0b1 | 2023-03-13 21:35:03 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2023, Regents of the University of California, |
Junxiao Shi | 05dd444 | 2017-02-06 22:50:07 +0000 | [diff] [blame] | 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis. |
| 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 24 | */ |
| 25 | |
Davide Pesavento | 990620a | 2022-06-05 00:25:53 -0400 | [diff] [blame] | 26 | #ifndef NFD_TOOLS_NFDC_FACE_HELPERS_HPP |
| 27 | #define NFD_TOOLS_NFDC_FACE_HELPERS_HPP |
Junxiao Shi | 05dd444 | 2017-02-06 22:50:07 +0000 | [diff] [blame] | 28 | |
| 29 | #include "execute-command.hpp" |
| 30 | |
Davide Pesavento | 4064127 | 2023-03-16 13:31:12 -0400 | [diff] [blame^] | 31 | #include <ndn-cxx/mgmt/nfd/face-query-filter.hpp> |
| 32 | #include <ndn-cxx/mgmt/nfd/face-status.hpp> |
| 33 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 34 | namespace nfd::tools::nfdc { |
Junxiao Shi | 05dd444 | 2017-02-06 22:50:07 +0000 | [diff] [blame] | 35 | |
| 36 | using ndn::nfd::FaceQueryFilter; |
| 37 | using ndn::nfd::FaceStatus; |
| 38 | |
Davide Pesavento | 990620a | 2022-06-05 00:25:53 -0400 | [diff] [blame] | 39 | /** |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 40 | * \brief Procedure to find a face. |
Junxiao Shi | 05dd444 | 2017-02-06 22:50:07 +0000 | [diff] [blame] | 41 | */ |
| 42 | class FindFace : noncopyable |
| 43 | { |
| 44 | public: |
| 45 | enum class Code { |
| 46 | OK = 0, ///< found exactly one face, or found multiple faces when allowMulti is true |
| 47 | ERROR = 1, ///< unspecified error |
| 48 | NOT_FOUND = 3, ///< found zero face |
| 49 | CANONIZE_ERROR = 4, ///< error during FaceUri canonization |
| 50 | AMBIGUOUS = 5, ///< found multiple faces and allowMulti is false |
| 51 | NOT_STARTED = -1, ///< for internal use |
| 52 | IN_PROGRESS = -2, ///< for internal use |
| 53 | }; |
| 54 | |
| 55 | enum class DisambiguationStyle |
| 56 | { |
| 57 | LOCAL_URI = 1 ///< print FaceId and LocalUri |
| 58 | }; |
| 59 | |
| 60 | explicit |
| 61 | FindFace(ExecuteContext& ctx); |
| 62 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 63 | /** \brief Find face by FaceUri. |
Junxiao Shi | 05dd444 | 2017-02-06 22:50:07 +0000 | [diff] [blame] | 64 | * \pre execute has not been invoked |
| 65 | */ |
| 66 | Code |
| 67 | execute(const FaceUri& faceUri, bool allowMulti = false); |
| 68 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 69 | /** \brief Find face by FaceId. |
Junxiao Shi | 05dd444 | 2017-02-06 22:50:07 +0000 | [diff] [blame] | 70 | * \pre execute has not been invoked |
| 71 | */ |
| 72 | Code |
| 73 | execute(uint64_t faceId); |
| 74 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 75 | /** \brief Find face by FaceId or FaceUri. |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 76 | * \param faceIdOrUri either a FaceId (uint64_t) or a FaceUri |
Junxiao Shi | 084b795 | 2017-02-26 22:00:53 +0000 | [diff] [blame] | 77 | * \param allowMulti effective only if \p faceIdOrUri contains a FaceUri |
Davide Pesavento | d6ea0b1 | 2023-03-13 21:35:03 -0400 | [diff] [blame] | 78 | * \throw std::bad_any_cast faceIdOrUri is neither uint64_t nor FaceUri |
Junxiao Shi | 918e5d4 | 2017-02-25 03:58:21 +0000 | [diff] [blame] | 79 | */ |
| 80 | Code |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 81 | execute(const std::any& faceIdOrUri, bool allowMulti = false); |
Junxiao Shi | 918e5d4 | 2017-02-25 03:58:21 +0000 | [diff] [blame] | 82 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 83 | /** \brief Find face by FaceQueryFilter. |
Junxiao Shi | 05dd444 | 2017-02-06 22:50:07 +0000 | [diff] [blame] | 84 | * \pre execute has not been invoked |
| 85 | */ |
| 86 | Code |
| 87 | execute(const FaceQueryFilter& filter, bool allowMulti = false); |
| 88 | |
| 89 | /** \return face status for all results |
| 90 | */ |
| 91 | const std::vector<FaceStatus>& |
| 92 | getResults() const |
| 93 | { |
| 94 | return m_results; |
| 95 | } |
| 96 | |
Junxiao Shi | 1d62e62 | 2017-03-08 22:39:28 +0000 | [diff] [blame] | 97 | /** \return FaceId for all results |
| 98 | */ |
| 99 | std::set<uint64_t> |
| 100 | getFaceIds() const; |
| 101 | |
Junxiao Shi | 05dd444 | 2017-02-06 22:50:07 +0000 | [diff] [blame] | 102 | /** \return a single face status |
| 103 | * \pre getResults().size() == 1 |
| 104 | */ |
| 105 | const FaceStatus& |
| 106 | getFaceStatus() const; |
| 107 | |
| 108 | uint64_t |
| 109 | getFaceId() const |
| 110 | { |
| 111 | return this->getFaceStatus().getFaceId(); |
| 112 | } |
| 113 | |
| 114 | const std::string& |
| 115 | getErrorReason() const |
| 116 | { |
| 117 | return m_errorReason; |
| 118 | } |
| 119 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 120 | /** \brief Print results for disambiguation. |
Junxiao Shi | 05dd444 | 2017-02-06 22:50:07 +0000 | [diff] [blame] | 121 | */ |
| 122 | void |
| 123 | printDisambiguation(std::ostream& os, DisambiguationStyle style) const; |
| 124 | |
| 125 | private: |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 126 | std::optional<FaceUri> |
Eric Newberry | d656aff | 2020-04-03 00:30:38 -0700 | [diff] [blame] | 127 | canonize(const std::string& fieldName, const FaceUri& uri); |
Junxiao Shi | 05dd444 | 2017-02-06 22:50:07 +0000 | [diff] [blame] | 128 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 129 | /** \brief Retrieve FaceStatus from filter. |
Junxiao Shi | 05dd444 | 2017-02-06 22:50:07 +0000 | [diff] [blame] | 130 | * \post m_res == Code::OK and m_results is populated if retrieval succeeds |
| 131 | * \post m_res == Code::ERROR and m_errorReason is set if retrieval fails |
| 132 | */ |
| 133 | void |
| 134 | query(); |
| 135 | |
| 136 | private: |
| 137 | ExecuteContext& m_ctx; |
| 138 | FaceQueryFilter m_filter; |
| 139 | Code m_res = Code::NOT_STARTED; |
| 140 | std::vector<FaceStatus> m_results; |
| 141 | std::string m_errorReason; |
| 142 | }; |
| 143 | |
Davide Pesavento | 990620a | 2022-06-05 00:25:53 -0400 | [diff] [blame] | 144 | /** |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 145 | * \brief Canonize a FaceUri. |
Davide Pesavento | 990620a | 2022-06-05 00:25:53 -0400 | [diff] [blame] | 146 | * \return canonical FaceUri (nullopt on failure) and error string |
| 147 | */ |
| 148 | std::pair<std::optional<FaceUri>, std::string> |
| 149 | canonize(ExecuteContext& ctx, const FaceUri& uri); |
| 150 | |
| 151 | /** |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 152 | * \brief Helper to generate exit code and error message for face canonization failures. |
Davide Pesavento | 990620a | 2022-06-05 00:25:53 -0400 | [diff] [blame] | 153 | * \param uri The FaceUri |
| 154 | * \param error The error string returned by the canonization process |
| 155 | * \param field An optional field identifier to include with the message |
| 156 | * \return exit code and error message |
| 157 | */ |
| 158 | std::pair<FindFace::Code, std::string> |
| 159 | canonizeErrorHelper(const FaceUri& uri, |
| 160 | const std::string& error, |
| 161 | const std::string& field = ""); |
| 162 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 163 | } // namespace nfd::tools::nfdc |
Junxiao Shi | 05dd444 | 2017-02-06 22:50:07 +0000 | [diff] [blame] | 164 | |
Davide Pesavento | 990620a | 2022-06-05 00:25:53 -0400 | [diff] [blame] | 165 | #endif // NFD_TOOLS_NFDC_FACE_HELPERS_HPP |