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