blob: c4240d98f6bf1a355a823900df4de7567ef32834 [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 */
Mickey Sweattf91ced42016-04-20 13:30:37 -070021#include "sequential-data-fetcher.hpp"
22#include "torrent-file.hpp"
23#include "util/io-util.hpp"
Mickey Sweatt617d2d42016-04-25 22:02:08 -070024#include "util/logging.hpp"
Mickey Sweattf91ced42016-04-20 13:30:37 -070025
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 Sweatt617d2d42016-04-25 22:02:08 -070033#include <boost/log/utility/setup/common_attributes.hpp>
Mickey Sweattf91ced42016-04-20 13:30:37 -070034
Mickey Sweatt617d2d42016-04-25 22:02:08 -070035namespace logging = boost::log;
Mickey Sweattf91ced42016-04-20 13:30:37 -070036namespace po = boost::program_options;
Mickey Sweatt617d2d42016-04-25 22:02:08 -070037
Mickey Sweatt15dde2d2016-04-28 23:42:45 -070038using namespace ndn;
Mickey Sweattf91ced42016-04-20 13:30:37 -070039using namespace ndn::ntorrent;
40
41namespace ndn {
42
43class Error : public std::runtime_error
44{
45public:
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
56int main(int argc, char *argv[])
57{
58 try {
Mickey Sweatt617d2d42016-04-25 22:02:08 -070059 LoggingUtil::init();
60 logging::add_common_attributes();
61
Mickey Sweattf91ced42016-04-20 13:30:37 -070062 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 Sweatt15dde2d2016-04-28 23:42:45 -070068 ("dump,d", "-d <file> Dump the contents of the Data stored at the <file>.")
Mickey Sweattf91ced42016-04-20 13:30:37 -070069 ("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 Sweatt15dde2d2016-04-28 23:42:45 -070083 if (vm.count("args")) {
Mickey Sweattf91ced42016-04-20 13:30:37 -070084 auto args = vm["args"].as<std::vector<std::string>>();
Mickey Sweatt15dde2d2016-04-28 23:42:45 -070085 // 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 Sweattf91ced42016-04-20 13:30:37 -070095
Mickey Sweatt15dde2d2016-04-28 23:42:45 -070096 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 Sweattf91ced42016-04-20 13:30:37 -0700121 }
122 }
Mickey Sweatt15dde2d2016-04-28 23:42:45 -0700123 // 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 Sweattf91ced42016-04-20 13:30:37 -0700135 }
136 }
Mickey Sweatt15dde2d2016-04-28 23:42:45 -0700137 // 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 Sweatt44e4fd92016-05-02 15:43:11 -0700144 auto dataPath = args[1];
145 auto seedFlag = (vm.count("seed") != 0);
146 SequentialDataFetcher fetcher(torrentName, dataPath, seedFlag);
Mickey Sweatt15dde2d2016-04-28 23:42:45 -0700147 fetcher.start();
Mickey Sweattf91ced42016-04-20 13:30:37 -0700148 }
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 Sweatt15dde2d2016-04-28 23:42:45 -0700163}