Junxiao Shi | d243d71 | 2016-08-19 06:45:31 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014-2016, 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 | #ifndef NFD_TOOLS_NFDC_FACE_ID_FETCHER_HPP |
| 27 | #define NFD_TOOLS_NFDC_FACE_ID_FETCHER_HPP |
| 28 | |
| 29 | #include "core/common.hpp" |
| 30 | #include <ndn-cxx/face.hpp> |
| 31 | #include <ndn-cxx/management/nfd-controller.hpp> |
| 32 | #include <ndn-cxx/util/face-uri.hpp> |
| 33 | #include <ndn-cxx/security/validator-null.hpp> |
| 34 | |
| 35 | namespace nfd { |
| 36 | namespace tools { |
| 37 | namespace nfdc { |
| 38 | |
| 39 | using ndn::util::FaceUri; |
| 40 | |
| 41 | class FaceIdFetcher |
| 42 | { |
| 43 | public: |
| 44 | typedef std::function<void(uint32_t)> SuccessCallback; |
| 45 | typedef std::function<void(const std::string&)> FailureCallback; |
| 46 | |
| 47 | /** \brief obtain FaceId from input |
| 48 | * \param face Reference to the Face that should be used to fetch data |
| 49 | * \param controller Reference to the controller that should be used to sign the Interest |
| 50 | * \param input User input, either FaceId or FaceUri |
| 51 | * \param allowCreate Whether creating face is allowed |
| 52 | * \param onSucceed Callback to be fired when faceId is obtained |
| 53 | * \param onFail Callback to be fired when an error occurs |
| 54 | */ |
| 55 | static void |
| 56 | start(ndn::Face& face, |
| 57 | ndn::nfd::Controller& controller, |
| 58 | const std::string& input, |
| 59 | bool allowCreate, |
| 60 | const SuccessCallback& onSucceed, |
| 61 | const FailureCallback& onFail); |
| 62 | |
| 63 | private: |
| 64 | FaceIdFetcher(ndn::Face& face, |
| 65 | ndn::nfd::Controller& controller, |
| 66 | bool allowCreate, |
| 67 | const SuccessCallback& onSucceed, |
| 68 | const FailureCallback& onFail); |
| 69 | |
| 70 | void |
| 71 | onQuerySuccess(const ndn::ConstBufferPtr& data, |
| 72 | const FaceUri& canonicalUri); |
| 73 | |
| 74 | void |
| 75 | onQueryFailure(uint32_t errorCode, |
| 76 | const FaceUri& canonicalUri); |
| 77 | |
| 78 | void |
| 79 | onCanonizeSuccess(const FaceUri& canonicalUri); |
| 80 | |
| 81 | void |
| 82 | onCanonizeFailure(const std::string& reason); |
| 83 | |
| 84 | void |
| 85 | startGetFaceId(const FaceUri& faceUri); |
| 86 | |
| 87 | void |
| 88 | startFaceCreate(const FaceUri& canonicalUri); |
| 89 | |
| 90 | void |
| 91 | onFaceCreateError(uint32_t code, |
| 92 | const std::string& error, |
| 93 | const std::string& message); |
| 94 | |
| 95 | void |
| 96 | succeed(uint32_t faceId); |
| 97 | |
| 98 | void |
| 99 | fail(const std::string& reason); |
| 100 | |
| 101 | private: |
| 102 | ndn::Face& m_face; |
| 103 | ndn::nfd::Controller& m_controller; |
| 104 | bool m_allowCreate; |
| 105 | SuccessCallback m_onSucceed; |
| 106 | FailureCallback m_onFail; |
| 107 | ndn::ValidatorNull m_validator; |
| 108 | }; |
| 109 | |
| 110 | } // namespace nfdc |
| 111 | } // namespace tools |
| 112 | } // namespace nfd |
| 113 | |
| 114 | #endif // NFD_TOOLS_NFDC_FACE_ID_FETCHER_HPP |