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