blob: 6b305cda71ad744e75d3904e244e8bfda72aad3c [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>
Davide Pesaventob61e3e22022-11-11 14:38:52 -050034#include <ndn-cxx/util/segmenter.hpp>
Chavoosh Ghasemi79991a02019-01-28 21:22:31 -080035
Davide Pesaventob3570c62022-02-19 19:19:00 -050036namespace ndn::chunks {
Andrea Tosatto672b9a72016-01-05 16:18:20 +010037
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
Davide Pesaventob61e3e22022-11-11 14:38:52 -050053 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 Tosatto672b9a72016-01-05 16:18:20 +010058
Davide Pesaventob61e3e22022-11-11 14:38:52 -050059 // register m_prefix without Interest handler
Davide Pesaventof8d9a532021-07-03 16:04:12 -040060 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 Ghasemi79991a02019-01-28 21:22:31 -080064
65 // match Interests whose name starts with m_versionedPrefix
Davide Pesaventof8d9a532021-07-03 16:04:12 -040066 face.setInterestFilter(m_versionedPrefix, [this] (const auto&, const auto& interest) {
67 processSegmentInterest(interest);
68 });
Chavoosh Ghasemi79991a02019-01-28 21:22:31 -080069
70 // match Interests whose name is exactly m_prefix
Davide Pesaventof8d9a532021-07-03 16:04:12 -040071 face.setInterestFilter(InterestFilter(m_prefix, ""), [this] (const auto&, const auto& interest) {
72 processSegmentInterest(interest);
73 });
Chavoosh Ghasemi79991a02019-01-28 21:22:31 -080074
75 // match discovery Interests
Davide Pesaventof8d9a532021-07-03 16:04:12 -040076 auto discoveryName = MetadataObject::makeDiscoveryInterest(m_prefix).getName();
77 face.setInterestFilter(discoveryName, [this] (const auto&, const auto& interest) {
78 processDiscoveryInterest(interest);
79 });
Andrea Tosatto672b9a72016-01-05 16:18:20 +010080
Davide Pesaventob61e3e22022-11-11 14:38:52 -050081 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 Tosatto672b9a72016-01-05 16:18:20 +010088}
89
90void
91Producer::run()
92{
93 m_face.processEvents();
94}
95
96void
Chavoosh Ghasemi79991a02019-01-28 21:22:31 -080097Producer::processDiscoveryInterest(const Interest& interest)
98{
99 if (m_options.isVerbose)
Davide Pesaventof8d9a532021-07-03 16:04:12 -0400100 std::cerr << "Discovery Interest: " << interest << "\n";
Chavoosh Ghasemi79991a02019-01-28 21:22:31 -0800101
102 if (!interest.getCanBePrefix()) {
Davide Pesaventob61e3e22022-11-11 14:38:52 -0500103 if (m_options.isVerbose) {
Davide Pesaventof8d9a532021-07-03 16:04:12 -0400104 std::cerr << "Discovery Interest lacks CanBePrefix, sending Nack\n";
Davide Pesaventob61e3e22022-11-11 14:38:52 -0500105 }
Chavoosh Ghasemi79991a02019-01-28 21:22:31 -0800106 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 Pesaventob61e3e22022-11-11 14:38:52 -0500114 auto mdata = mobject.makeData(interest.getName(), m_keyChain, m_options.signingInfo);
Chavoosh Ghasemi79991a02019-01-28 21:22:31 -0800115
116 if (m_options.isVerbose)
Davide Pesaventof8d9a532021-07-03 16:04:12 -0400117 std::cerr << "Sending metadata: " << mdata << "\n";
Chavoosh Ghasemi79991a02019-01-28 21:22:31 -0800118
119 m_face.put(mdata);
120}
121
122void
123Producer::processSegmentInterest(const Interest& interest)
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100124{
Davide Pesavento59984282022-02-16 22:41:03 -0500125 BOOST_ASSERT(!m_store.empty());
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100126
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400127 if (m_options.isVerbose)
Davide Pesaventof8d9a532021-07-03 16:04:12 -0400128 std::cerr << "Interest: " << interest << "\n";
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100129
130 const Name& name = interest.getName();
131 shared_ptr<Data> data;
132
Chavoosh Ghasemi79991a02019-01-28 21:22:31 -0800133 if (name.size() == m_versionedPrefix.size() + 1 && name[-1].isSegment()) {
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100134 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 Ghasemi79991a02019-01-28 21:22:31 -0800141 // unspecified version or segment number, return first segment
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100142 data = m_store[0];
143 }
144
145 if (data != nullptr) {
Davide Pesaventob61e3e22022-11-11 14:38:52 -0500146 if (m_options.isVerbose) {
Davide Pesaventof8d9a532021-07-03 16:04:12 -0400147 std::cerr << "Data: " << *data << "\n";
Davide Pesaventob61e3e22022-11-11 14:38:52 -0500148 }
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100149 m_face.put(*data);
150 }
Chavoosh Ghasemi79991a02019-01-28 21:22:31 -0800151 else {
Davide Pesaventob61e3e22022-11-11 14:38:52 -0500152 if (m_options.isVerbose) {
Davide Pesaventof8d9a532021-07-03 16:04:12 -0400153 std::cerr << "Interest cannot be satisfied, sending Nack\n";
Davide Pesaventob61e3e22022-11-11 14:38:52 -0500154 }
Chavoosh Ghasemi79991a02019-01-28 21:22:31 -0800155 m_face.put(lp::Nack(interest));
156 }
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100157}
158
Davide Pesaventob3570c62022-02-19 19:19:00 -0500159} // namespace ndn::chunks