blob: 550ed7f69987c594e8e1595f9b58e642c270c7b1 [file] [log] [blame]
Alexander Afanasyev2a655f72015-01-26 18:38:33 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shif748a4e2017-07-05 23:41:48 +00002/*
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -04003 * Copyright (c) 2014-2022, Regents of the University of California,
Alexander Afanasyev2a655f72015-01-26 18:38:33 -08004 * 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 "multicast-discovery.hpp"
Davide Pesavento14e71f02019-03-28 17:35:25 -040027
Junxiao Shia5a3a872017-07-08 12:42:11 +000028#include <boost/lexical_cast.hpp>
Davide Pesavento14e71f02019-03-28 17:35:25 -040029
Davide Pesavento7a294d42017-02-21 21:46:44 -050030#include <ndn-cxx/encoding/tlv-nfd.hpp>
31
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040032namespace ndn::autoconfig {
Alexander Afanasyev2a655f72015-01-26 18:38:33 -080033
Junxiao Shia5a3a872017-07-08 12:42:11 +000034using nfd::ControlParameters;
Junxiao Shia5a3a872017-07-08 12:42:11 +000035
Davide Pesavento14e71f02019-03-28 17:35:25 -040036const Name HUB_DISCOVERY_PREFIX("/localhop/ndn-autoconf/hub");
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040037constexpr uint64_t HUB_DISCOVERY_ROUTE_COST(1);
38constexpr time::milliseconds HUB_DISCOVERY_ROUTE_EXPIRATION = 30_s;
39constexpr time::milliseconds HUB_DISCOVERY_INTEREST_LIFETIME = 4_s;
Alexander Afanasyev2a655f72015-01-26 18:38:33 -080040
Junxiao Shicb766862017-07-07 22:21:04 +000041MulticastDiscovery::MulticastDiscovery(Face& face, nfd::Controller& controller)
42 : m_face(face)
43 , m_controller(controller)
Alexander Afanasyev2a655f72015-01-26 18:38:33 -080044{
45}
46
47void
Junxiao Shicb766862017-07-07 22:21:04 +000048MulticastDiscovery::doStart()
Alexander Afanasyev2a655f72015-01-26 18:38:33 -080049{
Junxiao Shicb766862017-07-07 22:21:04 +000050 nfd::FaceQueryFilter filter;
51 filter.setLinkType(nfd::LINK_TYPE_MULTI_ACCESS);
Junxiao Shia5a3a872017-07-08 12:42:11 +000052
Junxiao Shicb766862017-07-07 22:21:04 +000053 m_controller.fetch<nfd::FaceQueryDataset>(
Junxiao Shia8891112016-12-06 21:11:33 +000054 filter,
Davide Pesavento412c9822021-07-02 00:21:05 -040055 [this] (const auto& dataset) { registerHubDiscoveryPrefix(dataset); },
Junxiao Shia5a3a872017-07-08 12:42:11 +000056 [this] (uint32_t code, const std::string& reason) {
Davide Pesavento412c9822021-07-02 00:21:05 -040057 fail("Error " + to_string(code) + " when querying multi-access faces: " + reason);
Junxiao Shia5a3a872017-07-08 12:42:11 +000058 });
Junxiao Shia8891112016-12-06 21:11:33 +000059}
60
61void
Junxiao Shicb766862017-07-07 22:21:04 +000062MulticastDiscovery::registerHubDiscoveryPrefix(const std::vector<nfd::FaceStatus>& dataset)
Alexander Afanasyev2a655f72015-01-26 18:38:33 -080063{
Junxiao Shia5a3a872017-07-08 12:42:11 +000064 if (dataset.empty()) {
Davide Pesavento412c9822021-07-02 00:21:05 -040065 fail("No multi-access faces available");
Junxiao Shia5a3a872017-07-08 12:42:11 +000066 return;
67 }
Alexander Afanasyev2a655f72015-01-26 18:38:33 -080068
Junxiao Shia5a3a872017-07-08 12:42:11 +000069 m_nRegs = dataset.size();
70 m_nRegSuccess = 0;
71 m_nRegFailure = 0;
72
73 for (const auto& faceStatus : dataset) {
74 ControlParameters parameters;
75 parameters.setName(HUB_DISCOVERY_PREFIX)
76 .setFaceId(faceStatus.getFaceId())
77 .setCost(HUB_DISCOVERY_ROUTE_COST)
78 .setExpirationPeriod(HUB_DISCOVERY_ROUTE_EXPIRATION);
79
80 m_controller.start<nfd::RibRegisterCommand>(
81 parameters,
Davide Pesavento412c9822021-07-02 00:21:05 -040082 [this] (const auto&) {
Junxiao Shia5a3a872017-07-08 12:42:11 +000083 ++m_nRegSuccess;
84 afterReg();
85 },
Davide Pesavento412c9822021-07-02 00:21:05 -040086 [this, faceStatus] (const auto& resp) {
Junxiao Shia5a3a872017-07-08 12:42:11 +000087 std::cerr << "Error " << resp.getCode() << " when registering hub discovery prefix "
88 << "for face " << faceStatus.getFaceId() << " (" << faceStatus.getRemoteUri()
89 << "): " << resp.getText() << std::endl;
90 ++m_nRegFailure;
91 afterReg();
92 });
93 }
94}
95
96void
97MulticastDiscovery::afterReg()
98{
99 if (m_nRegSuccess + m_nRegFailure < m_nRegs) {
100 return; // continue waiting
101 }
102 if (m_nRegSuccess > 0) {
Davide Pesavento412c9822021-07-02 00:21:05 -0400103 setStrategy();
Alexander Afanasyev2a655f72015-01-26 18:38:33 -0800104 }
105 else {
Davide Pesavento412c9822021-07-02 00:21:05 -0400106 fail("Cannot register hub discovery prefix for any face");
Alexander Afanasyev2a655f72015-01-26 18:38:33 -0800107 }
108}
109
110void
111MulticastDiscovery::setStrategy()
112{
Junxiao Shia5a3a872017-07-08 12:42:11 +0000113 ControlParameters parameters;
114 parameters.setName(HUB_DISCOVERY_PREFIX)
Davide Pesavento22085362021-03-18 22:08:03 -0400115 .setStrategy("/localhost/nfd/strategy/multicast");
Alexander Afanasyev2a655f72015-01-26 18:38:33 -0800116
Junxiao Shicb766862017-07-07 22:21:04 +0000117 m_controller.start<nfd::StrategyChoiceSetCommand>(
Junxiao Shi52fa45c2016-11-29 21:18:13 +0000118 parameters,
Davide Pesavento412c9822021-07-02 00:21:05 -0400119 [this] (const auto&) { requestHubData(); },
120 [this] (const auto& resp) {
121 fail("Error " + to_string(resp.getCode()) + " when setting multicast strategy: " + resp.getText());
Junxiao Shia5a3a872017-07-08 12:42:11 +0000122 });
Alexander Afanasyev2a655f72015-01-26 18:38:33 -0800123}
124
125void
126MulticastDiscovery::requestHubData()
127{
Junxiao Shia5a3a872017-07-08 12:42:11 +0000128 Interest interest(HUB_DISCOVERY_PREFIX);
Junxiao Shi47c343b2019-05-25 07:53:01 +0000129 interest.setCanBePrefix(true);
Alexander Afanasyev2a655f72015-01-26 18:38:33 -0800130 interest.setMustBeFresh(true);
Junxiao Shi47c343b2019-05-25 07:53:01 +0000131 interest.setInterestLifetime(HUB_DISCOVERY_INTEREST_LIFETIME);
Alexander Afanasyev2a655f72015-01-26 18:38:33 -0800132
133 m_face.expressInterest(interest,
Junxiao Shia5a3a872017-07-08 12:42:11 +0000134 [this] (const Interest&, const Data& data) {
135 const Block& content = data.getContent();
136 content.parse();
Alexander Afanasyev2a655f72015-01-26 18:38:33 -0800137
Junxiao Shia5a3a872017-07-08 12:42:11 +0000138 auto i = content.find(tlv::nfd::Uri);
139 if (i == content.elements_end()) {
Davide Pesavento412c9822021-07-02 00:21:05 -0400140 fail("Malformed hub Data: missing Uri element");
Junxiao Shia5a3a872017-07-08 12:42:11 +0000141 return;
142 }
Alexander Afanasyev2a655f72015-01-26 18:38:33 -0800143
Davide Pesavento412c9822021-07-02 00:21:05 -0400144 provideHubFaceUri(std::string(reinterpret_cast<const char*>(i->value()), i->value_size()));
Junxiao Shia5a3a872017-07-08 12:42:11 +0000145 },
146 [this] (const Interest&, const lp::Nack& nack) {
Davide Pesavento412c9822021-07-02 00:21:05 -0400147 fail("Nack-" + boost::lexical_cast<std::string>(nack.getReason()) + " when retrieving hub Data");
Junxiao Shia5a3a872017-07-08 12:42:11 +0000148 },
149 [this] (const Interest&) {
Davide Pesavento412c9822021-07-02 00:21:05 -0400150 fail("Timeout when retrieving hub Data");
Junxiao Shia5a3a872017-07-08 12:42:11 +0000151 });
Alexander Afanasyev2a655f72015-01-26 18:38:33 -0800152}
153
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400154} // namespace ndn::autoconfig