procedure.cpp
Go to the documentation of this file.
1 /* -*- 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"
29 #include "multicast-discovery.hpp"
30 #include "ndn-fch-discovery.hpp"
31 
32 namespace ndn {
33 namespace tools {
34 namespace autoconfig {
35 
36 using nfd::ControlParameters;
37 using nfd::ControlResponse;
38 
39 static const time::nanoseconds FACEURI_CANONIZE_TIMEOUT = time::seconds(4);
40 static const std::vector<Name> HUB_PREFIXES{"/", "/localhop/nfd"};
41 static const nfd::RouteOrigin HUB_ROUTE_ORIGIN = nfd::ROUTE_ORIGIN_AUTOCONF;
42 static const uint64_t HUB_ROUTE_COST = 100;
43 
44 Procedure::Procedure(Face& face, KeyChain& keyChain)
45  : m_face(face)
46  , m_keyChain(keyChain)
47  , m_controller(face, keyChain)
48 {
49 }
50 
51 void
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 
69 void
70 Procedure::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 
78 void
80 {
81  BOOST_ASSERT(!m_stages.empty());
82  m_stages.front()->start();
83 }
84 
85 void
86 Procedure::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 
116 void
117 Procedure::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
Copyright (c) 2014-2017, Regents of the University of California, Arizona Board of Regents...
std::string ndnFchUrl
HTTP base URL of NDN-FCH service.
Definition: procedure.hpp:40
static const nfd::RouteOrigin HUB_ROUTE_ORIGIN
Definition: procedure.cpp:41
Procedure(Face &face, KeyChain &keyChain)
Definition: procedure.cpp:44
void initialize(const Options &options)
Definition: procedure.cpp:52
util::Signal< Procedure, bool > onComplete
signal when procedure completes
Definition: procedure.hpp:80
static const time::nanoseconds FACEURI_CANONIZE_TIMEOUT
Definition: procedure.cpp:39
static const std::vector< Name > HUB_PREFIXES
Definition: procedure.cpp:40
static const uint64_t HUB_ROUTE_COST
Definition: procedure.cpp:42
void runOnce()
run HUB discovery procedure once
Definition: procedure.cpp:79