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