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 | /* |
Davide Pesavento | 969cd5a | 2018-04-20 16:27:47 -0400 | [diff] [blame] | 3 | * Copyright (c) 2016-2018, 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 |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 28 | */ |
| 29 | |
| 30 | #include "producer.hpp" |
| 31 | |
| 32 | namespace ndn { |
| 33 | namespace chunks { |
| 34 | |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 35 | Producer::Producer(const Name& prefix, Face& face, KeyChain& keyChain, std::istream& is, |
| 36 | const Options& opts) |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 37 | : m_face(face) |
| 38 | , m_keyChain(keyChain) |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 39 | , m_options(opts) |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 40 | { |
| 41 | if (prefix.size() > 0 && prefix[-1].isVersion()) { |
| 42 | m_prefix = prefix.getPrefix(-1); |
| 43 | m_versionedPrefix = prefix; |
| 44 | } |
| 45 | else { |
| 46 | m_prefix = prefix; |
| 47 | m_versionedPrefix = Name(m_prefix).appendVersion(); |
| 48 | } |
| 49 | |
| 50 | populateStore(is); |
| 51 | |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 52 | if (m_options.wantShowVersion) |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 53 | std::cout << m_versionedPrefix[-1] << std::endl; |
| 54 | |
| 55 | m_face.setInterestFilter(m_prefix, |
| 56 | bind(&Producer::onInterest, this, _2), |
| 57 | RegisterPrefixSuccessCallback(), |
| 58 | bind(&Producer::onRegisterFailed, this, _1, _2)); |
| 59 | |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 60 | if (!m_options.isQuiet) |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 61 | std::cerr << "Data published with name: " << m_versionedPrefix << std::endl; |
| 62 | } |
| 63 | |
| 64 | void |
| 65 | Producer::run() |
| 66 | { |
| 67 | m_face.processEvents(); |
| 68 | } |
| 69 | |
| 70 | void |
| 71 | Producer::onInterest(const Interest& interest) |
| 72 | { |
| 73 | BOOST_ASSERT(m_store.size() > 0); |
| 74 | |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 75 | if (m_options.isVerbose) |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 76 | std::cerr << "Interest: " << interest << std::endl; |
| 77 | |
| 78 | const Name& name = interest.getName(); |
| 79 | shared_ptr<Data> data; |
| 80 | |
| 81 | // is this a discovery Interest or a sequence retrieval? |
| 82 | if (name.size() == m_versionedPrefix.size() + 1 && m_versionedPrefix.isPrefixOf(name) && |
| 83 | name[-1].isSegment()) { |
| 84 | const auto segmentNo = static_cast<size_t>(interest.getName()[-1].toSegment()); |
| 85 | // specific segment retrieval |
| 86 | if (segmentNo < m_store.size()) { |
| 87 | data = m_store[segmentNo]; |
| 88 | } |
| 89 | } |
| 90 | else if (interest.matchesData(*m_store[0])) { |
| 91 | // Interest has version and is looking for the first segment or has no version |
| 92 | data = m_store[0]; |
| 93 | } |
| 94 | |
| 95 | if (data != nullptr) { |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 96 | if (m_options.isVerbose) |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 97 | std::cerr << "Data: " << *data << std::endl; |
| 98 | |
| 99 | m_face.put(*data); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | void |
| 104 | Producer::populateStore(std::istream& is) |
| 105 | { |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 106 | BOOST_ASSERT(m_store.empty()); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 107 | |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 108 | if (!m_options.isQuiet) |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 109 | std::cerr << "Loading input ..." << std::endl; |
| 110 | |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 111 | std::vector<uint8_t> buffer(m_options.maxSegmentSize); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 112 | while (is.good()) { |
| 113 | is.read(reinterpret_cast<char*>(buffer.data()), buffer.size()); |
| 114 | const auto nCharsRead = is.gcount(); |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 115 | |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 116 | if (nCharsRead > 0) { |
| 117 | auto data = make_shared<Data>(Name(m_versionedPrefix).appendSegment(m_store.size())); |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 118 | data->setFreshnessPeriod(m_options.freshnessPeriod); |
| 119 | data->setContent(buffer.data(), static_cast<size_t>(nCharsRead)); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 120 | m_store.push_back(data); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | if (m_store.empty()) { |
| 125 | auto data = make_shared<Data>(Name(m_versionedPrefix).appendSegment(0)); |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 126 | data->setFreshnessPeriod(m_options.freshnessPeriod); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 127 | m_store.push_back(data); |
| 128 | } |
| 129 | |
| 130 | auto finalBlockId = name::Component::fromSegment(m_store.size() - 1); |
| 131 | for (const auto& data : m_store) { |
Davide Pesavento | 969cd5a | 2018-04-20 16:27:47 -0400 | [diff] [blame] | 132 | data->setFinalBlock(finalBlockId); |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 133 | m_keyChain.sign(*data, m_options.signingInfo); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 134 | } |
| 135 | |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 136 | if (!m_options.isQuiet) |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 137 | std::cerr << "Created " << m_store.size() << " chunks for prefix " << m_prefix << std::endl; |
| 138 | } |
| 139 | |
| 140 | void |
| 141 | Producer::onRegisterFailed(const Name& prefix, const std::string& reason) |
| 142 | { |
| 143 | std::cerr << "ERROR: Failed to register prefix '" |
| 144 | << prefix << "' (" << reason << ")" << std::endl; |
| 145 | m_face.shutdown(); |
| 146 | } |
| 147 | |
| 148 | } // namespace chunks |
| 149 | } // namespace ndn |