Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 887336c | 2017-09-19 15:25:23 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame^] | 3 | * Copyright (c) 2016-2022, Regents of the University of California, |
Davide Pesavento | 887336c | 2017-09-19 15:25:23 -0400 | [diff] [blame] | 4 | * Colorado State University, |
| 5 | * University Pierre & Marie Curie, Sorbonne University. |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 6 | * |
| 7 | * This file is part of ndn-tools (Named Data Networking Essential Tools). |
| 8 | * See AUTHORS.md for complete list of ndn-tools authors and contributors. |
| 9 | * |
| 10 | * ndn-tools is free software: you can redistribute it and/or modify it under the terms |
| 11 | * of the GNU General Public License as published by the Free Software Foundation, |
| 12 | * either version 3 of the License, or (at your option) any later version. |
| 13 | * |
| 14 | * ndn-tools is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 15 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE. See the GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along with |
| 19 | * ndn-tools, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 20 | * |
| 21 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 22 | * |
| 23 | * @author Wentao Shang |
| 24 | * @author Steve DiBenedetto |
| 25 | * @author Andrea Tosatto |
Chavoosh Ghasemi | bb2d280 | 2019-03-26 16:07:58 -0700 | [diff] [blame] | 26 | * @author Chavoosh Ghasemi |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 27 | */ |
| 28 | |
| 29 | #include "discover-version.hpp" |
| 30 | #include "data-fetcher.hpp" |
| 31 | |
Chavoosh Ghasemi | bb2d280 | 2019-03-26 16:07:58 -0700 | [diff] [blame] | 32 | #include <ndn-cxx/metadata-object.hpp> |
| 33 | |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame^] | 34 | namespace ndn::chunks { |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 35 | |
Davide Pesavento | 97a33b2 | 2019-10-17 22:10:47 -0400 | [diff] [blame] | 36 | DiscoverVersion::DiscoverVersion(Face& face, const Name& prefix, const Options& options) |
| 37 | : m_face(face) |
Chavoosh Ghasemi | bb2d280 | 2019-03-26 16:07:58 -0700 | [diff] [blame] | 38 | , m_prefix(prefix) |
Davide Pesavento | 97a33b2 | 2019-10-17 22:10:47 -0400 | [diff] [blame] | 39 | , m_options(options) |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 40 | { |
| 41 | } |
| 42 | |
| 43 | void |
Chavoosh Ghasemi | bb2d280 | 2019-03-26 16:07:58 -0700 | [diff] [blame] | 44 | DiscoverVersion::run() |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 45 | { |
Davide Pesavento | 296b385 | 2019-10-20 16:00:16 -0400 | [diff] [blame] | 46 | if (m_options.disableVersionDiscovery || (!m_prefix.empty() && m_prefix[-1].isVersion())) { |
Chavoosh Ghasemi | bb2d280 | 2019-03-26 16:07:58 -0700 | [diff] [blame] | 47 | onDiscoverySuccess(m_prefix); |
| 48 | return; |
| 49 | } |
| 50 | |
| 51 | Interest interest = MetadataObject::makeDiscoveryInterest(m_prefix) |
Davide Pesavento | 97a33b2 | 2019-10-17 22:10:47 -0400 | [diff] [blame] | 52 | .setInterestLifetime(m_options.interestLifetime); |
Chavoosh Ghasemi | bb2d280 | 2019-03-26 16:07:58 -0700 | [diff] [blame] | 53 | |
| 54 | m_fetcher = DataFetcher::fetch(m_face, interest, |
Davide Pesavento | f8d9a53 | 2021-07-03 16:04:12 -0400 | [diff] [blame] | 55 | m_options.maxRetriesOnTimeoutOrNack, |
| 56 | m_options.maxRetriesOnTimeoutOrNack, |
| 57 | FORWARD_TO_MEM_FN(handleData), |
| 58 | [this] (const auto&, const auto& reason) { |
Chavoosh Ghasemi | bb2d280 | 2019-03-26 16:07:58 -0700 | [diff] [blame] | 59 | onDiscoveryFailure(reason); |
| 60 | }, |
Davide Pesavento | f8d9a53 | 2021-07-03 16:04:12 -0400 | [diff] [blame] | 61 | [this] (const auto&, const auto& reason) { |
Chavoosh Ghasemi | bb2d280 | 2019-03-26 16:07:58 -0700 | [diff] [blame] | 62 | onDiscoveryFailure(reason); |
| 63 | }, |
Davide Pesavento | 97a33b2 | 2019-10-17 22:10:47 -0400 | [diff] [blame] | 64 | m_options.isVerbose); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | void |
Chavoosh Ghasemi | bb2d280 | 2019-03-26 16:07:58 -0700 | [diff] [blame] | 68 | DiscoverVersion::handleData(const Interest& interest, const Data& data) |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 69 | { |
Davide Pesavento | 97a33b2 | 2019-10-17 22:10:47 -0400 | [diff] [blame] | 70 | if (m_options.isVerbose) |
Davide Pesavento | f8d9a53 | 2021-07-03 16:04:12 -0400 | [diff] [blame] | 71 | std::cerr << "Data: " << data << "\n"; |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 72 | |
Chavoosh Ghasemi | bb2d280 | 2019-03-26 16:07:58 -0700 | [diff] [blame] | 73 | // make a metadata object from received metadata packet |
| 74 | MetadataObject mobject; |
| 75 | try { |
| 76 | mobject = MetadataObject(data); |
| 77 | } |
| 78 | catch (const tlv::Error& e) { |
| 79 | onDiscoveryFailure("Invalid metadata packet: "s + e.what()); |
| 80 | return; |
| 81 | } |
| 82 | |
| 83 | if (mobject.getVersionedName().empty() || !mobject.getVersionedName()[-1].isVersion()) { |
| 84 | onDiscoveryFailure(mobject.getVersionedName().toUri() + " is not a valid versioned name"); |
| 85 | return; |
| 86 | } |
| 87 | |
Davide Pesavento | 97a33b2 | 2019-10-17 22:10:47 -0400 | [diff] [blame] | 88 | if (m_options.isVerbose) { |
Davide Pesavento | f8d9a53 | 2021-07-03 16:04:12 -0400 | [diff] [blame] | 89 | std::cerr << "Discovered Data version: " << mobject.getVersionedName()[-1] << "\n"; |
Chavoosh Ghasemi | bb2d280 | 2019-03-26 16:07:58 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | onDiscoverySuccess(mobject.getVersionedName()); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 93 | } |
| 94 | |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame^] | 95 | } // namespace ndn::chunks |