blob: 24565c07043104b6ea320bd3c7876014a0f66d22 [file] [log] [blame]
Andrea Tosatto672b9a72016-01-05 16:18:20 +01001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Klaus Schneider59ec95a2017-09-14 17:50:00 -07002/*
Davide Pesavento59984282022-02-16 22:41:03 -05003 * Copyright (c) 2016-2022, Regents of the University of California,
Davide Pesavento6f76afc2017-09-19 18:43:51 -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
Davide Pesavento6f76afc2017-09-19 18:43:51 -040026 * @author Davide Pesavento
Klaus Schneider59ec95a2017-09-14 17:50:00 -070027 * @author Klaus Schneider
Chavoosh Ghasemi79991a02019-01-28 21:22:31 -080028 * @author Chavoosh Ghasemi
Andrea Tosatto672b9a72016-01-05 16:18:20 +010029 */
30
31#include "producer.hpp"
32
Chavoosh Ghasemi79991a02019-01-28 21:22:31 -080033#include <ndn-cxx/metadata-object.hpp>
34
Davide Pesaventob3570c62022-02-19 19:19:00 -050035namespace ndn::chunks {
Andrea Tosatto672b9a72016-01-05 16:18:20 +010036
Davide Pesavento6f76afc2017-09-19 18:43:51 -040037Producer::Producer(const Name& prefix, Face& face, KeyChain& keyChain, std::istream& is,
38 const Options& opts)
Andrea Tosatto672b9a72016-01-05 16:18:20 +010039 : m_face(face)
40 , m_keyChain(keyChain)
Davide Pesavento6f76afc2017-09-19 18:43:51 -040041 , m_options(opts)
Andrea Tosatto672b9a72016-01-05 16:18:20 +010042{
Davide Pesavento59984282022-02-16 22:41:03 -050043 if (!prefix.empty() && prefix[-1].isVersion()) {
Andrea Tosatto672b9a72016-01-05 16:18:20 +010044 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 Pesavento6f76afc2017-09-19 18:43:51 -040054 if (m_options.wantShowVersion)
Davide Pesaventof8d9a532021-07-03 16:04:12 -040055 std::cout << m_versionedPrefix[-1] << "\n";
Andrea Tosatto672b9a72016-01-05 16:18:20 +010056
Chavoosh Ghasemi79991a02019-01-28 21:22:31 -080057 // register m_prefix without interest handler
Davide Pesaventof8d9a532021-07-03 16:04:12 -040058 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 Ghasemi79991a02019-01-28 21:22:31 -080062
63 // match Interests whose name starts with m_versionedPrefix
Davide Pesaventof8d9a532021-07-03 16:04:12 -040064 face.setInterestFilter(m_versionedPrefix, [this] (const auto&, const auto& interest) {
65 processSegmentInterest(interest);
66 });
Chavoosh Ghasemi79991a02019-01-28 21:22:31 -080067
68 // match Interests whose name is exactly m_prefix
Davide Pesaventof8d9a532021-07-03 16:04:12 -040069 face.setInterestFilter(InterestFilter(m_prefix, ""), [this] (const auto&, const auto& interest) {
70 processSegmentInterest(interest);
71 });
Chavoosh Ghasemi79991a02019-01-28 21:22:31 -080072
73 // match discovery Interests
Davide Pesaventof8d9a532021-07-03 16:04:12 -040074 auto discoveryName = MetadataObject::makeDiscoveryInterest(m_prefix).getName();
75 face.setInterestFilter(discoveryName, [this] (const auto&, const auto& interest) {
76 processDiscoveryInterest(interest);
77 });
Andrea Tosatto672b9a72016-01-05 16:18:20 +010078
Davide Pesavento6f76afc2017-09-19 18:43:51 -040079 if (!m_options.isQuiet)
Davide Pesaventof8d9a532021-07-03 16:04:12 -040080 std::cerr << "Data published with name: " << m_versionedPrefix << "\n";
Andrea Tosatto672b9a72016-01-05 16:18:20 +010081}
82
83void
84Producer::run()
85{
86 m_face.processEvents();
87}
88
89void
Chavoosh Ghasemi79991a02019-01-28 21:22:31 -080090Producer::processDiscoveryInterest(const Interest& interest)
91{
92 if (m_options.isVerbose)
Davide Pesaventof8d9a532021-07-03 16:04:12 -040093 std::cerr << "Discovery Interest: " << interest << "\n";
Chavoosh Ghasemi79991a02019-01-28 21:22:31 -080094
95 if (!interest.getCanBePrefix()) {
96 if (m_options.isVerbose)
Davide Pesaventof8d9a532021-07-03 16:04:12 -040097 std::cerr << "Discovery Interest lacks CanBePrefix, sending Nack\n";
Chavoosh Ghasemi79991a02019-01-28 21:22:31 -080098 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 Pesaventof8d9a532021-07-03 16:04:12 -0400109 std::cerr << "Sending metadata: " << mdata << "\n";
Chavoosh Ghasemi79991a02019-01-28 21:22:31 -0800110
111 m_face.put(mdata);
112}
113
114void
115Producer::processSegmentInterest(const Interest& interest)
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100116{
Davide Pesavento59984282022-02-16 22:41:03 -0500117 BOOST_ASSERT(!m_store.empty());
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100118
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400119 if (m_options.isVerbose)
Davide Pesaventof8d9a532021-07-03 16:04:12 -0400120 std::cerr << "Interest: " << interest << "\n";
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100121
122 const Name& name = interest.getName();
123 shared_ptr<Data> data;
124
Chavoosh Ghasemi79991a02019-01-28 21:22:31 -0800125 if (name.size() == m_versionedPrefix.size() + 1 && name[-1].isSegment()) {
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100126 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 Ghasemi79991a02019-01-28 21:22:31 -0800133 // unspecified version or segment number, return first segment
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100134 data = m_store[0];
135 }
136
137 if (data != nullptr) {
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400138 if (m_options.isVerbose)
Davide Pesaventof8d9a532021-07-03 16:04:12 -0400139 std::cerr << "Data: " << *data << "\n";
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100140
141 m_face.put(*data);
142 }
Chavoosh Ghasemi79991a02019-01-28 21:22:31 -0800143 else {
144 if (m_options.isVerbose)
Davide Pesaventof8d9a532021-07-03 16:04:12 -0400145 std::cerr << "Interest cannot be satisfied, sending Nack\n";
Chavoosh Ghasemi79991a02019-01-28 21:22:31 -0800146 m_face.put(lp::Nack(interest));
147 }
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100148}
149
150void
151Producer::populateStore(std::istream& is)
152{
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400153 BOOST_ASSERT(m_store.empty());
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100154
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400155 if (!m_options.isQuiet)
Davide Pesaventof8d9a532021-07-03 16:04:12 -0400156 std::cerr << "Loading input ...\n";
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100157
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400158 std::vector<uint8_t> buffer(m_options.maxSegmentSize);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100159 while (is.good()) {
160 is.read(reinterpret_cast<char*>(buffer.data()), buffer.size());
161 const auto nCharsRead = is.gcount();
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400162
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100163 if (nCharsRead > 0) {
164 auto data = make_shared<Data>(Name(m_versionedPrefix).appendSegment(m_store.size()));
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400165 data->setFreshnessPeriod(m_options.freshnessPeriod);
Davide Pesavento59984282022-02-16 22:41:03 -0500166 data->setContent(make_span(buffer).first(nCharsRead));
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100167 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 Pesavento6f76afc2017-09-19 18:43:51 -0400173 data->setFreshnessPeriod(m_options.freshnessPeriod);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100174 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 Pesavento969cd5a2018-04-20 16:27:47 -0400179 data->setFinalBlock(finalBlockId);
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400180 m_keyChain.sign(*data, m_options.signingInfo);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100181 }
182
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400183 if (!m_options.isQuiet)
Davide Pesaventof8d9a532021-07-03 16:04:12 -0400184 std::cerr << "Created " << m_store.size() << " chunks for prefix " << m_prefix << "\n";
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100185}
186
Davide Pesaventob3570c62022-02-19 19:19:00 -0500187} // namespace ndn::chunks