blob: 8ae511f4f7eb0505d9099d2a71686e5ae88e7ae9 [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"
26
27#include <iostream>
28#include <iterator>
29#include <stdexcept>
30#include <algorithm>
31
32#include <boost/program_options.hpp>
33#include <boost/program_options/errors.hpp>
34
35namespace po = boost::program_options;
36using namespace ndn::ntorrent;
37
38namespace ndn {
39
40class Error : public std::runtime_error
41{
42public:
43 explicit
44 Error(const std::string& what)
45 : runtime_error(what)
46 {
47 }
48};
49
50} // end ndn
51
52// TODO(msweatt) Add options verification
53int main(int argc, char *argv[])
54{
55 try {
56 po::options_description desc("Allowed options");
57 desc.add_options()
58 // TODO(msweatt) Consider adding flagged args for other parameters
59 ("help,h", "produce help message")
60 ("generate,g" , "-g <data directory> <output-path>? <names-per-segment>? <names-per-manifest-segment>? <data-packet-size>?")
61 ("seed,s", "After download completes, continue to seed")
62 ("args", po::value<std::vector<std::string> >(), "For arguments you want to specify without flags")
63 ;
64 po::positional_options_description p;
65 p.add("args", -1);
66
67 po::variables_map vm;
68 po::store(po::command_line_parser(argc, argv).
69 options(desc).allow_unregistered().positional(p).run(), vm);
70 po::notify(vm);
71
72 if (vm.count("help")) {
73 std::cout << desc << std::endl;
74 return 1;
75 }
76 // if generate mode
77 if (vm.count("generate") && vm.count("args")) {
78 auto args = vm["args"].as<std::vector<std::string>>();
79 if (args.size() < 1 || args.size() > 5) {
80 throw ndn::Error("wrong number of arguments for generate");
81 }
82 auto dataPath = args[0];
83 auto outputPath = args.size() >= 2 ? args[1] : ".appdata/";
84 auto namesPerSegment = args.size() >= 3 ? boost::lexical_cast<size_t>(args[2]) : 1024;
85 auto namesPerManifest = args.size() >= 4 ? boost::lexical_cast<size_t>(args[3]) : 1024;
86 auto dataPacketSize = args.size() == 5 ? boost::lexical_cast<size_t>(args[4]) : 1024;
87
88 const auto& content = TorrentFile::generate(dataPath,
89 namesPerSegment,
90 namesPerManifest,
91 dataPacketSize);
92 const auto& torrentSegments = content.first;
93 std::vector<FileManifest> manifests;
94 for (const auto& ms : content.second) {
95 manifests.insert(manifests.end(), ms.first.begin(), ms.first.end());
96 }
97 auto torrentPrefix = fs::canonical(dataPath).filename().string();
98 outputPath += torrentPrefix;
99 auto torrentPath = outputPath + "/torrent_files/";
100 // write all the torrent segments
101 for (const TorrentFile& t : torrentSegments) {
102 if (!IoUtil::writeTorrentSegment(t, torrentPath)) {
103 std::cerr << "Write failed: " << t.getName() << std::endl;
104 return -1;
105 }
106 }
107 auto manifestPath = outputPath + "/manifests/";
108 for (const FileManifest& m : manifests) {
109 if (!IoUtil::writeFileManifest(m, manifestPath)) {
110 std::cerr << "Write failed: " << m.getName() << std::endl;
111 return -1;
112 }
113 }
114 }
115 // otherwise we are in torrent mode if we have the required args, start the sequential fetcher
116 else if (vm.count("args")) {
117 // <torrent-file-name> <data-path>
118 auto args = vm["args"].as<std::vector<std::string>>();
119 if (args.size() != 2) {
120 throw ndn::Error("wrong number of arguments for generate");
121 }
122 auto torrentName = args[0];
123 auto dataPath = args[1];
124 SequentialDataFetcher fetcher(torrentName, dataPath);
125 // download all the code
126 fetcher.start();
127 std::cout << torrentName << " done" << std::endl;
128 if (vm.count("seed")) {
129 fetcher.seed();
130 }
131 }
132 else {
133 std::cout << desc << std::endl;
134 return 1;
135 }
136 }
137 catch(std::exception& e) {
138 std::cerr << "error: " << e.what() << "\n";
139 return 1;
140 }
141 catch(...) {
142 std::cerr << "Exception of unknown type!\n";
143 }
144 return 0;
145}