Junxiao Shi | d243d71 | 2016-08-19 06:45:31 +0000 | [diff] [blame^] | 1 | /* -*- 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 | |
| 35 | namespace nfd { |
| 36 | namespace tools { |
| 37 | namespace nfdc { |
| 38 | |
| 39 | FaceIdFetcher::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 | |
| 52 | void |
| 53 | FaceIdFetcher::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 | |
| 95 | void |
| 96 | FaceIdFetcher::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 | |
| 103 | void |
| 104 | FaceIdFetcher::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 | |
| 122 | void |
| 123 | FaceIdFetcher::onCanonizeFailure(const std::string& reason) |
| 124 | { |
| 125 | fail("Canonize faceUri failed : " + reason); |
| 126 | } |
| 127 | |
| 128 | void |
| 129 | FaceIdFetcher::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 | |
| 157 | void |
| 158 | FaceIdFetcher::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 | |
| 166 | void |
| 167 | FaceIdFetcher::onFaceCreateError(uint32_t code, |
| 168 | const std::string& error, |
| 169 | const std::string& message) |
| 170 | { |
| 171 | std::stringstream ss; |
| 172 | ss << message << " : " << error << " (code " << code << ")"; |
| 173 | fail(ss.str()); |
| 174 | } |
| 175 | |
| 176 | void |
| 177 | FaceIdFetcher::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()); }, |
| 184 | bind(&FaceIdFetcher::onFaceCreateError, this, _1, _2, "Face creation failed")); |
| 185 | } |
| 186 | |
| 187 | void |
| 188 | FaceIdFetcher::succeed(uint32_t faceId) |
| 189 | { |
| 190 | m_onSucceed(faceId); |
| 191 | delete this; |
| 192 | } |
| 193 | |
| 194 | void |
| 195 | FaceIdFetcher::fail(const std::string& reason) |
| 196 | { |
| 197 | m_onFail(reason); |
| 198 | delete this; |
| 199 | } |
| 200 | |
| 201 | } // namespace nfdc |
| 202 | } // namespace tools |
| 203 | } // namespace nfd |