procedure.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2023, 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 "procedure.hpp"
29 #include "multicast-discovery.hpp"
30 #include "ndn-fch-discovery.hpp"
31 
32 namespace ndn::autoconfig {
33 
34 using nfd::ControlParameters;
35 using nfd::ControlResponse;
36 
37 constexpr time::nanoseconds FACEURI_CANONIZE_TIMEOUT = 4_s;
38 const std::vector<Name> HUB_PREFIXES{"/", "/localhop/nfd"};
39 constexpr nfd::RouteOrigin HUB_ROUTE_ORIGIN = nfd::ROUTE_ORIGIN_AUTOCONF;
40 constexpr uint64_t HUB_ROUTE_COST = 100;
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
51 {
52  BOOST_ASSERT(m_stages.empty());
53  makeStages(options);
54  BOOST_ASSERT(!m_stages.empty());
55 
56  for (size_t i = 0; i < m_stages.size(); ++i) {
57  m_stages[i]->onSuccess.connect([this] (const auto& uri) { connect(uri); });
58  if (i + 1 < m_stages.size()) {
59  m_stages[i]->onFailure.connect([=] (const auto&) { m_stages[i + 1]->start(); });
60  }
61  else {
62  m_stages[i]->onFailure.connect([=] (const auto&) { onComplete(false); });
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
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.getIoContext(), 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 
139 } // namespace ndn::autoconfig
Procedure(Face &face, KeyChain &keyChain)
Definition: procedure.cpp:42
signal::Signal< Procedure, bool > onComplete
Signal emitted when the procedure completes.
Definition: procedure.hpp:83
void initialize(const Options &options)
Definition: procedure.cpp:50
void runOnce()
Run the HUB discovery procedure once.
Definition: procedure.cpp:77
constexpr time::nanoseconds FACEURI_CANONIZE_TIMEOUT
Definition: procedure.cpp:37
constexpr nfd::RouteOrigin HUB_ROUTE_ORIGIN
Definition: procedure.cpp:39
const std::vector< Name > HUB_PREFIXES
Definition: procedure.cpp:38
constexpr uint64_t HUB_ROUTE_COST
Definition: procedure.cpp:40
std::string ndnFchUrl
HTTP base URL of NDN-FCH service.
Definition: procedure.hpp:41