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 | 31be3f6 | 2019-01-24 23:20:23 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2019, 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> |
Alexander Afanasyev | 2e8147c | 2017-11-09 14:17:02 -0500 | [diff] [blame] | 24 | #include <ndn-cxx/security/command-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 | |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 158 | using DataContainer = std::map<uint64_t, shared_ptr<ndn::Data>>; |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 159 | DataContainer m_data; |
Alexander Afanasyev | 2e8147c | 2017-11-09 14:17:02 -0500 | [diff] [blame] | 160 | ndn::security::CommandInterestSigner m_cmdSigner; |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 161 | }; |
| 162 | |
| 163 | void |
| 164 | NdnPutFile::prepareNextData(uint64_t referenceSegmentNo) |
| 165 | { |
| 166 | // make sure m_data has [referenceSegmentNo, referenceSegmentNo + PRE_SIGN_DATA_COUNT] Data |
| 167 | if (m_isFinished) |
| 168 | return; |
| 169 | |
| 170 | size_t nDataToPrepare = PRE_SIGN_DATA_COUNT; |
| 171 | |
| 172 | if (!m_data.empty()) { |
| 173 | uint64_t maxSegmentNo = m_data.rbegin()->first; |
| 174 | |
| 175 | if (maxSegmentNo - referenceSegmentNo >= nDataToPrepare) { |
| 176 | // nothing to prepare |
| 177 | return; |
| 178 | } |
| 179 | |
| 180 | nDataToPrepare -= maxSegmentNo - referenceSegmentNo; |
| 181 | } |
| 182 | |
| 183 | for (size_t i = 0; i < nDataToPrepare && !m_isFinished; ++i) { |
| 184 | uint8_t buffer[DEFAULT_BLOCK_SIZE]; |
Davide Pesavento | 31be3f6 | 2019-01-24 23:20:23 -0500 | [diff] [blame] | 185 | auto readSize = boost::iostreams::read(*insertStream, |
| 186 | reinterpret_cast<char*>(buffer), DEFAULT_BLOCK_SIZE); |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 187 | if (readSize <= 0) { |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 188 | BOOST_THROW_EXCEPTION(Error("Error reading from the input stream")); |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 189 | } |
| 190 | |
Davide Pesavento | 31be3f6 | 2019-01-24 23:20:23 -0500 | [diff] [blame] | 191 | auto data = make_shared<ndn::Data>(Name(m_dataPrefix).appendSegment(m_currentSegmentNo)); |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 192 | |
| 193 | if (insertStream->peek() == std::istream::traits_type::eof()) { |
Davide Pesavento | 0c13951 | 2018-11-03 18:23:38 -0400 | [diff] [blame] | 194 | data->setFinalBlock(ndn::name::Component::fromSegment(m_currentSegmentNo)); |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 195 | m_isFinished = true; |
| 196 | } |
| 197 | |
| 198 | data->setContent(buffer, readSize); |
| 199 | data->setFreshnessPeriod(freshnessPeriod); |
| 200 | signData(*data); |
| 201 | |
| 202 | m_data.insert(std::make_pair(m_currentSegmentNo, data)); |
| 203 | |
| 204 | ++m_currentSegmentNo; |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | void |
| 209 | NdnPutFile::run() |
| 210 | { |
| 211 | m_dataPrefix = ndnName; |
| 212 | if (!isUnversioned) |
| 213 | m_dataPrefix.appendVersion(m_timestampVersion); |
| 214 | |
| 215 | if (isVerbose) |
| 216 | std::cerr << "setInterestFilter for " << m_dataPrefix << std::endl; |
| 217 | m_face.setInterestFilter(m_dataPrefix, |
| 218 | isSingle ? |
Wentao Shang | a8f3c40 | 2014-10-30 14:03:27 -0700 | [diff] [blame] | 219 | bind(&NdnPutFile::onSingleInterest, this, _1, _2) |
| 220 | : |
| 221 | bind(&NdnPutFile::onInterest, this, _1, _2), |
| 222 | bind(&NdnPutFile::onRegisterSuccess, this, _1), |
| 223 | bind(&NdnPutFile::onRegisterFailed, this, _1, _2)); |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 224 | |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 225 | if (hasTimeout) |
Davide Pesavento | 8891c83 | 2019-03-20 23:20:35 -0400 | [diff] [blame] | 226 | m_scheduler.schedule(timeout, [this] { stopProcess(); }); |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 227 | |
| 228 | m_face.processEvents(); |
| 229 | } |
| 230 | |
| 231 | void |
Wentao Shang | 91fb4f2 | 2014-05-20 10:55:22 -0700 | [diff] [blame] | 232 | NdnPutFile::onRegisterSuccess(const Name& prefix) |
| 233 | { |
| 234 | startInsertCommand(); |
| 235 | } |
| 236 | |
| 237 | void |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 238 | NdnPutFile::startInsertCommand() |
| 239 | { |
| 240 | RepoCommandParameter parameters; |
| 241 | parameters.setName(m_dataPrefix); |
| 242 | if (!isSingle) { |
| 243 | parameters.setStartBlockId(0); |
| 244 | } |
| 245 | |
| 246 | ndn::Interest commandInterest = generateCommandInterest(repoPrefix, "insert", parameters); |
| 247 | m_face.expressInterest(commandInterest, |
Wentao Shang | a8f3c40 | 2014-10-30 14:03:27 -0700 | [diff] [blame] | 248 | bind(&NdnPutFile::onInsertCommandResponse, this, _1, _2), |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 249 | bind(&NdnPutFile::onInsertCommandTimeout, this, _1), // Nack |
Wentao Shang | a8f3c40 | 2014-10-30 14:03:27 -0700 | [diff] [blame] | 250 | bind(&NdnPutFile::onInsertCommandTimeout, this, _1)); |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | void |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 254 | NdnPutFile::onInsertCommandResponse(const ndn::Interest& interest, const ndn::Data& data) |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 255 | { |
| 256 | RepoCommandResponse response(data.getContent().blockFromValue()); |
Davide Pesavento | 31be3f6 | 2019-01-24 23:20:23 -0500 | [diff] [blame] | 257 | auto statusCode = response.getCode(); |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 258 | if (statusCode >= 400) { |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 259 | BOOST_THROW_EXCEPTION(Error("insert command failed with code " + |
| 260 | boost::lexical_cast<std::string>(statusCode))); |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 261 | } |
| 262 | m_processId = response.getProcessId(); |
| 263 | |
Davide Pesavento | 8891c83 | 2019-03-20 23:20:35 -0400 | [diff] [blame] | 264 | m_scheduler.schedule(m_checkPeriod, [this] { startCheckCommand(); }); |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | void |
| 268 | NdnPutFile::onInsertCommandTimeout(const ndn::Interest& interest) |
| 269 | { |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 270 | BOOST_THROW_EXCEPTION(Error("command response timeout")); |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | void |
| 274 | NdnPutFile::onInterest(const ndn::Name& prefix, const ndn::Interest& interest) |
| 275 | { |
| 276 | if (interest.getName().size() != prefix.size() + 1) { |
| 277 | if (isVerbose) { |
| 278 | std::cerr << "Error processing incoming interest " << interest << ": " |
| 279 | << "Unrecognized Interest" << std::endl; |
| 280 | } |
| 281 | return; |
| 282 | } |
| 283 | |
| 284 | uint64_t segmentNo; |
| 285 | try { |
| 286 | ndn::Name::Component segmentComponent = interest.getName().get(prefix.size()); |
| 287 | segmentNo = segmentComponent.toSegment(); |
| 288 | } |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 289 | catch (const tlv::Error& e) { |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 290 | if (isVerbose) { |
| 291 | std::cerr << "Error processing incoming interest " << interest << ": " |
| 292 | << e.what() << std::endl; |
| 293 | } |
| 294 | return; |
| 295 | } |
| 296 | |
| 297 | prepareNextData(segmentNo); |
| 298 | |
| 299 | DataContainer::iterator item = m_data.find(segmentNo); |
| 300 | if (item == m_data.end()) { |
| 301 | if (isVerbose) { |
| 302 | std::cerr << "Requested segment [" << segmentNo << "] does not exist" << std::endl; |
| 303 | } |
| 304 | return; |
| 305 | } |
| 306 | |
Weiqi Shi | 5822e34 | 2014-08-21 20:05:30 -0700 | [diff] [blame] | 307 | if (m_isFinished) { |
| 308 | uint64_t final = m_currentSegmentNo - 1; |
Davide Pesavento | 0c13951 | 2018-11-03 18:23:38 -0400 | [diff] [blame] | 309 | item->second->setFinalBlock(ndn::name::Component::fromSegment(final)); |
Weiqi Shi | 5822e34 | 2014-08-21 20:05:30 -0700 | [diff] [blame] | 310 | } |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 311 | m_face.put(*item->second); |
| 312 | } |
| 313 | |
| 314 | void |
| 315 | NdnPutFile::onSingleInterest(const ndn::Name& prefix, const ndn::Interest& interest) |
| 316 | { |
| 317 | BOOST_ASSERT(prefix == m_dataPrefix); |
| 318 | |
| 319 | if (prefix != interest.getName()) { |
| 320 | if (isVerbose) { |
| 321 | std::cerr << "Received unexpected interest " << interest << std::endl; |
| 322 | } |
| 323 | return; |
| 324 | } |
| 325 | |
| 326 | uint8_t buffer[DEFAULT_BLOCK_SIZE]; |
| 327 | std::streamsize readSize = |
| 328 | boost::iostreams::read(*insertStream, reinterpret_cast<char*>(buffer), DEFAULT_BLOCK_SIZE); |
| 329 | |
| 330 | if (readSize <= 0) { |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 331 | BOOST_THROW_EXCEPTION(Error("Error reading from the input stream")); |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | if (insertStream->peek() != std::istream::traits_type::eof()) { |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 335 | BOOST_THROW_EXCEPTION(Error("Input data does not fit into one Data packet")); |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 336 | } |
| 337 | |
Davide Pesavento | 31be3f6 | 2019-01-24 23:20:23 -0500 | [diff] [blame] | 338 | auto data = make_shared<ndn::Data>(m_dataPrefix); |
Weiqi Shi | 5822e34 | 2014-08-21 20:05:30 -0700 | [diff] [blame] | 339 | data->setContent(buffer, readSize); |
| 340 | data->setFreshnessPeriod(freshnessPeriod); |
| 341 | signData(*data); |
| 342 | m_face.put(*data); |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 343 | |
| 344 | m_isFinished = true; |
| 345 | } |
| 346 | |
| 347 | void |
| 348 | NdnPutFile::onRegisterFailed(const ndn::Name& prefix, const std::string& reason) |
| 349 | { |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 350 | BOOST_THROW_EXCEPTION(Error("onRegisterFailed: " + reason)); |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | void |
| 354 | NdnPutFile::stopProcess() |
| 355 | { |
| 356 | m_face.getIoService().stop(); |
| 357 | } |
| 358 | |
| 359 | void |
| 360 | NdnPutFile::signData(ndn::Data& data) |
| 361 | { |
| 362 | if (useDigestSha256) { |
Junxiao Shi | 047a6fb | 2017-06-08 16:16:05 +0000 | [diff] [blame] | 363 | m_keyChain.sign(data, ndn::signingWithSha256()); |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 364 | } |
Junxiao Shi | 047a6fb | 2017-06-08 16:16:05 +0000 | [diff] [blame] | 365 | else if (identityForData.empty()) |
| 366 | m_keyChain.sign(data); |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 367 | else { |
Junxiao Shi | 047a6fb | 2017-06-08 16:16:05 +0000 | [diff] [blame] | 368 | m_keyChain.sign(data, ndn::signingByIdentity(identityForData)); |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 369 | } |
| 370 | } |
| 371 | |
| 372 | void |
| 373 | NdnPutFile::startCheckCommand() |
| 374 | { |
| 375 | ndn::Interest checkInterest = generateCommandInterest(repoPrefix, "insert check", |
| 376 | RepoCommandParameter() |
| 377 | .setProcessId(m_processId)); |
| 378 | m_face.expressInterest(checkInterest, |
Wentao Shang | a8f3c40 | 2014-10-30 14:03:27 -0700 | [diff] [blame] | 379 | bind(&NdnPutFile::onCheckCommandResponse, this, _1, _2), |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 380 | bind(&NdnPutFile::onCheckCommandTimeout, this, _1), // Nack |
Wentao Shang | a8f3c40 | 2014-10-30 14:03:27 -0700 | [diff] [blame] | 381 | bind(&NdnPutFile::onCheckCommandTimeout, this, _1)); |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | void |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 385 | NdnPutFile::onCheckCommandResponse(const ndn::Interest& interest, const ndn::Data& data) |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 386 | { |
| 387 | RepoCommandResponse response(data.getContent().blockFromValue()); |
Davide Pesavento | 31be3f6 | 2019-01-24 23:20:23 -0500 | [diff] [blame] | 388 | auto statusCode = response.getCode(); |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 389 | if (statusCode >= 400) { |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 390 | BOOST_THROW_EXCEPTION(Error("Insert check command failed with code: " + |
| 391 | boost::lexical_cast<std::string>(statusCode))); |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | if (m_isFinished) { |
| 395 | uint64_t insertCount = response.getInsertNum(); |
| 396 | |
| 397 | if (isSingle) { |
| 398 | if (insertCount == 1) { |
| 399 | m_face.getIoService().stop(); |
| 400 | return; |
| 401 | } |
| 402 | } |
| 403 | // Technically, the check should not infer, but directly has signal from repo that |
| 404 | // write operation has been finished |
| 405 | |
| 406 | if (insertCount == m_currentSegmentNo) { |
| 407 | m_face.getIoService().stop(); |
| 408 | return; |
| 409 | } |
| 410 | } |
| 411 | |
Davide Pesavento | 8891c83 | 2019-03-20 23:20:35 -0400 | [diff] [blame] | 412 | m_scheduler.schedule(m_checkPeriod, [this] { startCheckCommand(); }); |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | void |
| 416 | NdnPutFile::onCheckCommandTimeout(const ndn::Interest& interest) |
| 417 | { |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 418 | BOOST_THROW_EXCEPTION(Error("check response timeout")); |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | ndn::Interest |
| 422 | NdnPutFile::generateCommandInterest(const ndn::Name& commandPrefix, const std::string& command, |
| 423 | const RepoCommandParameter& commandParameter) |
| 424 | { |
Alexander Afanasyev | 2e8147c | 2017-11-09 14:17:02 -0500 | [diff] [blame] | 425 | Name cmd = commandPrefix; |
| 426 | cmd |
| 427 | .append(command) |
| 428 | .append(commandParameter.wireEncode()); |
| 429 | ndn::Interest interest; |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 430 | |
| 431 | if (identityForCommand.empty()) |
Alexander Afanasyev | 2e8147c | 2017-11-09 14:17:02 -0500 | [diff] [blame] | 432 | interest = m_cmdSigner.makeCommandInterest(cmd); |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 433 | else { |
Alexander Afanasyev | 2e8147c | 2017-11-09 14:17:02 -0500 | [diff] [blame] | 434 | interest = m_cmdSigner.makeCommandInterest(cmd, ndn::signingByIdentity(identityForCommand)); |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 435 | } |
| 436 | |
Alexander Afanasyev | 2e8147c | 2017-11-09 14:17:02 -0500 | [diff] [blame] | 437 | interest.setInterestLifetime(interestLifetime); |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 438 | return interest; |
| 439 | } |
| 440 | |
| 441 | static void |
Davide Pesavento | 31be3f6 | 2019-01-24 23:20:23 -0500 | [diff] [blame] | 442 | usage(const char* programName) |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 443 | { |
Davide Pesavento | 31be3f6 | 2019-01-24 23:20:23 -0500 | [diff] [blame] | 444 | std::cerr << "Usage: " |
| 445 | << programName << " [-u] [-s] [-D] [-d] [-i identity] [-I identity] [-x freshness]" |
| 446 | " [-l lifetime] [-w timeout] repo-prefix ndn-name filename\n" |
| 447 | << "\n" |
| 448 | << "Write a file into a repo.\n" |
| 449 | << "\n" |
| 450 | << " -u: unversioned: do not add a version component\n" |
| 451 | << " -s: single: do not add version or segment component, implies -u\n" |
| 452 | << " -D: use DigestSha256 signing method instead of SignatureSha256WithRsa\n" |
| 453 | << " -i: specify identity used for signing Data\n" |
| 454 | << " -I: specify identity used for signing commands\n" |
| 455 | << " -x: FreshnessPeriod in milliseconds\n" |
| 456 | << " -l: InterestLifetime in milliseconds for each command\n" |
| 457 | << " -w: timeout in milliseconds for whole process (default unlimited)\n" |
| 458 | << " -v: be verbose\n" |
| 459 | << " repo-prefix: repo command prefix\n" |
| 460 | << " ndn-name: NDN Name prefix for written Data\n" |
| 461 | << " filename: local file name; \"-\" reads from stdin\n" |
| 462 | << std::endl; |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 463 | } |
| 464 | |
Davide Pesavento | 31be3f6 | 2019-01-24 23:20:23 -0500 | [diff] [blame] | 465 | static int |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 466 | main(int argc, char** argv) |
| 467 | { |
| 468 | NdnPutFile ndnPutFile; |
Davide Pesavento | 31be3f6 | 2019-01-24 23:20:23 -0500 | [diff] [blame] | 469 | |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 470 | int opt; |
Davide Pesavento | 31be3f6 | 2019-01-24 23:20:23 -0500 | [diff] [blame] | 471 | while ((opt = getopt(argc, argv, "husDi:I:x:l:w:v")) != -1) { |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 472 | switch (opt) { |
Davide Pesavento | 31be3f6 | 2019-01-24 23:20:23 -0500 | [diff] [blame] | 473 | case 'h': |
| 474 | usage(argv[0]); |
| 475 | return 0; |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 476 | case 'u': |
| 477 | ndnPutFile.isUnversioned = true; |
| 478 | break; |
| 479 | case 's': |
| 480 | ndnPutFile.isSingle = true; |
| 481 | break; |
| 482 | case 'D': |
| 483 | ndnPutFile.useDigestSha256 = true; |
| 484 | break; |
| 485 | case 'i': |
| 486 | ndnPutFile.identityForData = std::string(optarg); |
| 487 | break; |
| 488 | case 'I': |
| 489 | ndnPutFile.identityForCommand = std::string(optarg); |
| 490 | break; |
| 491 | case 'x': |
| 492 | try { |
| 493 | ndnPutFile.freshnessPeriod = milliseconds(boost::lexical_cast<uint64_t>(optarg)); |
| 494 | } |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 495 | catch (const boost::bad_lexical_cast&) { |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 496 | std::cerr << "-x option should be an integer" << std::endl;; |
Davide Pesavento | 31be3f6 | 2019-01-24 23:20:23 -0500 | [diff] [blame] | 497 | return 2; |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 498 | } |
| 499 | break; |
| 500 | case 'l': |
| 501 | try { |
| 502 | ndnPutFile.interestLifetime = milliseconds(boost::lexical_cast<uint64_t>(optarg)); |
| 503 | } |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 504 | catch (const boost::bad_lexical_cast&) { |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 505 | std::cerr << "-l option should be an integer" << std::endl;; |
Davide Pesavento | 31be3f6 | 2019-01-24 23:20:23 -0500 | [diff] [blame] | 506 | return 2; |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 507 | } |
| 508 | break; |
| 509 | case 'w': |
| 510 | ndnPutFile.hasTimeout = true; |
| 511 | try { |
| 512 | ndnPutFile.timeout = milliseconds(boost::lexical_cast<uint64_t>(optarg)); |
| 513 | } |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 514 | catch (const boost::bad_lexical_cast&) { |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 515 | std::cerr << "-w option should be an integer" << std::endl;; |
Davide Pesavento | 31be3f6 | 2019-01-24 23:20:23 -0500 | [diff] [blame] | 516 | return 2; |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 517 | } |
| 518 | break; |
| 519 | case 'v': |
| 520 | ndnPutFile.isVerbose = true; |
| 521 | break; |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 522 | default: |
Davide Pesavento | 31be3f6 | 2019-01-24 23:20:23 -0500 | [diff] [blame] | 523 | usage(argv[0]); |
| 524 | return 2; |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 525 | } |
| 526 | } |
| 527 | |
Davide Pesavento | 31be3f6 | 2019-01-24 23:20:23 -0500 | [diff] [blame] | 528 | if (argc != optind + 3) { |
| 529 | usage(argv[0]); |
| 530 | return 2; |
| 531 | } |
| 532 | |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 533 | argc -= optind; |
| 534 | argv += optind; |
| 535 | |
weijia yuan | 3aa8d2b | 2018-03-06 15:35:57 -0800 | [diff] [blame] | 536 | ndnPutFile.repoPrefix = Name(argv[0]); |
| 537 | ndnPutFile.ndnName = Name(argv[1]); |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 538 | if (strcmp(argv[2], "-") == 0) { |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 539 | ndnPutFile.insertStream = &std::cin; |
| 540 | ndnPutFile.run(); |
| 541 | } |
| 542 | else { |
| 543 | std::ifstream inputFileStream(argv[2], std::ios::in | std::ios::binary); |
| 544 | if (!inputFileStream.is_open()) { |
| 545 | std::cerr << "ERROR: cannot open " << argv[2] << std::endl; |
Davide Pesavento | 31be3f6 | 2019-01-24 23:20:23 -0500 | [diff] [blame] | 546 | return 2; |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | ndnPutFile.insertStream = &inputFileStream; |
| 550 | ndnPutFile.run(); |
| 551 | } |
| 552 | |
| 553 | // ndnPutFile MUST NOT be used anymore because .insertStream is a dangling pointer |
| 554 | |
| 555 | return 0; |
| 556 | } |
| 557 | |
| 558 | } // namespace repo |
| 559 | |
| 560 | int |
| 561 | main(int argc, char** argv) |
| 562 | { |
| 563 | try { |
| 564 | return repo::main(argc, argv); |
| 565 | } |
Alexander Afanasyev | 42290b2 | 2017-03-09 12:58:29 -0800 | [diff] [blame] | 566 | catch (const std::exception& e) { |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 567 | std::cerr << "ERROR: " << e.what() << std::endl; |
Davide Pesavento | 31be3f6 | 2019-01-24 23:20:23 -0500 | [diff] [blame] | 568 | return 1; |
Shuo Chen | ccfbe24 | 2014-04-29 23:57:51 +0800 | [diff] [blame] | 569 | } |
| 570 | } |