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