Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Qi Zhao | 6fc57a6 | 2017-04-05 16:53:10 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2017, Regents of the University of California, |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 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 | #ifndef NFD_TOOLS_NDN_AUTOCONFIG_BASE_HPP |
| 27 | #define NFD_TOOLS_NDN_AUTOCONFIG_BASE_HPP |
| 28 | |
Junxiao Shi | 9f5b01d | 2016-08-05 03:54:28 +0000 | [diff] [blame] | 29 | #include "core/common.hpp" |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 30 | |
| 31 | #include <ndn-cxx/face.hpp> |
Junxiao Shi | 25c6ce4 | 2016-09-09 13:49:59 +0000 | [diff] [blame] | 32 | #include <ndn-cxx/mgmt/nfd/controller.hpp> |
| 33 | #include <ndn-cxx/mgmt/nfd/face-status.hpp> |
Junxiao Shi | 52fa45c | 2016-11-29 21:18:13 +0000 | [diff] [blame] | 34 | #include <ndn-cxx/security/key-chain.hpp> |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 35 | #include <ndn-cxx/util/face-uri.hpp> |
| 36 | |
| 37 | namespace ndn { |
| 38 | namespace tools { |
| 39 | namespace autoconfig { |
| 40 | |
Junxiao Shi | 52fa45c | 2016-11-29 21:18:13 +0000 | [diff] [blame] | 41 | using ndn::nfd::ControlParameters; |
| 42 | using ndn::nfd::ControlResponse; |
| 43 | using ndn::util::FaceUri; |
| 44 | |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 45 | /** |
| 46 | * @brief Base class for discovery stages |
| 47 | */ |
| 48 | class Base : boost::noncopyable |
| 49 | { |
| 50 | public: |
| 51 | class Error : public std::runtime_error |
| 52 | { |
| 53 | public: |
| 54 | explicit |
| 55 | Error(const std::string& what) |
| 56 | : std::runtime_error(what) |
| 57 | { |
| 58 | } |
| 59 | }; |
| 60 | |
| 61 | /** |
| 62 | * @brief Callback to be called when the stage fails |
| 63 | */ |
| 64 | typedef std::function<void(const std::string&)> NextStageCallback; |
| 65 | |
| 66 | /** |
| 67 | * @brief Start the stage |
| 68 | */ |
| 69 | virtual void |
| 70 | start() = 0; |
| 71 | |
| 72 | protected: |
| 73 | /** |
Junxiao Shi | 52fa45c | 2016-11-29 21:18:13 +0000 | [diff] [blame] | 74 | * @brief Initialize variables and create Controller instance |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 75 | * @param face Face to be used for all operations (e.g., will send registration commands) |
| 76 | * @param keyChain KeyChain object |
| 77 | * @param nextStageOnFailure Callback to be called after the stage failed |
| 78 | */ |
| 79 | Base(Face& face, KeyChain& keyChain, const NextStageCallback& nextStageOnFailure); |
| 80 | |
| 81 | /** |
| 82 | * @brief Attempt to connect to local hub using the \p uri FaceUri |
| 83 | * @throw Base::Error when failed to establish the tunnel |
| 84 | */ |
| 85 | void |
| 86 | connectToHub(const std::string& uri); |
| 87 | |
| 88 | private: |
| 89 | void |
Junxiao Shi | 52fa45c | 2016-11-29 21:18:13 +0000 | [diff] [blame] | 90 | onCanonizeSuccess(const FaceUri& canonicalUri); |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 91 | |
| 92 | void |
| 93 | onCanonizeFailure(const std::string& reason); |
| 94 | |
| 95 | void |
Junxiao Shi | 52fa45c | 2016-11-29 21:18:13 +0000 | [diff] [blame] | 96 | onHubConnectSuccess(const ControlParameters& resp); |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 97 | |
| 98 | void |
Junxiao Shi | 52fa45c | 2016-11-29 21:18:13 +0000 | [diff] [blame] | 99 | onHubConnectError(const ControlResponse& response); |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 100 | |
| 101 | void |
Qi Zhao | 6fc57a6 | 2017-04-05 16:53:10 +0000 | [diff] [blame] | 102 | registerAutoConfigNames(uint64_t faceId); |
| 103 | |
| 104 | void |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 105 | registerPrefix(const Name& prefix, uint64_t faceId); |
| 106 | |
| 107 | void |
Junxiao Shi | 52fa45c | 2016-11-29 21:18:13 +0000 | [diff] [blame] | 108 | onPrefixRegistrationSuccess(const ControlParameters& commandSuccessResult); |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 109 | |
| 110 | void |
Junxiao Shi | 52fa45c | 2016-11-29 21:18:13 +0000 | [diff] [blame] | 111 | onPrefixRegistrationError(const ControlResponse& response); |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 112 | |
| 113 | protected: |
| 114 | Face& m_face; |
| 115 | KeyChain& m_keyChain; |
Junxiao Shi | 52fa45c | 2016-11-29 21:18:13 +0000 | [diff] [blame] | 116 | ndn::nfd::Controller m_controller; |
Alexander Afanasyev | 2a655f7 | 2015-01-26 18:38:33 -0800 | [diff] [blame] | 117 | NextStageCallback m_nextStageOnFailure; |
| 118 | }; |
| 119 | |
| 120 | } // namespace autoconfig |
| 121 | } // namespace tools |
| 122 | } // namespace ndn |
| 123 | |
| 124 | #endif // NFD_TOOLS_NDN_AUTOCONFIG_BASE_HPP |