blob: fff88f7219214e4f51898e028a30a5ba91785cf2 [file] [log] [blame]
Junxiao Shicb766862017-07-07 22:21:04 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2014-2017, 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 "procedure.hpp"
27#include "guess-from-identity-name.hpp"
28#include "guess-from-search-domains.hpp"
29#include "multicast-discovery.hpp"
30#include "ndn-fch-discovery.hpp"
31
32namespace ndn {
33namespace tools {
34namespace autoconfig {
35
36using nfd::ControlParameters;
37using nfd::ControlResponse;
38
39static const time::nanoseconds FACEURI_CANONIZE_TIMEOUT = time::seconds(4);
Md Ashiqur Rahmanc8f17e52017-08-19 19:18:43 +000040static const std::vector<Name> HUB_PREFIXES{"/", "/localhop/nfd"};
Junxiao Shicb766862017-07-07 22:21:04 +000041static const nfd::RouteOrigin HUB_ROUTE_ORIGIN = nfd::ROUTE_ORIGIN_AUTOCONF;
42static const uint64_t HUB_ROUTE_COST = 100;
43
44Procedure::Procedure(Face& face, KeyChain& keyChain)
45 : m_face(face)
46 , m_keyChain(keyChain)
47 , m_controller(face, keyChain)
48{
49}
50
51void
52Procedure::initialize(const Options& options)
53{
54 BOOST_ASSERT(m_stages.empty());
55 this->makeStages(options);
56 BOOST_ASSERT(!m_stages.empty());
57
58 for (size_t i = 0; i < m_stages.size(); ++i) {
59 m_stages[i]->onSuccess.connect(bind(&Procedure::connect, this, _1));
60 if (i + 1 < m_stages.size()) {
61 m_stages[i]->onFailure.connect([=] (const std::string&) { m_stages[i + 1]->start(); });
62 }
63 else {
64 m_stages[i]->onFailure.connect([=] (const std::string&) { this->onComplete(false); });
65 }
66 }
67}
68
69void
70Procedure::makeStages(const Options& options)
71{
72 m_stages.push_back(make_unique<MulticastDiscovery>(m_face, m_controller));
73 m_stages.push_back(make_unique<GuessFromSearchDomains>());
74 m_stages.push_back(make_unique<NdnFchDiscovery>(options.ndnFchUrl));
75 m_stages.push_back(make_unique<GuessFromIdentityName>(m_keyChain));
76}
77
78void
79Procedure::runOnce()
80{
81 BOOST_ASSERT(!m_stages.empty());
82 m_stages.front()->start();
83}
84
85void
86Procedure::connect(const FaceUri& hubFaceUri)
87{
88 hubFaceUri.canonize(
89 [this] (const FaceUri& canonicalUri) {
90 m_controller.start<nfd::FaceCreateCommand>(
91 ControlParameters().setUri(canonicalUri.toString()),
92 [this] (const ControlParameters& params) {
93 std::cerr << "Connected to HUB " << params.getUri() << std::endl;
94 this->registerPrefixes(params.getFaceId());
95 },
96 [this, canonicalUri] (const ControlResponse& resp) {
97 if (resp.getCode() == 409) {
98 ControlParameters params(resp.getBody());
99 std::cerr << "Already connected to HUB " << params.getUri() << std::endl;
100 this->registerPrefixes(params.getFaceId());
101 }
102 else {
103 std::cerr << "Failed to connect to HUB " << canonicalUri << ": "
104 << resp.getText() << " (" << resp.getCode() << ")" << std::endl;
105 this->onComplete(false);
106 }
107 });
108 },
109 [this] (const std::string& reason) {
110 std::cerr << "Failed to canonize HUB FaceUri: " << reason << std::endl;
111 this->onComplete(false);
112 },
113 m_face.getIoService(), FACEURI_CANONIZE_TIMEOUT);
114}
115
116void
117Procedure::registerPrefixes(uint64_t hubFaceId, size_t index)
118{
119 if (index >= HUB_PREFIXES.size()) {
120 this->onComplete(true);
121 return;
122 }
123
124 m_controller.start<nfd::RibRegisterCommand>(
125 ControlParameters()
126 .setName(HUB_PREFIXES[index])
127 .setFaceId(hubFaceId)
128 .setOrigin(HUB_ROUTE_ORIGIN)
129 .setCost(HUB_ROUTE_COST),
130 [=] (const ControlParameters&) {
131 std::cerr << "Registered prefix " << HUB_PREFIXES[index] << std::endl;
132 this->registerPrefixes(hubFaceId, index + 1);
133 },
134 [=] (const ControlResponse& resp) {
135 std::cerr << "Failed to register " << HUB_PREFIXES[index] << ": "
136 << resp.getText() << " (" << resp.getCode() << ")" << std::endl;
137 this->onComplete(false);
138 });
139}
140
141} // namespace autoconfig
142} // namespace tools
143} // namespace ndn