Mickey Sweatt | f91ced4 | 2016-04-20 13:30:37 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2016 Regents of the University of California. |
| 4 | * |
| 5 | * This file is part of the nTorrent codebase. |
| 6 | * |
| 7 | * nTorrent is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * nTorrent is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with nTorrent, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of nTorrent authors and contributors. |
| 20 | */ |
Mickey Sweatt | f91ced4 | 2016-04-20 13:30:37 -0700 | [diff] [blame] | 21 | #include "sequential-data-fetcher.hpp" |
| 22 | #include "torrent-file.hpp" |
| 23 | #include "util/io-util.hpp" |
Mickey Sweatt | 617d2d4 | 2016-04-25 22:02:08 -0700 | [diff] [blame] | 24 | #include "util/logging.hpp" |
Mickey Sweatt | f91ced4 | 2016-04-20 13:30:37 -0700 | [diff] [blame] | 25 | |
| 26 | #include <iostream> |
| 27 | #include <iterator> |
| 28 | #include <stdexcept> |
| 29 | #include <algorithm> |
| 30 | |
| 31 | #include <boost/program_options.hpp> |
| 32 | #include <boost/program_options/errors.hpp> |
Mickey Sweatt | 617d2d4 | 2016-04-25 22:02:08 -0700 | [diff] [blame] | 33 | #include <boost/log/utility/setup/common_attributes.hpp> |
Mickey Sweatt | f91ced4 | 2016-04-20 13:30:37 -0700 | [diff] [blame] | 34 | |
Mickey Sweatt | 617d2d4 | 2016-04-25 22:02:08 -0700 | [diff] [blame] | 35 | namespace logging = boost::log; |
Mickey Sweatt | f91ced4 | 2016-04-20 13:30:37 -0700 | [diff] [blame] | 36 | namespace po = boost::program_options; |
Mickey Sweatt | 617d2d4 | 2016-04-25 22:02:08 -0700 | [diff] [blame] | 37 | |
Mickey Sweatt | 15dde2d | 2016-04-28 23:42:45 -0700 | [diff] [blame] | 38 | using namespace ndn; |
Mickey Sweatt | f91ced4 | 2016-04-20 13:30:37 -0700 | [diff] [blame] | 39 | using namespace ndn::ntorrent; |
| 40 | |
| 41 | namespace ndn { |
| 42 | |
| 43 | class Error : public std::runtime_error |
| 44 | { |
| 45 | public: |
| 46 | explicit |
| 47 | Error(const std::string& what) |
| 48 | : runtime_error(what) |
| 49 | { |
| 50 | } |
| 51 | }; |
| 52 | |
| 53 | } // end ndn |
| 54 | |
| 55 | // TODO(msweatt) Add options verification |
| 56 | int main(int argc, char *argv[]) |
| 57 | { |
| 58 | try { |
Mickey Sweatt | 617d2d4 | 2016-04-25 22:02:08 -0700 | [diff] [blame] | 59 | LoggingUtil::init(); |
| 60 | logging::add_common_attributes(); |
| 61 | |
Mickey Sweatt | f91ced4 | 2016-04-20 13:30:37 -0700 | [diff] [blame] | 62 | po::options_description desc("Allowed options"); |
| 63 | desc.add_options() |
| 64 | // TODO(msweatt) Consider adding flagged args for other parameters |
| 65 | ("help,h", "produce help message") |
| 66 | ("generate,g" , "-g <data directory> <output-path>? <names-per-segment>? <names-per-manifest-segment>? <data-packet-size>?") |
| 67 | ("seed,s", "After download completes, continue to seed") |
Mickey Sweatt | 15dde2d | 2016-04-28 23:42:45 -0700 | [diff] [blame] | 68 | ("dump,d", "-d <file> Dump the contents of the Data stored at the <file>.") |
Mickey Sweatt | f91ced4 | 2016-04-20 13:30:37 -0700 | [diff] [blame] | 69 | ("args", po::value<std::vector<std::string> >(), "For arguments you want to specify without flags") |
| 70 | ; |
| 71 | po::positional_options_description p; |
| 72 | p.add("args", -1); |
| 73 | |
| 74 | po::variables_map vm; |
| 75 | po::store(po::command_line_parser(argc, argv). |
| 76 | options(desc).allow_unregistered().positional(p).run(), vm); |
| 77 | po::notify(vm); |
| 78 | |
| 79 | if (vm.count("help")) { |
| 80 | std::cout << desc << std::endl; |
| 81 | return 1; |
| 82 | } |
Mickey Sweatt | 15dde2d | 2016-04-28 23:42:45 -0700 | [diff] [blame] | 83 | if (vm.count("args")) { |
Mickey Sweatt | f91ced4 | 2016-04-20 13:30:37 -0700 | [diff] [blame] | 84 | auto args = vm["args"].as<std::vector<std::string>>(); |
Mickey Sweatt | 15dde2d | 2016-04-28 23:42:45 -0700 | [diff] [blame] | 85 | // if generate mode |
| 86 | if (vm.count("generate")) { |
| 87 | if (args.size() < 1 || args.size() > 5) { |
| 88 | throw ndn::Error("wrong number of arguments for generate"); |
| 89 | } |
| 90 | auto dataPath = args[0]; |
| 91 | auto outputPath = args.size() >= 2 ? args[1] : ".appdata/"; |
| 92 | auto namesPerSegment = args.size() >= 3 ? boost::lexical_cast<size_t>(args[2]) : 1024; |
| 93 | auto namesPerManifest = args.size() >= 4 ? boost::lexical_cast<size_t>(args[3]) : 1024; |
| 94 | auto dataPacketSize = args.size() == 5 ? boost::lexical_cast<size_t>(args[4]) : 1024; |
Mickey Sweatt | f91ced4 | 2016-04-20 13:30:37 -0700 | [diff] [blame] | 95 | |
Mickey Sweatt | 15dde2d | 2016-04-28 23:42:45 -0700 | [diff] [blame] | 96 | const auto& content = TorrentFile::generate(dataPath, |
| 97 | namesPerSegment, |
| 98 | namesPerManifest, |
| 99 | dataPacketSize); |
| 100 | const auto& torrentSegments = content.first; |
| 101 | std::vector<FileManifest> manifests; |
| 102 | for (const auto& ms : content.second) { |
| 103 | manifests.insert(manifests.end(), ms.first.begin(), ms.first.end()); |
| 104 | } |
| 105 | auto torrentPrefix = fs::canonical(dataPath).filename().string(); |
| 106 | outputPath += torrentPrefix; |
| 107 | auto torrentPath = outputPath + "/torrent_files/"; |
| 108 | // write all the torrent segments |
| 109 | for (const TorrentFile& t : torrentSegments) { |
| 110 | if (!IoUtil::writeTorrentSegment(t, torrentPath)) { |
| 111 | LOG_ERROR << "Write failed: " << t.getName() << std::endl; |
| 112 | return -1; |
| 113 | } |
| 114 | } |
| 115 | auto manifestPath = outputPath + "/manifests/"; |
| 116 | for (const FileManifest& m : manifests) { |
| 117 | if (!IoUtil::writeFileManifest(m, manifestPath)) { |
| 118 | LOG_ERROR << "Write failed: " << m.getName() << std::endl; |
| 119 | return -1; |
| 120 | } |
Mickey Sweatt | f91ced4 | 2016-04-20 13:30:37 -0700 | [diff] [blame] | 121 | } |
| 122 | } |
Mickey Sweatt | 15dde2d | 2016-04-28 23:42:45 -0700 | [diff] [blame] | 123 | // if dump mode |
| 124 | else if(vm.count("dump")) { |
| 125 | if (args.size() != 1) { |
| 126 | throw ndn::Error("wrong number of arguments for dump"); |
| 127 | } |
| 128 | auto filePath = args[0]; |
| 129 | auto data = io::load<Data>(filePath); |
| 130 | if (nullptr != data) { |
| 131 | std::cout << data->getFullName() << std::endl; |
| 132 | } |
| 133 | else { |
| 134 | throw ndn::Error("Invalid data."); |
Mickey Sweatt | f91ced4 | 2016-04-20 13:30:37 -0700 | [diff] [blame] | 135 | } |
| 136 | } |
Mickey Sweatt | 15dde2d | 2016-04-28 23:42:45 -0700 | [diff] [blame] | 137 | // standard torrent mode |
| 138 | else { |
| 139 | // <torrent-file-name> <data-path> |
| 140 | if (args.size() != 2) { |
| 141 | throw ndn::Error("wrong number of arguments for torrent"); |
| 142 | } |
| 143 | auto torrentName = args[0]; |
Mickey Sweatt | 44e4fd9 | 2016-05-02 15:43:11 -0700 | [diff] [blame^] | 144 | auto dataPath = args[1]; |
| 145 | auto seedFlag = (vm.count("seed") != 0); |
| 146 | SequentialDataFetcher fetcher(torrentName, dataPath, seedFlag); |
Mickey Sweatt | 15dde2d | 2016-04-28 23:42:45 -0700 | [diff] [blame] | 147 | fetcher.start(); |
Mickey Sweatt | f91ced4 | 2016-04-20 13:30:37 -0700 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | else { |
| 151 | std::cout << desc << std::endl; |
| 152 | return 1; |
| 153 | } |
| 154 | } |
| 155 | catch(std::exception& e) { |
| 156 | std::cerr << "error: " << e.what() << "\n"; |
| 157 | return 1; |
| 158 | } |
| 159 | catch(...) { |
| 160 | std::cerr << "Exception of unknown type!\n"; |
| 161 | } |
| 162 | return 0; |
Mickey Sweatt | 15dde2d | 2016-04-28 23:42:45 -0700 | [diff] [blame] | 163 | } |