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