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