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