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 | 7a294d4 | 2017-02-21 21:46:44 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2017, 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" |
| 27 | |
Davide Pesavento | 7a294d4 | 2017-02-21 21:46:44 -0500 | [diff] [blame] | 28 | #include <ndn-cxx/encoding/tlv-nfd.hpp> |
| 29 | |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 30 | namespace ndn { |
| 31 | namespace tools { |
| 32 | namespace autoconfig { |
| 33 | |
| 34 | static const Name LOCALHOP_HUB_DISCOVERY_PREFIX = "/localhop/ndn-autoconf/hub"; |
| 35 | |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame^] | 36 | MulticastDiscovery::MulticastDiscovery(Face& face, nfd::Controller& controller) |
| 37 | : m_face(face) |
| 38 | , m_controller(controller) |
Junxiao Shi | 52fa45c | 2016-11-29 21:18:13 +0000 | [diff] [blame] | 39 | , m_nRequestedRegs(0) |
| 40 | , m_nFinishedRegs(0) |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 41 | { |
| 42 | } |
| 43 | |
| 44 | void |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame^] | 45 | MulticastDiscovery::doStart() |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 46 | { |
Junxiao Shi | a889111 | 2016-12-06 21:11:33 +0000 | [diff] [blame] | 47 | this->collectMulticastFaces(); |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | void |
Junxiao Shi | a889111 | 2016-12-06 21:11:33 +0000 | [diff] [blame] | 51 | MulticastDiscovery::collectMulticastFaces() |
| 52 | { |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame^] | 53 | nfd::FaceQueryFilter filter; |
| 54 | filter.setLinkType(nfd::LINK_TYPE_MULTI_ACCESS); |
| 55 | m_controller.fetch<nfd::FaceQueryDataset>( |
Junxiao Shi | a889111 | 2016-12-06 21:11:33 +0000 | [diff] [blame] | 56 | filter, |
| 57 | bind(&MulticastDiscovery::registerHubDiscoveryPrefix, this, _1), |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame^] | 58 | bind(&MulticastDiscovery::fail, this, _2) |
Junxiao Shi | a889111 | 2016-12-06 21:11:33 +0000 | [diff] [blame] | 59 | ); |
| 60 | } |
| 61 | |
| 62 | void |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame^] | 63 | MulticastDiscovery::registerHubDiscoveryPrefix(const std::vector<nfd::FaceStatus>& dataset) |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 64 | { |
| 65 | std::vector<uint64_t> multicastFaces; |
Junxiao Shi | a889111 | 2016-12-06 21:11:33 +0000 | [diff] [blame] | 66 | std::transform(dataset.begin(), dataset.end(), std::back_inserter(multicastFaces), |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame^] | 67 | [] (const nfd::FaceStatus& faceStatus) { return faceStatus.getFaceId(); }); |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 68 | |
| 69 | if (multicastFaces.empty()) { |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame^] | 70 | this->fail("No multicast faces available, skipping multicast discovery stage"); |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 71 | } |
| 72 | else { |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame^] | 73 | nfd::ControlParameters parameters; |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 74 | parameters |
| 75 | .setName(LOCALHOP_HUB_DISCOVERY_PREFIX) |
| 76 | .setCost(1) |
| 77 | .setExpirationPeriod(time::seconds(30)); |
| 78 | |
Junxiao Shi | 52fa45c | 2016-11-29 21:18:13 +0000 | [diff] [blame] | 79 | m_nRequestedRegs = multicastFaces.size(); |
| 80 | m_nFinishedRegs = 0; |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 81 | |
| 82 | for (const auto& face : multicastFaces) { |
| 83 | parameters.setFaceId(face); |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame^] | 84 | m_controller.start<nfd::RibRegisterCommand>( |
Junxiao Shi | 52fa45c | 2016-11-29 21:18:13 +0000 | [diff] [blame] | 85 | parameters, |
| 86 | bind(&MulticastDiscovery::onRegisterSuccess, this), |
| 87 | bind(&MulticastDiscovery::onRegisterFailure, this, _1)); |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | void |
| 93 | MulticastDiscovery::onRegisterSuccess() |
| 94 | { |
Junxiao Shi | 52fa45c | 2016-11-29 21:18:13 +0000 | [diff] [blame] | 95 | ++m_nFinishedRegs; |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 96 | |
Junxiao Shi | 52fa45c | 2016-11-29 21:18:13 +0000 | [diff] [blame] | 97 | if (m_nRequestedRegs == m_nFinishedRegs) { |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 98 | MulticastDiscovery::setStrategy(); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | void |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame^] | 103 | MulticastDiscovery::onRegisterFailure(const nfd::ControlResponse& response) |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 104 | { |
Junxiao Shi | 29b4128 | 2016-08-22 03:47:02 +0000 | [diff] [blame] | 105 | std::cerr << "ERROR: " << response.getText() << " (code: " << response.getCode() << ")" << std::endl; |
Junxiao Shi | 52fa45c | 2016-11-29 21:18:13 +0000 | [diff] [blame] | 106 | --m_nRequestedRegs; |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 107 | |
Junxiao Shi | 52fa45c | 2016-11-29 21:18:13 +0000 | [diff] [blame] | 108 | if (m_nRequestedRegs == m_nFinishedRegs) { |
| 109 | if (m_nRequestedRegs > 0) { |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 110 | MulticastDiscovery::setStrategy(); |
Junxiao Shi | 52fa45c | 2016-11-29 21:18:13 +0000 | [diff] [blame] | 111 | } |
| 112 | else { |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame^] | 113 | this->fail("Failed to register " + LOCALHOP_HUB_DISCOVERY_PREFIX.toUri() + |
| 114 | " for all multicast faces, skipping multicast discovery stage"); |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | void |
| 120 | MulticastDiscovery::setStrategy() |
| 121 | { |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame^] | 122 | nfd::ControlParameters parameters; |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 123 | parameters |
| 124 | .setName(LOCALHOP_HUB_DISCOVERY_PREFIX) |
Junxiao Shi | 67ba8d2 | 2015-08-21 21:21:28 -0700 | [diff] [blame] | 125 | .setStrategy("/localhost/nfd/strategy/multicast"); |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 126 | |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame^] | 127 | m_controller.start<nfd::StrategyChoiceSetCommand>( |
Junxiao Shi | 52fa45c | 2016-11-29 21:18:13 +0000 | [diff] [blame] | 128 | parameters, |
| 129 | bind(&MulticastDiscovery::requestHubData, this), |
| 130 | bind(&MulticastDiscovery::onSetStrategyFailure, this, _1)); |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | void |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame^] | 134 | MulticastDiscovery::onSetStrategyFailure(const nfd::ControlResponse& response) |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 135 | { |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame^] | 136 | this->fail("Failed to set multicast strategy for " + |
| 137 | LOCALHOP_HUB_DISCOVERY_PREFIX.toUri() + " namespace (" + response.getText() + "). " |
| 138 | "Skipping multicast discovery stage"); |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | void |
| 142 | MulticastDiscovery::requestHubData() |
| 143 | { |
| 144 | Interest interest(LOCALHOP_HUB_DISCOVERY_PREFIX); |
| 145 | interest.setInterestLifetime(time::milliseconds(4000)); // 4 seconds |
| 146 | interest.setMustBeFresh(true); |
| 147 | |
| 148 | m_face.expressInterest(interest, |
| 149 | bind(&MulticastDiscovery::onSuccess, this, _2), |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame^] | 150 | bind(&MulticastDiscovery::fail, this, "HUB Data not received: nacked"), |
| 151 | bind(&MulticastDiscovery::fail, this, "HUB Data not received: timeout")); |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | void |
Junxiao Shi | d0e276f | 2016-12-14 23:13:39 +0000 | [diff] [blame] | 155 | MulticastDiscovery::onSuccess(const Data& data) |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 156 | { |
| 157 | const Block& content = data.getContent(); |
| 158 | content.parse(); |
| 159 | |
| 160 | // Get Uri |
| 161 | Block::element_const_iterator blockValue = content.find(tlv::nfd::Uri); |
| 162 | if (blockValue == content.elements_end()) { |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame^] | 163 | this->fail("Incorrect reply to multicast discovery stage"); |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 164 | return; |
| 165 | } |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame^] | 166 | this->provideHubFaceUri(std::string(reinterpret_cast<const char*>(blockValue->value()), blockValue->value_size())); |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | } // namespace autoconfig |
| 170 | } // namespace tools |
| 171 | } // namespace ndn |