main.cc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - a simple utility for downloading files faster
  4. *
  5. * Copyright (C) 2006 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. /* copyright --> */
  22. #include "HttpInitiateConnectionCommand.h"
  23. #include "ConsoleDownloadEngine.h"
  24. #include "TorrentConsoleDownloadEngine.h"
  25. #include "SegmentMan.h"
  26. #include "TorrentMan.h"
  27. #include "SplitSlowestSegmentSplitter.h"
  28. #include "SimpleLogger.h"
  29. #include "common.h"
  30. #include "DefaultDiskWriter.h"
  31. #include "Util.h"
  32. #include "InitiateConnectionCommandFactory.h"
  33. #include "prefs.h"
  34. #include "TrackerInitCommand.h"
  35. #include "PeerListenCommand.h"
  36. #include "TorrentAutoSaveCommand.h"
  37. #include "TrackerWatcherCommand.h"
  38. #include <deque>
  39. #include <algorithm>
  40. #include <time.h>
  41. #include <signal.h>
  42. #include <unistd.h>
  43. #include <libgen.h>
  44. #include <utility>
  45. extern char* optarg;
  46. extern int optind, opterr, optopt;
  47. #include <getopt.h>
  48. #ifdef HAVE_LIBSSL
  49. // for SSL
  50. # include <openssl/err.h>
  51. # include <openssl/ssl.h>
  52. #endif // HAVE_LIBSSL
  53. #ifdef HAVE_LIBGNUTLS
  54. # include <gnutls/gnutls.h>
  55. #endif // HAVE_LIBGNUTLS
  56. using namespace std;
  57. typedef deque<Request*> Requests;
  58. void printDownloadCompeleteMessage(string filename) {
  59. printf(_("\nThe download was complete. <%s>\n"), filename.c_str());
  60. }
  61. void printDownloadAbortMessage() {
  62. printf(_("\nThe download was not complete because of errors. Check the log.\n"));
  63. }
  64. void clearRequest(Request* req) {
  65. delete(req);
  66. }
  67. DownloadEngine* e;
  68. TorrentDownloadEngine* te;
  69. void handler(int signal) {
  70. cout << _("\nSIGINT signal received.") << endl;
  71. e->segmentMan->save();
  72. if(e->diskWriter != NULL) {
  73. e->diskWriter->closeFile();
  74. }
  75. exit(0);
  76. }
  77. void torrentHandler(int signal) {
  78. cout << _("\nSIGINT signal received.") << endl;
  79. if(te->torrentMan->diskWriter != NULL) {
  80. te->torrentMan->diskWriter->closeFile();
  81. }
  82. if(te->torrentMan->downloadComplete() && te->isFilenameFixed()) {
  83. te->torrentMan->remove();
  84. te->torrentMan->deleteTempFile();
  85. printDownloadCompeleteMessage(te->torrentMan->getFilePath());
  86. } else {
  87. te->torrentMan->save();
  88. }
  89. exit(0);
  90. }
  91. void addCommand(int cuid, const char* url, string referer, Requests& requests) {
  92. Request* req = new Request();
  93. req->setReferer(referer);
  94. if(req->setUrl(url)) {
  95. e->commands.push(InitiateConnectionCommandFactory::createInitiateConnectionCommand(cuid, req, e));
  96. requests.push_back(req);
  97. } else {
  98. fprintf(stderr, _("Unrecognized URL or unsupported protocol: %s\n"), req->getUrl().c_str());
  99. delete(req);
  100. }
  101. }
  102. void showVersion() {
  103. cout << PACKAGE << _(" version ") << PACKAGE_VERSION << endl;
  104. cout << "Copyright (C) 2006 Tatsuhiro Tsujikawa" << endl;
  105. cout << endl;
  106. cout <<
  107. _("This program is free software; you can redistribute it and/or modify\n"
  108. "it under the terms of the GNU General Public License as published by\n"
  109. "the Free Software Foundation; either version 2 of the License, or\n"
  110. "(at your option) any later version.\n"
  111. "\n"
  112. "This program is distributed in the hope that it will be useful,\n"
  113. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  114. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
  115. "GNU General Public License for more details.\n"
  116. "\n"
  117. "You should have received a copy of the GNU General Public License\n"
  118. "along with this program; if not, write to the Free Software\n"
  119. "Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA\n");
  120. cout << endl;
  121. printf(_("Contact Info: %s\n"), "Tasuhiro Tsujikawa <tujikawa at users dot sourceforge dot net>");
  122. cout << endl;
  123. }
  124. void showUsage() {
  125. printf(_("Usage: %s [options] URL ...\n"), PACKAGE_NAME);
  126. cout << endl;
  127. cout << _("Options:") << endl;
  128. cout << _(" -d, --dir=DIR The directory to store downloaded file.") << endl;
  129. cout << _(" -o, --out=FILE The file name for downloaded file.") << endl;
  130. cout << _(" -l, --log=LOG The file path to store log. If '-' is specified,\n"
  131. " log is written to stdout.") << endl;
  132. cout << _(" -D, --daemon Run as daemon.") << endl;
  133. cout << _(" -s, --split=N Download a file using N connections. N must be\n"
  134. " between 1 and 5. This option affects all URLs.\n"
  135. " Thus, aria2 connects to each URL with\n"
  136. " N connections.") << endl;
  137. cout << _(" --retry-wait=SEC Set amount of time in second between requests\n"
  138. " for errors. Specify a value between 0 and 60.\n"
  139. " Default: 5") << endl;
  140. cout << _(" -t, --timeout=SEC Set timeout in second. Default: 60") << endl;
  141. cout << _(" -m, --max-tries=N Set number of tries. 0 means unlimited.\n"
  142. " Default: 5") << endl;
  143. cout << _(" --min-segment-size=SIZE[K|M] Set minimum segment size. You can append\n"
  144. " K or M(1K = 1024, 1M = 1024K). This\n"
  145. " value must be greater than or equal to\n"
  146. " 1024.") << endl;
  147. cout << _(" --http-proxy=HOST:PORT Use HTTP proxy server. This affects to all\n"
  148. " URLs.") << endl;
  149. cout << _(" --http-user=USER Set HTTP user. This affects to all URLs.") << endl;
  150. cout << _(" --http-passwd=PASSWD Set HTTP password. This affects to all URLs.") << endl;
  151. cout << _(" --http-proxy-user=USER Set HTTP proxy user. This affects to all URLs") << endl;
  152. cout << _(" --http-proxy-passwd=PASSWD Set HTTP proxy password. This affects to all URLs.") << endl;
  153. cout << _(" --http-proxy-method=METHOD Set the method to use in proxy request.\n"
  154. " METHOD is either 'get' or 'tunnel'.\n"
  155. " Default: tunnel") << endl;
  156. cout << _(" --http-auth-scheme=SCHEME Set HTTP authentication scheme. Currently, basic\n"
  157. " is the only supported scheme. You MUST specify\n"
  158. " this option in order to use HTTP authentication\n"
  159. " as well as --http-user and --http-passwd.") << endl;
  160. cout << _(" --referer=REFERER Set Referer. This affects to all URLs.") << endl;
  161. cout << _(" --ftp-user=USER Set FTP user. This affects to all URLs.\n"
  162. " Default: anonymous") << endl;
  163. cout << _(" --ftp-passwd=PASSWD Set FTP password. This affects to all URLs.\n"
  164. " Default: ARIA2USER@") << endl;
  165. cout << _(" --ftp-type=TYPE Set FTP transfer type. TYPE is either 'binary'\n"
  166. " or 'ascii'.\n"
  167. " Default: binary") << endl;
  168. cout << _(" -p, --ftp-pasv Use passive mode in FTP.") << endl;
  169. cout << _(" --ftp-via-http-proxy=METHOD Use HTTP proxy in FTP. METHOD is either 'get' or\n"
  170. " 'tunnel'.\n"
  171. " Default: tunnel") << endl;
  172. #ifdef ENABLE_BITTORRENT
  173. cout << _(" --torrent-file=TORRENT_FILE The file path to .torrent file.") << endl;
  174. cout << _(" --follow-torrent=true|false Setting this option to false prevents aria2 to\n"
  175. " enter BitTorrent mode even if the filename of\n"
  176. " downloaded file ends with .torrent.\n"
  177. " Default: true") << endl;
  178. #endif // ENABLE_BITTORRENT
  179. cout << _(" -v, --version Print the version number and exit.") << endl;
  180. cout << _(" -h, --help Print this message and exit.") << endl;
  181. cout << endl;
  182. cout << "URL:" << endl;
  183. cout << _(" You can specify multiple URLs. All URLs must point to the same file\n"
  184. " or downloading fails.") << endl;
  185. cout << endl;
  186. cout << _("Examples:") << endl;
  187. cout << _(" Download a file by 1 connection:") << endl;
  188. cout << " aria2c http://AAA.BBB.CCC/file.zip" << endl;
  189. cout << _(" Download a file by 2 connections:") << endl;
  190. cout << " aria2c -s 2 http://AAA.BBB.CCC/file.zip" << endl;
  191. cout << _(" Download a file by 2 connections, each connects to a different server:") << endl;
  192. cout << " aria2c http://AAA.BBB.CCC/file.zip http://DDD.EEE.FFF/GGG/file.zip" << endl;
  193. cout << _(" You can mix up different protocols:") << endl;
  194. cout << " aria2c http://AAA.BBB.CCC/file.zip ftp://DDD.EEE.FFF/GGG/file.zip" << endl;
  195. #ifdef ENABLE_BITTORRENT
  196. cout << _(" Download a torrent:") << endl;
  197. cout << " aria2c -o test.torrent http://AAA.BBB.CCC/file.torrent" << endl;
  198. cout << _(" Download a torrent using local .torrent file:") << endl;
  199. cout << " aria2c --torrent-file test.torrent" << endl;
  200. cout << endl;
  201. #endif // ENABLE_BITTORRENT
  202. printf(_("Reports bugs to %s"), "<tujikawa at users dot sourceforge dot net>");
  203. cout << endl;
  204. }
  205. int main(int argc, char* argv[]) {
  206. #ifdef ENABLE_NLS
  207. setlocale (LC_CTYPE, "");
  208. setlocale (LC_MESSAGES, "");
  209. bindtextdomain (PACKAGE, LOCALEDIR);
  210. textdomain (PACKAGE);
  211. #endif // ENABLE_NLS
  212. bool stdoutLog = false;
  213. string logfile;
  214. string dir = ".";
  215. string ufilename;
  216. int split = 1;
  217. bool daemonMode = false;
  218. string referer;
  219. string torrentFile;
  220. #ifdef ENABLE_BITTORRENT
  221. bool followTorrent = true;
  222. #else
  223. bool followTorrent = false;
  224. #endif // ENABLE_BITTORRENT
  225. int c;
  226. Option* op = new Option();
  227. op->put(PREF_RETRY_WAIT, "5");
  228. op->put(PREF_TIMEOUT, "60");
  229. op->put(PREF_PEER_CONNECTION_TIMEOUT, "60");
  230. op->put(PREF_MIN_SEGMENT_SIZE, "1048576");// 1M
  231. op->put(PREF_MAX_TRIES, "5");
  232. op->put(PREF_HTTP_PROXY_METHOD, V_TUNNEL);
  233. op->put(PREF_FTP_USER, "anonymous");
  234. op->put(PREF_FTP_PASSWD, "ARIA2USER@");
  235. op->put(PREF_FTP_TYPE, V_BINARY);
  236. op->put(PREF_FTP_VIA_HTTP_PROXY, V_TUNNEL);
  237. op->put(PREF_AUTO_SAVE_INTERVAL, "60");
  238. while(1) {
  239. int optIndex = 0;
  240. int lopt;
  241. static struct option longOpts[] = {
  242. { "daemon", no_argument, NULL, 'D' },
  243. { "dir", required_argument, NULL, 'd' },
  244. { "out", required_argument, NULL, 'o' },
  245. { "log", required_argument, NULL, 'l' },
  246. { "split", required_argument, NULL, 's' },
  247. { "timeout", required_argument, NULL, 't' },
  248. { "max-tries", required_argument, NULL, 'm' },
  249. { "http-proxy", required_argument, &lopt, 1 },
  250. { "http-user", required_argument, &lopt, 2 },
  251. { "http-passwd", required_argument, &lopt, 3 },
  252. { "http-proxy-user", required_argument, &lopt, 4 },
  253. { "http-proxy-passwd", required_argument, &lopt, 5 },
  254. { "http-auth-scheme", required_argument, &lopt, 6 },
  255. { "referer", required_argument, &lopt, 7 },
  256. { "retry-wait", required_argument, &lopt, 8 },
  257. { "ftp-user", required_argument, &lopt, 9 },
  258. { "ftp-passwd", required_argument, &lopt, 10 },
  259. { "ftp-type", required_argument, &lopt, 11 },
  260. { "ftp-pasv", no_argument, NULL, 'p' },
  261. { "ftp-via-http-proxy", required_argument, &lopt, 12 },
  262. { "min-segment-size", required_argument, &lopt, 13 },
  263. { "http-proxy-method", required_argument, &lopt, 14 },
  264. #ifdef ENABLE_BITTORRENT
  265. { "torrent-file", required_argument, &lopt, 15 },
  266. { "follow-torrent", required_argument, &lopt, 16 },
  267. #endif // ENABLE_BITTORRENT
  268. { "version", no_argument, NULL, 'v' },
  269. { "help", no_argument, NULL, 'h' },
  270. { 0, 0, 0, 0 }
  271. };
  272. c = getopt_long(argc, argv, "Dd:o:l:s:pt:m:vh", longOpts, &optIndex);
  273. if(c == -1) {
  274. break;
  275. }
  276. switch(c) {
  277. case 0:{
  278. switch(lopt) {
  279. case 1: {
  280. pair<string, string> proxy;
  281. Util::split(proxy, optarg, ':');
  282. int port = (int)strtol(proxy.second.c_str(), NULL, 10);
  283. if(proxy.first.empty() || proxy.second.empty() ||
  284. !(0 < port && port <= 65535)) {
  285. cerr << _("unrecognized proxy format") << endl;
  286. showUsage();
  287. exit(1);
  288. }
  289. op->put(PREF_HTTP_PROXY_HOST, proxy.first);
  290. op->put(PREF_HTTP_PROXY_PORT, Util::itos(port));
  291. op->put(PREF_HTTP_PROXY_ENABLED, V_TRUE);
  292. break;
  293. }
  294. case 2:
  295. op->put(PREF_HTTP_USER, optarg);
  296. break;
  297. case 3:
  298. op->put(PREF_HTTP_PASSWD, optarg);
  299. break;
  300. case 4:
  301. op->put(PREF_HTTP_PROXY_USER, optarg);
  302. op->put(PREF_HTTP_PROXY_AUTH_ENABLED, V_TRUE);
  303. break;
  304. case 5:
  305. op->put(PREF_HTTP_PROXY_PASSWD, optarg);
  306. break;
  307. case 6:
  308. if(string(V_BASIC) == optarg) {
  309. op->put(PREF_HTTP_AUTH_SCHEME, V_BASIC);
  310. } else {
  311. cerr << _("Currently, supported authentication scheme is basic.") << endl;
  312. }
  313. break;
  314. case 7:
  315. referer = optarg;
  316. break;
  317. case 8: {
  318. int wait = (int)strtol(optarg, NULL, 10);
  319. if(!(0 <= wait && wait <= 60)) {
  320. cerr << _("retry-wait must be between 0 and 60.") << endl;
  321. showUsage();
  322. exit(1);
  323. }
  324. op->put(PREF_RETRY_WAIT, Util::itos(wait));
  325. break;
  326. }
  327. case 9:
  328. op->put(PREF_FTP_USER, optarg);
  329. break;
  330. case 10:
  331. op->put(PREF_FTP_PASSWD, optarg);
  332. break;
  333. case 11:
  334. if(string(optarg) == V_BINARY || string(optarg) == V_ASCII) {
  335. op->put(PREF_FTP_TYPE, optarg);
  336. } else {
  337. cerr << _("ftp-type must be either 'binary' or 'ascii'.") << endl;
  338. showUsage();
  339. exit(1);
  340. }
  341. break;
  342. case 12:
  343. if(string(optarg) == V_GET || string(optarg) == V_TUNNEL) {
  344. op->put(PREF_FTP_VIA_HTTP_PROXY, optarg);
  345. } else {
  346. cerr << _("ftp-via-http-proxy must be either 'get' or 'tunnel'.") << endl;
  347. showUsage();
  348. exit(1);
  349. }
  350. break;
  351. case 13: {
  352. string::size_type p = string(optarg).find_first_of("KM");
  353. int mult = 1;
  354. if(p != string::npos) {
  355. if(optarg[p] == 'K') {
  356. mult = 1024;
  357. } else if(optarg[p] == 'M') {
  358. mult = 1024*1024;
  359. }
  360. optarg[p] = '\0';
  361. }
  362. long long int size = strtoll(optarg, NULL, 10)*mult;
  363. if(size < 1024) {
  364. cerr << _("min-segment-size invalid") << endl;
  365. showUsage();
  366. exit(1);
  367. }
  368. op->put(PREF_MIN_SEGMENT_SIZE, Util::llitos(size));
  369. break;
  370. }
  371. case 14:
  372. if(string(optarg) == V_GET || string(optarg) == V_TUNNEL) {
  373. op->put(PREF_HTTP_PROXY_METHOD, optarg);
  374. } else {
  375. cerr << _("http-proxy-method must be either 'get' or 'tunnel'.") << endl;
  376. showUsage();
  377. exit(1);
  378. }
  379. break;
  380. case 15:
  381. torrentFile = string(optarg);
  382. break;
  383. case 16:
  384. if(string(optarg) == "true") {
  385. followTorrent = true;
  386. } else if(string(optarg) == "false") {
  387. followTorrent = false;
  388. } else {
  389. cerr << _("follow-torrent must be either 'true' or 'false'.") << endl;
  390. showUsage();
  391. exit(1);
  392. }
  393. }
  394. break;
  395. }
  396. case 'D':
  397. daemonMode = true;
  398. break;
  399. case 'd':
  400. dir = optarg;
  401. break;
  402. case 'o':
  403. ufilename = optarg;
  404. break;
  405. case 'l':
  406. if(strcmp("-", optarg) == 0) {
  407. stdoutLog = true;
  408. } else {
  409. logfile = optarg;
  410. }
  411. break;
  412. case 's':
  413. split = (int)strtol(optarg, NULL, 10);
  414. if(!(1 <= split && split <= 5)) {
  415. cerr << _("split must be between 1 and 5.") << endl;
  416. showUsage();
  417. exit(1);
  418. }
  419. break;
  420. case 't': {
  421. int timeout = (int)strtol(optarg, NULL, 10);
  422. if(1 <= timeout && timeout <= 600) {
  423. op->put(PREF_TIMEOUT, Util::itos(timeout));
  424. } else {
  425. cerr << _("timeout must be between 1 and 600") << endl;
  426. showUsage();
  427. exit(1);
  428. }
  429. break;
  430. }
  431. case 'm': {
  432. int retries = (int)strtol(optarg, NULL, 10);
  433. if(retries < 0) {
  434. cerr << _("max-tries invalid") << endl;
  435. showUsage();
  436. exit(1);
  437. }
  438. op->put(PREF_MAX_TRIES, Util::itos(retries));
  439. break;
  440. }
  441. case 'p':
  442. op->put(PREF_FTP_PASV_ENABLED, V_TRUE);
  443. break;
  444. case 'v':
  445. showVersion();
  446. exit(0);
  447. case 'h':
  448. showUsage();
  449. exit(0);
  450. default:
  451. showUsage();
  452. exit(1);
  453. }
  454. }
  455. if(torrentFile.empty()) {
  456. if(optind == argc) {
  457. cerr << _("specify at least one URL") << endl;
  458. showUsage();
  459. exit(1);
  460. }
  461. }
  462. if(daemonMode) {
  463. if(daemon(1, 1) < 0) {
  464. perror("daemon failed");
  465. exit(1);
  466. }
  467. }
  468. #ifdef HAVE_LIBSSL
  469. // for SSL initialization
  470. SSL_load_error_strings();
  471. SSL_library_init();
  472. #endif // HAVE_LIBSSL
  473. #ifdef HAVE_LIBGNUTLS
  474. gnutls_global_init();
  475. #endif // HAVE_LIBGNUTLS
  476. srandom(time(NULL));
  477. SimpleLogger* logger;
  478. if(stdoutLog) {
  479. logger = new SimpleLogger(stdout);
  480. } else if(logfile.size()) {
  481. logger = new SimpleLogger(logfile);
  482. } else {
  483. logger = new SimpleLogger("/dev/null");
  484. }
  485. SegmentSplitter* splitter = new SplitSlowestSegmentSplitter();
  486. splitter->setMinSegmentSize(op->getAsLLInt(PREF_MIN_SEGMENT_SIZE));
  487. struct sigaction sigactIgn;
  488. sigactIgn.sa_handler = SIG_IGN;
  489. sigactIgn.sa_flags = 0;
  490. sigemptyset(&sigactIgn.sa_mask);
  491. sigaction(SIGPIPE, &sigactIgn, NULL);
  492. bool readyToTorrentMode = false;
  493. string downloadedTorrentFile;
  494. if(torrentFile.empty()) {
  495. struct sigaction sigact;
  496. sigact.sa_handler = handler;
  497. sigact.sa_flags = 0;
  498. sigemptyset(&sigact.sa_mask);
  499. sigaction(SIGINT, &sigact, NULL);
  500. splitter->logger = logger;
  501. e = new ConsoleDownloadEngine();
  502. e->logger = logger;
  503. e->option = op;
  504. e->diskWriter = new DefaultDiskWriter();
  505. e->segmentMan = new SegmentMan();
  506. e->segmentMan->dir = dir;
  507. e->segmentMan->ufilename = ufilename;
  508. e->segmentMan->logger = logger;
  509. e->segmentMan->option = op;
  510. e->segmentMan->splitter = splitter;
  511. Requests requests;
  512. for(int i = 1; optind+i-1 < argc; i++) {
  513. for(int s = 1; s <= split; s++) {
  514. addCommand(split*(i-1)+s, argv[optind+i-1], referer, requests);
  515. }
  516. }
  517. e->run();
  518. if(e->segmentMan->finished()) {
  519. printDownloadCompeleteMessage(e->segmentMan->getFilePath());
  520. if(Util::endsWith(e->segmentMan->getFilePath(), ".torrent")) {
  521. downloadedTorrentFile = e->segmentMan->getFilePath();
  522. readyToTorrentMode = true;
  523. }
  524. } else {
  525. printDownloadAbortMessage();
  526. }
  527. for_each(requests.begin(), requests.end(), clearRequest);
  528. requests.clear();
  529. delete(e->segmentMan);
  530. delete(e->diskWriter);
  531. delete(e);
  532. }
  533. if(!torrentFile.empty() || followTorrent && readyToTorrentMode) {
  534. try {
  535. //op->put(PREF_MAX_TRIES, "0");
  536. struct sigaction sigact;
  537. sigact.sa_handler = torrentHandler;
  538. sigact.sa_flags = 0;
  539. sigemptyset(&sigact.sa_mask);
  540. sigaction(SIGINT, &sigact, NULL);
  541. Request* req = new Request();
  542. req->isTorrent = true;
  543. req->setTrackerEvent(Request::STARTED);
  544. te = new TorrentConsoleDownloadEngine();
  545. te->logger = logger;
  546. te->option = op;
  547. te->diskWriter = new DefaultDiskWriter();
  548. te->segmentMan = new SegmentMan();
  549. te->segmentMan->logger = logger;
  550. te->segmentMan->option = op;
  551. te->segmentMan->splitter = splitter;
  552. te->torrentMan = new TorrentMan();
  553. te->torrentMan->setStoreDir(dir);
  554. te->torrentMan->logger = logger;
  555. te->torrentMan->setup(torrentFile.empty() ?
  556. downloadedTorrentFile : torrentFile);
  557. PeerListenCommand* listenCommand =
  558. new PeerListenCommand(te->torrentMan->getNewCuid(), te);
  559. int port = listenCommand->bindPort(6881, 6999);
  560. if(port == -1) {
  561. printf("Errors occurred while binding port.\n");
  562. exit(1);
  563. }
  564. te->torrentMan->setPort(port);
  565. te->commands.push(listenCommand);
  566. te->commands.push(new TrackerWatcherCommand(te->torrentMan->getNewCuid(),
  567. req, te));
  568. te->commands.push(new TorrentAutoSaveCommand(te->torrentMan->getNewCuid(),
  569. te,
  570. op->getAsInt(PREF_AUTO_SAVE_INTERVAL)));
  571. te->run();
  572. if(te->torrentMan->downloadComplete()) {
  573. printDownloadCompeleteMessage(te->torrentMan->getFilePath());
  574. } else {
  575. printDownloadAbortMessage();
  576. }
  577. delete(req);
  578. delete(te->segmentMan);
  579. delete(te->torrentMan);
  580. delete(te->diskWriter);
  581. delete(te);
  582. } catch(Exception* ex) {
  583. cerr << ex->getMsg() << endl;
  584. delete ex;
  585. exit(1);
  586. }
  587. }
  588. delete(logger);
  589. delete(op);
  590. delete(splitter);
  591. #ifdef HAVE_LIBGNUTLS
  592. gnutls_global_deinit();
  593. #endif // HAVE_LIBGNUTLS
  594. return 0;
  595. }