blob: a47489925e55a506ab43ad280e70302848b7a7f2 [file] [log] [blame]
Alexander Afanasyev2a655f72015-01-26 18:38:33 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi29b41282016-08-22 03:47:02 +00003 * Copyright (c) 2014-2016, 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"
27
28#include <ndn-cxx/util/segment-fetcher.hpp>
29
30namespace ndn {
31namespace tools {
32namespace autoconfig {
33
34static const Name LOCALHOP_HUB_DISCOVERY_PREFIX = "/localhop/ndn-autoconf/hub";
35
36MulticastDiscovery::MulticastDiscovery(Face& face, KeyChain& keyChain,
37 const NextStageCallback& nextStageOnFailure)
38 : Base(face, keyChain, nextStageOnFailure)
39 , nRequestedRegs(0)
40 , nFinishedRegs(0)
41{
42}
43
44void
45MulticastDiscovery::start()
46{
47 std::cerr << "Trying multicast discovery..." << std::endl;
48
49 util::SegmentFetcher::fetch(m_face, Interest("/localhost/nfd/faces/list"),
Muktadir R Chowdhury6c4a67a2015-09-07 22:13:01 -050050 m_validator,
Alexander Afanasyev2a655f72015-01-26 18:38:33 -080051 [this] (const ConstBufferPtr& data) {
52 registerHubDiscoveryPrefix(data);
53 },
54 [this] (uint32_t code, const std::string& msg) {
55 m_nextStageOnFailure(msg);
56 });
57}
58
59void
60MulticastDiscovery::registerHubDiscoveryPrefix(const ConstBufferPtr& buffer)
61{
62 std::vector<uint64_t> multicastFaces;
63
64 size_t offset = 0;
65 while (offset < buffer->size()) {
Junxiao Shi78926c92015-02-28 22:56:06 -070066 bool isOk = false;
Alexander Afanasyev2a655f72015-01-26 18:38:33 -080067 Block block;
Junxiao Shi78926c92015-02-28 22:56:06 -070068 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 Afanasyev2a655f72015-01-26 18:38:33 -080073
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 Shi29b41282016-08-22 03:47:02 +0000111 this, _1));
Alexander Afanasyev2a655f72015-01-26 18:38:33 -0800112 }
113 }
114}
115
116void
117MulticastDiscovery::onRegisterSuccess()
118{
119 ++nFinishedRegs;
120
121 if (nRequestedRegs == nFinishedRegs) {
122 MulticastDiscovery::setStrategy();
123 }
124}
125
126void
Junxiao Shi29b41282016-08-22 03:47:02 +0000127MulticastDiscovery::onRegisterFailure(const nfd::ControlResponse& response)
Alexander Afanasyev2a655f72015-01-26 18:38:33 -0800128{
Junxiao Shi29b41282016-08-22 03:47:02 +0000129 std::cerr << "ERROR: " << response.getText() << " (code: " << response.getCode() << ")" << std::endl;
Alexander Afanasyev2a655f72015-01-26 18:38:33 -0800130 --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
142void
143MulticastDiscovery::setStrategy()
144{
145 nfd::ControlParameters parameters;
146 parameters
147 .setName(LOCALHOP_HUB_DISCOVERY_PREFIX)
Junxiao Shi67ba8d22015-08-21 21:21:28 -0700148 .setStrategy("/localhost/nfd/strategy/multicast");
Alexander Afanasyev2a655f72015-01-26 18:38:33 -0800149
150 m_controller.start<nfd::StrategyChoiceSetCommand>(parameters,
151 bind(&MulticastDiscovery::requestHubData, this),
152 bind(&MulticastDiscovery::onSetStrategyFailure,
Junxiao Shi29b41282016-08-22 03:47:02 +0000153 this, _1));
Alexander Afanasyev2a655f72015-01-26 18:38:33 -0800154}
155
156void
Junxiao Shi29b41282016-08-22 03:47:02 +0000157MulticastDiscovery::onSetStrategyFailure(const nfd::ControlResponse& response)
Alexander Afanasyev2a655f72015-01-26 18:38:33 -0800158{
Junxiao Shi67ba8d22015-08-21 21:21:28 -0700159 m_nextStageOnFailure("Failed to set multicast strategy for " +
Junxiao Shi29b41282016-08-22 03:47:02 +0000160 LOCALHOP_HUB_DISCOVERY_PREFIX.toUri() + " namespace (" + response.getText() + "). "
Alexander Afanasyev2a655f72015-01-26 18:38:33 -0800161 "Skipping multicast discovery stage");
162}
163
164void
165MulticastDiscovery::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
176void
177MulticastDiscovery::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