blob: 267dfd94313c3d55904b30309ec985073ac820fe [file] [log] [blame]
Alexander Afanasyev2a655f72015-01-26 18:38:33 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shif748a4e2017-07-05 23:41:48 +00002/*
Davide Pesavento14e71f02019-03-28 17:35:25 -04003 * Copyright (c) 2014-2019, Regents of the University of California,
Alexander Afanasyev2a655f72015-01-26 18:38:33 -08004 * 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 Pesavento14e71f02019-03-28 17:35:25 -040027
Junxiao Shia5a3a872017-07-08 12:42:11 +000028#include <boost/lexical_cast.hpp>
Davide Pesavento14e71f02019-03-28 17:35:25 -040029
Davide Pesavento7a294d42017-02-21 21:46:44 -050030#include <ndn-cxx/encoding/tlv-nfd.hpp>
31
Alexander Afanasyev2a655f72015-01-26 18:38:33 -080032namespace ndn {
33namespace tools {
34namespace autoconfig {
35
Junxiao Shia5a3a872017-07-08 12:42:11 +000036using nfd::ControlParameters;
37using nfd::ControlResponse;
38
Davide Pesavento14e71f02019-03-28 17:35:25 -040039const Name HUB_DISCOVERY_PREFIX("/localhop/ndn-autoconf/hub");
40const uint64_t HUB_DISCOVERY_ROUTE_COST(1);
41const time::milliseconds HUB_DISCOVERY_ROUTE_EXPIRATION = 30_s;
42const time::milliseconds HUB_DISCOVERY_INTEREST_LIFETIME = 4_s;
Alexander Afanasyev2a655f72015-01-26 18:38:33 -080043
Junxiao Shicb766862017-07-07 22:21:04 +000044MulticastDiscovery::MulticastDiscovery(Face& face, nfd::Controller& controller)
45 : m_face(face)
46 , m_controller(controller)
Alexander Afanasyev2a655f72015-01-26 18:38:33 -080047{
48}
49
50void
Junxiao Shicb766862017-07-07 22:21:04 +000051MulticastDiscovery::doStart()
Alexander Afanasyev2a655f72015-01-26 18:38:33 -080052{
Junxiao Shicb766862017-07-07 22:21:04 +000053 nfd::FaceQueryFilter filter;
54 filter.setLinkType(nfd::LINK_TYPE_MULTI_ACCESS);
Junxiao Shia5a3a872017-07-08 12:42:11 +000055
Junxiao Shicb766862017-07-07 22:21:04 +000056 m_controller.fetch<nfd::FaceQueryDataset>(
Junxiao Shia8891112016-12-06 21:11:33 +000057 filter,
58 bind(&MulticastDiscovery::registerHubDiscoveryPrefix, this, _1),
Junxiao Shia5a3a872017-07-08 12:42:11 +000059 [this] (uint32_t code, const std::string& reason) {
60 this->fail("Error " + to_string(code) + " when querying multi-access faces: " + reason);
61 });
Junxiao Shia8891112016-12-06 21:11:33 +000062}
63
64void
Junxiao Shicb766862017-07-07 22:21:04 +000065MulticastDiscovery::registerHubDiscoveryPrefix(const std::vector<nfd::FaceStatus>& dataset)
Alexander Afanasyev2a655f72015-01-26 18:38:33 -080066{
Junxiao Shia5a3a872017-07-08 12:42:11 +000067 if (dataset.empty()) {
68 this->fail("No multi-access faces available");
69 return;
70 }
Alexander Afanasyev2a655f72015-01-26 18:38:33 -080071
Junxiao Shia5a3a872017-07-08 12:42:11 +000072 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
99void
100MulticastDiscovery::afterReg()
101{
102 if (m_nRegSuccess + m_nRegFailure < m_nRegs) {
103 return; // continue waiting
104 }
105 if (m_nRegSuccess > 0) {
106 this->setStrategy();
Alexander Afanasyev2a655f72015-01-26 18:38:33 -0800107 }
108 else {
Junxiao Shia5a3a872017-07-08 12:42:11 +0000109 this->fail("Cannot register hub discovery prefix for any face");
Alexander Afanasyev2a655f72015-01-26 18:38:33 -0800110 }
111}
112
113void
114MulticastDiscovery::setStrategy()
115{
Junxiao Shia5a3a872017-07-08 12:42:11 +0000116 ControlParameters parameters;
117 parameters.setName(HUB_DISCOVERY_PREFIX)
118 .setStrategy("/localhost/nfd/strategy/multicast"),
Alexander Afanasyev2a655f72015-01-26 18:38:33 -0800119
Junxiao Shicb766862017-07-07 22:21:04 +0000120 m_controller.start<nfd::StrategyChoiceSetCommand>(
Junxiao Shi52fa45c2016-11-29 21:18:13 +0000121 parameters,
122 bind(&MulticastDiscovery::requestHubData, this),
Junxiao Shia5a3a872017-07-08 12:42:11 +0000123 [this] (const ControlResponse& resp) {
124 this->fail("Error " + to_string(resp.getCode()) + " when setting multicast strategy: " +
125 resp.getText());
126 });
Alexander Afanasyev2a655f72015-01-26 18:38:33 -0800127}
128
129void
130MulticastDiscovery::requestHubData()
131{
Junxiao Shia5a3a872017-07-08 12:42:11 +0000132 Interest interest(HUB_DISCOVERY_PREFIX);
Junxiao Shi47c343b2019-05-25 07:53:01 +0000133 interest.setCanBePrefix(true);
Alexander Afanasyev2a655f72015-01-26 18:38:33 -0800134 interest.setMustBeFresh(true);
Junxiao Shi47c343b2019-05-25 07:53:01 +0000135 interest.setInterestLifetime(HUB_DISCOVERY_INTEREST_LIFETIME);
Alexander Afanasyev2a655f72015-01-26 18:38:33 -0800136
137 m_face.expressInterest(interest,
Junxiao Shia5a3a872017-07-08 12:42:11 +0000138 [this] (const Interest&, const Data& data) {
139 const Block& content = data.getContent();
140 content.parse();
Alexander Afanasyev2a655f72015-01-26 18:38:33 -0800141
Junxiao Shia5a3a872017-07-08 12:42:11 +0000142 auto i = content.find(tlv::nfd::Uri);
143 if (i == content.elements_end()) {
144 this->fail("Malformed hub Data: missing Uri element");
145 return;
146 }
Alexander Afanasyev2a655f72015-01-26 18:38:33 -0800147
Junxiao Shia5a3a872017-07-08 12:42:11 +0000148 this->provideHubFaceUri(std::string(reinterpret_cast<const char*>(i->value()), i->value_size()));
149 },
150 [this] (const Interest&, const lp::Nack& nack) {
151 this->fail("Nack-" + boost::lexical_cast<std::string>(nack.getReason()) + " when retrieving hub Data");
152 },
153 [this] (const Interest&) {
154 this->fail("Timeout when retrieving hub Data");
155 });
Alexander Afanasyev2a655f72015-01-26 18:38:33 -0800156}
157
158} // namespace autoconfig
159} // namespace tools
160} // namespace ndn