Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014-2015, 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 "base.hpp" |
| 27 | |
| 28 | namespace ndn { |
| 29 | namespace tools { |
| 30 | namespace autoconfig { |
| 31 | |
| 32 | Base::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 | |
| 40 | void |
| 41 | Base::connectToHub(const std::string& uri) |
| 42 | { |
| 43 | util::FaceUri faceUri(uri); |
| 44 | |
| 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 | |
| 52 | void |
| 53 | Base::onCanonizeSuccess(const util::FaceUri& canonicalUri) |
| 54 | { |
| 55 | std::cerr << "About to connect to: " << canonicalUri.toString() << std::endl; |
| 56 | |
| 57 | m_controller.start<nfd::FaceCreateCommand>(nfd::ControlParameters() |
| 58 | .setUri(canonicalUri.toString()), |
| 59 | bind(&Base::onHubConnectSuccess, this, _1), |
| 60 | bind(&Base::onHubConnectError, this, _1, _2)); |
| 61 | } |
| 62 | |
| 63 | void |
| 64 | Base::onCanonizeFailure(const std::string& reason) |
| 65 | { |
| 66 | std::ostringstream os; |
| 67 | os << "FaceUri canonization failed: " << reason; |
Spyridon Mastorakis | 149e02c | 2015-07-27 13:22:22 -0700 | [diff] [blame^] | 68 | BOOST_THROW_EXCEPTION(Error(os.str())); |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | void |
| 72 | Base::onHubConnectSuccess(const nfd::ControlParameters& resp) |
| 73 | { |
| 74 | std::cerr << "Successfully created face: " << resp << std::endl; |
| 75 | |
| 76 | static const Name TESTBED_PREFIX = "/ndn"; |
| 77 | registerPrefix(TESTBED_PREFIX, resp.getFaceId()); |
Alexander Afanasyev | bda8366 | 2015-01-26 18:46:07 -0800 | [diff] [blame] | 78 | |
| 79 | static const Name LOCALHOP_NFD_PREFIX = "/localhop/nfd"; |
| 80 | registerPrefix(LOCALHOP_NFD_PREFIX, resp.getFaceId()); |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | void |
| 84 | Base::onHubConnectError(uint32_t code, const std::string& error) |
| 85 | { |
| 86 | std::ostringstream os; |
| 87 | os << "Failed to create face: " << error << " (code: " << code << ")"; |
Spyridon Mastorakis | 149e02c | 2015-07-27 13:22:22 -0700 | [diff] [blame^] | 88 | BOOST_THROW_EXCEPTION(Error(os.str())); |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | void |
| 92 | Base::registerPrefix(const Name& prefix, uint64_t faceId) |
| 93 | { |
| 94 | // Register a prefix in RIB |
| 95 | m_controller.start<nfd::RibRegisterCommand>(nfd::ControlParameters() |
| 96 | .setName(prefix) |
| 97 | .setFaceId(faceId) |
| 98 | .setOrigin(nfd::ROUTE_ORIGIN_AUTOCONF) |
| 99 | .setCost(100) |
| 100 | .setExpirationPeriod(time::milliseconds::max()), |
| 101 | bind(&Base::onPrefixRegistrationSuccess, this, _1), |
| 102 | bind(&Base::onPrefixRegistrationError, this, _1, _2)); |
| 103 | } |
| 104 | |
| 105 | void |
| 106 | Base::onPrefixRegistrationSuccess(const nfd::ControlParameters& commandSuccessResult) |
| 107 | { |
| 108 | std::cerr << "Successful in name registration: " << commandSuccessResult << std::endl; |
| 109 | } |
| 110 | |
| 111 | void |
| 112 | Base::onPrefixRegistrationError(uint32_t code, const std::string& error) |
| 113 | { |
| 114 | std::ostringstream os; |
| 115 | os << "Failed in name registration, " << error << " (code: " << code << ")"; |
Spyridon Mastorakis | 149e02c | 2015-07-27 13:22:22 -0700 | [diff] [blame^] | 116 | BOOST_THROW_EXCEPTION(Error(os.str())); |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | |
| 120 | } // namespace autoconfig |
| 121 | } // namespace tools |
| 122 | } // namespace ndn |