Junxiao Shi | 05dd444 | 2017-02-06 22:50:07 +0000 | [diff] [blame^] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014-2017, Regents of the University of California, |
| 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 | #include "find-face.hpp" |
| 27 | #include "format-helpers.hpp" |
| 28 | #include <ndn-cxx/util/logger.hpp> |
| 29 | |
| 30 | namespace nfd { |
| 31 | namespace tools { |
| 32 | namespace nfdc { |
| 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()); |
| 46 | return this->execute(filter); |
| 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 |
| 58 | FindFace::execute(const FaceQueryFilter& filter, bool allowMulti) |
| 59 | { |
| 60 | BOOST_ASSERT(m_res == Code::NOT_STARTED); |
| 61 | m_res = Code::IN_PROGRESS; |
| 62 | m_filter = filter; |
| 63 | |
| 64 | if (m_filter.hasRemoteUri()) { |
| 65 | auto remoteUri = this->canonize("remote", FaceUri(m_filter.getRemoteUri())); |
| 66 | if (!remoteUri) { |
| 67 | m_res = Code::CANONIZE_ERROR; |
| 68 | return m_res; |
| 69 | } |
| 70 | m_filter.setRemoteUri(remoteUri->toString()); |
| 71 | } |
| 72 | |
| 73 | ///\todo #3864 canonize localUri |
| 74 | |
| 75 | this->query(); |
| 76 | if (m_res == Code::OK) { |
| 77 | if (m_results.size() == 0) { |
| 78 | m_res = Code::NOT_FOUND; |
| 79 | m_errorReason = "Face not found"; |
| 80 | } |
| 81 | else if (m_results.size() > 1 && !allowMulti) { |
| 82 | m_res = Code::AMBIGUOUS; |
| 83 | m_errorReason = "Multiple faces match the query"; |
| 84 | } |
| 85 | } |
| 86 | return m_res; |
| 87 | } |
| 88 | |
| 89 | ndn::optional<FaceUri> |
| 90 | FindFace::canonize(const std::string& fieldName, const FaceUri& input) |
| 91 | { |
| 92 | if (!FaceUri::canCanonize(input.getScheme())) { |
| 93 | NDN_LOG_DEBUG("Using " << fieldName << '=' << input << " without canonization"); |
| 94 | return input; |
| 95 | } |
| 96 | |
| 97 | ndn::optional<FaceUri> result; |
| 98 | input.canonize( |
| 99 | [&result] (const FaceUri& canonicalUri) { result = canonicalUri; }, |
| 100 | [this, fieldName] (const std::string& errorReason) { |
| 101 | m_errorReason = "Error during " + fieldName + " FaceUri canonization: " + errorReason; |
| 102 | }, |
| 103 | m_ctx.face.getIoService(), m_ctx.getTimeout()); |
| 104 | m_ctx.face.processEvents(); |
| 105 | |
| 106 | return result; |
| 107 | } |
| 108 | |
| 109 | void |
| 110 | FindFace::query() |
| 111 | { |
| 112 | m_ctx.controller.fetch<ndn::nfd::FaceQueryDataset>( |
| 113 | m_filter, |
| 114 | [this] (const std::vector<ndn::nfd::FaceStatus>& result) { |
| 115 | m_res = Code::OK; |
| 116 | m_results = result; |
| 117 | }, |
| 118 | [this] (uint32_t code, const std::string& reason) { |
| 119 | m_res = Code::ERROR; |
| 120 | m_errorReason = "Error " + std::to_string(code) + " when querying face: " + reason; |
| 121 | }, |
| 122 | m_ctx.makeCommandOptions()); |
| 123 | m_ctx.face.processEvents(); |
| 124 | } |
| 125 | |
| 126 | const FaceStatus& |
| 127 | FindFace::getFaceStatus() const |
| 128 | { |
| 129 | BOOST_ASSERT(m_results.size() == 1); |
| 130 | return m_results.front(); |
| 131 | } |
| 132 | |
| 133 | void |
| 134 | FindFace::printDisambiguation(std::ostream& os, DisambiguationStyle style) const |
| 135 | { |
| 136 | text::Separator sep(" ", ", "); |
| 137 | for (const auto& item : m_results) { |
| 138 | os << sep; |
| 139 | switch (style) { |
| 140 | case DisambiguationStyle::LOCAL_URI: |
| 141 | os << item.getFaceId() << " (local=" << item.getLocalUri() << ')'; |
| 142 | break; |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | } // namespace nfdc |
| 148 | } // namespace tools |
| 149 | } // namespace nfd |