blob: 5500adae5ef04a30761679a2bb61fbc77dd68799 [file] [log] [blame]
Mickey Sweattf91ced42016-04-20 13:30:37 -07001/* -*- 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 */
21
22
23#include "sequential-data-fetcher.hpp"
24#include "torrent-file.hpp"
25#include "util/io-util.hpp"
Mickey Sweatt617d2d42016-04-25 22:02:08 -070026#include "util/logging.hpp"
Mickey Sweattf91ced42016-04-20 13:30:37 -070027
28#include <iostream>
29#include <iterator>
30#include <stdexcept>
31#include <algorithm>
32
33#include <boost/program_options.hpp>
34#include <boost/program_options/errors.hpp>
Mickey Sweatt617d2d42016-04-25 22:02:08 -070035#include <boost/log/utility/setup/common_attributes.hpp>
Mickey Sweattf91ced42016-04-20 13:30:37 -070036
Mickey Sweatt617d2d42016-04-25 22:02:08 -070037namespace logging = boost::log;
Mickey Sweattf91ced42016-04-20 13:30:37 -070038namespace po = boost::program_options;
Mickey Sweatt617d2d42016-04-25 22:02:08 -070039
Mickey Sweattf91ced42016-04-20 13:30:37 -070040using namespace ndn::ntorrent;
41
42namespace ndn {
43
44class Error : public std::runtime_error
45{
46public:
47 explicit
48 Error(const std::string& what)
49 : runtime_error(what)
50 {
51 }
52};
53
54} // end ndn
55
56// TODO(msweatt) Add options verification
57int main(int argc, char *argv[])
58{
59 try {
Mickey Sweatt617d2d42016-04-25 22:02:08 -070060 LoggingUtil::init();
61 logging::add_common_attributes();
62
Mickey Sweattf91ced42016-04-20 13:30:37 -070063 po::options_description desc("Allowed options");
64 desc.add_options()
65 // TODO(msweatt) Consider adding flagged args for other parameters
66 ("help,h", "produce help message")
67 ("generate,g" , "-g <data directory> <output-path>? <names-per-segment>? <names-per-manifest-segment>? <data-packet-size>?")
68 ("seed,s", "After download completes, continue to seed")
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 }
83 // if generate mode
84 if (vm.count("generate") && vm.count("args")) {
85 auto args = vm["args"].as<std::vector<std::string>>();
86 if (args.size() < 1 || args.size() > 5) {
87 throw ndn::Error("wrong number of arguments for generate");
88 }
89 auto dataPath = args[0];
90 auto outputPath = args.size() >= 2 ? args[1] : ".appdata/";
91 auto namesPerSegment = args.size() >= 3 ? boost::lexical_cast<size_t>(args[2]) : 1024;
92 auto namesPerManifest = args.size() >= 4 ? boost::lexical_cast<size_t>(args[3]) : 1024;
93 auto dataPacketSize = args.size() == 5 ? boost::lexical_cast<size_t>(args[4]) : 1024;
94
95 const auto& content = TorrentFile::generate(dataPath,
96 namesPerSegment,
97 namesPerManifest,
98 dataPacketSize);
99 const auto& torrentSegments = content.first;
100 std::vector<FileManifest> manifests;
101 for (const auto& ms : content.second) {
102 manifests.insert(manifests.end(), ms.first.begin(), ms.first.end());
103 }
104 auto torrentPrefix = fs::canonical(dataPath).filename().string();
105 outputPath += torrentPrefix;
106 auto torrentPath = outputPath + "/torrent_files/";
107 // write all the torrent segments
108 for (const TorrentFile& t : torrentSegments) {
109 if (!IoUtil::writeTorrentSegment(t, torrentPath)) {
Mickey Sweatt617d2d42016-04-25 22:02:08 -0700110 LOG_ERROR << "Write failed: " << t.getName() << std::endl;
Mickey Sweattf91ced42016-04-20 13:30:37 -0700111 return -1;
112 }
113 }
114 auto manifestPath = outputPath + "/manifests/";
115 for (const FileManifest& m : manifests) {
116 if (!IoUtil::writeFileManifest(m, manifestPath)) {
Mickey Sweatt617d2d42016-04-25 22:02:08 -0700117 LOG_ERROR << "Write failed: " << m.getName() << std::endl;
Mickey Sweattf91ced42016-04-20 13:30:37 -0700118 return -1;
119 }
120 }
121 }
122 // otherwise we are in torrent mode if we have the required args, start the sequential fetcher
123 else if (vm.count("args")) {
124 // <torrent-file-name> <data-path>
125 auto args = vm["args"].as<std::vector<std::string>>();
126 if (args.size() != 2) {
127 throw ndn::Error("wrong number of arguments for generate");
128 }
129 auto torrentName = args[0];
130 auto dataPath = args[1];
131 SequentialDataFetcher fetcher(torrentName, dataPath);
132 // download all the code
133 fetcher.start();
134 std::cout << torrentName << " done" << std::endl;
135 if (vm.count("seed")) {
136 fetcher.seed();
137 }
138 }
139 else {
140 std::cout << desc << std::endl;
141 return 1;
142 }
143 }
144 catch(std::exception& e) {
145 std::cerr << "error: " << e.what() << "\n";
146 return 1;
147 }
148 catch(...) {
149 std::cerr << "Exception of unknown type!\n";
150 }
151 return 0;
152}