blob: 0a76b22804e77fb9c81d3ce59b7b3d69ea528161 [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 Pesavento22085362021-03-18 22:08:03 -04003 * Copyright (c) 2014-2021, 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
Alexander Afanasyev2a655f72015-01-26 18:38:33 -080032namespace ndn {
33namespace tools {
34namespace autoconfig {
35
Junxiao Shia5a3a872017-07-08 12:42:11 +000036using nfd::ControlParameters;
Junxiao Shia5a3a872017-07-08 12:42:11 +000037
Davide Pesavento14e71f02019-03-28 17:35:25 -040038const Name HUB_DISCOVERY_PREFIX("/localhop/ndn-autoconf/hub");
39const uint64_t HUB_DISCOVERY_ROUTE_COST(1);
40const time::milliseconds HUB_DISCOVERY_ROUTE_EXPIRATION = 30_s;
41const time::milliseconds HUB_DISCOVERY_INTEREST_LIFETIME = 4_s;
Alexander Afanasyev2a655f72015-01-26 18:38:33 -080042
Junxiao Shicb766862017-07-07 22:21:04 +000043MulticastDiscovery::MulticastDiscovery(Face& face, nfd::Controller& controller)
44 : m_face(face)
45 , m_controller(controller)
Alexander Afanasyev2a655f72015-01-26 18:38:33 -080046{
47}
48
49void
Junxiao Shicb766862017-07-07 22:21:04 +000050MulticastDiscovery::doStart()
Alexander Afanasyev2a655f72015-01-26 18:38:33 -080051{
Junxiao Shicb766862017-07-07 22:21:04 +000052 nfd::FaceQueryFilter filter;
53 filter.setLinkType(nfd::LINK_TYPE_MULTI_ACCESS);
Junxiao Shia5a3a872017-07-08 12:42:11 +000054
Junxiao Shicb766862017-07-07 22:21:04 +000055 m_controller.fetch<nfd::FaceQueryDataset>(
Junxiao Shia8891112016-12-06 21:11:33 +000056 filter,
Davide Pesavento412c9822021-07-02 00:21:05 -040057 [this] (const auto& dataset) { registerHubDiscoveryPrefix(dataset); },
Junxiao Shia5a3a872017-07-08 12:42:11 +000058 [this] (uint32_t code, const std::string& reason) {
Davide Pesavento412c9822021-07-02 00:21:05 -040059 fail("Error " + to_string(code) + " when querying multi-access faces: " + reason);
Junxiao Shia5a3a872017-07-08 12:42:11 +000060 });
Junxiao Shia8891112016-12-06 21:11:33 +000061}
62
63void
Junxiao Shicb766862017-07-07 22:21:04 +000064MulticastDiscovery::registerHubDiscoveryPrefix(const std::vector<nfd::FaceStatus>& dataset)
Alexander Afanasyev2a655f72015-01-26 18:38:33 -080065{
Junxiao Shia5a3a872017-07-08 12:42:11 +000066 if (dataset.empty()) {
Davide Pesavento412c9822021-07-02 00:21:05 -040067 fail("No multi-access faces available");
Junxiao Shia5a3a872017-07-08 12:42:11 +000068 return;
69 }
Alexander Afanasyev2a655f72015-01-26 18:38:33 -080070
Junxiao Shia5a3a872017-07-08 12:42:11 +000071 m_nRegs = dataset.size();
72 m_nRegSuccess = 0;
73 m_nRegFailure = 0;
74
75 for (const auto& faceStatus : dataset) {
76 ControlParameters parameters;
77 parameters.setName(HUB_DISCOVERY_PREFIX)
78 .setFaceId(faceStatus.getFaceId())
79 .setCost(HUB_DISCOVERY_ROUTE_COST)
80 .setExpirationPeriod(HUB_DISCOVERY_ROUTE_EXPIRATION);
81
82 m_controller.start<nfd::RibRegisterCommand>(
83 parameters,
Davide Pesavento412c9822021-07-02 00:21:05 -040084 [this] (const auto&) {
Junxiao Shia5a3a872017-07-08 12:42:11 +000085 ++m_nRegSuccess;
86 afterReg();
87 },
Davide Pesavento412c9822021-07-02 00:21:05 -040088 [this, faceStatus] (const auto& resp) {
Junxiao Shia5a3a872017-07-08 12:42:11 +000089 std::cerr << "Error " << resp.getCode() << " when registering hub discovery prefix "
90 << "for face " << faceStatus.getFaceId() << " (" << faceStatus.getRemoteUri()
91 << "): " << resp.getText() << std::endl;
92 ++m_nRegFailure;
93 afterReg();
94 });
95 }
96}
97
98void
99MulticastDiscovery::afterReg()
100{
101 if (m_nRegSuccess + m_nRegFailure < m_nRegs) {
102 return; // continue waiting
103 }
104 if (m_nRegSuccess > 0) {
Davide Pesavento412c9822021-07-02 00:21:05 -0400105 setStrategy();
Alexander Afanasyev2a655f72015-01-26 18:38:33 -0800106 }
107 else {
Davide Pesavento412c9822021-07-02 00:21:05 -0400108 fail("Cannot register hub discovery prefix for any face");
Alexander Afanasyev2a655f72015-01-26 18:38:33 -0800109 }
110}
111
112void
113MulticastDiscovery::setStrategy()
114{
Junxiao Shia5a3a872017-07-08 12:42:11 +0000115 ControlParameters parameters;
116 parameters.setName(HUB_DISCOVERY_PREFIX)
Davide Pesavento22085362021-03-18 22:08:03 -0400117 .setStrategy("/localhost/nfd/strategy/multicast");
Alexander Afanasyev2a655f72015-01-26 18:38:33 -0800118
Junxiao Shicb766862017-07-07 22:21:04 +0000119 m_controller.start<nfd::StrategyChoiceSetCommand>(
Junxiao Shi52fa45c2016-11-29 21:18:13 +0000120 parameters,
Davide Pesavento412c9822021-07-02 00:21:05 -0400121 [this] (const auto&) { requestHubData(); },
122 [this] (const auto& resp) {
123 fail("Error " + to_string(resp.getCode()) + " when setting multicast strategy: " + resp.getText());
Junxiao Shia5a3a872017-07-08 12:42:11 +0000124 });
Alexander Afanasyev2a655f72015-01-26 18:38:33 -0800125}
126
127void
128MulticastDiscovery::requestHubData()
129{
Junxiao Shia5a3a872017-07-08 12:42:11 +0000130 Interest interest(HUB_DISCOVERY_PREFIX);
Junxiao Shi47c343b2019-05-25 07:53:01 +0000131 interest.setCanBePrefix(true);
Alexander Afanasyev2a655f72015-01-26 18:38:33 -0800132 interest.setMustBeFresh(true);
Junxiao Shi47c343b2019-05-25 07:53:01 +0000133 interest.setInterestLifetime(HUB_DISCOVERY_INTEREST_LIFETIME);
Alexander Afanasyev2a655f72015-01-26 18:38:33 -0800134
135 m_face.expressInterest(interest,
Junxiao Shia5a3a872017-07-08 12:42:11 +0000136 [this] (const Interest&, const Data& data) {
137 const Block& content = data.getContent();
138 content.parse();
Alexander Afanasyev2a655f72015-01-26 18:38:33 -0800139
Junxiao Shia5a3a872017-07-08 12:42:11 +0000140 auto i = content.find(tlv::nfd::Uri);
141 if (i == content.elements_end()) {
Davide Pesavento412c9822021-07-02 00:21:05 -0400142 fail("Malformed hub Data: missing Uri element");
Junxiao Shia5a3a872017-07-08 12:42:11 +0000143 return;
144 }
Alexander Afanasyev2a655f72015-01-26 18:38:33 -0800145
Davide Pesavento412c9822021-07-02 00:21:05 -0400146 provideHubFaceUri(std::string(reinterpret_cast<const char*>(i->value()), i->value_size()));
Junxiao Shia5a3a872017-07-08 12:42:11 +0000147 },
148 [this] (const Interest&, const lp::Nack& nack) {
Davide Pesavento412c9822021-07-02 00:21:05 -0400149 fail("Nack-" + boost::lexical_cast<std::string>(nack.getReason()) + " when retrieving hub Data");
Junxiao Shia5a3a872017-07-08 12:42:11 +0000150 },
151 [this] (const Interest&) {
Davide Pesavento412c9822021-07-02 00:21:05 -0400152 fail("Timeout when retrieving hub Data");
Junxiao Shia5a3a872017-07-08 12:42:11 +0000153 });
Alexander Afanasyev2a655f72015-01-26 18:38:33 -0800154}
155
156} // namespace autoconfig
157} // namespace tools
158} // namespace ndn