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 | 2c9d2ca | 2024-01-27 16:36:51 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2024, 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 | |
Davide Pesavento | 2c9d2ca | 2024-01-27 16:36:51 -0500 | [diff] [blame] | 29 | #include "core/common.hpp" |
Junxiao Shi | 05dd444 | 2017-02-06 22:50:07 +0000 | [diff] [blame] | 30 | #include "execute-command.hpp" |
| 31 | |
Davide Pesavento | 4064127 | 2023-03-16 13:31:12 -0400 | [diff] [blame] | 32 | #include <ndn-cxx/mgmt/nfd/face-query-filter.hpp> |
| 33 | #include <ndn-cxx/mgmt/nfd/face-status.hpp> |
Davide Pesavento | 2c9d2ca | 2024-01-27 16:36:51 -0500 | [diff] [blame] | 34 | #include <ndn-cxx/net/face-uri.hpp> |
| 35 | |
| 36 | #include <set> |
Davide Pesavento | 4064127 | 2023-03-16 13:31:12 -0400 | [diff] [blame] | 37 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 38 | namespace nfd::tools::nfdc { |
Junxiao Shi | 05dd444 | 2017-02-06 22:50:07 +0000 | [diff] [blame] | 39 | |
Davide Pesavento | 2c9d2ca | 2024-01-27 16:36:51 -0500 | [diff] [blame] | 40 | using ndn::FaceUri; |
Junxiao Shi | 05dd444 | 2017-02-06 22:50:07 +0000 | [diff] [blame] | 41 | using ndn::nfd::FaceQueryFilter; |
| 42 | using ndn::nfd::FaceStatus; |
| 43 | |
Davide Pesavento | 990620a | 2022-06-05 00:25:53 -0400 | [diff] [blame] | 44 | /** |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 45 | * \brief Procedure to find a face. |
Junxiao Shi | 05dd444 | 2017-02-06 22:50:07 +0000 | [diff] [blame] | 46 | */ |
| 47 | class FindFace : noncopyable |
| 48 | { |
| 49 | public: |
| 50 | enum class Code { |
| 51 | OK = 0, ///< found exactly one face, or found multiple faces when allowMulti is true |
| 52 | ERROR = 1, ///< unspecified error |
| 53 | NOT_FOUND = 3, ///< found zero face |
| 54 | CANONIZE_ERROR = 4, ///< error during FaceUri canonization |
| 55 | AMBIGUOUS = 5, ///< found multiple faces and allowMulti is false |
| 56 | NOT_STARTED = -1, ///< for internal use |
| 57 | IN_PROGRESS = -2, ///< for internal use |
| 58 | }; |
| 59 | |
| 60 | enum class DisambiguationStyle |
| 61 | { |
| 62 | LOCAL_URI = 1 ///< print FaceId and LocalUri |
| 63 | }; |
| 64 | |
| 65 | explicit |
| 66 | FindFace(ExecuteContext& ctx); |
| 67 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 68 | /** \brief Find face by FaceUri. |
Junxiao Shi | 05dd444 | 2017-02-06 22:50:07 +0000 | [diff] [blame] | 69 | * \pre execute has not been invoked |
| 70 | */ |
| 71 | Code |
| 72 | execute(const FaceUri& faceUri, bool allowMulti = false); |
| 73 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 74 | /** \brief Find face by FaceId. |
Junxiao Shi | 05dd444 | 2017-02-06 22:50:07 +0000 | [diff] [blame] | 75 | * \pre execute has not been invoked |
| 76 | */ |
| 77 | Code |
| 78 | execute(uint64_t faceId); |
| 79 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 80 | /** \brief Find face by FaceId or FaceUri. |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 81 | * \param faceIdOrUri either a FaceId (uint64_t) or a FaceUri |
Junxiao Shi | 084b795 | 2017-02-26 22:00:53 +0000 | [diff] [blame] | 82 | * \param allowMulti effective only if \p faceIdOrUri contains a FaceUri |
Davide Pesavento | d6ea0b1 | 2023-03-13 21:35:03 -0400 | [diff] [blame] | 83 | * \throw std::bad_any_cast faceIdOrUri is neither uint64_t nor FaceUri |
Junxiao Shi | 918e5d4 | 2017-02-25 03:58:21 +0000 | [diff] [blame] | 84 | */ |
| 85 | Code |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 86 | execute(const std::any& faceIdOrUri, bool allowMulti = false); |
Junxiao Shi | 918e5d4 | 2017-02-25 03:58:21 +0000 | [diff] [blame] | 87 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 88 | /** \brief Find face by FaceQueryFilter. |
Junxiao Shi | 05dd444 | 2017-02-06 22:50:07 +0000 | [diff] [blame] | 89 | * \pre execute has not been invoked |
| 90 | */ |
| 91 | Code |
| 92 | execute(const FaceQueryFilter& filter, bool allowMulti = false); |
| 93 | |
| 94 | /** \return face status for all results |
| 95 | */ |
| 96 | const std::vector<FaceStatus>& |
| 97 | getResults() const |
| 98 | { |
| 99 | return m_results; |
| 100 | } |
| 101 | |
Junxiao Shi | 1d62e62 | 2017-03-08 22:39:28 +0000 | [diff] [blame] | 102 | /** \return FaceId for all results |
| 103 | */ |
| 104 | std::set<uint64_t> |
| 105 | getFaceIds() const; |
| 106 | |
Junxiao Shi | 05dd444 | 2017-02-06 22:50:07 +0000 | [diff] [blame] | 107 | /** \return a single face status |
| 108 | * \pre getResults().size() == 1 |
| 109 | */ |
| 110 | const FaceStatus& |
| 111 | getFaceStatus() const; |
| 112 | |
| 113 | uint64_t |
| 114 | getFaceId() const |
| 115 | { |
| 116 | return this->getFaceStatus().getFaceId(); |
| 117 | } |
| 118 | |
| 119 | const std::string& |
| 120 | getErrorReason() const |
| 121 | { |
| 122 | return m_errorReason; |
| 123 | } |
| 124 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 125 | /** \brief Print results for disambiguation. |
Junxiao Shi | 05dd444 | 2017-02-06 22:50:07 +0000 | [diff] [blame] | 126 | */ |
| 127 | void |
| 128 | printDisambiguation(std::ostream& os, DisambiguationStyle style) const; |
| 129 | |
| 130 | private: |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 131 | std::optional<FaceUri> |
Eric Newberry | d656aff | 2020-04-03 00:30:38 -0700 | [diff] [blame] | 132 | canonize(const std::string& fieldName, const FaceUri& uri); |
Junxiao Shi | 05dd444 | 2017-02-06 22:50:07 +0000 | [diff] [blame] | 133 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 134 | /** \brief Retrieve FaceStatus from filter. |
Junxiao Shi | 05dd444 | 2017-02-06 22:50:07 +0000 | [diff] [blame] | 135 | * \post m_res == Code::OK and m_results is populated if retrieval succeeds |
| 136 | * \post m_res == Code::ERROR and m_errorReason is set if retrieval fails |
| 137 | */ |
| 138 | void |
| 139 | query(); |
| 140 | |
| 141 | private: |
| 142 | ExecuteContext& m_ctx; |
| 143 | FaceQueryFilter m_filter; |
| 144 | Code m_res = Code::NOT_STARTED; |
| 145 | std::vector<FaceStatus> m_results; |
| 146 | std::string m_errorReason; |
| 147 | }; |
| 148 | |
Davide Pesavento | 990620a | 2022-06-05 00:25:53 -0400 | [diff] [blame] | 149 | /** |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 150 | * \brief Canonize a FaceUri. |
Davide Pesavento | 990620a | 2022-06-05 00:25:53 -0400 | [diff] [blame] | 151 | * \return canonical FaceUri (nullopt on failure) and error string |
| 152 | */ |
| 153 | std::pair<std::optional<FaceUri>, std::string> |
| 154 | canonize(ExecuteContext& ctx, const FaceUri& uri); |
| 155 | |
| 156 | /** |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 157 | * \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] | 158 | * \param uri The FaceUri |
| 159 | * \param error The error string returned by the canonization process |
| 160 | * \param field An optional field identifier to include with the message |
| 161 | * \return exit code and error message |
| 162 | */ |
| 163 | std::pair<FindFace::Code, std::string> |
| 164 | canonizeErrorHelper(const FaceUri& uri, |
| 165 | const std::string& error, |
| 166 | const std::string& field = ""); |
| 167 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 168 | } // namespace nfd::tools::nfdc |
Junxiao Shi | 05dd444 | 2017-02-06 22:50:07 +0000 | [diff] [blame] | 169 | |
Davide Pesavento | 990620a | 2022-06-05 00:25:53 -0400 | [diff] [blame] | 170 | #endif // NFD_TOOLS_NFDC_FACE_HELPERS_HPP |