Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Klaus Schneider | 59ec95a | 2017-09-14 17:50:00 -0700 | [diff] [blame] | 2 | /* |
Davide Pesavento | 5748e82 | 2024-01-26 18:40:22 -0500 | [diff] [blame] | 3 | * Copyright (c) 2016-2024, Regents of the University of California, |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -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 |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 26 | * @author Davide Pesavento |
Klaus Schneider | 59ec95a | 2017-09-14 17:50:00 -0700 | [diff] [blame] | 27 | * @author Klaus Schneider |
Chavoosh Ghasemi | 79991a0 | 2019-01-28 21:22:31 -0800 | [diff] [blame] | 28 | * @author Chavoosh Ghasemi |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 29 | */ |
| 30 | |
| 31 | #include "producer.hpp" |
| 32 | |
Chavoosh Ghasemi | 79991a0 | 2019-01-28 21:22:31 -0800 | [diff] [blame] | 33 | #include <ndn-cxx/metadata-object.hpp> |
Davide Pesavento | b61e3e2 | 2022-11-11 14:38:52 -0500 | [diff] [blame] | 34 | #include <ndn-cxx/util/segmenter.hpp> |
Chavoosh Ghasemi | 79991a0 | 2019-01-28 21:22:31 -0800 | [diff] [blame] | 35 | |
Davide Pesavento | 5748e82 | 2024-01-26 18:40:22 -0500 | [diff] [blame] | 36 | #include <iostream> |
| 37 | |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 38 | namespace ndn::chunks { |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 39 | |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 40 | Producer::Producer(const Name& prefix, Face& face, KeyChain& keyChain, std::istream& is, |
| 41 | const Options& opts) |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 42 | : m_face(face) |
| 43 | , m_keyChain(keyChain) |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 44 | , m_options(opts) |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 45 | { |
Davide Pesavento | 5998428 | 2022-02-16 22:41:03 -0500 | [diff] [blame] | 46 | if (!prefix.empty() && prefix[-1].isVersion()) { |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 47 | m_prefix = prefix.getPrefix(-1); |
| 48 | m_versionedPrefix = prefix; |
| 49 | } |
| 50 | else { |
| 51 | m_prefix = prefix; |
| 52 | m_versionedPrefix = Name(m_prefix).appendVersion(); |
| 53 | } |
| 54 | |
Davide Pesavento | b61e3e2 | 2022-11-11 14:38:52 -0500 | [diff] [blame] | 55 | if (!m_options.isQuiet) { |
| 56 | std::cerr << "Loading input ...\n"; |
| 57 | } |
Junxiao Shi | 869d73e | 2023-08-10 22:52:26 +0000 | [diff] [blame] | 58 | Segmenter segmenter(m_keyChain, m_options.signingInfo); |
Davide Pesavento | b61e3e2 | 2022-11-11 14:38:52 -0500 | [diff] [blame] | 59 | m_store = segmenter.segment(is, m_versionedPrefix, m_options.maxSegmentSize, m_options.freshnessPeriod); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 60 | |
Davide Pesavento | b61e3e2 | 2022-11-11 14:38:52 -0500 | [diff] [blame] | 61 | // register m_prefix without Interest handler |
Davide Pesavento | f8d9a53 | 2021-07-03 16:04:12 -0400 | [diff] [blame] | 62 | m_face.registerPrefix(m_prefix, nullptr, [this] (const Name& prefix, const auto& reason) { |
| 63 | std::cerr << "ERROR: Failed to register prefix '" << prefix << "' (" << reason << ")\n"; |
| 64 | m_face.shutdown(); |
| 65 | }); |
Chavoosh Ghasemi | 79991a0 | 2019-01-28 21:22:31 -0800 | [diff] [blame] | 66 | |
| 67 | // match Interests whose name starts with m_versionedPrefix |
Davide Pesavento | f8d9a53 | 2021-07-03 16:04:12 -0400 | [diff] [blame] | 68 | face.setInterestFilter(m_versionedPrefix, [this] (const auto&, const auto& interest) { |
| 69 | processSegmentInterest(interest); |
| 70 | }); |
Chavoosh Ghasemi | 79991a0 | 2019-01-28 21:22:31 -0800 | [diff] [blame] | 71 | |
| 72 | // match Interests whose name is exactly m_prefix |
Davide Pesavento | f8d9a53 | 2021-07-03 16:04:12 -0400 | [diff] [blame] | 73 | face.setInterestFilter(InterestFilter(m_prefix, ""), [this] (const auto&, const auto& interest) { |
| 74 | processSegmentInterest(interest); |
| 75 | }); |
Chavoosh Ghasemi | 79991a0 | 2019-01-28 21:22:31 -0800 | [diff] [blame] | 76 | |
| 77 | // match discovery Interests |
Davide Pesavento | f8d9a53 | 2021-07-03 16:04:12 -0400 | [diff] [blame] | 78 | auto discoveryName = MetadataObject::makeDiscoveryInterest(m_prefix).getName(); |
| 79 | face.setInterestFilter(discoveryName, [this] (const auto&, const auto& interest) { |
| 80 | processDiscoveryInterest(interest); |
| 81 | }); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 82 | |
Davide Pesavento | b61e3e2 | 2022-11-11 14:38:52 -0500 | [diff] [blame] | 83 | if (m_options.wantShowVersion) { |
| 84 | std::cout << m_versionedPrefix[-1] << "\n"; |
| 85 | } |
| 86 | if (!m_options.isQuiet) { |
| 87 | std::cerr << "Published " << m_store.size() << " Data packet" << (m_store.size() > 1 ? "s" : "") |
| 88 | << " with prefix " << m_versionedPrefix << "\n"; |
| 89 | } |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | void |
| 93 | Producer::run() |
| 94 | { |
| 95 | m_face.processEvents(); |
| 96 | } |
| 97 | |
| 98 | void |
Chavoosh Ghasemi | 79991a0 | 2019-01-28 21:22:31 -0800 | [diff] [blame] | 99 | Producer::processDiscoveryInterest(const Interest& interest) |
| 100 | { |
| 101 | if (m_options.isVerbose) |
Davide Pesavento | f8d9a53 | 2021-07-03 16:04:12 -0400 | [diff] [blame] | 102 | std::cerr << "Discovery Interest: " << interest << "\n"; |
Chavoosh Ghasemi | 79991a0 | 2019-01-28 21:22:31 -0800 | [diff] [blame] | 103 | |
| 104 | if (!interest.getCanBePrefix()) { |
Davide Pesavento | b61e3e2 | 2022-11-11 14:38:52 -0500 | [diff] [blame] | 105 | if (m_options.isVerbose) { |
Davide Pesavento | f8d9a53 | 2021-07-03 16:04:12 -0400 | [diff] [blame] | 106 | std::cerr << "Discovery Interest lacks CanBePrefix, sending Nack\n"; |
Davide Pesavento | b61e3e2 | 2022-11-11 14:38:52 -0500 | [diff] [blame] | 107 | } |
Chavoosh Ghasemi | 79991a0 | 2019-01-28 21:22:31 -0800 | [diff] [blame] | 108 | m_face.put(lp::Nack(interest)); |
| 109 | return; |
| 110 | } |
| 111 | |
| 112 | MetadataObject mobject; |
| 113 | mobject.setVersionedName(m_versionedPrefix); |
| 114 | |
| 115 | // make a metadata packet based on the received discovery Interest name |
Davide Pesavento | b61e3e2 | 2022-11-11 14:38:52 -0500 | [diff] [blame] | 116 | auto mdata = mobject.makeData(interest.getName(), m_keyChain, m_options.signingInfo); |
Chavoosh Ghasemi | 79991a0 | 2019-01-28 21:22:31 -0800 | [diff] [blame] | 117 | |
| 118 | if (m_options.isVerbose) |
Davide Pesavento | f8d9a53 | 2021-07-03 16:04:12 -0400 | [diff] [blame] | 119 | std::cerr << "Sending metadata: " << mdata << "\n"; |
Chavoosh Ghasemi | 79991a0 | 2019-01-28 21:22:31 -0800 | [diff] [blame] | 120 | |
| 121 | m_face.put(mdata); |
| 122 | } |
| 123 | |
| 124 | void |
| 125 | Producer::processSegmentInterest(const Interest& interest) |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 126 | { |
Davide Pesavento | 5998428 | 2022-02-16 22:41:03 -0500 | [diff] [blame] | 127 | BOOST_ASSERT(!m_store.empty()); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 128 | |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 129 | if (m_options.isVerbose) |
Davide Pesavento | f8d9a53 | 2021-07-03 16:04:12 -0400 | [diff] [blame] | 130 | std::cerr << "Interest: " << interest << "\n"; |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 131 | |
| 132 | const Name& name = interest.getName(); |
Davide Pesavento | 5748e82 | 2024-01-26 18:40:22 -0500 | [diff] [blame] | 133 | std::shared_ptr<Data> data; |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 134 | |
Chavoosh Ghasemi | 79991a0 | 2019-01-28 21:22:31 -0800 | [diff] [blame] | 135 | if (name.size() == m_versionedPrefix.size() + 1 && name[-1].isSegment()) { |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 136 | const auto segmentNo = static_cast<size_t>(interest.getName()[-1].toSegment()); |
| 137 | // specific segment retrieval |
| 138 | if (segmentNo < m_store.size()) { |
| 139 | data = m_store[segmentNo]; |
| 140 | } |
| 141 | } |
| 142 | else if (interest.matchesData(*m_store[0])) { |
Chavoosh Ghasemi | 79991a0 | 2019-01-28 21:22:31 -0800 | [diff] [blame] | 143 | // unspecified version or segment number, return first segment |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 144 | data = m_store[0]; |
| 145 | } |
| 146 | |
| 147 | if (data != nullptr) { |
Davide Pesavento | b61e3e2 | 2022-11-11 14:38:52 -0500 | [diff] [blame] | 148 | if (m_options.isVerbose) { |
Davide Pesavento | f8d9a53 | 2021-07-03 16:04:12 -0400 | [diff] [blame] | 149 | std::cerr << "Data: " << *data << "\n"; |
Davide Pesavento | b61e3e2 | 2022-11-11 14:38:52 -0500 | [diff] [blame] | 150 | } |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 151 | m_face.put(*data); |
| 152 | } |
Chavoosh Ghasemi | 79991a0 | 2019-01-28 21:22:31 -0800 | [diff] [blame] | 153 | else { |
Davide Pesavento | b61e3e2 | 2022-11-11 14:38:52 -0500 | [diff] [blame] | 154 | if (m_options.isVerbose) { |
Davide Pesavento | f8d9a53 | 2021-07-03 16:04:12 -0400 | [diff] [blame] | 155 | std::cerr << "Interest cannot be satisfied, sending Nack\n"; |
Davide Pesavento | b61e3e2 | 2022-11-11 14:38:52 -0500 | [diff] [blame] | 156 | } |
Chavoosh Ghasemi | 79991a0 | 2019-01-28 21:22:31 -0800 | [diff] [blame] | 157 | m_face.put(lp::Nack(interest)); |
| 158 | } |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 159 | } |
| 160 | |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 161 | } // namespace ndn::chunks |