Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | f748a4e | 2017-07-05 23:41:48 +0000 | [diff] [blame] | 2 | /* |
Davide Pesavento | 2208536 | 2021-03-18 22:08:03 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2021, Regents of the University of California, |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [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 "multicast-discovery.hpp" |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 27 | |
Junxiao Shi | a5a3a87 | 2017-07-08 12:42:11 +0000 | [diff] [blame] | 28 | #include <boost/lexical_cast.hpp> |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 29 | |
Davide Pesavento | 7a294d4 | 2017-02-21 21:46:44 -0500 | [diff] [blame] | 30 | #include <ndn-cxx/encoding/tlv-nfd.hpp> |
| 31 | |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 32 | namespace ndn { |
| 33 | namespace tools { |
| 34 | namespace autoconfig { |
| 35 | |
Junxiao Shi | a5a3a87 | 2017-07-08 12:42:11 +0000 | [diff] [blame] | 36 | using nfd::ControlParameters; |
Junxiao Shi | a5a3a87 | 2017-07-08 12:42:11 +0000 | [diff] [blame] | 37 | |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 38 | const Name HUB_DISCOVERY_PREFIX("/localhop/ndn-autoconf/hub"); |
| 39 | const uint64_t HUB_DISCOVERY_ROUTE_COST(1); |
| 40 | const time::milliseconds HUB_DISCOVERY_ROUTE_EXPIRATION = 30_s; |
| 41 | const time::milliseconds HUB_DISCOVERY_INTEREST_LIFETIME = 4_s; |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 42 | |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 43 | MulticastDiscovery::MulticastDiscovery(Face& face, nfd::Controller& controller) |
| 44 | : m_face(face) |
| 45 | , m_controller(controller) |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 46 | { |
| 47 | } |
| 48 | |
| 49 | void |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 50 | MulticastDiscovery::doStart() |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 51 | { |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 52 | nfd::FaceQueryFilter filter; |
| 53 | filter.setLinkType(nfd::LINK_TYPE_MULTI_ACCESS); |
Junxiao Shi | a5a3a87 | 2017-07-08 12:42:11 +0000 | [diff] [blame] | 54 | |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 55 | m_controller.fetch<nfd::FaceQueryDataset>( |
Junxiao Shi | a889111 | 2016-12-06 21:11:33 +0000 | [diff] [blame] | 56 | filter, |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame^] | 57 | [this] (const auto& dataset) { registerHubDiscoveryPrefix(dataset); }, |
Junxiao Shi | a5a3a87 | 2017-07-08 12:42:11 +0000 | [diff] [blame] | 58 | [this] (uint32_t code, const std::string& reason) { |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame^] | 59 | fail("Error " + to_string(code) + " when querying multi-access faces: " + reason); |
Junxiao Shi | a5a3a87 | 2017-07-08 12:42:11 +0000 | [diff] [blame] | 60 | }); |
Junxiao Shi | a889111 | 2016-12-06 21:11:33 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | void |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 64 | MulticastDiscovery::registerHubDiscoveryPrefix(const std::vector<nfd::FaceStatus>& dataset) |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 65 | { |
Junxiao Shi | a5a3a87 | 2017-07-08 12:42:11 +0000 | [diff] [blame] | 66 | if (dataset.empty()) { |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame^] | 67 | fail("No multi-access faces available"); |
Junxiao Shi | a5a3a87 | 2017-07-08 12:42:11 +0000 | [diff] [blame] | 68 | return; |
| 69 | } |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 70 | |
Junxiao Shi | a5a3a87 | 2017-07-08 12:42:11 +0000 | [diff] [blame] | 71 | 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 Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame^] | 84 | [this] (const auto&) { |
Junxiao Shi | a5a3a87 | 2017-07-08 12:42:11 +0000 | [diff] [blame] | 85 | ++m_nRegSuccess; |
| 86 | afterReg(); |
| 87 | }, |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame^] | 88 | [this, faceStatus] (const auto& resp) { |
Junxiao Shi | a5a3a87 | 2017-07-08 12:42:11 +0000 | [diff] [blame] | 89 | 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 | |
| 98 | void |
| 99 | MulticastDiscovery::afterReg() |
| 100 | { |
| 101 | if (m_nRegSuccess + m_nRegFailure < m_nRegs) { |
| 102 | return; // continue waiting |
| 103 | } |
| 104 | if (m_nRegSuccess > 0) { |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame^] | 105 | setStrategy(); |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 106 | } |
| 107 | else { |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame^] | 108 | fail("Cannot register hub discovery prefix for any face"); |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | |
| 112 | void |
| 113 | MulticastDiscovery::setStrategy() |
| 114 | { |
Junxiao Shi | a5a3a87 | 2017-07-08 12:42:11 +0000 | [diff] [blame] | 115 | ControlParameters parameters; |
| 116 | parameters.setName(HUB_DISCOVERY_PREFIX) |
Davide Pesavento | 2208536 | 2021-03-18 22:08:03 -0400 | [diff] [blame] | 117 | .setStrategy("/localhost/nfd/strategy/multicast"); |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 118 | |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 119 | m_controller.start<nfd::StrategyChoiceSetCommand>( |
Junxiao Shi | 52fa45c | 2016-11-29 21:18:13 +0000 | [diff] [blame] | 120 | parameters, |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame^] | 121 | [this] (const auto&) { requestHubData(); }, |
| 122 | [this] (const auto& resp) { |
| 123 | fail("Error " + to_string(resp.getCode()) + " when setting multicast strategy: " + resp.getText()); |
Junxiao Shi | a5a3a87 | 2017-07-08 12:42:11 +0000 | [diff] [blame] | 124 | }); |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | void |
| 128 | MulticastDiscovery::requestHubData() |
| 129 | { |
Junxiao Shi | a5a3a87 | 2017-07-08 12:42:11 +0000 | [diff] [blame] | 130 | Interest interest(HUB_DISCOVERY_PREFIX); |
Junxiao Shi | 47c343b | 2019-05-25 07:53:01 +0000 | [diff] [blame] | 131 | interest.setCanBePrefix(true); |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 132 | interest.setMustBeFresh(true); |
Junxiao Shi | 47c343b | 2019-05-25 07:53:01 +0000 | [diff] [blame] | 133 | interest.setInterestLifetime(HUB_DISCOVERY_INTEREST_LIFETIME); |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 134 | |
| 135 | m_face.expressInterest(interest, |
Junxiao Shi | a5a3a87 | 2017-07-08 12:42:11 +0000 | [diff] [blame] | 136 | [this] (const Interest&, const Data& data) { |
| 137 | const Block& content = data.getContent(); |
| 138 | content.parse(); |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 139 | |
Junxiao Shi | a5a3a87 | 2017-07-08 12:42:11 +0000 | [diff] [blame] | 140 | auto i = content.find(tlv::nfd::Uri); |
| 141 | if (i == content.elements_end()) { |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame^] | 142 | fail("Malformed hub Data: missing Uri element"); |
Junxiao Shi | a5a3a87 | 2017-07-08 12:42:11 +0000 | [diff] [blame] | 143 | return; |
| 144 | } |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 145 | |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame^] | 146 | provideHubFaceUri(std::string(reinterpret_cast<const char*>(i->value()), i->value_size())); |
Junxiao Shi | a5a3a87 | 2017-07-08 12:42:11 +0000 | [diff] [blame] | 147 | }, |
| 148 | [this] (const Interest&, const lp::Nack& nack) { |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame^] | 149 | fail("Nack-" + boost::lexical_cast<std::string>(nack.getReason()) + " when retrieving hub Data"); |
Junxiao Shi | a5a3a87 | 2017-07-08 12:42:11 +0000 | [diff] [blame] | 150 | }, |
| 151 | [this] (const Interest&) { |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame^] | 152 | fail("Timeout when retrieving hub Data"); |
Junxiao Shi | a5a3a87 | 2017-07-08 12:42:11 +0000 | [diff] [blame] | 153 | }); |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | } // namespace autoconfig |
| 157 | } // namespace tools |
| 158 | } // namespace ndn |