Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | c0e2658 | 2017-08-13 21:16:49 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | 9c0bd8d | 2022-03-14 16:48:12 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2022, Regents of the University of California. |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 4 | * |
| 5 | * This file is part of NDN repo-ng (Next generation of NDN repository). |
| 6 | * See AUTHORS.md for complete list of repo-ng authors and contributors. |
| 7 | * |
| 8 | * repo-ng is free software: you can redistribute it and/or modify it under the terms |
| 9 | * of the GNU General Public License as published by the Free Software Foundation, |
| 10 | * either version 3 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * repo-ng is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 13 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 | * PURPOSE. See the GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along with |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 17 | * repo-ng, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 18 | */ |
| 19 | |
Alexander Afanasyev | 39d9807 | 2014-05-04 12:46:29 -0700 | [diff] [blame] | 20 | #include "../src/repo-command-parameter.hpp" |
| 21 | #include "../src/repo-command-response.hpp" |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 22 | |
| 23 | #include <ndn-cxx/face.hpp> |
Davide Pesavento | 75e8d46 | 2021-05-16 17:00:42 -0400 | [diff] [blame] | 24 | #include <ndn-cxx/security/interest-signer.hpp> |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 25 | #include <ndn-cxx/security/key-chain.hpp> |
Junxiao Shi | 959c5b9 | 2016-08-23 01:05:27 +0000 | [diff] [blame] | 26 | #include <ndn-cxx/security/signing-helpers.hpp> |
Wentao Shang | 91fb4f2 | 2014-05-20 10:55:22 -0700 | [diff] [blame] | 27 | #include <ndn-cxx/util/scheduler.hpp> |
Alexander Afanasyev | c0e2658 | 2017-08-13 21:16:49 -0400 | [diff] [blame] | 28 | |
Wentao Shang | 91fb4f2 | 2014-05-20 10:55:22 -0700 | [diff] [blame] | 29 | #include <stdint.h> |
Alexander Afanasyev | c0e2658 | 2017-08-13 21:16:49 -0400 | [diff] [blame] | 30 | #include <stdlib.h> |
Davide Pesavento | 31be3f6 | 2019-01-24 23:20:23 -0500 | [diff] [blame] | 31 | #include <unistd.h> |
Alexander Afanasyev | c0e2658 | 2017-08-13 21:16:49 -0400 | [diff] [blame] | 32 | |
| 33 | #include <fstream> |
| 34 | #include <iostream> |
| 35 | #include <string> |
| 36 | |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 37 | #include <boost/asio.hpp> |
Wentao Shang | 91fb4f2 | 2014-05-20 10:55:22 -0700 | [diff] [blame] | 38 | #include <boost/lexical_cast.hpp> |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 39 | #include <boost/iostreams/operations.hpp> |
| 40 | #include <boost/iostreams/read.hpp> |
| 41 | |
| 42 | namespace repo { |
| 43 | |
| 44 | using namespace ndn::time; |
| 45 | |
Wentao Shang | a8f3c40 | 2014-10-30 14:03:27 -0700 | [diff] [blame] | 46 | using std::shared_ptr; |
| 47 | using std::make_shared; |
| 48 | using std::bind; |
Wentao Shang | a8f3c40 | 2014-10-30 14:03:27 -0700 | [diff] [blame] | 49 | |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 50 | static const uint64_t DEFAULT_BLOCK_SIZE = 1000; |
| 51 | static const uint64_t DEFAULT_INTEREST_LIFETIME = 4000; |
| 52 | static const uint64_t DEFAULT_FRESHNESS_PERIOD = 10000; |
| 53 | static const uint64_t DEFAULT_CHECK_PERIOD = 1000; |
Weiqi Shi | 5822e34 | 2014-08-21 20:05:30 -0700 | [diff] [blame] | 54 | static const size_t PRE_SIGN_DATA_COUNT = 11; |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 55 | |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 56 | class NdnPutFile : boost::noncopyable |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 57 | { |
| 58 | public: |
| 59 | class Error : public std::runtime_error |
| 60 | { |
| 61 | public: |
Davide Pesavento | 31be3f6 | 2019-01-24 23:20:23 -0500 | [diff] [blame] | 62 | using std::runtime_error::runtime_error; |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | NdnPutFile() |
| 66 | : isUnversioned(false) |
| 67 | , isSingle(false) |
| 68 | , useDigestSha256(false) |
| 69 | , freshnessPeriod(DEFAULT_FRESHNESS_PERIOD) |
| 70 | , interestLifetime(DEFAULT_INTEREST_LIFETIME) |
| 71 | , hasTimeout(false) |
| 72 | , timeout(0) |
Davide Pesavento | 31be3f6 | 2019-01-24 23:20:23 -0500 | [diff] [blame] | 73 | , insertStream(nullptr) |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 74 | , isVerbose(false) |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 75 | , m_scheduler(m_face.getIoService()) |
| 76 | , m_timestampVersion(toUnixTimestamp(system_clock::now()).count()) |
| 77 | , m_processId(0) |
| 78 | , m_checkPeriod(DEFAULT_CHECK_PERIOD) |
| 79 | , m_currentSegmentNo(0) |
| 80 | , m_isFinished(false) |
Alexander Afanasyev | 2e8147c | 2017-11-09 14:17:02 -0500 | [diff] [blame] | 81 | , m_cmdSigner(m_keyChain) |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 82 | { |
| 83 | } |
| 84 | |
| 85 | void |
| 86 | run(); |
| 87 | |
| 88 | private: |
| 89 | void |
| 90 | prepareNextData(uint64_t referenceSegmentNo); |
| 91 | |
| 92 | void |
| 93 | startInsertCommand(); |
| 94 | |
| 95 | void |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 96 | onInsertCommandResponse(const ndn::Interest& interest, const ndn::Data& data); |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 97 | |
| 98 | void |
| 99 | onInsertCommandTimeout(const ndn::Interest& interest); |
| 100 | |
| 101 | void |
| 102 | onInterest(const ndn::Name& prefix, const ndn::Interest& interest); |
| 103 | |
| 104 | void |
| 105 | onSingleInterest(const ndn::Name& prefix, const ndn::Interest& interest); |
| 106 | |
| 107 | void |
Wentao Shang | 91fb4f2 | 2014-05-20 10:55:22 -0700 | [diff] [blame] | 108 | onRegisterSuccess(const ndn::Name& prefix); |
| 109 | |
| 110 | void |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 111 | onRegisterFailed(const ndn::Name& prefix, const std::string& reason); |
| 112 | |
| 113 | void |
| 114 | stopProcess(); |
| 115 | |
| 116 | void |
| 117 | signData(ndn::Data& data); |
| 118 | |
| 119 | void |
| 120 | startCheckCommand(); |
| 121 | |
| 122 | void |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 123 | onCheckCommandResponse(const ndn::Interest& interest, const ndn::Data& data); |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 124 | |
| 125 | void |
| 126 | onCheckCommandTimeout(const ndn::Interest& interest); |
| 127 | |
| 128 | ndn::Interest |
| 129 | generateCommandInterest(const ndn::Name& commandPrefix, const std::string& command, |
| 130 | const RepoCommandParameter& commandParameter); |
| 131 | |
| 132 | public: |
| 133 | bool isUnversioned; |
| 134 | bool isSingle; |
| 135 | bool useDigestSha256; |
| 136 | std::string identityForData; |
| 137 | std::string identityForCommand; |
| 138 | milliseconds freshnessPeriod; |
| 139 | milliseconds interestLifetime; |
| 140 | bool hasTimeout; |
| 141 | milliseconds timeout; |
| 142 | ndn::Name repoPrefix; |
| 143 | ndn::Name ndnName; |
| 144 | std::istream* insertStream; |
| 145 | bool isVerbose; |
| 146 | |
| 147 | private: |
| 148 | ndn::Face m_face; |
| 149 | ndn::Scheduler m_scheduler; |
| 150 | ndn::KeyChain m_keyChain; |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 151 | uint64_t m_timestampVersion; |
| 152 | uint64_t m_processId; |
| 153 | milliseconds m_checkPeriod; |
| 154 | size_t m_currentSegmentNo; |
| 155 | bool m_isFinished; |
| 156 | ndn::Name m_dataPrefix; |
| 157 | |
Davide Pesavento | 9c0bd8d | 2022-03-14 16:48:12 -0400 | [diff] [blame] | 158 | std::map<uint64_t, std::shared_ptr<ndn::Data>> m_data; |
Davide Pesavento | 75e8d46 | 2021-05-16 17:00:42 -0400 | [diff] [blame] | 159 | ndn::security::InterestSigner m_cmdSigner; |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 160 | }; |
| 161 | |
| 162 | void |
| 163 | NdnPutFile::prepareNextData(uint64_t referenceSegmentNo) |
| 164 | { |
| 165 | // make sure m_data has [referenceSegmentNo, referenceSegmentNo + PRE_SIGN_DATA_COUNT] Data |
| 166 | if (m_isFinished) |
| 167 | return; |
| 168 | |
| 169 | size_t nDataToPrepare = PRE_SIGN_DATA_COUNT; |
| 170 | |
| 171 | if (!m_data.empty()) { |
| 172 | uint64_t maxSegmentNo = m_data.rbegin()->first; |
| 173 | |
| 174 | if (maxSegmentNo - referenceSegmentNo >= nDataToPrepare) { |
| 175 | // nothing to prepare |
| 176 | return; |
| 177 | } |
| 178 | |
| 179 | nDataToPrepare -= maxSegmentNo - referenceSegmentNo; |
| 180 | } |
| 181 | |
| 182 | for (size_t i = 0; i < nDataToPrepare && !m_isFinished; ++i) { |
| 183 | uint8_t buffer[DEFAULT_BLOCK_SIZE]; |
Davide Pesavento | 31be3f6 | 2019-01-24 23:20:23 -0500 | [diff] [blame] | 184 | auto readSize = boost::iostreams::read(*insertStream, |
| 185 | reinterpret_cast<char*>(buffer), DEFAULT_BLOCK_SIZE); |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 186 | if (readSize <= 0) { |
Davide Pesavento | 9c0bd8d | 2022-03-14 16:48:12 -0400 | [diff] [blame] | 187 | NDN_THROW(Error("Error reading from the input stream")); |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 188 | } |
| 189 | |
Davide Pesavento | 31be3f6 | 2019-01-24 23:20:23 -0500 | [diff] [blame] | 190 | auto data = make_shared<ndn::Data>(Name(m_dataPrefix).appendSegment(m_currentSegmentNo)); |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 191 | |
| 192 | if (insertStream->peek() == std::istream::traits_type::eof()) { |
Davide Pesavento | 0c13951 | 2018-11-03 18:23:38 -0400 | [diff] [blame] | 193 | data->setFinalBlock(ndn::name::Component::fromSegment(m_currentSegmentNo)); |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 194 | m_isFinished = true; |
| 195 | } |
| 196 | |
Davide Pesavento | 9c0bd8d | 2022-03-14 16:48:12 -0400 | [diff] [blame] | 197 | data->setContent(ndn::make_span(buffer, readSize)); |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 198 | data->setFreshnessPeriod(freshnessPeriod); |
| 199 | signData(*data); |
| 200 | |
| 201 | m_data.insert(std::make_pair(m_currentSegmentNo, data)); |
| 202 | |
| 203 | ++m_currentSegmentNo; |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | void |
| 208 | NdnPutFile::run() |
| 209 | { |
| 210 | m_dataPrefix = ndnName; |
| 211 | if (!isUnversioned) |
| 212 | m_dataPrefix.appendVersion(m_timestampVersion); |
| 213 | |
| 214 | if (isVerbose) |
| 215 | std::cerr << "setInterestFilter for " << m_dataPrefix << std::endl; |
Davide Pesavento | 75e8d46 | 2021-05-16 17:00:42 -0400 | [diff] [blame] | 216 | |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 217 | m_face.setInterestFilter(m_dataPrefix, |
| 218 | isSingle ? |
Davide Pesavento | 75e8d46 | 2021-05-16 17:00:42 -0400 | [diff] [blame] | 219 | bind(&NdnPutFile::onSingleInterest, this, _1, _2) : |
| 220 | bind(&NdnPutFile::onInterest, this, _1, _2), |
Wentao Shang | a8f3c40 | 2014-10-30 14:03:27 -0700 | [diff] [blame] | 221 | bind(&NdnPutFile::onRegisterSuccess, this, _1), |
| 222 | bind(&NdnPutFile::onRegisterFailed, this, _1, _2)); |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 223 | |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 224 | if (hasTimeout) |
Davide Pesavento | 8891c83 | 2019-03-20 23:20:35 -0400 | [diff] [blame] | 225 | m_scheduler.schedule(timeout, [this] { stopProcess(); }); |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 226 | |
| 227 | m_face.processEvents(); |
| 228 | } |
| 229 | |
| 230 | void |
Wentao Shang | 91fb4f2 | 2014-05-20 10:55:22 -0700 | [diff] [blame] | 231 | NdnPutFile::onRegisterSuccess(const Name& prefix) |
| 232 | { |
| 233 | startInsertCommand(); |
| 234 | } |
| 235 | |
| 236 | void |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 237 | NdnPutFile::startInsertCommand() |
| 238 | { |
| 239 | RepoCommandParameter parameters; |
| 240 | parameters.setName(m_dataPrefix); |
| 241 | if (!isSingle) { |
| 242 | parameters.setStartBlockId(0); |
| 243 | } |
| 244 | |
| 245 | ndn::Interest commandInterest = generateCommandInterest(repoPrefix, "insert", parameters); |
| 246 | m_face.expressInterest(commandInterest, |
Wentao Shang | a8f3c40 | 2014-10-30 14:03:27 -0700 | [diff] [blame] | 247 | bind(&NdnPutFile::onInsertCommandResponse, this, _1, _2), |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 248 | bind(&NdnPutFile::onInsertCommandTimeout, this, _1), // Nack |
Wentao Shang | a8f3c40 | 2014-10-30 14:03:27 -0700 | [diff] [blame] | 249 | bind(&NdnPutFile::onInsertCommandTimeout, this, _1)); |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | void |
Davide Pesavento | 9c0bd8d | 2022-03-14 16:48:12 -0400 | [diff] [blame] | 253 | NdnPutFile::onInsertCommandResponse(const ndn::Interest&, const ndn::Data& data) |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 254 | { |
| 255 | RepoCommandResponse response(data.getContent().blockFromValue()); |
Davide Pesavento | 31be3f6 | 2019-01-24 23:20:23 -0500 | [diff] [blame] | 256 | auto statusCode = response.getCode(); |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 257 | if (statusCode >= 400) { |
Davide Pesavento | 9c0bd8d | 2022-03-14 16:48:12 -0400 | [diff] [blame] | 258 | NDN_THROW(Error("insert command failed with code " + std::to_string(statusCode))); |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 259 | } |
| 260 | m_processId = response.getProcessId(); |
| 261 | |
Davide Pesavento | 8891c83 | 2019-03-20 23:20:35 -0400 | [diff] [blame] | 262 | m_scheduler.schedule(m_checkPeriod, [this] { startCheckCommand(); }); |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | void |
Davide Pesavento | 9c0bd8d | 2022-03-14 16:48:12 -0400 | [diff] [blame] | 266 | NdnPutFile::onInsertCommandTimeout(const ndn::Interest&) |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 267 | { |
Davide Pesavento | 9c0bd8d | 2022-03-14 16:48:12 -0400 | [diff] [blame] | 268 | NDN_THROW(Error("command response timeout")); |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | void |
| 272 | NdnPutFile::onInterest(const ndn::Name& prefix, const ndn::Interest& interest) |
| 273 | { |
| 274 | if (interest.getName().size() != prefix.size() + 1) { |
| 275 | if (isVerbose) { |
| 276 | std::cerr << "Error processing incoming interest " << interest << ": " |
| 277 | << "Unrecognized Interest" << std::endl; |
| 278 | } |
| 279 | return; |
| 280 | } |
| 281 | |
| 282 | uint64_t segmentNo; |
| 283 | try { |
| 284 | ndn::Name::Component segmentComponent = interest.getName().get(prefix.size()); |
| 285 | segmentNo = segmentComponent.toSegment(); |
| 286 | } |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 287 | catch (const tlv::Error& e) { |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 288 | if (isVerbose) { |
| 289 | std::cerr << "Error processing incoming interest " << interest << ": " |
| 290 | << e.what() << std::endl; |
| 291 | } |
| 292 | return; |
| 293 | } |
| 294 | |
| 295 | prepareNextData(segmentNo); |
| 296 | |
Davide Pesavento | 9c0bd8d | 2022-03-14 16:48:12 -0400 | [diff] [blame] | 297 | auto item = m_data.find(segmentNo); |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 298 | if (item == m_data.end()) { |
| 299 | if (isVerbose) { |
| 300 | std::cerr << "Requested segment [" << segmentNo << "] does not exist" << std::endl; |
| 301 | } |
| 302 | return; |
| 303 | } |
| 304 | |
Weiqi Shi | 5822e34 | 2014-08-21 20:05:30 -0700 | [diff] [blame] | 305 | if (m_isFinished) { |
| 306 | uint64_t final = m_currentSegmentNo - 1; |
Davide Pesavento | 0c13951 | 2018-11-03 18:23:38 -0400 | [diff] [blame] | 307 | item->second->setFinalBlock(ndn::name::Component::fromSegment(final)); |
Weiqi Shi | 5822e34 | 2014-08-21 20:05:30 -0700 | [diff] [blame] | 308 | } |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 309 | m_face.put(*item->second); |
| 310 | } |
| 311 | |
| 312 | void |
| 313 | NdnPutFile::onSingleInterest(const ndn::Name& prefix, const ndn::Interest& interest) |
| 314 | { |
| 315 | BOOST_ASSERT(prefix == m_dataPrefix); |
| 316 | |
| 317 | if (prefix != interest.getName()) { |
| 318 | if (isVerbose) { |
| 319 | std::cerr << "Received unexpected interest " << interest << std::endl; |
| 320 | } |
| 321 | return; |
| 322 | } |
| 323 | |
| 324 | uint8_t buffer[DEFAULT_BLOCK_SIZE]; |
| 325 | std::streamsize readSize = |
| 326 | boost::iostreams::read(*insertStream, reinterpret_cast<char*>(buffer), DEFAULT_BLOCK_SIZE); |
| 327 | |
| 328 | if (readSize <= 0) { |
Davide Pesavento | 9c0bd8d | 2022-03-14 16:48:12 -0400 | [diff] [blame] | 329 | NDN_THROW(Error("Error reading from the input stream")); |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | if (insertStream->peek() != std::istream::traits_type::eof()) { |
Davide Pesavento | 9c0bd8d | 2022-03-14 16:48:12 -0400 | [diff] [blame] | 333 | NDN_THROW(Error("Input data does not fit into one Data packet")); |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 334 | } |
| 335 | |
Davide Pesavento | 31be3f6 | 2019-01-24 23:20:23 -0500 | [diff] [blame] | 336 | auto data = make_shared<ndn::Data>(m_dataPrefix); |
Davide Pesavento | 9c0bd8d | 2022-03-14 16:48:12 -0400 | [diff] [blame] | 337 | data->setContent(ndn::make_span(buffer, readSize)); |
Weiqi Shi | 5822e34 | 2014-08-21 20:05:30 -0700 | [diff] [blame] | 338 | data->setFreshnessPeriod(freshnessPeriod); |
| 339 | signData(*data); |
| 340 | m_face.put(*data); |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 341 | |
| 342 | m_isFinished = true; |
| 343 | } |
| 344 | |
| 345 | void |
| 346 | NdnPutFile::onRegisterFailed(const ndn::Name& prefix, const std::string& reason) |
| 347 | { |
Davide Pesavento | 9c0bd8d | 2022-03-14 16:48:12 -0400 | [diff] [blame] | 348 | NDN_THROW(Error("onRegisterFailed: " + reason)); |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | void |
| 352 | NdnPutFile::stopProcess() |
| 353 | { |
| 354 | m_face.getIoService().stop(); |
| 355 | } |
| 356 | |
| 357 | void |
| 358 | NdnPutFile::signData(ndn::Data& data) |
| 359 | { |
| 360 | if (useDigestSha256) { |
Junxiao Shi | 047a6fb | 2017-06-08 16:16:05 +0000 | [diff] [blame] | 361 | m_keyChain.sign(data, ndn::signingWithSha256()); |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 362 | } |
Junxiao Shi | 047a6fb | 2017-06-08 16:16:05 +0000 | [diff] [blame] | 363 | else if (identityForData.empty()) |
| 364 | m_keyChain.sign(data); |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 365 | else { |
Junxiao Shi | 047a6fb | 2017-06-08 16:16:05 +0000 | [diff] [blame] | 366 | m_keyChain.sign(data, ndn::signingByIdentity(identityForData)); |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 367 | } |
| 368 | } |
| 369 | |
| 370 | void |
| 371 | NdnPutFile::startCheckCommand() |
| 372 | { |
| 373 | ndn::Interest checkInterest = generateCommandInterest(repoPrefix, "insert check", |
| 374 | RepoCommandParameter() |
| 375 | .setProcessId(m_processId)); |
| 376 | m_face.expressInterest(checkInterest, |
Wentao Shang | a8f3c40 | 2014-10-30 14:03:27 -0700 | [diff] [blame] | 377 | bind(&NdnPutFile::onCheckCommandResponse, this, _1, _2), |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 378 | bind(&NdnPutFile::onCheckCommandTimeout, this, _1), // Nack |
Wentao Shang | a8f3c40 | 2014-10-30 14:03:27 -0700 | [diff] [blame] | 379 | bind(&NdnPutFile::onCheckCommandTimeout, this, _1)); |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | void |
Davide Pesavento | 9c0bd8d | 2022-03-14 16:48:12 -0400 | [diff] [blame] | 383 | NdnPutFile::onCheckCommandResponse(const ndn::Interest&, const ndn::Data& data) |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 384 | { |
| 385 | RepoCommandResponse response(data.getContent().blockFromValue()); |
Davide Pesavento | 31be3f6 | 2019-01-24 23:20:23 -0500 | [diff] [blame] | 386 | auto statusCode = response.getCode(); |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 387 | if (statusCode >= 400) { |
Davide Pesavento | 9c0bd8d | 2022-03-14 16:48:12 -0400 | [diff] [blame] | 388 | NDN_THROW(Error("Insert check command failed with code " + std::to_string(statusCode))); |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | if (m_isFinished) { |
| 392 | uint64_t insertCount = response.getInsertNum(); |
| 393 | |
| 394 | if (isSingle) { |
| 395 | if (insertCount == 1) { |
| 396 | m_face.getIoService().stop(); |
| 397 | return; |
| 398 | } |
| 399 | } |
| 400 | // Technically, the check should not infer, but directly has signal from repo that |
| 401 | // write operation has been finished |
| 402 | |
| 403 | if (insertCount == m_currentSegmentNo) { |
| 404 | m_face.getIoService().stop(); |
| 405 | return; |
| 406 | } |
| 407 | } |
| 408 | |
Davide Pesavento | 8891c83 | 2019-03-20 23:20:35 -0400 | [diff] [blame] | 409 | m_scheduler.schedule(m_checkPeriod, [this] { startCheckCommand(); }); |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | void |
Davide Pesavento | 9c0bd8d | 2022-03-14 16:48:12 -0400 | [diff] [blame] | 413 | NdnPutFile::onCheckCommandTimeout(const ndn::Interest&) |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 414 | { |
Davide Pesavento | 9c0bd8d | 2022-03-14 16:48:12 -0400 | [diff] [blame] | 415 | NDN_THROW(Error("check response timeout")); |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | ndn::Interest |
| 419 | NdnPutFile::generateCommandInterest(const ndn::Name& commandPrefix, const std::string& command, |
| 420 | const RepoCommandParameter& commandParameter) |
| 421 | { |
Alexander Afanasyev | 2e8147c | 2017-11-09 14:17:02 -0500 | [diff] [blame] | 422 | Name cmd = commandPrefix; |
Davide Pesavento | 9c0bd8d | 2022-03-14 16:48:12 -0400 | [diff] [blame] | 423 | cmd.append(command).append(tlv::GenericNameComponent, commandParameter.wireEncode()); |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 424 | |
Davide Pesavento | 9c0bd8d | 2022-03-14 16:48:12 -0400 | [diff] [blame] | 425 | ndn::Interest interest; |
| 426 | if (identityForCommand.empty()) { |
Alexander Afanasyev | 2e8147c | 2017-11-09 14:17:02 -0500 | [diff] [blame] | 427 | interest = m_cmdSigner.makeCommandInterest(cmd); |
Davide Pesavento | 9c0bd8d | 2022-03-14 16:48:12 -0400 | [diff] [blame] | 428 | } |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 429 | else { |
Alexander Afanasyev | 2e8147c | 2017-11-09 14:17:02 -0500 | [diff] [blame] | 430 | interest = m_cmdSigner.makeCommandInterest(cmd, ndn::signingByIdentity(identityForCommand)); |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 431 | } |
| 432 | |
Alexander Afanasyev | 2e8147c | 2017-11-09 14:17:02 -0500 | [diff] [blame] | 433 | interest.setInterestLifetime(interestLifetime); |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 434 | return interest; |
| 435 | } |
| 436 | |
| 437 | static void |
Davide Pesavento | 31be3f6 | 2019-01-24 23:20:23 -0500 | [diff] [blame] | 438 | usage(const char* programName) |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 439 | { |
Davide Pesavento | 31be3f6 | 2019-01-24 23:20:23 -0500 | [diff] [blame] | 440 | std::cerr << "Usage: " |
| 441 | << programName << " [-u] [-s] [-D] [-d] [-i identity] [-I identity] [-x freshness]" |
| 442 | " [-l lifetime] [-w timeout] repo-prefix ndn-name filename\n" |
| 443 | << "\n" |
| 444 | << "Write a file into a repo.\n" |
| 445 | << "\n" |
| 446 | << " -u: unversioned: do not add a version component\n" |
| 447 | << " -s: single: do not add version or segment component, implies -u\n" |
| 448 | << " -D: use DigestSha256 signing method instead of SignatureSha256WithRsa\n" |
| 449 | << " -i: specify identity used for signing Data\n" |
| 450 | << " -I: specify identity used for signing commands\n" |
| 451 | << " -x: FreshnessPeriod in milliseconds\n" |
| 452 | << " -l: InterestLifetime in milliseconds for each command\n" |
| 453 | << " -w: timeout in milliseconds for whole process (default unlimited)\n" |
| 454 | << " -v: be verbose\n" |
| 455 | << " repo-prefix: repo command prefix\n" |
| 456 | << " ndn-name: NDN Name prefix for written Data\n" |
| 457 | << " filename: local file name; \"-\" reads from stdin\n" |
| 458 | << std::endl; |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 459 | } |
| 460 | |
Davide Pesavento | 31be3f6 | 2019-01-24 23:20:23 -0500 | [diff] [blame] | 461 | static int |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 462 | main(int argc, char** argv) |
| 463 | { |
| 464 | NdnPutFile ndnPutFile; |
Davide Pesavento | 31be3f6 | 2019-01-24 23:20:23 -0500 | [diff] [blame] | 465 | |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 466 | int opt; |
Davide Pesavento | 31be3f6 | 2019-01-24 23:20:23 -0500 | [diff] [blame] | 467 | while ((opt = getopt(argc, argv, "husDi:I:x:l:w:v")) != -1) { |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 468 | switch (opt) { |
Davide Pesavento | 31be3f6 | 2019-01-24 23:20:23 -0500 | [diff] [blame] | 469 | case 'h': |
| 470 | usage(argv[0]); |
| 471 | return 0; |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 472 | case 'u': |
| 473 | ndnPutFile.isUnversioned = true; |
| 474 | break; |
| 475 | case 's': |
| 476 | ndnPutFile.isSingle = true; |
| 477 | break; |
| 478 | case 'D': |
| 479 | ndnPutFile.useDigestSha256 = true; |
| 480 | break; |
| 481 | case 'i': |
| 482 | ndnPutFile.identityForData = std::string(optarg); |
| 483 | break; |
| 484 | case 'I': |
| 485 | ndnPutFile.identityForCommand = std::string(optarg); |
| 486 | break; |
| 487 | case 'x': |
| 488 | try { |
| 489 | ndnPutFile.freshnessPeriod = milliseconds(boost::lexical_cast<uint64_t>(optarg)); |
| 490 | } |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 491 | catch (const boost::bad_lexical_cast&) { |
Davide Pesavento | 9c0bd8d | 2022-03-14 16:48:12 -0400 | [diff] [blame] | 492 | std::cerr << "-x option should be an integer" << std::endl; |
Davide Pesavento | 31be3f6 | 2019-01-24 23:20:23 -0500 | [diff] [blame] | 493 | return 2; |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 494 | } |
| 495 | break; |
| 496 | case 'l': |
| 497 | try { |
| 498 | ndnPutFile.interestLifetime = milliseconds(boost::lexical_cast<uint64_t>(optarg)); |
| 499 | } |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 500 | catch (const boost::bad_lexical_cast&) { |
Davide Pesavento | 9c0bd8d | 2022-03-14 16:48:12 -0400 | [diff] [blame] | 501 | std::cerr << "-l option should be an integer" << std::endl; |
Davide Pesavento | 31be3f6 | 2019-01-24 23:20:23 -0500 | [diff] [blame] | 502 | return 2; |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 503 | } |
| 504 | break; |
| 505 | case 'w': |
| 506 | ndnPutFile.hasTimeout = true; |
| 507 | try { |
| 508 | ndnPutFile.timeout = milliseconds(boost::lexical_cast<uint64_t>(optarg)); |
| 509 | } |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 510 | catch (const boost::bad_lexical_cast&) { |
Davide Pesavento | 9c0bd8d | 2022-03-14 16:48:12 -0400 | [diff] [blame] | 511 | std::cerr << "-w option should be an integer" << std::endl; |
Davide Pesavento | 31be3f6 | 2019-01-24 23:20:23 -0500 | [diff] [blame] | 512 | return 2; |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 513 | } |
| 514 | break; |
| 515 | case 'v': |
| 516 | ndnPutFile.isVerbose = true; |
| 517 | break; |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 518 | default: |
Davide Pesavento | 31be3f6 | 2019-01-24 23:20:23 -0500 | [diff] [blame] | 519 | usage(argv[0]); |
| 520 | return 2; |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 521 | } |
| 522 | } |
| 523 | |
Davide Pesavento | 31be3f6 | 2019-01-24 23:20:23 -0500 | [diff] [blame] | 524 | if (argc != optind + 3) { |
| 525 | usage(argv[0]); |
| 526 | return 2; |
| 527 | } |
| 528 | |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 529 | argc -= optind; |
| 530 | argv += optind; |
| 531 | |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 532 | ndnPutFile.repoPrefix = Name(argv[0]); |
| 533 | ndnPutFile.ndnName = Name(argv[1]); |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 534 | if (strcmp(argv[2], "-") == 0) { |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 535 | ndnPutFile.insertStream = &std::cin; |
| 536 | ndnPutFile.run(); |
| 537 | } |
| 538 | else { |
| 539 | std::ifstream inputFileStream(argv[2], std::ios::in | std::ios::binary); |
| 540 | if (!inputFileStream.is_open()) { |
| 541 | std::cerr << "ERROR: cannot open " << argv[2] << std::endl; |
Davide Pesavento | 31be3f6 | 2019-01-24 23:20:23 -0500 | [diff] [blame] | 542 | return 2; |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | ndnPutFile.insertStream = &inputFileStream; |
| 546 | ndnPutFile.run(); |
| 547 | } |
| 548 | |
| 549 | // ndnPutFile MUST NOT be used anymore because .insertStream is a dangling pointer |
| 550 | |
| 551 | return 0; |
| 552 | } |
| 553 | |
| 554 | } // namespace repo |
| 555 | |
| 556 | int |
| 557 | main(int argc, char** argv) |
| 558 | { |
| 559 | try { |
| 560 | return repo::main(argc, argv); |
| 561 | } |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 562 | catch (const std::exception& e) { |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 563 | std::cerr << "ERROR: " << e.what() << std::endl; |
Davide Pesavento | 31be3f6 | 2019-01-24 23:20:23 -0500 | [diff] [blame] | 564 | return 1; |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 565 | } |
| 566 | } |