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 | |
spirosmastorakis | d351c6b | 2016-05-06 17:02:48 -0700 | [diff] [blame^] | 53 | namespace ntorrent { |
| 54 | |
| 55 | const char * SharedConstants::commonPrefix = "/ndn"; |
| 56 | |
| 57 | } // end ntorrent |
Mickey Sweatt | f91ced4 | 2016-04-20 13:30:37 -0700 | [diff] [blame] | 58 | } // end ndn |
| 59 | |
| 60 | // TODO(msweatt) Add options verification |
| 61 | int main(int argc, char *argv[]) |
| 62 | { |
| 63 | try { |
Mickey Sweatt | 617d2d4 | 2016-04-25 22:02:08 -0700 | [diff] [blame] | 64 | LoggingUtil::init(); |
| 65 | logging::add_common_attributes(); |
| 66 | |
Mickey Sweatt | f91ced4 | 2016-04-20 13:30:37 -0700 | [diff] [blame] | 67 | po::options_description desc("Allowed options"); |
| 68 | desc.add_options() |
| 69 | // TODO(msweatt) Consider adding flagged args for other parameters |
| 70 | ("help,h", "produce help message") |
| 71 | ("generate,g" , "-g <data directory> <output-path>? <names-per-segment>? <names-per-manifest-segment>? <data-packet-size>?") |
| 72 | ("seed,s", "After download completes, continue to seed") |
Mickey Sweatt | 15dde2d | 2016-04-28 23:42:45 -0700 | [diff] [blame] | 73 | ("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] | 74 | ("args", po::value<std::vector<std::string> >(), "For arguments you want to specify without flags") |
| 75 | ; |
| 76 | po::positional_options_description p; |
| 77 | p.add("args", -1); |
| 78 | |
| 79 | po::variables_map vm; |
| 80 | po::store(po::command_line_parser(argc, argv). |
| 81 | options(desc).allow_unregistered().positional(p).run(), vm); |
| 82 | po::notify(vm); |
| 83 | |
| 84 | if (vm.count("help")) { |
| 85 | std::cout << desc << std::endl; |
| 86 | return 1; |
| 87 | } |
Mickey Sweatt | 15dde2d | 2016-04-28 23:42:45 -0700 | [diff] [blame] | 88 | if (vm.count("args")) { |
Mickey Sweatt | f91ced4 | 2016-04-20 13:30:37 -0700 | [diff] [blame] | 89 | auto args = vm["args"].as<std::vector<std::string>>(); |
Mickey Sweatt | 15dde2d | 2016-04-28 23:42:45 -0700 | [diff] [blame] | 90 | // if generate mode |
| 91 | if (vm.count("generate")) { |
| 92 | if (args.size() < 1 || args.size() > 5) { |
| 93 | throw ndn::Error("wrong number of arguments for generate"); |
| 94 | } |
| 95 | auto dataPath = args[0]; |
| 96 | auto outputPath = args.size() >= 2 ? args[1] : ".appdata/"; |
| 97 | auto namesPerSegment = args.size() >= 3 ? boost::lexical_cast<size_t>(args[2]) : 1024; |
| 98 | auto namesPerManifest = args.size() >= 4 ? boost::lexical_cast<size_t>(args[3]) : 1024; |
| 99 | 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] | 100 | |
Mickey Sweatt | 15dde2d | 2016-04-28 23:42:45 -0700 | [diff] [blame] | 101 | const auto& content = TorrentFile::generate(dataPath, |
| 102 | namesPerSegment, |
| 103 | namesPerManifest, |
| 104 | dataPacketSize); |
| 105 | const auto& torrentSegments = content.first; |
| 106 | std::vector<FileManifest> manifests; |
| 107 | for (const auto& ms : content.second) { |
| 108 | manifests.insert(manifests.end(), ms.first.begin(), ms.first.end()); |
| 109 | } |
| 110 | auto torrentPrefix = fs::canonical(dataPath).filename().string(); |
Mickey Sweatt | 0dc0a1e | 2016-05-04 11:25:49 -0700 | [diff] [blame] | 111 | outputPath += ("/" + torrentPrefix); |
Mickey Sweatt | 15dde2d | 2016-04-28 23:42:45 -0700 | [diff] [blame] | 112 | auto torrentPath = outputPath + "/torrent_files/"; |
| 113 | // write all the torrent segments |
| 114 | for (const TorrentFile& t : torrentSegments) { |
| 115 | if (!IoUtil::writeTorrentSegment(t, torrentPath)) { |
| 116 | LOG_ERROR << "Write failed: " << t.getName() << std::endl; |
| 117 | return -1; |
| 118 | } |
| 119 | } |
| 120 | auto manifestPath = outputPath + "/manifests/"; |
| 121 | for (const FileManifest& m : manifests) { |
| 122 | if (!IoUtil::writeFileManifest(m, manifestPath)) { |
| 123 | LOG_ERROR << "Write failed: " << m.getName() << std::endl; |
| 124 | return -1; |
| 125 | } |
Mickey Sweatt | f91ced4 | 2016-04-20 13:30:37 -0700 | [diff] [blame] | 126 | } |
| 127 | } |
Mickey Sweatt | 15dde2d | 2016-04-28 23:42:45 -0700 | [diff] [blame] | 128 | // if dump mode |
| 129 | else if(vm.count("dump")) { |
| 130 | if (args.size() != 1) { |
| 131 | throw ndn::Error("wrong number of arguments for dump"); |
| 132 | } |
| 133 | auto filePath = args[0]; |
| 134 | auto data = io::load<Data>(filePath); |
| 135 | if (nullptr != data) { |
| 136 | std::cout << data->getFullName() << std::endl; |
| 137 | } |
| 138 | else { |
| 139 | throw ndn::Error("Invalid data."); |
Mickey Sweatt | f91ced4 | 2016-04-20 13:30:37 -0700 | [diff] [blame] | 140 | } |
| 141 | } |
Mickey Sweatt | 15dde2d | 2016-04-28 23:42:45 -0700 | [diff] [blame] | 142 | // standard torrent mode |
| 143 | else { |
| 144 | // <torrent-file-name> <data-path> |
| 145 | if (args.size() != 2) { |
| 146 | throw ndn::Error("wrong number of arguments for torrent"); |
| 147 | } |
| 148 | auto torrentName = args[0]; |
Mickey Sweatt | 44e4fd9 | 2016-05-02 15:43:11 -0700 | [diff] [blame] | 149 | auto dataPath = args[1]; |
| 150 | auto seedFlag = (vm.count("seed") != 0); |
| 151 | SequentialDataFetcher fetcher(torrentName, dataPath, seedFlag); |
Mickey Sweatt | 15dde2d | 2016-04-28 23:42:45 -0700 | [diff] [blame] | 152 | fetcher.start(); |
Mickey Sweatt | f91ced4 | 2016-04-20 13:30:37 -0700 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | else { |
| 156 | std::cout << desc << std::endl; |
| 157 | return 1; |
| 158 | } |
| 159 | } |
| 160 | catch(std::exception& e) { |
| 161 | std::cerr << "error: " << e.what() << "\n"; |
| 162 | return 1; |
| 163 | } |
| 164 | catch(...) { |
| 165 | std::cerr << "Exception of unknown type!\n"; |
| 166 | } |
| 167 | return 0; |
Mickey Sweatt | 15dde2d | 2016-04-28 23:42:45 -0700 | [diff] [blame] | 168 | } |