blob: f0bf841746b877c6f5bc5a200c4b739d0b3a877f [file] [log] [blame]
Junxiao Shid243d712016-08-19 06:45:31 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shib347b7f2017-07-23 14:01:58 +00002/*
3 * Copyright (c) 2014-2017, Regents of the University of California,
Junxiao Shid243d712016-08-19 06:45:31 +00004 * 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 "face-id-fetcher.hpp"
27
28#include <boost/lexical_cast.hpp>
29#include <boost/regex.hpp>
30
Junxiao Shi25c6ce42016-09-09 13:49:59 +000031#include <ndn-cxx/mgmt/nfd/face-query-filter.hpp>
32#include <ndn-cxx/mgmt/nfd/face-status.hpp>
Junxiao Shib347b7f2017-07-23 14:01:58 +000033#include <ndn-cxx/security/validator-null.hpp>
Junxiao Shid243d712016-08-19 06:45:31 +000034#include <ndn-cxx/util/segment-fetcher.hpp>
35
36namespace nfd {
37namespace tools {
38namespace nfdc {
39
40FaceIdFetcher::FaceIdFetcher(ndn::Face& face,
41 ndn::nfd::Controller& controller,
42 bool allowCreate,
43 const SuccessCallback& onSucceed,
44 const FailureCallback& onFail)
45 : m_face(face)
46 , m_controller(controller)
47 , m_allowCreate(allowCreate)
48 , m_onSucceed(onSucceed)
49 , m_onFail(onFail)
50{
51}
52
53void
54FaceIdFetcher::start(ndn::Face& face,
55 ndn::nfd::Controller& controller,
56 const std::string& input,
57 bool allowCreate,
58 const SuccessCallback& onSucceed,
59 const FailureCallback& onFail)
60{
61 // 1. Try parse input as FaceId, if input is FaceId, succeed with parsed FaceId
62 // 2. Try parse input as FaceUri, if input is not FaceUri, fail
63 // 3. Canonize faceUri
64 // 4. If canonization fails, fail
65 // 5. Query for face
66 // 6. If query succeeds and finds a face, succeed with found FaceId
67 // 7. Create face
68 // 8. If face creation succeeds, succeed with created FaceId
69 // 9. Fail
70
71 boost::regex e("^[a-z0-9]+\\:.*");
72 if (!boost::regex_match(input, e)) {
73 try {
74 uint32_t faceId = boost::lexical_cast<uint32_t>(input);
75 onSucceed(faceId);
76 return;
77 }
78 catch (const boost::bad_lexical_cast&) {
79 onFail("No valid faceId or faceUri is provided");
80 return;
81 }
82 }
83 else {
84 FaceUri faceUri;
85 if (!faceUri.parse(input)) {
86 onFail("FaceUri parse failed");
87 return;
88 }
89
90 auto fetcher = new FaceIdFetcher(std::ref(face), std::ref(controller),
91 allowCreate, onSucceed, onFail);
92 fetcher->startGetFaceId(faceUri);
93 }
94}
95
96void
97FaceIdFetcher::startGetFaceId(const FaceUri& faceUri)
98{
99 faceUri.canonize(bind(&FaceIdFetcher::onCanonizeSuccess, this, _1),
100 bind(&FaceIdFetcher::onCanonizeFailure, this, _1),
101 m_face.getIoService(), time::seconds(4));
102}
103
104void
105FaceIdFetcher::onCanonizeSuccess(const FaceUri& canonicalUri)
106{
107 ndn::Name queryName("/localhost/nfd/faces/query");
108 ndn::nfd::FaceQueryFilter queryFilter;
109 queryFilter.setRemoteUri(canonicalUri.toString());
110 queryName.append(queryFilter.wireEncode());
111
112 ndn::Interest interestPacket(queryName);
113 interestPacket.setMustBeFresh(true);
114 interestPacket.setInterestLifetime(time::milliseconds(4000));
115 auto interest = std::make_shared<ndn::Interest>(interestPacket);
116
117 ndn::util::SegmentFetcher::fetch(
Junxiao Shib347b7f2017-07-23 14:01:58 +0000118 m_face, *interest, ndn::security::v2::getAcceptAllValidator(),
Junxiao Shid243d712016-08-19 06:45:31 +0000119 bind(&FaceIdFetcher::onQuerySuccess, this, _1, canonicalUri),
120 bind(&FaceIdFetcher::onQueryFailure, this, _1, canonicalUri));
121}
122
123void
124FaceIdFetcher::onCanonizeFailure(const std::string& reason)
125{
126 fail("Canonize faceUri failed : " + reason);
127}
128
129void
130FaceIdFetcher::onQuerySuccess(const ndn::ConstBufferPtr& data,
131 const FaceUri& canonicalUri)
132{
133 size_t offset = 0;
134 bool isOk = false;
135 ndn::Block block;
136 std::tie(isOk, block) = ndn::Block::fromBuffer(data, offset);
137
138 if (!isOk) {
139 if (m_allowCreate) {
140 startFaceCreate(canonicalUri);
141 }
142 else {
143 fail("Fail to find faceId");
144 }
145 }
146 else {
147 try {
148 ndn::nfd::FaceStatus status(block);
149 succeed(status.getFaceId());
150 }
151 catch (const ndn::tlv::Error& e) {
152 std::string errorMessage(e.what());
153 fail("ERROR: " + errorMessage);
154 }
155 }
156}
157
158void
159FaceIdFetcher::onQueryFailure(uint32_t errorCode,
160 const FaceUri& canonicalUri)
161{
162 std::stringstream ss;
163 ss << "Cannot fetch data (code " << errorCode << ")";
164 fail(ss.str());
165}
166
167void
Junxiao Shi29b41282016-08-22 03:47:02 +0000168FaceIdFetcher::onFaceCreateError(const ndn::nfd::ControlResponse& response,
Junxiao Shid243d712016-08-19 06:45:31 +0000169 const std::string& message)
170{
171 std::stringstream ss;
Junxiao Shi29b41282016-08-22 03:47:02 +0000172 ss << message << " : " << response.getText() << " (code " << response.getCode() << ")";
Junxiao Shid243d712016-08-19 06:45:31 +0000173 fail(ss.str());
174}
175
176void
177FaceIdFetcher::startFaceCreate(const FaceUri& canonicalUri)
178{
179 ndn::nfd::ControlParameters parameters;
180 parameters.setUri(canonicalUri.toString());
181
182 m_controller.start<ndn::nfd::FaceCreateCommand>(parameters,
183 [this] (const ndn::nfd::ControlParameters& result) { succeed(result.getFaceId()); },
Junxiao Shi29b41282016-08-22 03:47:02 +0000184 bind(&FaceIdFetcher::onFaceCreateError, this, _1, "Face creation failed"));
Junxiao Shid243d712016-08-19 06:45:31 +0000185}
186
187void
188FaceIdFetcher::succeed(uint32_t faceId)
189{
190 m_onSucceed(faceId);
191 delete this;
192}
193
194void
195FaceIdFetcher::fail(const std::string& reason)
196{
197 m_onFail(reason);
198 delete this;
199}
200
201} // namespace nfdc
202} // namespace tools
203} // namespace nfd