2006-08-07 16:05:00 +00:00
|
|
|
/* <!-- copyright */
|
|
|
|
/*
|
2006-09-21 15:31:24 +00:00
|
|
|
* aria2 - The high speed download utility
|
2006-08-07 16:05:00 +00:00
|
|
|
*
|
|
|
|
* 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
|
2010-01-05 16:01:46 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2006-09-21 15:31:24 +00:00
|
|
|
*
|
|
|
|
* In addition, as a special exception, the copyright holders give
|
|
|
|
* permission to link the code of portions of this program with the
|
|
|
|
* OpenSSL library under certain conditions as described in each
|
|
|
|
* individual source file, and distribute linked combinations
|
|
|
|
* including the two.
|
|
|
|
* You must obey the GNU General Public License in all respects
|
|
|
|
* for all of the code used other than OpenSSL. If you modify
|
|
|
|
* file(s) with this exception, you may extend this exception to your
|
|
|
|
* version of the file(s), but you are not obligated to do so. If you
|
|
|
|
* do not wish to do so, delete this exception statement from your
|
|
|
|
* version. If you delete this exception statement from all source
|
|
|
|
* files in the program, then also delete it here.
|
2006-08-07 16:05:00 +00:00
|
|
|
*/
|
|
|
|
/* copyright --> */
|
|
|
|
#include "DownloadEngineFactory.h"
|
2008-11-03 07:49:13 +00:00
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
2007-10-11 16:58:24 +00:00
|
|
|
#include "Option.h"
|
|
|
|
#include "RequestGroup.h"
|
|
|
|
#include "DownloadEngine.h"
|
|
|
|
#include "RequestGroupMan.h"
|
|
|
|
#include "FileAllocationMan.h"
|
2010-06-28 12:45:12 +00:00
|
|
|
#include "CheckIntegrityMan.h"
|
|
|
|
#include "CheckIntegrityEntry.h"
|
2007-10-11 16:58:24 +00:00
|
|
|
#ifdef ENABLE_MESSAGE_DIGEST
|
2009-01-24 10:40:42 +00:00
|
|
|
# include "CheckIntegrityDispatcherCommand.h"
|
2007-10-11 16:58:24 +00:00
|
|
|
#endif // ENABLE_MESSAGE_DIGEST
|
2006-08-07 16:05:00 +00:00
|
|
|
#include "prefs.h"
|
2007-10-11 16:58:24 +00:00
|
|
|
#include "FillRequestGroupCommand.h"
|
2007-05-20 13:51:52 +00:00
|
|
|
#include "FileAllocationDispatcherCommand.h"
|
2007-09-03 10:32:19 +00:00
|
|
|
#include "AutoSaveCommand.h"
|
2007-10-11 16:58:24 +00:00
|
|
|
#include "HaveEraseCommand.h"
|
2008-02-11 05:07:08 +00:00
|
|
|
#include "TimedHaltCommand.h"
|
2007-11-27 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Rewritten to add content-type support.
* src/DownloadHandler.{h, cc}
* src/BtPostDownloadHandler.{h, cc}
* test/BtPostDownloadHandlerTest.cc
* src/MetalinkPostDownloadHandler.{h, cc}
* test/MetalinkPostDownloadHandlerTest.cc
* src/PostDownloadHandler.{h, cc}
* src/DownloadHandlerConstants.{h, cc}
* src/RequestGroup.cc
* src/HttpResponseCommand.cc
* src/FtpNegotiationCommand.cc
* src/SingleFileDownloadContext.{h, cc}
* src/RequestGroup.h
* src/RequestGroupCriteria.h
* src/ContentTypeRequestGroupCriteria.h
Added 'mem' option value for --follow-metalink,
--follow-torrent.
If it is give, metalink/torrent file is not written to the disk,
but
just is kept in memory. Parsing is occurred on memory.
* src/MetalinkHelper.{h, cc}
* src/MetalinkProcessor.h
* src/Xml2MetalinkProcessor.{h, cc}
* test/Xml2MetalinkProcessorTest.cc
* src/DownloadHandlerFactory.{h, cc}
* test/DownloadHandlerFactoryTest.cc
* src/PreDownloadHandler.{h, cc}
* src/OptionHandlerFactory.cc
* src/DefaultBtContext.{h, cc}
* test/DefaultBtContextTest.cc
* src/version_usage.cc
* src/Metalink2RequestGroup.{h, cc}
* src/RequestGroup.{h, cc}
* src/a2functional.h
* test/a2functionalTest.cc
* src/MemoryBufferPreDownloadHandler.{h, cc}
* src/OptionHandlerImpl.h
* src/prefs.h
* src/Util.{h, cc}
* test/UtilTest.cc
Keep DownloadResult rather than RequestGroup after downloads to
reduce
memory usage.
* src/RequestGroupMan.{h, cc}
* src/DownloadEngine.cc
* src/BtDependency.{h, cc}: Changed the type of dependee from
WeakHandle to SharedHandle because WeakHandle could be null.
* src/RequestGroup.{h, cc}
* src/DownloadEngineFactory.cc
* src/DownloadResult.h
Set totalLength after download finished
* src/UnknownLengthPieceStorage.{h, cc}
Keep torrent file specified in metalink in memory.
* src/Metalink2RequestGroup.cc
* src/BtDependency.cc
* src/TrueRequestGroupCriteria.h
Fixed the bug: seekg is used where seekp should be used.
* src/ByteArrayDiskWriter.cc
* test/ByteArraydiskWriterTest.cc
2007-11-27 12:27:10 +00:00
|
|
|
#include "DownloadResult.h"
|
2008-08-04 17:06:47 +00:00
|
|
|
#include "ServerStatMan.h"
|
2009-01-15 15:23:16 +00:00
|
|
|
#include "a2io.h"
|
2009-06-28 10:37:15 +00:00
|
|
|
#include "DownloadContext.h"
|
2010-08-07 15:04:21 +00:00
|
|
|
#include "array_fun.h"
|
2009-01-15 15:23:16 +00:00
|
|
|
#ifdef HAVE_EPOLL
|
|
|
|
# include "EpollEventPoll.h"
|
|
|
|
#endif // HAVE_EPOLL
|
2010-04-21 14:31:44 +00:00
|
|
|
#ifdef HAVE_PORT_ASSOCIATE
|
|
|
|
# include "PortEventPoll.h"
|
|
|
|
#endif // HAVE_PORT_ASSOCIATE
|
2010-04-24 13:07:35 +00:00
|
|
|
#ifdef HAVE_KQUEUE
|
|
|
|
# include "KqueueEventPoll.h"
|
|
|
|
#endif // HAVE_KQUEUE
|
2010-04-25 10:12:33 +00:00
|
|
|
#ifdef HAVE_POLL
|
|
|
|
# include "PollEventPoll.h"
|
|
|
|
#endif // HAVE_POLL
|
2009-01-15 15:23:16 +00:00
|
|
|
#include "SelectEventPoll.h"
|
|
|
|
#include "DlAbortEx.h"
|
2009-01-24 09:00:36 +00:00
|
|
|
#include "FileAllocationEntry.h"
|
2011-03-14 07:38:54 +00:00
|
|
|
#include "HttpListenCommand.h"
|
2006-08-07 16:05:00 +00:00
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
namespace aria2 {
|
|
|
|
|
2010-11-20 08:21:36 +00:00
|
|
|
DownloadEngineFactory::DownloadEngineFactory() {}
|
2007-10-11 16:58:24 +00:00
|
|
|
|
|
|
|
DownloadEngineHandle
|
2009-10-31 09:53:23 +00:00
|
|
|
DownloadEngineFactory::newDownloadEngine
|
2010-02-28 12:30:11 +00:00
|
|
|
(Option* op, const std::vector<SharedHandle<RequestGroup> >& requestGroups)
|
2007-05-20 13:51:52 +00:00
|
|
|
{
|
2009-10-31 09:53:23 +00:00
|
|
|
const size_t MAX_CONCURRENT_DOWNLOADS =
|
|
|
|
op->getAsInt(PREF_MAX_CONCURRENT_DOWNLOADS);
|
2009-01-15 15:23:16 +00:00
|
|
|
SharedHandle<EventPoll> eventPoll;
|
2010-04-21 14:31:44 +00:00
|
|
|
const std::string& pollMethod = op->get(PREF_EVENT_POLL);
|
2009-01-15 15:23:16 +00:00
|
|
|
#ifdef HAVE_EPOLL
|
2010-04-21 14:31:44 +00:00
|
|
|
if(pollMethod == V_EPOLL) {
|
2009-01-15 15:23:16 +00:00
|
|
|
SharedHandle<EpollEventPoll> ep(new EpollEventPoll());
|
|
|
|
if(ep->good()) {
|
|
|
|
eventPoll = ep;
|
|
|
|
} else {
|
2009-05-18 15:07:15 +00:00
|
|
|
throw DL_ABORT_EX("Initializing EpollEventPoll failed."
|
2010-01-05 16:01:46 +00:00
|
|
|
" Try --event-poll=select");
|
2009-01-15 15:23:16 +00:00
|
|
|
}
|
|
|
|
} else
|
|
|
|
#endif // HAVE_EPLL
|
2010-04-24 13:07:35 +00:00
|
|
|
#ifdef HAVE_KQUEUE
|
|
|
|
if(pollMethod == V_KQUEUE) {
|
|
|
|
SharedHandle<KqueueEventPoll> kp(new KqueueEventPoll());
|
|
|
|
if(kp->good()) {
|
|
|
|
eventPoll = kp;
|
2010-04-19 12:53:58 +00:00
|
|
|
} else {
|
2010-04-24 13:07:35 +00:00
|
|
|
throw DL_ABORT_EX("Initializing KqueueEventPoll failed."
|
2010-04-21 14:31:44 +00:00
|
|
|
" Try --event-poll=select");
|
2010-04-19 12:53:58 +00:00
|
|
|
}
|
2010-04-21 14:31:44 +00:00
|
|
|
} else
|
2010-04-24 13:07:35 +00:00
|
|
|
#endif // HAVE_KQUEUE
|
|
|
|
#ifdef HAVE_PORT_ASSOCIATE
|
|
|
|
if(pollMethod == V_PORT) {
|
|
|
|
SharedHandle<PortEventPoll> pp(new PortEventPoll());
|
|
|
|
if(pp->good()) {
|
|
|
|
eventPoll = pp;
|
|
|
|
} else {
|
|
|
|
throw DL_ABORT_EX("Initializing PortEventPoll failed."
|
|
|
|
" Try --event-poll=select");
|
|
|
|
}
|
|
|
|
} else
|
2010-04-21 15:54:39 +00:00
|
|
|
#endif // HAVE_PORT_ASSOCIATE
|
2010-04-21 14:31:44 +00:00
|
|
|
#ifdef HAVE_POLL
|
2010-04-24 13:07:35 +00:00
|
|
|
if(pollMethod == V_POLL) {
|
|
|
|
eventPoll.reset(new PollEventPoll());
|
|
|
|
} else
|
2010-04-21 14:31:44 +00:00
|
|
|
#endif // HAVE_POLL
|
2010-04-24 13:07:35 +00:00
|
|
|
if(pollMethod == V_SELECT) {
|
|
|
|
eventPoll.reset(new SelectEventPoll());
|
|
|
|
} else {
|
|
|
|
abort();
|
|
|
|
}
|
2009-01-15 15:23:16 +00:00
|
|
|
DownloadEngineHandle e(new DownloadEngine(eventPoll));
|
2010-06-08 14:11:36 +00:00
|
|
|
e->setOption(op);
|
2008-11-08 10:48:02 +00:00
|
|
|
|
2008-04-20 00:50:22 +00:00
|
|
|
RequestGroupManHandle
|
2009-10-31 09:53:23 +00:00
|
|
|
requestGroupMan(new RequestGroupMan(requestGroups, MAX_CONCURRENT_DOWNLOADS,
|
2010-01-05 16:01:46 +00:00
|
|
|
op));
|
2010-06-08 14:11:36 +00:00
|
|
|
e->setRequestGroupMan(requestGroupMan);
|
|
|
|
e->setFileAllocationMan
|
|
|
|
(SharedHandle<FileAllocationMan>(new FileAllocationMan()));
|
2007-07-04 16:04:57 +00:00
|
|
|
#ifdef ENABLE_MESSAGE_DIGEST
|
2010-06-08 14:11:36 +00:00
|
|
|
e->setCheckIntegrityMan
|
|
|
|
(SharedHandle<CheckIntegrityMan>(new CheckIntegrityMan()));
|
2007-07-04 16:04:57 +00:00
|
|
|
#endif // ENABLE_MESSAGE_DIGEST
|
2010-04-11 08:28:22 +00:00
|
|
|
e->addRoutineCommand(new FillRequestGroupCommand(e->newCUID(), e.get()));
|
2009-01-24 09:49:37 +00:00
|
|
|
e->addRoutineCommand(new FileAllocationDispatcherCommand
|
2010-06-08 14:11:36 +00:00
|
|
|
(e->newCUID(), e->getFileAllocationMan(), e.get()));
|
2009-01-24 10:40:42 +00:00
|
|
|
#ifdef ENABLE_MESSAGE_DIGEST
|
|
|
|
e->addRoutineCommand(new CheckIntegrityDispatcherCommand
|
2010-06-08 14:11:36 +00:00
|
|
|
(e->newCUID(), e->getCheckIntegrityMan(), e.get()));
|
2009-01-24 10:40:42 +00:00
|
|
|
#endif // ENABLE_MESSAGE_DIGEST
|
|
|
|
|
2008-10-13 13:07:53 +00:00
|
|
|
if(op->getAsInt(PREF_AUTO_SAVE_INTERVAL) > 0) {
|
|
|
|
e->addRoutineCommand
|
2008-11-03 07:49:13 +00:00
|
|
|
(new AutoSaveCommand(e->newCUID(), e.get(),
|
2010-01-05 16:01:46 +00:00
|
|
|
op->getAsInt(PREF_AUTO_SAVE_INTERVAL)));
|
2008-10-13 13:07:53 +00:00
|
|
|
}
|
2008-11-03 07:49:13 +00:00
|
|
|
e->addRoutineCommand(new HaveEraseCommand(e->newCUID(), e.get(), 10));
|
2008-02-11 05:07:08 +00:00
|
|
|
{
|
2008-03-09 12:24:01 +00:00
|
|
|
time_t stopSec = op->getAsInt(PREF_STOP);
|
2008-02-20 12:33:16 +00:00
|
|
|
if(stopSec > 0) {
|
2008-11-03 07:49:13 +00:00
|
|
|
e->addRoutineCommand(new TimedHaltCommand(e->newCUID(), e.get(),
|
2010-01-05 16:01:46 +00:00
|
|
|
stopSec));
|
2008-02-11 05:07:08 +00:00
|
|
|
}
|
|
|
|
}
|
2011-03-14 12:02:25 +00:00
|
|
|
if(op->getAsBool(PREF_ENABLE_RPC)) {
|
2011-06-15 13:19:09 +00:00
|
|
|
bool ok = false;
|
2010-08-07 15:04:21 +00:00
|
|
|
static int families[] = { AF_INET, AF_INET6 };
|
2010-08-27 11:48:09 +00:00
|
|
|
size_t familiesLength = op->getAsBool(PREF_DISABLE_IPV6)?1:2;
|
|
|
|
for(size_t i = 0; i < familiesLength; ++i) {
|
2010-08-07 15:04:21 +00:00
|
|
|
HttpListenCommand* httpListenCommand =
|
|
|
|
new HttpListenCommand(e->newCUID(), e.get(), families[i]);
|
2011-03-14 12:02:25 +00:00
|
|
|
if(httpListenCommand->bindPort(op->getAsInt(PREF_RPC_LISTEN_PORT))){
|
2011-06-15 13:19:09 +00:00
|
|
|
e->addCommand(httpListenCommand);
|
|
|
|
ok = true;
|
2010-08-07 15:04:21 +00:00
|
|
|
} else {
|
|
|
|
delete httpListenCommand;
|
|
|
|
}
|
2009-01-25 09:58:40 +00:00
|
|
|
}
|
2011-06-15 13:19:09 +00:00
|
|
|
if(!ok) {
|
|
|
|
throw DL_ABORT_EX("Failed to setup RPC server.");
|
|
|
|
}
|
2009-01-25 09:58:40 +00:00
|
|
|
}
|
2007-10-11 16:58:24 +00:00
|
|
|
return e;
|
2006-08-07 16:05:00 +00:00
|
|
|
}
|
2008-02-08 15:53:45 +00:00
|
|
|
|
|
|
|
} // namespace aria2
|