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