find-face.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2020, 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 "canonizer.hpp"
28 #include "format-helpers.hpp"
29 
30 #include <ndn-cxx/util/logger.hpp>
31 
32 namespace nfd {
33 namespace tools {
34 namespace nfdc {
35 
36 NDN_LOG_INIT(nfdc.FindFace);
37 
39  : m_ctx(ctx)
40 {
41 }
42 
44 FindFace::execute(const FaceUri& faceUri, bool allowMulti)
45 {
46  FaceQueryFilter filter;
47  filter.setRemoteUri(faceUri.toString());
48  return this->execute(filter, allowMulti);
49 }
50 
52 FindFace::execute(uint64_t faceId)
53 {
54  FaceQueryFilter filter;
55  filter.setFaceId(faceId);
56  return this->execute(filter);
57 }
58 
60 FindFace::execute(const ndn::any& faceIdOrUri, bool allowMulti)
61 {
62  const uint64_t* faceId = ndn::any_cast<uint64_t>(&faceIdOrUri);
63  if (faceId != nullptr) {
64  return this->execute(*faceId);
65  }
66  else {
67  return this->execute(ndn::any_cast<FaceUri>(faceIdOrUri), allowMulti);
68  }
69 }
70 
72 FindFace::execute(const FaceQueryFilter& filter, bool allowMulti)
73 {
74  BOOST_ASSERT(m_res == Code::NOT_STARTED);
75  m_res = Code::IN_PROGRESS;
76  m_filter = filter;
77 
78  if (m_filter.hasRemoteUri()) {
79  auto remoteUri = canonize("remote FaceUri", FaceUri(m_filter.getRemoteUri()));
80  if (!remoteUri) {
81  return m_res;
82  }
83  m_filter.setRemoteUri(remoteUri->toString());
84  }
85 
86  if (m_filter.hasLocalUri()) {
87  auto localUri = canonize("local FaceUri", FaceUri(m_filter.getLocalUri()));
88  if (!localUri) {
89  return m_res;
90  }
91  m_filter.setLocalUri(localUri->toString());
92  }
93 
94  this->query();
95  if (m_res == Code::OK) {
96  if (m_results.size() == 0) {
97  m_res = Code::NOT_FOUND;
98  m_errorReason = "Face not found";
99  }
100  else if (m_results.size() > 1 && !allowMulti) {
101  m_res = Code::AMBIGUOUS;
102  m_errorReason = "Multiple faces match the query";
103  }
104  }
105  return m_res;
106 }
107 
108 optional<FaceUri>
109 FindFace::canonize(const std::string& fieldName, const FaceUri& uri)
110 {
111  // We use a wrapper because we want to accept FaceUris that cannot be canonized
112  if (!FaceUri::canCanonize(uri.getScheme())) {
113  NDN_LOG_DEBUG("Using " << fieldName << "=" << uri << " without canonization");
114  return uri;
115  }
116 
117  optional<FaceUri> result;
118  std::string error;
119  std::tie(result, error) = nfdc::canonize(m_ctx, uri);
120 
121  if (result) {
122  // Canonization succeeded
123  return result;
124  }
125  else {
126  // Canonization failed
127  std::tie(m_res, m_errorReason) = canonizeErrorHelper(uri, error);
128  return nullopt;
129  }
130 }
131 
132 void
133 FindFace::query()
134 {
135  auto datasetCb = [this] (const std::vector<ndn::nfd::FaceStatus>& result) {
136  m_res = Code::OK;
137  m_results = result;
138  };
139  auto failureCb = [this] (uint32_t code, const std::string& reason) {
140  m_res = Code::ERROR;
141  m_errorReason = "Error " + to_string(code) + " when querying face: " + reason;
142  };
143 
144  if (m_filter.empty()) {
145  m_ctx.controller.fetch<ndn::nfd::FaceDataset>(
146  datasetCb, failureCb, m_ctx.makeCommandOptions());
147  }
148  else {
149  m_ctx.controller.fetch<ndn::nfd::FaceQueryDataset>(
150  m_filter, datasetCb, failureCb, m_ctx.makeCommandOptions());
151  }
152  m_ctx.face.processEvents();
153 }
154 
155 std::set<uint64_t>
157 {
158  std::set<uint64_t> faceIds;
159  for (const FaceStatus& faceStatus : m_results) {
160  faceIds.insert(faceStatus.getFaceId());
161  }
162  return faceIds;
163 }
164 
165 const FaceStatus&
167 {
168  BOOST_ASSERT(m_results.size() == 1);
169  return m_results.front();
170 }
171 
172 void
174 {
175  text::Separator sep(" ", ", ");
176  for (const auto& item : m_results) {
177  os << sep;
178  switch (style) {
180  os << item.getFaceId() << " (local=" << item.getLocalUri() << ')';
181  break;
182  }
183  }
184 }
185 
186 } // namespace nfdc
187 } // namespace tools
188 } // namespace nfd
const FaceStatus & getFaceStatus() const
Definition: find-face.cpp:166
FindFace(ExecuteContext &ctx)
Definition: find-face.cpp:38
context for command execution
NDN_LOG_INIT(nfdc.CommandDefinition)
found exactly one face, or found multiple faces when allowMulti is true
ndn::nfd::CommandOptions makeCommandOptions() const
found multiple faces and allowMulti is false
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
Definition: algorithm.hpp:32
Code execute(const FaceUri &faceUri, bool allowMulti=false)
find face by FaceUri
Definition: find-face.cpp:44
std::pair< FindFace::Code, std::string > canonizeErrorHelper(const FaceUri &uri, const std::string &error, const std::string &field)
helper to generate exit code and error message for face canonization failures
Definition: canonizer.cpp:47
std::pair< optional< FaceUri >, std::string > canonize(ExecuteContext &ctx, const FaceUri &uri)
canonize FaceUri
Definition: canonizer.cpp:33
std::set< uint64_t > getFaceIds() const
Definition: find-face.cpp:156
print different string on first and subsequent usage
void printDisambiguation(std::ostream &os, DisambiguationStyle style) const
print results for disambiguation
Definition: find-face.cpp:173