blob: f7295d3be0913d5a66504e68bb82250bad5efd70 [file] [log] [blame]
Junxiao Shicb766862017-07-07 22:21:04 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -04003 * Copyright (c) 2014-2022, Regents of the University of California,
Junxiao Shicb766862017-07-07 22:21:04 +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 "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 Pesaventoe422f9e2022-06-03 01:30:23 -040032namespace ndn::autoconfig {
Junxiao Shicb766862017-07-07 22:21:04 +000033
34using nfd::ControlParameters;
35using nfd::ControlResponse;
36
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040037constexpr time::nanoseconds FACEURI_CANONIZE_TIMEOUT = 4_s;
Davide Pesavento14e71f02019-03-28 17:35:25 -040038const std::vector<Name> HUB_PREFIXES{"/", "/localhop/nfd"};
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040039constexpr nfd::RouteOrigin HUB_ROUTE_ORIGIN = nfd::ROUTE_ORIGIN_AUTOCONF;
40constexpr uint64_t HUB_ROUTE_COST = 100;
Junxiao Shicb766862017-07-07 22:21:04 +000041
42Procedure::Procedure(Face& face, KeyChain& keyChain)
43 : m_face(face)
44 , m_keyChain(keyChain)
45 , m_controller(face, keyChain)
46{
47}
48
49void
50Procedure::initialize(const Options& options)
51{
52 BOOST_ASSERT(m_stages.empty());
Davide Pesavento412c9822021-07-02 00:21:05 -040053 makeStages(options);
Junxiao Shicb766862017-07-07 22:21:04 +000054 BOOST_ASSERT(!m_stages.empty());
55
56 for (size_t i = 0; i < m_stages.size(); ++i) {
Davide Pesavento412c9822021-07-02 00:21:05 -040057 m_stages[i]->onSuccess.connect([this] (const auto& uri) { connect(uri); });
Junxiao Shicb766862017-07-07 22:21:04 +000058 if (i + 1 < m_stages.size()) {
Davide Pesavento412c9822021-07-02 00:21:05 -040059 m_stages[i]->onFailure.connect([=] (const auto&) { m_stages[i + 1]->start(); });
Junxiao Shicb766862017-07-07 22:21:04 +000060 }
61 else {
Davide Pesavento412c9822021-07-02 00:21:05 -040062 m_stages[i]->onFailure.connect([=] (const auto&) { onComplete(false); });
Junxiao Shicb766862017-07-07 22:21:04 +000063 }
64 }
65}
66
67void
68Procedure::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
76void
77Procedure::runOnce()
78{
79 BOOST_ASSERT(!m_stages.empty());
80 m_stages.front()->start();
81}
82
83void
84Procedure::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
114void
115Procedure::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 Pesaventoe422f9e2022-06-03 01:30:23 -0400139} // namespace ndn::autoconfig