Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | 29b4128 | 2016-08-22 03:47:02 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2016, 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 | |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 28 | namespace ndn { |
| 29 | namespace tools { |
| 30 | namespace autoconfig { |
| 31 | |
| 32 | static const Name LOCALHOP_HUB_DISCOVERY_PREFIX = "/localhop/ndn-autoconf/hub"; |
| 33 | |
| 34 | MulticastDiscovery::MulticastDiscovery(Face& face, KeyChain& keyChain, |
| 35 | const NextStageCallback& nextStageOnFailure) |
| 36 | : Base(face, keyChain, nextStageOnFailure) |
Junxiao Shi | 52fa45c | 2016-11-29 21:18:13 +0000 | [diff] [blame] | 37 | , m_nRequestedRegs(0) |
| 38 | , m_nFinishedRegs(0) |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 39 | { |
| 40 | } |
| 41 | |
| 42 | void |
| 43 | MulticastDiscovery::start() |
| 44 | { |
| 45 | std::cerr << "Trying multicast discovery..." << std::endl; |
| 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 | { |
| 53 | ndn::nfd::FaceQueryFilter filter; |
| 54 | filter.setLinkType(ndn::nfd::LINK_TYPE_MULTI_ACCESS); |
| 55 | m_controller.fetch<ndn::nfd::FaceQueryDataset>( |
| 56 | filter, |
| 57 | bind(&MulticastDiscovery::registerHubDiscoveryPrefix, this, _1), |
| 58 | bind(m_nextStageOnFailure, _2) |
| 59 | ); |
| 60 | } |
| 61 | |
| 62 | void |
| 63 | MulticastDiscovery::registerHubDiscoveryPrefix(const std::vector<ndn::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), |
| 67 | [] (const ndn::nfd::FaceStatus& faceStatus) { return faceStatus.getFaceId(); }); |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 68 | |
| 69 | if (multicastFaces.empty()) { |
| 70 | m_nextStageOnFailure("No multicast faces available, skipping multicast discovery stage"); |
| 71 | } |
| 72 | else { |
Junxiao Shi | 52fa45c | 2016-11-29 21:18:13 +0000 | [diff] [blame] | 73 | 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 | 52fa45c | 2016-11-29 21:18:13 +0000 | [diff] [blame] | 84 | m_controller.start<ndn::nfd::RibRegisterCommand>( |
| 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 | 52fa45c | 2016-11-29 21:18:13 +0000 | [diff] [blame] | 103 | MulticastDiscovery::onRegisterFailure(const 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 { |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 113 | m_nextStageOnFailure("Failed to register " + LOCALHOP_HUB_DISCOVERY_PREFIX.toUri() + |
| 114 | " for all multicast faces, skipping multicast discovery stage"); |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | void |
| 120 | MulticastDiscovery::setStrategy() |
| 121 | { |
Junxiao Shi | 52fa45c | 2016-11-29 21:18:13 +0000 | [diff] [blame] | 122 | 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 | 52fa45c | 2016-11-29 21:18:13 +0000 | [diff] [blame] | 127 | m_controller.start<ndn::nfd::StrategyChoiceSetCommand>( |
| 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 | 52fa45c | 2016-11-29 21:18:13 +0000 | [diff] [blame] | 134 | MulticastDiscovery::onSetStrategyFailure(const ControlResponse& response) |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 135 | { |
Junxiao Shi | 67ba8d2 | 2015-08-21 21:21:28 -0700 | [diff] [blame] | 136 | m_nextStageOnFailure("Failed to set multicast strategy for " + |
Junxiao Shi | 29b4128 | 2016-08-22 03:47:02 +0000 | [diff] [blame] | 137 | LOCALHOP_HUB_DISCOVERY_PREFIX.toUri() + " namespace (" + response.getText() + "). " |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 138 | "Skipping multicast discovery stage"); |
| 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 | d0e276f | 2016-12-14 23:13:39 +0000 | [diff] [blame] | 150 | bind(m_nextStageOnFailure, "HUB Data not received: nacked"), |
| 151 | bind(m_nextStageOnFailure, "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()) { |
| 163 | m_nextStageOnFailure("Incorrect reply to multicast discovery stage"); |
| 164 | return; |
| 165 | } |
| 166 | std::string hubUri(reinterpret_cast<const char*>(blockValue->value()), blockValue->value_size()); |
| 167 | this->connectToHub(hubUri); |
| 168 | } |
| 169 | |
| 170 | } // namespace autoconfig |
| 171 | } // namespace tools |
| 172 | } // namespace ndn |