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