blob: e35fc21d4063e15c645faadc3219b6e9104feac8 [file] [log] [blame]
Andrea Tosatto672b9a72016-01-05 16:18:20 +01001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento887336c2017-09-19 15:25:23 -04002/*
Davide Pesaventof8d9a532021-07-03 16:04:12 -04003 * Copyright (c) 2016-2021, Regents of the University of California,
Davide Pesavento887336c2017-09-19 15:25:23 -04004 * Colorado State University,
5 * University Pierre & Marie Curie, Sorbonne University.
Andrea Tosatto672b9a72016-01-05 16:18:20 +01006 *
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 Ghasemibb2d2802019-03-26 16:07:58 -070026 * @author Chavoosh Ghasemi
Andrea Tosatto672b9a72016-01-05 16:18:20 +010027 */
28
29#include "discover-version.hpp"
30#include "data-fetcher.hpp"
31
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -070032#include <ndn-cxx/metadata-object.hpp>
33
Andrea Tosatto672b9a72016-01-05 16:18:20 +010034namespace ndn {
35namespace chunks {
36
Davide Pesavento97a33b22019-10-17 22:10:47 -040037DiscoverVersion::DiscoverVersion(Face& face, const Name& prefix, const Options& options)
38 : m_face(face)
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -070039 , m_prefix(prefix)
Davide Pesavento97a33b22019-10-17 22:10:47 -040040 , m_options(options)
Andrea Tosatto672b9a72016-01-05 16:18:20 +010041{
42}
43
44void
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -070045DiscoverVersion::run()
Andrea Tosatto672b9a72016-01-05 16:18:20 +010046{
Davide Pesavento296b3852019-10-20 16:00:16 -040047 if (m_options.disableVersionDiscovery || (!m_prefix.empty() && m_prefix[-1].isVersion())) {
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -070048 onDiscoverySuccess(m_prefix);
49 return;
50 }
51
52 Interest interest = MetadataObject::makeDiscoveryInterest(m_prefix)
Davide Pesavento97a33b22019-10-17 22:10:47 -040053 .setInterestLifetime(m_options.interestLifetime);
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -070054
55 m_fetcher = DataFetcher::fetch(m_face, interest,
Davide Pesaventof8d9a532021-07-03 16:04:12 -040056 m_options.maxRetriesOnTimeoutOrNack,
57 m_options.maxRetriesOnTimeoutOrNack,
58 FORWARD_TO_MEM_FN(handleData),
59 [this] (const auto&, const auto& reason) {
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -070060 onDiscoveryFailure(reason);
61 },
Davide Pesaventof8d9a532021-07-03 16:04:12 -040062 [this] (const auto&, const auto& reason) {
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -070063 onDiscoveryFailure(reason);
64 },
Davide Pesavento97a33b22019-10-17 22:10:47 -040065 m_options.isVerbose);
Andrea Tosatto672b9a72016-01-05 16:18:20 +010066}
67
68void
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -070069DiscoverVersion::handleData(const Interest& interest, const Data& data)
Andrea Tosatto672b9a72016-01-05 16:18:20 +010070{
Davide Pesavento97a33b22019-10-17 22:10:47 -040071 if (m_options.isVerbose)
Davide Pesaventof8d9a532021-07-03 16:04:12 -040072 std::cerr << "Data: " << data << "\n";
Andrea Tosatto672b9a72016-01-05 16:18:20 +010073
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -070074 // make a metadata object from received metadata packet
75 MetadataObject mobject;
76 try {
77 mobject = MetadataObject(data);
78 }
79 catch (const tlv::Error& e) {
80 onDiscoveryFailure("Invalid metadata packet: "s + e.what());
81 return;
82 }
83
84 if (mobject.getVersionedName().empty() || !mobject.getVersionedName()[-1].isVersion()) {
85 onDiscoveryFailure(mobject.getVersionedName().toUri() + " is not a valid versioned name");
86 return;
87 }
88
Davide Pesavento97a33b22019-10-17 22:10:47 -040089 if (m_options.isVerbose) {
Davide Pesaventof8d9a532021-07-03 16:04:12 -040090 std::cerr << "Discovered Data version: " << mobject.getVersionedName()[-1] << "\n";
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -070091 }
92
93 onDiscoverySuccess(mobject.getVersionedName());
Andrea Tosatto672b9a72016-01-05 16:18:20 +010094}
95
96} // namespace chunks
97} // namespace ndn