blob: c95eb21c832379acd5cfa46f80598504f810f64e [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 "base.hpp"
27
28namespace ndn {
29namespace tools {
30namespace autoconfig {
31
32Base::Base(Face& face, KeyChain& keyChain, const NextStageCallback& nextStageOnFailure)
33 : m_face(face)
34 , m_keyChain(keyChain)
35 , m_controller(face, keyChain)
36 , m_nextStageOnFailure(nextStageOnFailure)
37{
38}
39
40void
41Base::connectToHub(const std::string& uri)
42{
Junxiao Shi52fa45c2016-11-29 21:18:13 +000043 FaceUri faceUri(uri);
Alexander Afanasyev2a655f72015-01-26 18:38:33 -080044
45 faceUri.canonize(bind(&Base::onCanonizeSuccess, this, _1),
46 bind(&Base::onCanonizeFailure, this, _1),
47 m_face.getIoService(), time::seconds(4));
48
49}
50
51
52void
Junxiao Shi52fa45c2016-11-29 21:18:13 +000053Base::onCanonizeSuccess(const FaceUri& canonicalUri)
Alexander Afanasyev2a655f72015-01-26 18:38:33 -080054{
55 std::cerr << "About to connect to: " << canonicalUri.toString() << std::endl;
56
Junxiao Shi52fa45c2016-11-29 21:18:13 +000057 m_controller.start<ndn::nfd::FaceCreateCommand>(
58 ControlParameters().setUri(canonicalUri.toString()),
59 bind(&Base::onHubConnectSuccess, this, _1),
60 bind(&Base::onHubConnectError, this, _1));
Alexander Afanasyev2a655f72015-01-26 18:38:33 -080061}
62
63void
64Base::onCanonizeFailure(const std::string& reason)
65{
Junxiao Shi52fa45c2016-11-29 21:18:13 +000066 BOOST_THROW_EXCEPTION(Error("FaceUri canonization failed: " + reason));
Alexander Afanasyev2a655f72015-01-26 18:38:33 -080067}
68
69void
Junxiao Shi52fa45c2016-11-29 21:18:13 +000070Base::onHubConnectSuccess(const ControlParameters& resp)
Alexander Afanasyev2a655f72015-01-26 18:38:33 -080071{
72 std::cerr << "Successfully created face: " << resp << std::endl;
73
74 static const Name TESTBED_PREFIX = "/ndn";
75 registerPrefix(TESTBED_PREFIX, resp.getFaceId());
Alexander Afanasyevbda83662015-01-26 18:46:07 -080076
77 static const Name LOCALHOP_NFD_PREFIX = "/localhop/nfd";
78 registerPrefix(LOCALHOP_NFD_PREFIX, resp.getFaceId());
Alexander Afanasyev2a655f72015-01-26 18:38:33 -080079}
80
81void
Junxiao Shi52fa45c2016-11-29 21:18:13 +000082Base::onHubConnectError(const ControlResponse& response)
Alexander Afanasyev2a655f72015-01-26 18:38:33 -080083{
84 std::ostringstream os;
Junxiao Shi29b41282016-08-22 03:47:02 +000085 os << "Failed to create face: " << response.getText() << " (code: " << response.getCode() << ")";
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -070086 BOOST_THROW_EXCEPTION(Error(os.str()));
Alexander Afanasyev2a655f72015-01-26 18:38:33 -080087}
88
89void
90Base::registerPrefix(const Name& prefix, uint64_t faceId)
91{
92 // Register a prefix in RIB
Junxiao Shi52fa45c2016-11-29 21:18:13 +000093 m_controller.start<ndn::nfd::RibRegisterCommand>(
94 ControlParameters()
95 .setName(prefix)
96 .setFaceId(faceId)
97 .setOrigin(ndn::nfd::ROUTE_ORIGIN_AUTOCONF)
98 .setCost(100)
99 .setExpirationPeriod(time::milliseconds::max()),
100 bind(&Base::onPrefixRegistrationSuccess, this, _1),
101 bind(&Base::onPrefixRegistrationError, this, _1));
Alexander Afanasyev2a655f72015-01-26 18:38:33 -0800102}
103
104void
Junxiao Shi52fa45c2016-11-29 21:18:13 +0000105Base::onPrefixRegistrationSuccess(const ControlParameters& commandSuccessResult)
Alexander Afanasyev2a655f72015-01-26 18:38:33 -0800106{
107 std::cerr << "Successful in name registration: " << commandSuccessResult << std::endl;
108}
109
110void
Junxiao Shi52fa45c2016-11-29 21:18:13 +0000111Base::onPrefixRegistrationError(const ControlResponse& response)
Alexander Afanasyev2a655f72015-01-26 18:38:33 -0800112{
Junxiao Shi52fa45c2016-11-29 21:18:13 +0000113 BOOST_THROW_EXCEPTION(Error("Failed in name registration, " + response.getText() +
114 " (code: " + to_string(response.getCode()) + ")"));
Alexander Afanasyev2a655f72015-01-26 18:38:33 -0800115}
116
117
118} // namespace autoconfig
119} // namespace tools
120} // namespace ndn