Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Qi Zhao | 6fc57a6 | 2017-04-05 16:53:10 +0000 | [diff] [blame^] | 3 | * Copyright (c) 2014-2017, Regents of the University of California, |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 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 "base.hpp" |
| 27 | |
| 28 | namespace ndn { |
| 29 | namespace tools { |
| 30 | namespace autoconfig { |
| 31 | |
| 32 | Base::Base(Face& face, KeyChain& keyChain, const NextStageCallback& nextStageOnFailure) |
| 33 | : m_face(face) |
| 34 | , m_keyChain(keyChain) |
| 35 | , m_controller(face, keyChain) |
| 36 | , m_nextStageOnFailure(nextStageOnFailure) |
| 37 | { |
| 38 | } |
| 39 | |
| 40 | void |
| 41 | Base::connectToHub(const std::string& uri) |
| 42 | { |
Junxiao Shi | 52fa45c | 2016-11-29 21:18:13 +0000 | [diff] [blame] | 43 | FaceUri faceUri(uri); |
Alexander Afanasyev | 2a00194 | 2016-12-14 18:18:41 -0800 | [diff] [blame] | 44 | std::cerr << "About to connect to: " << uri << std::endl; |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 45 | |
| 46 | faceUri.canonize(bind(&Base::onCanonizeSuccess, this, _1), |
| 47 | bind(&Base::onCanonizeFailure, this, _1), |
| 48 | m_face.getIoService(), time::seconds(4)); |
| 49 | |
| 50 | } |
| 51 | |
| 52 | |
| 53 | void |
Junxiao Shi | 52fa45c | 2016-11-29 21:18:13 +0000 | [diff] [blame] | 54 | Base::onCanonizeSuccess(const FaceUri& canonicalUri) |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 55 | { |
Junxiao Shi | 52fa45c | 2016-11-29 21:18:13 +0000 | [diff] [blame] | 56 | m_controller.start<ndn::nfd::FaceCreateCommand>( |
| 57 | ControlParameters().setUri(canonicalUri.toString()), |
| 58 | bind(&Base::onHubConnectSuccess, this, _1), |
| 59 | bind(&Base::onHubConnectError, this, _1)); |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | void |
| 63 | Base::onCanonizeFailure(const std::string& reason) |
| 64 | { |
Junxiao Shi | 52fa45c | 2016-11-29 21:18:13 +0000 | [diff] [blame] | 65 | BOOST_THROW_EXCEPTION(Error("FaceUri canonization failed: " + reason)); |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | void |
Junxiao Shi | 52fa45c | 2016-11-29 21:18:13 +0000 | [diff] [blame] | 69 | Base::onHubConnectSuccess(const ControlParameters& resp) |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 70 | { |
| 71 | std::cerr << "Successfully created face: " << resp << std::endl; |
| 72 | |
Qi Zhao | 6fc57a6 | 2017-04-05 16:53:10 +0000 | [diff] [blame^] | 73 | registerAutoConfigNames(resp.getFaceId()); |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | void |
Junxiao Shi | 52fa45c | 2016-11-29 21:18:13 +0000 | [diff] [blame] | 77 | Base::onHubConnectError(const ControlResponse& response) |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 78 | { |
Qi Zhao | 6fc57a6 | 2017-04-05 16:53:10 +0000 | [diff] [blame^] | 79 | // If face exists, continue proceeding with the existing face |
| 80 | if (response.getCode() == 409) { |
| 81 | std::cerr << "Face exists. Proceeding with existing face: " << ControlParameters(response.getBody()) << std::endl; |
| 82 | |
| 83 | registerAutoConfigNames(ControlParameters(response.getBody()).getFaceId()); |
| 84 | } |
| 85 | // Otherwise, report the failure and throw out exception |
| 86 | else { |
| 87 | std::ostringstream os; |
| 88 | os << "Failed to create face: " << response.getText() << " (code: " << response.getCode() << ")"; |
| 89 | BOOST_THROW_EXCEPTION(Error(os.str())); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | void |
| 94 | Base::registerAutoConfigNames(uint64_t faceId) |
| 95 | { |
| 96 | static const Name TESTBED_PREFIX = "/ndn"; |
| 97 | registerPrefix(TESTBED_PREFIX, faceId); |
| 98 | |
| 99 | static const Name LOCALHOP_NFD_PREFIX = "/localhop/nfd"; |
| 100 | registerPrefix(LOCALHOP_NFD_PREFIX, faceId); |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | void |
| 104 | Base::registerPrefix(const Name& prefix, uint64_t faceId) |
| 105 | { |
| 106 | // Register a prefix in RIB |
Junxiao Shi | 52fa45c | 2016-11-29 21:18:13 +0000 | [diff] [blame] | 107 | m_controller.start<ndn::nfd::RibRegisterCommand>( |
| 108 | ControlParameters() |
| 109 | .setName(prefix) |
| 110 | .setFaceId(faceId) |
| 111 | .setOrigin(ndn::nfd::ROUTE_ORIGIN_AUTOCONF) |
| 112 | .setCost(100) |
| 113 | .setExpirationPeriod(time::milliseconds::max()), |
| 114 | bind(&Base::onPrefixRegistrationSuccess, this, _1), |
| 115 | bind(&Base::onPrefixRegistrationError, this, _1)); |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | void |
Junxiao Shi | 52fa45c | 2016-11-29 21:18:13 +0000 | [diff] [blame] | 119 | Base::onPrefixRegistrationSuccess(const ControlParameters& commandSuccessResult) |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 120 | { |
| 121 | std::cerr << "Successful in name registration: " << commandSuccessResult << std::endl; |
| 122 | } |
| 123 | |
| 124 | void |
Junxiao Shi | 52fa45c | 2016-11-29 21:18:13 +0000 | [diff] [blame] | 125 | Base::onPrefixRegistrationError(const ControlResponse& response) |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 126 | { |
Junxiao Shi | 52fa45c | 2016-11-29 21:18:13 +0000 | [diff] [blame] | 127 | BOOST_THROW_EXCEPTION(Error("Failed in name registration, " + response.getText() + |
| 128 | " (code: " + to_string(response.getCode()) + ")")); |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | |
| 132 | } // namespace autoconfig |
| 133 | } // namespace tools |
| 134 | } // namespace ndn |