multicast-discovery.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2022, Regents of the University of California,
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 <boost/lexical_cast.hpp>
29 
30 #include <ndn-cxx/encoding/tlv-nfd.hpp>
31 
32 namespace ndn::autoconfig {
33 
34 using nfd::ControlParameters;
35 
36 const Name HUB_DISCOVERY_PREFIX{"/localhop/ndn-autoconf/hub"};
37 constexpr uint64_t HUB_DISCOVERY_ROUTE_COST = 1;
38 constexpr time::milliseconds HUB_DISCOVERY_ROUTE_EXPIRATION = 30_s;
39 constexpr time::milliseconds HUB_DISCOVERY_INTEREST_LIFETIME = 4_s;
40 
41 MulticastDiscovery::MulticastDiscovery(Face& face, nfd::Controller& controller)
42  : m_face(face)
43  , m_controller(controller)
44 {
45 }
46 
47 void
48 MulticastDiscovery::doStart()
49 {
50  nfd::FaceQueryFilter filter;
51  filter.setLinkType(nfd::LINK_TYPE_MULTI_ACCESS);
52 
53  m_controller.fetch<nfd::FaceQueryDataset>(
54  filter,
55  [this] (const auto& dataset) { registerHubDiscoveryPrefix(dataset); },
56  [this] (uint32_t code, const std::string& reason) {
57  fail("Error " + to_string(code) + " when querying multi-access faces: " + reason);
58  });
59 }
60 
61 void
62 MulticastDiscovery::registerHubDiscoveryPrefix(const std::vector<nfd::FaceStatus>& dataset)
63 {
64  if (dataset.empty()) {
65  fail("No multi-access faces available");
66  return;
67  }
68 
69  m_nRegs = dataset.size();
70  m_nRegSuccess = 0;
71  m_nRegFailure = 0;
72 
73  for (const auto& faceStatus : dataset) {
74  ControlParameters parameters;
75  parameters.setName(HUB_DISCOVERY_PREFIX)
76  .setFaceId(faceStatus.getFaceId())
77  .setCost(HUB_DISCOVERY_ROUTE_COST)
78  .setExpirationPeriod(HUB_DISCOVERY_ROUTE_EXPIRATION);
79 
80  m_controller.start<nfd::RibRegisterCommand>(
81  parameters,
82  [this] (const auto&) {
83  ++m_nRegSuccess;
84  afterReg();
85  },
86  [this, faceStatus] (const auto& resp) {
87  std::cerr << "Error " << resp.getCode() << " when registering hub discovery prefix "
88  << "for face " << faceStatus.getFaceId() << " (" << faceStatus.getRemoteUri()
89  << "): " << resp.getText() << std::endl;
90  ++m_nRegFailure;
91  afterReg();
92  });
93  }
94 }
95 
96 void
97 MulticastDiscovery::afterReg()
98 {
99  if (m_nRegSuccess + m_nRegFailure < m_nRegs) {
100  return; // continue waiting
101  }
102  if (m_nRegSuccess > 0) {
103  setStrategy();
104  }
105  else {
106  fail("Cannot register hub discovery prefix for any face");
107  }
108 }
109 
110 void
111 MulticastDiscovery::setStrategy()
112 {
113  ControlParameters parameters;
114  parameters.setName(HUB_DISCOVERY_PREFIX)
115  .setStrategy("/localhost/nfd/strategy/multicast");
116 
117  m_controller.start<nfd::StrategyChoiceSetCommand>(
118  parameters,
119  [this] (const auto&) { requestHubData(); },
120  [this] (const auto& resp) {
121  fail("Error " + to_string(resp.getCode()) + " when setting multicast strategy: " + resp.getText());
122  });
123 }
124 
125 void
126 MulticastDiscovery::requestHubData()
127 {
128  Interest interest(HUB_DISCOVERY_PREFIX);
129  interest.setCanBePrefix(true);
130  interest.setMustBeFresh(true);
131  interest.setInterestLifetime(HUB_DISCOVERY_INTEREST_LIFETIME);
132 
133  m_face.expressInterest(interest,
134  [this] (const Interest&, const Data& data) {
135  const Block& content = data.getContent();
136  content.parse();
137 
138  auto i = content.find(tlv::nfd::Uri);
139  if (i == content.elements_end()) {
140  fail("Malformed hub Data: missing Uri element");
141  return;
142  }
143 
144  provideHubFaceUri(std::string(reinterpret_cast<const char*>(i->value()), i->value_size()));
145  },
146  [this] (const Interest&, const lp::Nack& nack) {
147  fail("Nack-" + boost::lexical_cast<std::string>(nack.getReason()) + " when retrieving hub Data");
148  },
149  [this] (const Interest&) {
150  fail("Timeout when retrieving hub Data");
151  });
152 }
153 
154 } // namespace ndn::autoconfig
MulticastDiscovery(Face &face, nfd::Controller &controller)
void provideHubFaceUri(const std::string &s)
Parse HUB FaceUri from string and declare success.
Definition: stage.cpp:43
void fail(const std::string &msg)
Definition: stage.cpp:63
constexpr uint64_t HUB_DISCOVERY_ROUTE_COST
const Name HUB_DISCOVERY_PREFIX
constexpr time::milliseconds HUB_DISCOVERY_INTEREST_LIFETIME
constexpr time::milliseconds HUB_DISCOVERY_ROUTE_EXPIRATION