summaryrefslogtreecommitdiff
path: root/src/DownloadEngineFactory.cc
blob: 52da662c1978c4193fe3b3fbab83aa9b7f087989 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/* <!-- copyright */
/*
 * aria2 - a simple utility for downloading files faster
 *
 * Copyright (C) 2006 Tatsuhiro Tsujikawa
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
/* copyright --> */
#include "DownloadEngineFactory.h"
#include "prefs.h"
#include "DefaultDiskWriter.h"
#include "SplitSlowestSegmentSplitter.h"
#include "InitiateConnectionCommandFactory.h"
#include "ByteArrayDiskWriter.h"
#include "Util.h"
#ifdef ENABLE_BITTORRENT
# include "PeerListenCommand.h"
# include "TrackerWatcherCommand.h"
# include "TrackerUpdateCommand.h"
# include "TorrentAutoSaveCommand.h"
# include "PeerChokeCommand.h"
# include "HaveEraseCommand.h"
#endif // ENABLE_BITTORRENT

ConsoleDownloadEngine*
DownloadEngineFactory::newConsoleEngine(const Option* op,
					const Requests& requests,
					const Requests& reserved)
{
  ConsoleDownloadEngine* e = new ConsoleDownloadEngine();
  e->option = op;
  e->segmentMan = new SegmentMan();
  e->segmentMan->diskWriter = new DefaultDiskWriter();
  e->segmentMan->dir = op->get(PREF_DIR);
  e->segmentMan->ufilename = op->get(PREF_OUT);
  e->segmentMan->option = op;
  e->segmentMan->splitter = new SplitSlowestSegmentSplitter();
  e->segmentMan->splitter->setMinSegmentSize(op->getAsLLInt(PREF_MIN_SEGMENT_SIZE));
  e->segmentMan->reserved = reserved;
  
  int cuidCounter = 1;
  for(Requests::const_iterator itr = requests.begin();
      itr != requests.end();
      itr++, cuidCounter++) {
    e->commands.push_back(InitiateConnectionCommandFactory::createInitiateConnectionCommand(cuidCounter, *itr, e));
  }
  return e;
}

#ifdef ENABLE_BITTORRENT
TorrentConsoleDownloadEngine*
DownloadEngineFactory::newTorrentConsoleEngine(const Option* op,
					       const string& torrentFile,
					       const Strings& targetFiles)
{
  Request* req = new Request();
  req->isTorrent = true;
  req->setTrackerEvent(Request::STARTED);
  TorrentConsoleDownloadEngine* te = new TorrentConsoleDownloadEngine();
  te->option = op;
  ByteArrayDiskWriter* byteArrayDiskWriter = new ByteArrayDiskWriter();
  te->segmentMan = new SegmentMan();
  te->segmentMan->diskWriter = byteArrayDiskWriter;
  te->segmentMan->option = op;
  te->segmentMan->splitter = new SplitSlowestSegmentSplitter();
  te->segmentMan->splitter->setMinSegmentSize(op->getAsLLInt(PREF_MIN_SEGMENT_SIZE));
  te->torrentMan = new TorrentMan();
  te->torrentMan->setStoreDir(op->get(PREF_DIR));
  te->torrentMan->option = op;
  te->torrentMan->req = req;
  Integers selectIndexes;
  Util::unfoldRange(op->get(PREF_SELECT_FILE), selectIndexes);
  if(selectIndexes.size()) {
    te->torrentMan->setup(torrentFile, selectIndexes);
  } else {
    te->torrentMan->setup(torrentFile, targetFiles);
  }

  PeerListenCommand* listenCommand =
    new PeerListenCommand(te->torrentMan->getNewCuid(), te);
  int port;
  int listenPort = op->getAsInt(PREF_LISTEN_PORT);
  if(listenPort == -1) {
    port = listenCommand->bindPort(6881, 6999);
  } else {
    port = listenCommand->bindPort(listenPort, listenPort);
  }
  if(port == -1) {
    printf(_("Errors occurred while binding port.\n"));
    exit(EXIT_FAILURE);
  }
  te->torrentMan->setPort(port);
  te->commands.push_back(listenCommand);
  
  te->commands.push_back(new TrackerWatcherCommand(te->torrentMan->getNewCuid(),
						   te,
						   te->torrentMan->minInterval));
  te->commands.push_back(new TrackerUpdateCommand(te->torrentMan->getNewCuid(),
						  te));
  te->commands.push_back(new TorrentAutoSaveCommand(te->torrentMan->getNewCuid(),
						    te,
						    op->getAsInt(PREF_AUTO_SAVE_INTERVAL)));
  te->commands.push_back(new PeerChokeCommand(te->torrentMan->getNewCuid(),
					      te, 10));
  te->commands.push_back(new HaveEraseCommand(te->torrentMan->getNewCuid(),
					      te, 10));
  
  return te;
}
#endif // ENABLE_BITTORRENT