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 | |
| 28 | #include <ndn-cxx/util/segment-fetcher.hpp> |
| 29 | |
| 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) |
| 39 | , nRequestedRegs(0) |
| 40 | , nFinishedRegs(0) |
| 41 | { |
| 42 | } |
| 43 | |
| 44 | void |
| 45 | MulticastDiscovery::start() |
| 46 | { |
| 47 | std::cerr << "Trying multicast discovery..." << std::endl; |
| 48 | |
| 49 | util::SegmentFetcher::fetch(m_face, Interest("/localhost/nfd/faces/list"), |
Muktadir R Chowdhury | 6c4a67a | 2015-09-07 22:13:01 -0500 | [diff] [blame] | 50 | m_validator, |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 51 | [this] (const ConstBufferPtr& data) { |
| 52 | registerHubDiscoveryPrefix(data); |
| 53 | }, |
| 54 | [this] (uint32_t code, const std::string& msg) { |
| 55 | m_nextStageOnFailure(msg); |
| 56 | }); |
| 57 | } |
| 58 | |
| 59 | void |
| 60 | MulticastDiscovery::registerHubDiscoveryPrefix(const ConstBufferPtr& buffer) |
| 61 | { |
| 62 | std::vector<uint64_t> multicastFaces; |
| 63 | |
| 64 | size_t offset = 0; |
| 65 | while (offset < buffer->size()) { |
Junxiao Shi | 78926c9 | 2015-02-28 22:56:06 -0700 | [diff] [blame] | 66 | bool isOk = false; |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 67 | Block block; |
Junxiao Shi | 78926c9 | 2015-02-28 22:56:06 -0700 | [diff] [blame] | 68 | std::tie(isOk, block) = Block::fromBuffer(buffer, offset); |
| 69 | if (!isOk) { |
| 70 | std::cerr << "ERROR: cannot decode FaceStatus TLV" << std::endl; |
| 71 | break; |
| 72 | } |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 73 | |
| 74 | offset += block.size(); |
| 75 | |
| 76 | nfd::FaceStatus faceStatus(block); |
| 77 | |
| 78 | ndn::util::FaceUri uri(faceStatus.getRemoteUri()); |
| 79 | if (uri.getScheme() == "udp4") { |
| 80 | namespace ip = boost::asio::ip; |
| 81 | boost::system::error_code ec; |
| 82 | ip::address address = ip::address::from_string(uri.getHost(), ec); |
| 83 | |
| 84 | if (!ec && address.is_multicast()) { |
| 85 | multicastFaces.push_back(faceStatus.getFaceId()); |
| 86 | } |
| 87 | else |
| 88 | continue; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | if (multicastFaces.empty()) { |
| 93 | m_nextStageOnFailure("No multicast faces available, skipping multicast discovery stage"); |
| 94 | } |
| 95 | else { |
| 96 | nfd::ControlParameters parameters; |
| 97 | parameters |
| 98 | .setName(LOCALHOP_HUB_DISCOVERY_PREFIX) |
| 99 | .setCost(1) |
| 100 | .setExpirationPeriod(time::seconds(30)); |
| 101 | |
| 102 | nRequestedRegs = multicastFaces.size(); |
| 103 | nFinishedRegs = 0; |
| 104 | |
| 105 | for (const auto& face : multicastFaces) { |
| 106 | parameters.setFaceId(face); |
| 107 | m_controller.start<nfd::RibRegisterCommand>(parameters, |
| 108 | bind(&MulticastDiscovery::onRegisterSuccess, |
| 109 | this), |
| 110 | bind(&MulticastDiscovery::onRegisterFailure, |
Junxiao Shi | 29b4128 | 2016-08-22 03:47:02 +0000 | [diff] [blame^] | 111 | this, _1)); |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | void |
| 117 | MulticastDiscovery::onRegisterSuccess() |
| 118 | { |
| 119 | ++nFinishedRegs; |
| 120 | |
| 121 | if (nRequestedRegs == nFinishedRegs) { |
| 122 | MulticastDiscovery::setStrategy(); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | void |
Junxiao Shi | 29b4128 | 2016-08-22 03:47:02 +0000 | [diff] [blame^] | 127 | MulticastDiscovery::onRegisterFailure(const nfd::ControlResponse& response) |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 128 | { |
Junxiao Shi | 29b4128 | 2016-08-22 03:47:02 +0000 | [diff] [blame^] | 129 | std::cerr << "ERROR: " << response.getText() << " (code: " << response.getCode() << ")" << std::endl; |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 130 | --nRequestedRegs; |
| 131 | |
| 132 | if (nRequestedRegs == nFinishedRegs) { |
| 133 | if (nRequestedRegs > 0) { |
| 134 | MulticastDiscovery::setStrategy(); |
| 135 | } else { |
| 136 | m_nextStageOnFailure("Failed to register " + LOCALHOP_HUB_DISCOVERY_PREFIX.toUri() + |
| 137 | " for all multicast faces, skipping multicast discovery stage"); |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | void |
| 143 | MulticastDiscovery::setStrategy() |
| 144 | { |
| 145 | nfd::ControlParameters parameters; |
| 146 | parameters |
| 147 | .setName(LOCALHOP_HUB_DISCOVERY_PREFIX) |
Junxiao Shi | 67ba8d2 | 2015-08-21 21:21:28 -0700 | [diff] [blame] | 148 | .setStrategy("/localhost/nfd/strategy/multicast"); |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 149 | |
| 150 | m_controller.start<nfd::StrategyChoiceSetCommand>(parameters, |
| 151 | bind(&MulticastDiscovery::requestHubData, this), |
| 152 | bind(&MulticastDiscovery::onSetStrategyFailure, |
Junxiao Shi | 29b4128 | 2016-08-22 03:47:02 +0000 | [diff] [blame^] | 153 | this, _1)); |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | void |
Junxiao Shi | 29b4128 | 2016-08-22 03:47:02 +0000 | [diff] [blame^] | 157 | MulticastDiscovery::onSetStrategyFailure(const nfd::ControlResponse& response) |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 158 | { |
Junxiao Shi | 67ba8d2 | 2015-08-21 21:21:28 -0700 | [diff] [blame] | 159 | m_nextStageOnFailure("Failed to set multicast strategy for " + |
Junxiao Shi | 29b4128 | 2016-08-22 03:47:02 +0000 | [diff] [blame^] | 160 | LOCALHOP_HUB_DISCOVERY_PREFIX.toUri() + " namespace (" + response.getText() + "). " |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 161 | "Skipping multicast discovery stage"); |
| 162 | } |
| 163 | |
| 164 | void |
| 165 | MulticastDiscovery::requestHubData() |
| 166 | { |
| 167 | Interest interest(LOCALHOP_HUB_DISCOVERY_PREFIX); |
| 168 | interest.setInterestLifetime(time::milliseconds(4000)); // 4 seconds |
| 169 | interest.setMustBeFresh(true); |
| 170 | |
| 171 | m_face.expressInterest(interest, |
| 172 | bind(&MulticastDiscovery::onSuccess, this, _2), |
| 173 | bind(m_nextStageOnFailure, "Timeout")); |
| 174 | } |
| 175 | |
| 176 | void |
| 177 | MulticastDiscovery::onSuccess(Data& data) |
| 178 | { |
| 179 | const Block& content = data.getContent(); |
| 180 | content.parse(); |
| 181 | |
| 182 | // Get Uri |
| 183 | Block::element_const_iterator blockValue = content.find(tlv::nfd::Uri); |
| 184 | if (blockValue == content.elements_end()) { |
| 185 | m_nextStageOnFailure("Incorrect reply to multicast discovery stage"); |
| 186 | return; |
| 187 | } |
| 188 | std::string hubUri(reinterpret_cast<const char*>(blockValue->value()), blockValue->value_size()); |
| 189 | this->connectToHub(hubUri); |
| 190 | } |
| 191 | |
| 192 | } // namespace autoconfig |
| 193 | } // namespace tools |
| 194 | } // namespace ndn |