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