blob: 520ea92f2ea6e47145dc8fe0306eeedf65dd4468 [file] [log] [blame]
Junxiao Shi05dd4442017-02-06 22:50:07 +00001/* -*- 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
30namespace nfd {
31namespace tools {
32namespace nfdc {
33
34NDN_LOG_INIT(nfdc.FindFace);
35
36FindFace::FindFace(ExecuteContext& ctx)
37 : m_ctx(ctx)
38{
39}
40
41FindFace::Code
42FindFace::execute(const FaceUri& faceUri, bool allowMulti)
43{
44 FaceQueryFilter filter;
45 filter.setRemoteUri(faceUri.toString());
46 return this->execute(filter);
47}
48
49FindFace::Code
50FindFace::execute(uint64_t faceId)
51{
52 FaceQueryFilter filter;
53 filter.setFaceId(faceId);
54 return this->execute(filter);
55}
56
57FindFace::Code
58FindFace::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
Junxiao Shi36e54292017-02-17 18:43:16 +000073 if (m_filter.hasLocalUri()) {
74 auto localUri = this->canonize("local", FaceUri(m_filter.getLocalUri()));
75 if (!localUri) {
76 m_res = Code::CANONIZE_ERROR;
77 return m_res;
78 }
79 m_filter.setLocalUri(localUri->toString());
80 }
Junxiao Shi05dd4442017-02-06 22:50:07 +000081
82 this->query();
83 if (m_res == Code::OK) {
84 if (m_results.size() == 0) {
85 m_res = Code::NOT_FOUND;
86 m_errorReason = "Face not found";
87 }
88 else if (m_results.size() > 1 && !allowMulti) {
89 m_res = Code::AMBIGUOUS;
90 m_errorReason = "Multiple faces match the query";
91 }
92 }
93 return m_res;
94}
95
96ndn::optional<FaceUri>
97FindFace::canonize(const std::string& fieldName, const FaceUri& input)
98{
99 if (!FaceUri::canCanonize(input.getScheme())) {
100 NDN_LOG_DEBUG("Using " << fieldName << '=' << input << " without canonization");
101 return input;
102 }
103
104 ndn::optional<FaceUri> result;
105 input.canonize(
106 [&result] (const FaceUri& canonicalUri) { result = canonicalUri; },
107 [this, fieldName] (const std::string& errorReason) {
108 m_errorReason = "Error during " + fieldName + " FaceUri canonization: " + errorReason;
109 },
110 m_ctx.face.getIoService(), m_ctx.getTimeout());
111 m_ctx.face.processEvents();
112
113 return result;
114}
115
116void
117FindFace::query()
118{
Junxiao Shi36e54292017-02-17 18:43:16 +0000119 auto datasetCb = [this] (const std::vector<ndn::nfd::FaceStatus>& result) {
120 m_res = Code::OK;
121 m_results = result;
122 };
123 auto failureCb = [this] (uint32_t code, const std::string& reason) {
124 m_res = Code::ERROR;
125 m_errorReason = "Error " + std::to_string(code) + " when querying face: " + reason;
126 };
127
128 if (m_filter.empty()) {
129 m_ctx.controller.fetch<ndn::nfd::FaceDataset>(
130 datasetCb, failureCb, m_ctx.makeCommandOptions());
131 }
132 else {
133 m_ctx.controller.fetch<ndn::nfd::FaceQueryDataset>(
134 m_filter, datasetCb, failureCb, m_ctx.makeCommandOptions());
135 }
Junxiao Shi05dd4442017-02-06 22:50:07 +0000136 m_ctx.face.processEvents();
137}
138
139const FaceStatus&
140FindFace::getFaceStatus() const
141{
142 BOOST_ASSERT(m_results.size() == 1);
143 return m_results.front();
144}
145
146void
147FindFace::printDisambiguation(std::ostream& os, DisambiguationStyle style) const
148{
149 text::Separator sep(" ", ", ");
150 for (const auto& item : m_results) {
151 os << sep;
152 switch (style) {
153 case DisambiguationStyle::LOCAL_URI:
154 os << item.getFaceId() << " (local=" << item.getLocalUri() << ')';
155 break;
156 }
157 }
158}
159
160} // namespace nfdc
161} // namespace tools
162} // namespace nfd