Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2022, Regents of the University of California, |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [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 "procedure.hpp" |
| 27 | #include "guess-from-identity-name.hpp" |
| 28 | #include "guess-from-search-domains.hpp" |
| 29 | #include "multicast-discovery.hpp" |
| 30 | #include "ndn-fch-discovery.hpp" |
| 31 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 32 | namespace ndn::autoconfig { |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 33 | |
| 34 | using nfd::ControlParameters; |
| 35 | using nfd::ControlResponse; |
| 36 | |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 37 | constexpr time::nanoseconds FACEURI_CANONIZE_TIMEOUT = 4_s; |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 38 | const std::vector<Name> HUB_PREFIXES{"/", "/localhop/nfd"}; |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 39 | constexpr nfd::RouteOrigin HUB_ROUTE_ORIGIN = nfd::ROUTE_ORIGIN_AUTOCONF; |
| 40 | constexpr uint64_t HUB_ROUTE_COST = 100; |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 41 | |
| 42 | Procedure::Procedure(Face& face, KeyChain& keyChain) |
| 43 | : m_face(face) |
| 44 | , m_keyChain(keyChain) |
| 45 | , m_controller(face, keyChain) |
| 46 | { |
| 47 | } |
| 48 | |
| 49 | void |
| 50 | Procedure::initialize(const Options& options) |
| 51 | { |
| 52 | BOOST_ASSERT(m_stages.empty()); |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 53 | makeStages(options); |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 54 | BOOST_ASSERT(!m_stages.empty()); |
| 55 | |
| 56 | for (size_t i = 0; i < m_stages.size(); ++i) { |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 57 | m_stages[i]->onSuccess.connect([this] (const auto& uri) { connect(uri); }); |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 58 | if (i + 1 < m_stages.size()) { |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 59 | m_stages[i]->onFailure.connect([=] (const auto&) { m_stages[i + 1]->start(); }); |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 60 | } |
| 61 | else { |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 62 | m_stages[i]->onFailure.connect([=] (const auto&) { onComplete(false); }); |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 63 | } |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | void |
| 68 | Procedure::makeStages(const Options& options) |
| 69 | { |
| 70 | m_stages.push_back(make_unique<MulticastDiscovery>(m_face, m_controller)); |
| 71 | m_stages.push_back(make_unique<GuessFromSearchDomains>()); |
| 72 | m_stages.push_back(make_unique<NdnFchDiscovery>(options.ndnFchUrl)); |
| 73 | m_stages.push_back(make_unique<GuessFromIdentityName>(m_keyChain)); |
| 74 | } |
| 75 | |
| 76 | void |
| 77 | Procedure::runOnce() |
| 78 | { |
| 79 | BOOST_ASSERT(!m_stages.empty()); |
| 80 | m_stages.front()->start(); |
| 81 | } |
| 82 | |
| 83 | void |
| 84 | Procedure::connect(const FaceUri& hubFaceUri) |
| 85 | { |
| 86 | hubFaceUri.canonize( |
| 87 | [this] (const FaceUri& canonicalUri) { |
| 88 | m_controller.start<nfd::FaceCreateCommand>( |
| 89 | ControlParameters().setUri(canonicalUri.toString()), |
| 90 | [this] (const ControlParameters& params) { |
| 91 | std::cerr << "Connected to HUB " << params.getUri() << std::endl; |
| 92 | this->registerPrefixes(params.getFaceId()); |
| 93 | }, |
| 94 | [this, canonicalUri] (const ControlResponse& resp) { |
| 95 | if (resp.getCode() == 409) { |
| 96 | ControlParameters params(resp.getBody()); |
| 97 | std::cerr << "Already connected to HUB " << params.getUri() << std::endl; |
| 98 | this->registerPrefixes(params.getFaceId()); |
| 99 | } |
| 100 | else { |
| 101 | std::cerr << "Failed to connect to HUB " << canonicalUri << ": " |
| 102 | << resp.getText() << " (" << resp.getCode() << ")" << std::endl; |
| 103 | this->onComplete(false); |
| 104 | } |
| 105 | }); |
| 106 | }, |
| 107 | [this] (const std::string& reason) { |
| 108 | std::cerr << "Failed to canonize HUB FaceUri: " << reason << std::endl; |
| 109 | this->onComplete(false); |
| 110 | }, |
| 111 | m_face.getIoService(), FACEURI_CANONIZE_TIMEOUT); |
| 112 | } |
| 113 | |
| 114 | void |
| 115 | Procedure::registerPrefixes(uint64_t hubFaceId, size_t index) |
| 116 | { |
| 117 | if (index >= HUB_PREFIXES.size()) { |
| 118 | this->onComplete(true); |
| 119 | return; |
| 120 | } |
| 121 | |
| 122 | m_controller.start<nfd::RibRegisterCommand>( |
| 123 | ControlParameters() |
| 124 | .setName(HUB_PREFIXES[index]) |
| 125 | .setFaceId(hubFaceId) |
| 126 | .setOrigin(HUB_ROUTE_ORIGIN) |
| 127 | .setCost(HUB_ROUTE_COST), |
| 128 | [=] (const ControlParameters&) { |
| 129 | std::cerr << "Registered prefix " << HUB_PREFIXES[index] << std::endl; |
| 130 | this->registerPrefixes(hubFaceId, index + 1); |
| 131 | }, |
| 132 | [=] (const ControlResponse& resp) { |
| 133 | std::cerr << "Failed to register " << HUB_PREFIXES[index] << ": " |
| 134 | << resp.getText() << " (" << resp.getCode() << ")" << std::endl; |
| 135 | this->onComplete(false); |
| 136 | }); |
| 137 | } |
| 138 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 139 | } // namespace ndn::autoconfig |