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