Junxiao Shi | 05dd444 | 2017-02-06 22:50:07 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | 0c63c63 | 2017-12-05 11:17:09 -0500 | [diff] [blame] | 2 | /* |
Davide Pesavento | 4064127 | 2023-03-16 13:31:12 -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 | #include "face-helpers.hpp" |
Junxiao Shi | 05dd444 | 2017-02-06 22:50:07 +0000 | [diff] [blame] | 27 | #include "format-helpers.hpp" |
Davide Pesavento | 87fc0f8 | 2018-04-11 23:43:51 -0400 | [diff] [blame] | 28 | |
Davide Pesavento | 4064127 | 2023-03-16 13:31:12 -0400 | [diff] [blame] | 29 | #include <ndn-cxx/mgmt/nfd/status-dataset.hpp> |
Junxiao Shi | 05dd444 | 2017-02-06 22:50:07 +0000 | [diff] [blame] | 30 | #include <ndn-cxx/util/logger.hpp> |
| 31 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 32 | namespace nfd::tools::nfdc { |
Junxiao Shi | 05dd444 | 2017-02-06 22:50:07 +0000 | [diff] [blame] | 33 | |
| 34 | NDN_LOG_INIT(nfdc.FindFace); |
| 35 | |
| 36 | FindFace::FindFace(ExecuteContext& ctx) |
| 37 | : m_ctx(ctx) |
| 38 | { |
| 39 | } |
| 40 | |
| 41 | FindFace::Code |
| 42 | FindFace::execute(const FaceUri& faceUri, bool allowMulti) |
| 43 | { |
| 44 | FaceQueryFilter filter; |
| 45 | filter.setRemoteUri(faceUri.toString()); |
Junxiao Shi | 084b795 | 2017-02-26 22:00:53 +0000 | [diff] [blame] | 46 | return this->execute(filter, allowMulti); |
Junxiao Shi | 05dd444 | 2017-02-06 22:50:07 +0000 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | FindFace::Code |
| 50 | FindFace::execute(uint64_t faceId) |
| 51 | { |
| 52 | FaceQueryFilter filter; |
| 53 | filter.setFaceId(faceId); |
| 54 | return this->execute(filter); |
| 55 | } |
| 56 | |
| 57 | FindFace::Code |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 58 | FindFace::execute(const std::any& faceIdOrUri, bool allowMulti) |
Junxiao Shi | 918e5d4 | 2017-02-25 03:58:21 +0000 | [diff] [blame] | 59 | { |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 60 | const uint64_t* faceId = std::any_cast<uint64_t>(&faceIdOrUri); |
Junxiao Shi | 918e5d4 | 2017-02-25 03:58:21 +0000 | [diff] [blame] | 61 | if (faceId != nullptr) { |
| 62 | return this->execute(*faceId); |
| 63 | } |
| 64 | else { |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 65 | return this->execute(std::any_cast<FaceUri>(faceIdOrUri), allowMulti); |
Junxiao Shi | 918e5d4 | 2017-02-25 03:58:21 +0000 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | |
| 69 | FindFace::Code |
Junxiao Shi | 05dd444 | 2017-02-06 22:50:07 +0000 | [diff] [blame] | 70 | FindFace::execute(const FaceQueryFilter& filter, bool allowMulti) |
| 71 | { |
| 72 | BOOST_ASSERT(m_res == Code::NOT_STARTED); |
| 73 | m_res = Code::IN_PROGRESS; |
| 74 | m_filter = filter; |
| 75 | |
| 76 | if (m_filter.hasRemoteUri()) { |
Eric Newberry | d656aff | 2020-04-03 00:30:38 -0700 | [diff] [blame] | 77 | auto remoteUri = canonize("remote FaceUri", FaceUri(m_filter.getRemoteUri())); |
Junxiao Shi | 05dd444 | 2017-02-06 22:50:07 +0000 | [diff] [blame] | 78 | if (!remoteUri) { |
Junxiao Shi | 05dd444 | 2017-02-06 22:50:07 +0000 | [diff] [blame] | 79 | return m_res; |
| 80 | } |
| 81 | m_filter.setRemoteUri(remoteUri->toString()); |
| 82 | } |
| 83 | |
Junxiao Shi | 36e5429 | 2017-02-17 18:43:16 +0000 | [diff] [blame] | 84 | if (m_filter.hasLocalUri()) { |
Eric Newberry | d656aff | 2020-04-03 00:30:38 -0700 | [diff] [blame] | 85 | auto localUri = canonize("local FaceUri", FaceUri(m_filter.getLocalUri())); |
Junxiao Shi | 36e5429 | 2017-02-17 18:43:16 +0000 | [diff] [blame] | 86 | if (!localUri) { |
Junxiao Shi | 36e5429 | 2017-02-17 18:43:16 +0000 | [diff] [blame] | 87 | return m_res; |
| 88 | } |
| 89 | m_filter.setLocalUri(localUri->toString()); |
| 90 | } |
Junxiao Shi | 05dd444 | 2017-02-06 22:50:07 +0000 | [diff] [blame] | 91 | |
| 92 | this->query(); |
| 93 | if (m_res == Code::OK) { |
| 94 | if (m_results.size() == 0) { |
| 95 | m_res = Code::NOT_FOUND; |
| 96 | m_errorReason = "Face not found"; |
| 97 | } |
| 98 | else if (m_results.size() > 1 && !allowMulti) { |
| 99 | m_res = Code::AMBIGUOUS; |
| 100 | m_errorReason = "Multiple faces match the query"; |
| 101 | } |
| 102 | } |
| 103 | return m_res; |
| 104 | } |
| 105 | |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 106 | std::optional<FaceUri> |
Eric Newberry | d656aff | 2020-04-03 00:30:38 -0700 | [diff] [blame] | 107 | FindFace::canonize(const std::string& fieldName, const FaceUri& uri) |
Junxiao Shi | 05dd444 | 2017-02-06 22:50:07 +0000 | [diff] [blame] | 108 | { |
Eric Newberry | d656aff | 2020-04-03 00:30:38 -0700 | [diff] [blame] | 109 | // We use a wrapper because we want to accept FaceUris that cannot be canonized |
| 110 | if (!FaceUri::canCanonize(uri.getScheme())) { |
| 111 | NDN_LOG_DEBUG("Using " << fieldName << "=" << uri << " without canonization"); |
| 112 | return uri; |
Junxiao Shi | 05dd444 | 2017-02-06 22:50:07 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 115 | auto [result, error] = nfdc::canonize(m_ctx, uri); |
Eric Newberry | d656aff | 2020-04-03 00:30:38 -0700 | [diff] [blame] | 116 | if (result) { |
| 117 | // Canonization succeeded |
| 118 | return result; |
| 119 | } |
| 120 | else { |
| 121 | // Canonization failed |
| 122 | std::tie(m_res, m_errorReason) = canonizeErrorHelper(uri, error); |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 123 | return std::nullopt; |
Eric Newberry | d656aff | 2020-04-03 00:30:38 -0700 | [diff] [blame] | 124 | } |
Junxiao Shi | 05dd444 | 2017-02-06 22:50:07 +0000 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | void |
| 128 | FindFace::query() |
| 129 | { |
Davide Pesavento | 990620a | 2022-06-05 00:25:53 -0400 | [diff] [blame] | 130 | auto datasetCb = [this] (const auto& result) { |
Junxiao Shi | 36e5429 | 2017-02-17 18:43:16 +0000 | [diff] [blame] | 131 | m_res = Code::OK; |
| 132 | m_results = result; |
| 133 | }; |
Davide Pesavento | 990620a | 2022-06-05 00:25:53 -0400 | [diff] [blame] | 134 | auto failureCb = [this] (uint32_t code, const auto& reason) { |
Junxiao Shi | 36e5429 | 2017-02-17 18:43:16 +0000 | [diff] [blame] | 135 | m_res = Code::ERROR; |
Alexander Afanasyev | 0c63c63 | 2017-12-05 11:17:09 -0500 | [diff] [blame] | 136 | m_errorReason = "Error " + to_string(code) + " when querying face: " + reason; |
Junxiao Shi | 36e5429 | 2017-02-17 18:43:16 +0000 | [diff] [blame] | 137 | }; |
| 138 | |
| 139 | if (m_filter.empty()) { |
| 140 | m_ctx.controller.fetch<ndn::nfd::FaceDataset>( |
| 141 | datasetCb, failureCb, m_ctx.makeCommandOptions()); |
| 142 | } |
| 143 | else { |
| 144 | m_ctx.controller.fetch<ndn::nfd::FaceQueryDataset>( |
| 145 | m_filter, datasetCb, failureCb, m_ctx.makeCommandOptions()); |
| 146 | } |
Junxiao Shi | 05dd444 | 2017-02-06 22:50:07 +0000 | [diff] [blame] | 147 | m_ctx.face.processEvents(); |
| 148 | } |
| 149 | |
Junxiao Shi | 1d62e62 | 2017-03-08 22:39:28 +0000 | [diff] [blame] | 150 | std::set<uint64_t> |
| 151 | FindFace::getFaceIds() const |
| 152 | { |
| 153 | std::set<uint64_t> faceIds; |
| 154 | for (const FaceStatus& faceStatus : m_results) { |
| 155 | faceIds.insert(faceStatus.getFaceId()); |
| 156 | } |
| 157 | return faceIds; |
| 158 | } |
| 159 | |
Junxiao Shi | 05dd444 | 2017-02-06 22:50:07 +0000 | [diff] [blame] | 160 | const FaceStatus& |
| 161 | FindFace::getFaceStatus() const |
| 162 | { |
| 163 | BOOST_ASSERT(m_results.size() == 1); |
| 164 | return m_results.front(); |
| 165 | } |
| 166 | |
| 167 | void |
| 168 | FindFace::printDisambiguation(std::ostream& os, DisambiguationStyle style) const |
| 169 | { |
| 170 | text::Separator sep(" ", ", "); |
| 171 | for (const auto& item : m_results) { |
| 172 | os << sep; |
| 173 | switch (style) { |
| 174 | case DisambiguationStyle::LOCAL_URI: |
| 175 | os << item.getFaceId() << " (local=" << item.getLocalUri() << ')'; |
| 176 | break; |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | |
Davide Pesavento | 990620a | 2022-06-05 00:25:53 -0400 | [diff] [blame] | 181 | std::pair<std::optional<FaceUri>, std::string> |
| 182 | canonize(ExecuteContext& ctx, const FaceUri& uri) |
| 183 | { |
| 184 | std::optional<FaceUri> result; |
| 185 | std::string error; |
| 186 | uri.canonize( |
| 187 | [&result] (const auto& canonicalUri) { result = canonicalUri; }, |
| 188 | [&error] (const auto& errorReason) { error = errorReason; }, |
| 189 | ctx.face.getIoService(), ctx.getTimeout()); |
| 190 | ctx.face.processEvents(); |
| 191 | |
| 192 | return {result, error}; |
| 193 | } |
| 194 | |
| 195 | std::pair<FindFace::Code, std::string> |
| 196 | canonizeErrorHelper(const FaceUri& uri, |
| 197 | const std::string& error, |
| 198 | const std::string& field) |
| 199 | { |
| 200 | std::string msg = "Error during canonization of "; |
| 201 | if (!field.empty()) { |
| 202 | msg += field + " "; |
| 203 | } |
| 204 | msg += "'" + uri.toString() + "': " + error; |
| 205 | return {FindFace::Code::CANONIZE_ERROR, msg}; |
| 206 | } |
| 207 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 208 | } // namespace nfd::tools::nfdc |