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