blob: c3538174c4395942527087733f021d243a5e4c5c [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
Andrea Tosatto672b9a72016-01-05 16:18:20 +010035namespace ndn {
36namespace chunks {
37
Davide Pesavento6f76afc2017-09-19 18:43:51 -040038Producer::Producer(const Name& prefix, Face& face, KeyChain& keyChain, std::istream& is,
39 const Options& opts)
Andrea Tosatto672b9a72016-01-05 16:18:20 +010040 : m_face(face)
41 , m_keyChain(keyChain)
Davide Pesavento6f76afc2017-09-19 18:43:51 -040042 , m_options(opts)
Andrea Tosatto672b9a72016-01-05 16:18:20 +010043{
Davide Pesavento59984282022-02-16 22:41:03 -050044 if (!prefix.empty() && prefix[-1].isVersion()) {
Andrea Tosatto672b9a72016-01-05 16:18:20 +010045 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
53 populateStore(is);
54
Davide Pesavento6f76afc2017-09-19 18:43:51 -040055 if (m_options.wantShowVersion)
Davide Pesaventof8d9a532021-07-03 16:04:12 -040056 std::cout << m_versionedPrefix[-1] << "\n";
Andrea Tosatto672b9a72016-01-05 16:18:20 +010057
Chavoosh Ghasemi79991a02019-01-28 21:22:31 -080058 // register m_prefix without interest handler
Davide Pesaventof8d9a532021-07-03 16:04:12 -040059 m_face.registerPrefix(m_prefix, nullptr, [this] (const Name& prefix, const auto& reason) {
60 std::cerr << "ERROR: Failed to register prefix '" << prefix << "' (" << reason << ")\n";
61 m_face.shutdown();
62 });
Chavoosh Ghasemi79991a02019-01-28 21:22:31 -080063
64 // match Interests whose name starts with m_versionedPrefix
Davide Pesaventof8d9a532021-07-03 16:04:12 -040065 face.setInterestFilter(m_versionedPrefix, [this] (const auto&, const auto& interest) {
66 processSegmentInterest(interest);
67 });
Chavoosh Ghasemi79991a02019-01-28 21:22:31 -080068
69 // match Interests whose name is exactly m_prefix
Davide Pesaventof8d9a532021-07-03 16:04:12 -040070 face.setInterestFilter(InterestFilter(m_prefix, ""), [this] (const auto&, const auto& interest) {
71 processSegmentInterest(interest);
72 });
Chavoosh Ghasemi79991a02019-01-28 21:22:31 -080073
74 // match discovery Interests
Davide Pesaventof8d9a532021-07-03 16:04:12 -040075 auto discoveryName = MetadataObject::makeDiscoveryInterest(m_prefix).getName();
76 face.setInterestFilter(discoveryName, [this] (const auto&, const auto& interest) {
77 processDiscoveryInterest(interest);
78 });
Andrea Tosatto672b9a72016-01-05 16:18:20 +010079
Davide Pesavento6f76afc2017-09-19 18:43:51 -040080 if (!m_options.isQuiet)
Davide Pesaventof8d9a532021-07-03 16:04:12 -040081 std::cerr << "Data published with name: " << m_versionedPrefix << "\n";
Andrea Tosatto672b9a72016-01-05 16:18:20 +010082}
83
84void
85Producer::run()
86{
87 m_face.processEvents();
88}
89
90void
Chavoosh Ghasemi79991a02019-01-28 21:22:31 -080091Producer::processDiscoveryInterest(const Interest& interest)
92{
93 if (m_options.isVerbose)
Davide Pesaventof8d9a532021-07-03 16:04:12 -040094 std::cerr << "Discovery Interest: " << interest << "\n";
Chavoosh Ghasemi79991a02019-01-28 21:22:31 -080095
96 if (!interest.getCanBePrefix()) {
97 if (m_options.isVerbose)
Davide Pesaventof8d9a532021-07-03 16:04:12 -040098 std::cerr << "Discovery Interest lacks CanBePrefix, sending Nack\n";
Chavoosh Ghasemi79991a02019-01-28 21:22:31 -080099 m_face.put(lp::Nack(interest));
100 return;
101 }
102
103 MetadataObject mobject;
104 mobject.setVersionedName(m_versionedPrefix);
105
106 // make a metadata packet based on the received discovery Interest name
107 Data mdata(mobject.makeData(interest.getName(), m_keyChain, m_options.signingInfo));
108
109 if (m_options.isVerbose)
Davide Pesaventof8d9a532021-07-03 16:04:12 -0400110 std::cerr << "Sending metadata: " << mdata << "\n";
Chavoosh Ghasemi79991a02019-01-28 21:22:31 -0800111
112 m_face.put(mdata);
113}
114
115void
116Producer::processSegmentInterest(const Interest& interest)
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100117{
Davide Pesavento59984282022-02-16 22:41:03 -0500118 BOOST_ASSERT(!m_store.empty());
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100119
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400120 if (m_options.isVerbose)
Davide Pesaventof8d9a532021-07-03 16:04:12 -0400121 std::cerr << "Interest: " << interest << "\n";
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100122
123 const Name& name = interest.getName();
124 shared_ptr<Data> data;
125
Chavoosh Ghasemi79991a02019-01-28 21:22:31 -0800126 if (name.size() == m_versionedPrefix.size() + 1 && name[-1].isSegment()) {
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100127 const auto segmentNo = static_cast<size_t>(interest.getName()[-1].toSegment());
128 // specific segment retrieval
129 if (segmentNo < m_store.size()) {
130 data = m_store[segmentNo];
131 }
132 }
133 else if (interest.matchesData(*m_store[0])) {
Chavoosh Ghasemi79991a02019-01-28 21:22:31 -0800134 // unspecified version or segment number, return first segment
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100135 data = m_store[0];
136 }
137
138 if (data != nullptr) {
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400139 if (m_options.isVerbose)
Davide Pesaventof8d9a532021-07-03 16:04:12 -0400140 std::cerr << "Data: " << *data << "\n";
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100141
142 m_face.put(*data);
143 }
Chavoosh Ghasemi79991a02019-01-28 21:22:31 -0800144 else {
145 if (m_options.isVerbose)
Davide Pesaventof8d9a532021-07-03 16:04:12 -0400146 std::cerr << "Interest cannot be satisfied, sending Nack\n";
Chavoosh Ghasemi79991a02019-01-28 21:22:31 -0800147 m_face.put(lp::Nack(interest));
148 }
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100149}
150
151void
152Producer::populateStore(std::istream& is)
153{
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400154 BOOST_ASSERT(m_store.empty());
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100155
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400156 if (!m_options.isQuiet)
Davide Pesaventof8d9a532021-07-03 16:04:12 -0400157 std::cerr << "Loading input ...\n";
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100158
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400159 std::vector<uint8_t> buffer(m_options.maxSegmentSize);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100160 while (is.good()) {
161 is.read(reinterpret_cast<char*>(buffer.data()), buffer.size());
162 const auto nCharsRead = is.gcount();
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400163
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100164 if (nCharsRead > 0) {
165 auto data = make_shared<Data>(Name(m_versionedPrefix).appendSegment(m_store.size()));
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400166 data->setFreshnessPeriod(m_options.freshnessPeriod);
Davide Pesavento59984282022-02-16 22:41:03 -0500167 data->setContent(make_span(buffer).first(nCharsRead));
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100168 m_store.push_back(data);
169 }
170 }
171
172 if (m_store.empty()) {
173 auto data = make_shared<Data>(Name(m_versionedPrefix).appendSegment(0));
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400174 data->setFreshnessPeriod(m_options.freshnessPeriod);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100175 m_store.push_back(data);
176 }
177
178 auto finalBlockId = name::Component::fromSegment(m_store.size() - 1);
179 for (const auto& data : m_store) {
Davide Pesavento969cd5a2018-04-20 16:27:47 -0400180 data->setFinalBlock(finalBlockId);
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400181 m_keyChain.sign(*data, m_options.signingInfo);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100182 }
183
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400184 if (!m_options.isQuiet)
Davide Pesaventof8d9a532021-07-03 16:04:12 -0400185 std::cerr << "Created " << m_store.size() << " chunks for prefix " << m_prefix << "\n";
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100186}
187
188} // namespace chunks
189} // namespace ndn