blob: d9591d1b914bfa78f9664e30fcca34748c877515 [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 Pesavento5748e822024-01-26 18:40:22 -05003 * Copyright (c) 2016-2024, 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>
Davide Pesaventob61e3e22022-11-11 14:38:52 -050034#include <ndn-cxx/util/segmenter.hpp>
Chavoosh Ghasemi79991a02019-01-28 21:22:31 -080035
Davide Pesavento5748e822024-01-26 18:40:22 -050036#include <iostream>
37
Davide Pesaventob3570c62022-02-19 19:19:00 -050038namespace ndn::chunks {
Andrea Tosatto672b9a72016-01-05 16:18:20 +010039
Davide Pesavento6f76afc2017-09-19 18:43:51 -040040Producer::Producer(const Name& prefix, Face& face, KeyChain& keyChain, std::istream& is,
41 const Options& opts)
Andrea Tosatto672b9a72016-01-05 16:18:20 +010042 : m_face(face)
43 , m_keyChain(keyChain)
Davide Pesavento6f76afc2017-09-19 18:43:51 -040044 , m_options(opts)
Andrea Tosatto672b9a72016-01-05 16:18:20 +010045{
Davide Pesavento59984282022-02-16 22:41:03 -050046 if (!prefix.empty() && prefix[-1].isVersion()) {
Andrea Tosatto672b9a72016-01-05 16:18:20 +010047 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 Pesaventob61e3e22022-11-11 14:38:52 -050055 if (!m_options.isQuiet) {
56 std::cerr << "Loading input ...\n";
57 }
Junxiao Shi869d73e2023-08-10 22:52:26 +000058 Segmenter segmenter(m_keyChain, m_options.signingInfo);
Davide Pesaventob61e3e22022-11-11 14:38:52 -050059 m_store = segmenter.segment(is, m_versionedPrefix, m_options.maxSegmentSize, m_options.freshnessPeriod);
Andrea Tosatto672b9a72016-01-05 16:18:20 +010060
Davide Pesaventob61e3e22022-11-11 14:38:52 -050061 // register m_prefix without Interest handler
Davide Pesaventof8d9a532021-07-03 16:04:12 -040062 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 Ghasemi79991a02019-01-28 21:22:31 -080066
67 // match Interests whose name starts with m_versionedPrefix
Davide Pesaventof8d9a532021-07-03 16:04:12 -040068 face.setInterestFilter(m_versionedPrefix, [this] (const auto&, const auto& interest) {
69 processSegmentInterest(interest);
70 });
Chavoosh Ghasemi79991a02019-01-28 21:22:31 -080071
72 // match Interests whose name is exactly m_prefix
Davide Pesaventof8d9a532021-07-03 16:04:12 -040073 face.setInterestFilter(InterestFilter(m_prefix, ""), [this] (const auto&, const auto& interest) {
74 processSegmentInterest(interest);
75 });
Chavoosh Ghasemi79991a02019-01-28 21:22:31 -080076
77 // match discovery Interests
Davide Pesaventof8d9a532021-07-03 16:04:12 -040078 auto discoveryName = MetadataObject::makeDiscoveryInterest(m_prefix).getName();
79 face.setInterestFilter(discoveryName, [this] (const auto&, const auto& interest) {
80 processDiscoveryInterest(interest);
81 });
Andrea Tosatto672b9a72016-01-05 16:18:20 +010082
Davide Pesaventob61e3e22022-11-11 14:38:52 -050083 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 Tosatto672b9a72016-01-05 16:18:20 +010090}
91
92void
93Producer::run()
94{
95 m_face.processEvents();
96}
97
98void
Chavoosh Ghasemi79991a02019-01-28 21:22:31 -080099Producer::processDiscoveryInterest(const Interest& interest)
100{
101 if (m_options.isVerbose)
Davide Pesaventof8d9a532021-07-03 16:04:12 -0400102 std::cerr << "Discovery Interest: " << interest << "\n";
Chavoosh Ghasemi79991a02019-01-28 21:22:31 -0800103
104 if (!interest.getCanBePrefix()) {
Davide Pesaventob61e3e22022-11-11 14:38:52 -0500105 if (m_options.isVerbose) {
Davide Pesaventof8d9a532021-07-03 16:04:12 -0400106 std::cerr << "Discovery Interest lacks CanBePrefix, sending Nack\n";
Davide Pesaventob61e3e22022-11-11 14:38:52 -0500107 }
Chavoosh Ghasemi79991a02019-01-28 21:22:31 -0800108 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 Pesaventob61e3e22022-11-11 14:38:52 -0500116 auto mdata = mobject.makeData(interest.getName(), m_keyChain, m_options.signingInfo);
Chavoosh Ghasemi79991a02019-01-28 21:22:31 -0800117
118 if (m_options.isVerbose)
Davide Pesaventof8d9a532021-07-03 16:04:12 -0400119 std::cerr << "Sending metadata: " << mdata << "\n";
Chavoosh Ghasemi79991a02019-01-28 21:22:31 -0800120
121 m_face.put(mdata);
122}
123
124void
125Producer::processSegmentInterest(const Interest& interest)
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100126{
Davide Pesavento59984282022-02-16 22:41:03 -0500127 BOOST_ASSERT(!m_store.empty());
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100128
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400129 if (m_options.isVerbose)
Davide Pesaventof8d9a532021-07-03 16:04:12 -0400130 std::cerr << "Interest: " << interest << "\n";
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100131
132 const Name& name = interest.getName();
Davide Pesavento5748e822024-01-26 18:40:22 -0500133 std::shared_ptr<Data> data;
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100134
Chavoosh Ghasemi79991a02019-01-28 21:22:31 -0800135 if (name.size() == m_versionedPrefix.size() + 1 && name[-1].isSegment()) {
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100136 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 Ghasemi79991a02019-01-28 21:22:31 -0800143 // unspecified version or segment number, return first segment
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100144 data = m_store[0];
145 }
146
147 if (data != nullptr) {
Davide Pesaventob61e3e22022-11-11 14:38:52 -0500148 if (m_options.isVerbose) {
Davide Pesaventof8d9a532021-07-03 16:04:12 -0400149 std::cerr << "Data: " << *data << "\n";
Davide Pesaventob61e3e22022-11-11 14:38:52 -0500150 }
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100151 m_face.put(*data);
152 }
Chavoosh Ghasemi79991a02019-01-28 21:22:31 -0800153 else {
Davide Pesaventob61e3e22022-11-11 14:38:52 -0500154 if (m_options.isVerbose) {
Davide Pesaventof8d9a532021-07-03 16:04:12 -0400155 std::cerr << "Interest cannot be satisfied, sending Nack\n";
Davide Pesaventob61e3e22022-11-11 14:38:52 -0500156 }
Chavoosh Ghasemi79991a02019-01-28 21:22:31 -0800157 m_face.put(lp::Nack(interest));
158 }
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100159}
160
Davide Pesaventob3570c62022-02-19 19:19:00 -0500161} // namespace ndn::chunks