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 | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame^] | 3 | * Copyright (c) 2014-2019, 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; |
| 37 | using nfd::ControlResponse; |
| 38 | |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame^] | 39 | const Name HUB_DISCOVERY_PREFIX("/localhop/ndn-autoconf/hub"); |
| 40 | const uint64_t HUB_DISCOVERY_ROUTE_COST(1); |
| 41 | const time::milliseconds HUB_DISCOVERY_ROUTE_EXPIRATION = 30_s; |
| 42 | const time::milliseconds HUB_DISCOVERY_INTEREST_LIFETIME = 4_s; |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 43 | |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 44 | MulticastDiscovery::MulticastDiscovery(Face& face, nfd::Controller& controller) |
| 45 | : m_face(face) |
| 46 | , m_controller(controller) |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 47 | { |
| 48 | } |
| 49 | |
| 50 | void |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 51 | MulticastDiscovery::doStart() |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 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); |
Junxiao Shi | a5a3a87 | 2017-07-08 12:42:11 +0000 | [diff] [blame] | 55 | |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 56 | m_controller.fetch<nfd::FaceQueryDataset>( |
Junxiao Shi | a889111 | 2016-12-06 21:11:33 +0000 | [diff] [blame] | 57 | filter, |
| 58 | bind(&MulticastDiscovery::registerHubDiscoveryPrefix, this, _1), |
Junxiao Shi | a5a3a87 | 2017-07-08 12:42:11 +0000 | [diff] [blame] | 59 | [this] (uint32_t code, const std::string& reason) { |
| 60 | this->fail("Error " + to_string(code) + " when querying multi-access faces: " + reason); |
| 61 | }); |
Junxiao Shi | a889111 | 2016-12-06 21:11:33 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | void |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 65 | MulticastDiscovery::registerHubDiscoveryPrefix(const std::vector<nfd::FaceStatus>& dataset) |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 66 | { |
Junxiao Shi | a5a3a87 | 2017-07-08 12:42:11 +0000 | [diff] [blame] | 67 | if (dataset.empty()) { |
| 68 | this->fail("No multi-access faces available"); |
| 69 | return; |
| 70 | } |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 71 | |
Junxiao Shi | a5a3a87 | 2017-07-08 12:42:11 +0000 | [diff] [blame] | 72 | m_nRegs = dataset.size(); |
| 73 | m_nRegSuccess = 0; |
| 74 | m_nRegFailure = 0; |
| 75 | |
| 76 | for (const auto& faceStatus : dataset) { |
| 77 | ControlParameters parameters; |
| 78 | parameters.setName(HUB_DISCOVERY_PREFIX) |
| 79 | .setFaceId(faceStatus.getFaceId()) |
| 80 | .setCost(HUB_DISCOVERY_ROUTE_COST) |
| 81 | .setExpirationPeriod(HUB_DISCOVERY_ROUTE_EXPIRATION); |
| 82 | |
| 83 | m_controller.start<nfd::RibRegisterCommand>( |
| 84 | parameters, |
| 85 | [this] (const ControlParameters&) { |
| 86 | ++m_nRegSuccess; |
| 87 | afterReg(); |
| 88 | }, |
| 89 | [this, faceStatus] (const ControlResponse& resp) { |
| 90 | std::cerr << "Error " << resp.getCode() << " when registering hub discovery prefix " |
| 91 | << "for face " << faceStatus.getFaceId() << " (" << faceStatus.getRemoteUri() |
| 92 | << "): " << resp.getText() << std::endl; |
| 93 | ++m_nRegFailure; |
| 94 | afterReg(); |
| 95 | }); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | void |
| 100 | MulticastDiscovery::afterReg() |
| 101 | { |
| 102 | if (m_nRegSuccess + m_nRegFailure < m_nRegs) { |
| 103 | return; // continue waiting |
| 104 | } |
| 105 | if (m_nRegSuccess > 0) { |
| 106 | this->setStrategy(); |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 107 | } |
| 108 | else { |
Junxiao Shi | a5a3a87 | 2017-07-08 12:42:11 +0000 | [diff] [blame] | 109 | this->fail("Cannot register hub discovery prefix for any face"); |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | |
| 113 | void |
| 114 | MulticastDiscovery::setStrategy() |
| 115 | { |
Junxiao Shi | a5a3a87 | 2017-07-08 12:42:11 +0000 | [diff] [blame] | 116 | ControlParameters parameters; |
| 117 | parameters.setName(HUB_DISCOVERY_PREFIX) |
| 118 | .setStrategy("/localhost/nfd/strategy/multicast"), |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 119 | |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 120 | m_controller.start<nfd::StrategyChoiceSetCommand>( |
Junxiao Shi | 52fa45c | 2016-11-29 21:18:13 +0000 | [diff] [blame] | 121 | parameters, |
| 122 | bind(&MulticastDiscovery::requestHubData, this), |
Junxiao Shi | a5a3a87 | 2017-07-08 12:42:11 +0000 | [diff] [blame] | 123 | [this] (const ControlResponse& resp) { |
| 124 | this->fail("Error " + to_string(resp.getCode()) + " when setting multicast strategy: " + |
| 125 | resp.getText()); |
| 126 | }); |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | void |
| 130 | MulticastDiscovery::requestHubData() |
| 131 | { |
Junxiao Shi | a5a3a87 | 2017-07-08 12:42:11 +0000 | [diff] [blame] | 132 | Interest interest(HUB_DISCOVERY_PREFIX); |
| 133 | interest.setInterestLifetime(HUB_DISCOVERY_INTEREST_LIFETIME); |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 134 | interest.setMustBeFresh(true); |
| 135 | |
| 136 | m_face.expressInterest(interest, |
Junxiao Shi | a5a3a87 | 2017-07-08 12:42:11 +0000 | [diff] [blame] | 137 | [this] (const Interest&, const Data& data) { |
| 138 | const Block& content = data.getContent(); |
| 139 | content.parse(); |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 140 | |
Junxiao Shi | a5a3a87 | 2017-07-08 12:42:11 +0000 | [diff] [blame] | 141 | auto i = content.find(tlv::nfd::Uri); |
| 142 | if (i == content.elements_end()) { |
| 143 | this->fail("Malformed hub Data: missing Uri element"); |
| 144 | return; |
| 145 | } |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 146 | |
Junxiao Shi | a5a3a87 | 2017-07-08 12:42:11 +0000 | [diff] [blame] | 147 | this->provideHubFaceUri(std::string(reinterpret_cast<const char*>(i->value()), i->value_size())); |
| 148 | }, |
| 149 | [this] (const Interest&, const lp::Nack& nack) { |
| 150 | this->fail("Nack-" + boost::lexical_cast<std::string>(nack.getReason()) + " when retrieving hub Data"); |
| 151 | }, |
| 152 | [this] (const Interest&) { |
| 153 | this->fail("Timeout when retrieving hub Data"); |
| 154 | }); |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | } // namespace autoconfig |
| 158 | } // namespace tools |
| 159 | } // namespace ndn |