Merge branch 'libaria2'

pull/89/head
Tatsuhiro Tsujikawa 2013-05-05 00:13:17 +09:00
commit 2bf2dba544
34 changed files with 1904 additions and 459 deletions

3
.gitignore vendored
View File

@ -1,5 +1,7 @@
*~
*.o
*.lo
*.la
*.ce
*.cce
*.he
@ -31,3 +33,4 @@ libtool
ltmain.sh
po/aria2.pot
po/remove-potcdate.sed
src/libaria2.pc

View File

@ -6,6 +6,12 @@ LT_PREREQ([2.2.6])
AC_INIT([aria2],[1.17.0],[t-tujikawa@users.sourceforge.net],[aria2],[http://aria2.sourceforge.net/])
AC_USE_SYSTEM_EXTENSIONS
LT_INIT()
dnl See versioning rule:
dnl http://www.gnu.org/software/libtool/manual/html_node/Updating-version-info.html
AC_SUBST(LT_CURRENT, 0)
AC_SUBST(LT_REVISION, 0)
AC_SUBST(LT_AGE, 0)
AC_CONFIG_MACRO_DIR([m4])
AC_CANONICAL_HOST
AC_CANONICAL_TARGET
@ -789,7 +795,8 @@ if test "x$enable_message_digest" = "xyes"; then
enable_websocket=yes
AC_DEFINE([ENABLE_WEBSOCKET], [1],
[Define 1 if WebSocket support is enabled.])
LIBS="\$(top_builddir)/deps/wslay/lib/libwslay.la $LIBS"
WSLAY_LIBS="\$(top_builddir)/deps/wslay/lib/libwslay.la"
AC_SUBST([WSLAY_LIBS])
# $(top_srcdir) for `make distcheck`
CPPFLAGS="-I\$(top_builddir)/deps/wslay/lib/includes -I\$(top_srcdir)/deps/wslay/lib/includes $CPPFLAGS"
fi
@ -815,6 +822,8 @@ fi
AC_CONFIG_FILES([Makefile
src/Makefile
src/libaria2.pc
src/includes/Makefile
test/Makefile
po/Makefile.in
intl/Makefile

View File

@ -23,7 +23,7 @@ dnl WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
AC_PREREQ(2.61)
AC_INIT([wslay], [0.1.1], [t-tujikawa@users.sourceforge.net])
LT_PREREQ([2.2.6])
LT_INIT([disable-shared])
LT_INIT()
AC_CONFIG_AUX_DIR([.])
dnl See versioning rule:
dnl http://www.gnu.org/software/libtool/manual/html_node/Updating-version-info.html

294
src/Context.cc Normal file
View File

@ -0,0 +1,294 @@
/* <!-- copyright */
/*
* aria2 - The high speed download utility
*
* Copyright (C) 2013 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* 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.
*/
/* copyright --> */
#include "Context.h"
#include <unistd.h>
#include <getopt.h>
#include <numeric>
#include <vector>
#include <iostream>
#include "SharedHandle.h"
#include "LogFactory.h"
#include "Logger.h"
#include "util.h"
#include "FeatureConfig.h"
#include "MultiUrlRequestInfo.h"
#include "SimpleRandomizer.h"
#include "File.h"
#include "message.h"
#include "prefs.h"
#include "Option.h"
#include "a2algo.h"
#include "a2io.h"
#include "a2time.h"
#include "Platform.h"
#include "FileEntry.h"
#include "RequestGroup.h"
#include "ConsoleStatCalc.h"
#include "NullStatCalc.h"
#include "download_helper.h"
#include "Exception.h"
#include "ProtocolDetector.h"
#include "RecoverableException.h"
#include "SocketCore.h"
#include "DownloadContext.h"
#include "fmt.h"
#include "NullOutputFile.h"
#include "console.h"
#include "UriListParser.h"
#ifdef ENABLE_BITTORRENT
# include "bittorrent_helper.h"
#endif // ENABLE_BITTORRENT
#ifdef ENABLE_METALINK
# include "metalink_helper.h"
# include "MetalinkEntry.h"
#endif // ENABLE_METALINK
#ifdef ENABLE_MESSAGE_DIGEST
# include "message_digest_helper.h"
#endif // ENABLE_MESSAGE_DIGEST
extern char* optarg;
extern int optind, opterr, optopt;
namespace aria2 {
SharedHandle<StatCalc> getStatCalc(const SharedHandle<Option>& op)
{
SharedHandle<StatCalc> statCalc;
if(op->getAsBool(PREF_QUIET)) {
statCalc.reset(new NullStatCalc());
} else {
SharedHandle<ConsoleStatCalc> impl
(new ConsoleStatCalc(op->getAsInt(PREF_SUMMARY_INTERVAL),
op->getAsBool(PREF_HUMAN_READABLE)));
impl->setReadoutVisibility(op->getAsBool(PREF_SHOW_CONSOLE_READOUT));
impl->setTruncate(op->getAsBool(PREF_TRUNCATE_CONSOLE_READOUT));
statCalc = impl;
}
return statCalc;
}
SharedHandle<OutputFile> getSummaryOut(const SharedHandle<Option>& op)
{
if(op->getAsBool(PREF_QUIET)) {
return SharedHandle<OutputFile>(new NullOutputFile());
} else {
return global::cout();
}
}
#ifdef ENABLE_BITTORRENT
namespace {
void showTorrentFile(const std::string& uri)
{
SharedHandle<Option> op(new Option());
SharedHandle<DownloadContext> dctx(new DownloadContext());
bittorrent::load(uri, dctx, op);
bittorrent::print(*global::cout(), dctx);
}
} // namespace
#endif // ENABLE_BITTORRENT
#ifdef ENABLE_METALINK
namespace {
void showMetalinkFile
(const std::string& uri, const SharedHandle<Option>& op)
{
std::vector<SharedHandle<MetalinkEntry> > metalinkEntries;
metalink::parseAndQuery(metalinkEntries, uri, op.get(),
op->get(PREF_METALINK_BASE_URI));
std::vector<SharedHandle<FileEntry> > fileEntries;
MetalinkEntry::toFileEntry(fileEntries, metalinkEntries);
util::toStream(fileEntries.begin(), fileEntries.end(), *global::cout());
global::cout()->write("\n");
global::cout()->flush();
}
} // namespace
#endif // ENABLE_METALINK
#if defined ENABLE_BITTORRENT || defined ENABLE_METALINK
namespace {
void showFiles
(const std::vector<std::string>& uris, const SharedHandle<Option>& op)
{
ProtocolDetector dt;
for(std::vector<std::string>::const_iterator i = uris.begin(),
eoi = uris.end(); i != eoi; ++i) {
printf(">>> ");
printf(MSG_SHOW_FILES, (*i).c_str());
printf("\n");
try {
#ifdef ENABLE_BITTORRENT
if(dt.guessTorrentFile(*i)) {
showTorrentFile(*i);
} else
#endif // ENABLE_BITTORRENT
#ifdef ENABLE_METALINK
if(dt.guessMetalinkFile(*i)) {
showMetalinkFile(*i, op);
} else
#endif // ENABLE_METALINK
{
printf(MSG_NOT_TORRENT_METALINK);
printf("\n\n");
}
} catch(RecoverableException& e) {
global::cout()->printf("%s\n", e.stackTrace().c_str());
}
}
}
} // namespace
#endif // ENABLE_BITTORRENT || ENABLE_METALINK
extern error_code::Value option_processing(Option& option, bool standalone,
std::vector<std::string>& uris,
int argc, char** argv,
const KeyVals& options);
Context::Context(bool standalone,
int argc, char** argv, const KeyVals& options)
{
std::vector<std::string> args;
SharedHandle<Option> op(new Option());
error_code::Value rv;
rv = option_processing(*op.get(), standalone, args, argc, argv, options);
if(rv != error_code::FINISHED) {
if(standalone) {
exit(rv);
} else {
throw DL_ABORT_EX("Option processing failed");
}
}
SimpleRandomizer::init();
#ifdef ENABLE_BITTORRENT
bittorrent::generateStaticPeerId(op->get(PREF_PEER_ID_PREFIX));
#endif // ENABLE_BITTORRENT
LogFactory::setLogFile(op->get(PREF_LOG));
LogFactory::setLogLevel(op->get(PREF_LOG_LEVEL));
LogFactory::setConsoleLogLevel(op->get(PREF_CONSOLE_LOG_LEVEL));
if(op->getAsBool(PREF_QUIET)) {
LogFactory::setConsoleOutput(false);
}
LogFactory::reconfigure();
A2_LOG_INFO("<<--- --- --- ---");
A2_LOG_INFO(" --- --- --- ---");
A2_LOG_INFO(" --- --- --- --->>");
A2_LOG_INFO(fmt("%s %s %s", PACKAGE, PACKAGE_VERSION, TARGET));
A2_LOG_INFO(MSG_LOGGING_STARTED);
if(op->getAsBool(PREF_DISABLE_IPV6)) {
SocketCore::setProtocolFamily(AF_INET);
// Get rid of AI_ADDRCONFIG. It causes name resolution error
// when none of network interface has IPv4 address.
setDefaultAIFlags(0);
}
net::checkAddrconfig();
// Bind interface
if(!op->get(PREF_INTERFACE).empty()) {
std::string iface = op->get(PREF_INTERFACE);
SocketCore::bindAddress(iface);
}
std::vector<SharedHandle<RequestGroup> > requestGroups;
SharedHandle<UriListParser> uriListParser;
#ifdef ENABLE_BITTORRENT
if(!op->blank(PREF_TORRENT_FILE)) {
if(op->get(PREF_SHOW_FILES) == A2_V_TRUE) {
showTorrentFile(op->get(PREF_TORRENT_FILE));
return;
} else {
createRequestGroupForBitTorrent(requestGroups, op, args,
op->get(PREF_TORRENT_FILE));
}
}
else
#endif // ENABLE_BITTORRENT
#ifdef ENABLE_METALINK
if(!op->blank(PREF_METALINK_FILE)) {
if(op->get(PREF_SHOW_FILES) == A2_V_TRUE) {
showMetalinkFile(op->get(PREF_METALINK_FILE), op);
return;
} else {
createRequestGroupForMetalink(requestGroups, op);
}
}
else
#endif // ENABLE_METALINK
if(!op->blank(PREF_INPUT_FILE)) {
if(op->getAsBool(PREF_DEFERRED_INPUT)) {
uriListParser = openUriListParser(op->get(PREF_INPUT_FILE));
} else {
createRequestGroupForUriList(requestGroups, op);
}
#if defined ENABLE_BITTORRENT || defined ENABLE_METALINK
} else if(op->get(PREF_SHOW_FILES) == A2_V_TRUE) {
showFiles(args, op);
return;
#endif // ENABLE_METALINK || ENABLE_METALINK
} else {
createRequestGroupForUri(requestGroups, op, args, false, false, true);
}
// Remove option values which is only valid for URIs specified in
// command-line. If they are left, because op is used as a template
// for new RequestGroup(such as created in RPC command), they causes
// unintentional effect.
for(SharedHandle<Option> i = op; i; i = i->getParent()) {
i->remove(PREF_OUT);
i->remove(PREF_FORCE_SEQUENTIAL);
i->remove(PREF_INPUT_FILE);
i->remove(PREF_INDEX_OUT);
i->remove(PREF_SELECT_FILE);
i->remove(PREF_PAUSE);
i->remove(PREF_CHECKSUM);
i->remove(PREF_GID);
}
if(standalone &&
!op->getAsBool(PREF_ENABLE_RPC) && requestGroups.empty() &&
!uriListParser) {
global::cout()->printf("%s\n", MSG_NO_FILES_TO_DOWNLOAD);
} else {
reqinfo.reset(new MultiUrlRequestInfo(requestGroups, op, getStatCalc(op),
getSummaryOut(op),
uriListParser));
}
}
Context::~Context()
{
}
} // namespace aria2

59
src/Context.h Normal file
View File

@ -0,0 +1,59 @@
/* <!-- copyright */
/*
* aria2 - The high speed download utility
*
* Copyright (C) 2013 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* 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.
*/
/* copyright --> */
#ifndef CONTEXT_H
#define CONTEXT_H
#include "common.h"
#include <aria2/aria2.h>
#include "SharedHandle.h"
namespace aria2 {
class MultiUrlRequestInfo;
struct Context {
// Set the |standalone| false if the context is created via libaria2
// API. The |argc| and |argv| is expected to the command-line
// arguments, which will be passed to getopt_long(3) in the end.
// The |options| is the additional option values and is considered
// as a part of command-line arguments.
Context(bool standalone, int argc, char** argv, const KeyVals& options);
~Context();
SharedHandle<MultiUrlRequestInfo> reqinfo;
};
} // namespace aria2
#endif // CONTEXT_H

View File

@ -86,8 +86,9 @@ volatile sig_atomic_t globalHaltRequested = 0;
DownloadEngine::DownloadEngine(const SharedHandle<EventPoll>& eventPoll)
: eventPoll_(eventPoll),
haltRequested_(0),
noWait_(false),
noWait_(true),
refreshInterval_(DEFAULT_REFRESH_INTERVAL),
lastRefresh_(0),
cookieStorage_(new CookieStorage()),
#ifdef ENABLE_BITTORRENT
btRegistry_(new BtRegistry()),
@ -139,29 +140,31 @@ void executeCommand(std::deque<Command*>& commands,
}
} // namespace
void DownloadEngine::run()
int DownloadEngine::run(bool oneshot)
{
Timer cp;
cp.reset(0);
while(!commands_.empty() || !routineCommands_.empty()) {
if(!commands_.empty()) {
waitData();
}
noWait_ = false;
global::wallclock().reset();
calculateStatistics();
if(cp.differenceInMillis(global::wallclock())+A2_DELTA_MILLIS >=
if(lastRefresh_.differenceInMillis(global::wallclock())+A2_DELTA_MILLIS >=
refreshInterval_) {
refreshInterval_ = DEFAULT_REFRESH_INTERVAL;
cp = global::wallclock();
lastRefresh_ = global::wallclock();
executeCommand(commands_, Command::STATUS_ALL);
} else {
executeCommand(commands_, Command::STATUS_ACTIVE);
}
executeCommand(routineCommands_, Command::STATUS_ALL);
afterEachIteration();
if(!commands_.empty()) {
waitData();
if(!noWait_ && oneshot) {
return 1;
}
noWait_ = false;
}
onEndOfRun();
return 0;
}
void DownloadEngine::waitData()

View File

@ -124,6 +124,7 @@ private:
// Milliseconds
int64_t refreshInterval_;
Timer lastRefresh_;
std::deque<Command*> routineCommands_;
@ -167,7 +168,11 @@ public:
~DownloadEngine();
void run();
// If oneshot is true, this function returns after one event polling
// and performing action for them. This function returns 1 when
// oneshot is true and there are still downloads to be
// processed. Otherwise, returns 0.
int run(bool oneshot=false);
void cleanQueue();

View File

@ -60,7 +60,7 @@ bool FillRequestGroupCommand::execute()
if(e_->isHaltRequested()) {
return true;
}
SharedHandle<RequestGroupMan> rgman = e_->getRequestGroupMan();
const SharedHandle<RequestGroupMan>& rgman = e_->getRequestGroupMan();
if(rgman->queueCheckRequested()) {
while(rgman->queueCheckRequested()) {
try {

59
src/KeepRunningCommand.cc Normal file
View File

@ -0,0 +1,59 @@
/* <!-- copyright */
/*
* aria2 - The high speed download utility
*
* Copyright (C) 2013 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* 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.
*/
/* copyright --> */
#include "KeepRunningCommand.h"
#include "DownloadEngine.h"
#include "RequestGroupMan.h"
namespace aria2 {
KeepRunningCommand::KeepRunningCommand(cuid_t cuid, DownloadEngine* e)
: Command(cuid),
e_(e)
{
setStatusRealtime();
}
KeepRunningCommand::~KeepRunningCommand() {}
bool KeepRunningCommand::execute()
{
if(e_->isHaltRequested()) {
return true;
}
e_->addCommand(this);
return false;
}
} // namespace aria2

57
src/KeepRunningCommand.h Normal file
View File

@ -0,0 +1,57 @@
/* <!-- copyright */
/*
* aria2 - The high speed download utility
*
* Copyright (C) 2013 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* 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.
*/
/* copyright --> */
#ifndef KEEP_RUNNING_COMMAND_H
#define KEEP_RUNNING_COMMAND_H
#include "Command.h"
namespace aria2 {
class DownloadEngine;
// This object does nothing but it is added to DownloadEngine command
// queue (not routine one), and keeps event polling work.
class KeepRunningCommand : public Command {
public:
KeepRunningCommand(cuid_t cuid, DownloadEngine* e);
virtual ~KeepRunningCommand();
virtual bool execute();
private:
DownloadEngine* e_;
};
} // namespace aria2
#endif // KEEP_RUNNING_COMMAND_H

View File

@ -1,8 +1,10 @@
SUBDIRS = includes
bin_PROGRAMS = aria2c
aria2c_SOURCES = main.cc\
option_processing.cc\
version_usage.cc
SRCS = SocketCore.cc SocketCore.h\
aria2c_SOURCES = main.cc
SRCS = option_processing.cc\
version_usage.cc\
Context.cc Context.h\
SocketCore.cc SocketCore.h\
BinaryStream.h\
Command.cc Command.h\
AbstractCommand.cc AbstractCommand.h\
@ -634,10 +636,20 @@ SRCS += LibuvEventPoll.cc LibuvEventPoll.h
endif # HAVE_LIBUV
AR = @AR@
noinst_LIBRARIES = libaria2c.a
libaria2c_a_SOURCES = $(SRCS)
aria2c_LDADD = libaria2c.a @LIBINTL@ @ALLOCA@ #-lprofiler
pkgconfigdir = $(libdir)/pkgconfig
pkgconfig_DATA = libaria2.pc
DISTCLEANFILES = $(pkgconfig_DATA)
lib_LTLIBRARIES = libaria2.la
libaria2_la_SOURCES = $(SRCS) \
aria2api.cc aria2api.h \
KeepRunningCommand.cc KeepRunningCommand.h
libaria2_la_LIBADD = @WSLAY_LIBS@
LDADD = libaria2.la @LIBINTL@ @ALLOCA@ #-lprofiler
#aria2c_LDFLAGS = -pg
AM_CPPFLAGS = -Wall\
-I$(top_srcdir)/lib -I$(top_srcdir)/intl\
-I$(srcdir)/includes -I$(builddir)/includes\
-DLOCALEDIR=\"@localedir@\" -DCA_BUNDLE=\"$(ca_bundle)\" @DEFS@ #-pg

View File

@ -109,12 +109,15 @@ MultiUrlRequestInfo::MultiUrlRequestInfo
const SharedHandle<StatCalc>& statCalc,
const SharedHandle<OutputFile>& summaryOut,
const SharedHandle<UriListParser>& uriListParser)
: requestGroups_(requestGroups),
option_(op),
: option_(op),
statCalc_(statCalc),
summaryOut_(summaryOut),
uriListParser_(uriListParser)
{}
uriListParser_(uriListParser),
// TODO init mask_
useSignalHandler_(true)
{
requestGroups_.swap(requestGroups);
}
MultiUrlRequestInfo::~MultiUrlRequestInfo() {}
@ -126,17 +129,15 @@ void MultiUrlRequestInfo::printMessageForContinue()
_("If there are any errors, then see the log file. See '-l' option in help/man page for details."));
}
error_code::Value MultiUrlRequestInfo::execute()
int MultiUrlRequestInfo::prepare()
{
error_code::Value returnValue = error_code::FINISHED;
sigset_t mask;
try {
SharedHandle<rpc::WebSocketSessionMan> wsSessionMan;
if(option_->getAsBool(PREF_ENABLE_RPC)) {
wsSessionMan.reset(new rpc::WebSocketSessionMan());
}
Notifier notifier(wsSessionMan);
SingletonHolder<Notifier>::instance(&notifier);
SharedHandle<Notifier> notifier(new Notifier(wsSessionMan));
SingletonHolder<Notifier>::instance(notifier);
#ifdef ENABLE_SSL
if(option_->getAsBool(PREF_ENABLE_RPC) &&
@ -159,15 +160,16 @@ error_code::Value MultiUrlRequestInfo::execute()
}
#endif // ENABLE_SSL
SharedHandle<DownloadEngine> e =
DownloadEngineFactory().newDownloadEngine(option_.get(), requestGroups_);
e_ = DownloadEngineFactory().newDownloadEngine(option_.get(),
requestGroups_);
// Avoid keeping RequestGroups alive longer than necessary
requestGroups_.clear();
if(!option_->blank(PREF_LOAD_COOKIES)) {
File cookieFile(option_->get(PREF_LOAD_COOKIES));
if(cookieFile.isFile() &&
e->getCookieStorage()->load(cookieFile.getPath(), Time().getTime())) {
e_->getCookieStorage()->load(cookieFile.getPath(),
Time().getTime())) {
A2_LOG_INFO(fmt("Loaded cookies from '%s'.",
cookieFile.getPath().c_str()));
} else {
@ -194,7 +196,7 @@ error_code::Value MultiUrlRequestInfo::execute()
authConfigFactory->setNetrc(netrc);
}
}
e->setAuthConfigFactory(authConfigFactory);
e_->setAuthConfigFactory(authConfigFactory);
#ifdef ENABLE_SSL
SharedHandle<TLSContext> clTlsContext(TLSContext::make(TLS_CLIENT));
@ -220,7 +222,7 @@ error_code::Value MultiUrlRequestInfo::execute()
#ifdef HAVE_ARES_ADDR_NODE
ares_addr_node* asyncDNSServers =
parseAsyncDNSServers(option_->get(PREF_ASYNC_DNS_SERVER));
e->setAsyncDNSServers(asyncDNSServers);
e_->setAsyncDNSServers(asyncDNSServers);
#endif // HAVE_ARES_ADDR_NODE
if(!Timer::monotonicClock()) {
A2_LOG_WARN("Don't change system time while aria2c is running."
@ -229,48 +231,45 @@ error_code::Value MultiUrlRequestInfo::execute()
std::string serverStatIf = option_->get(PREF_SERVER_STAT_IF);
if(!serverStatIf.empty()) {
e->getRequestGroupMan()->loadServerStat(serverStatIf);
e->getRequestGroupMan()->removeStaleServerStat
e_->getRequestGroupMan()->loadServerStat(serverStatIf);
e_->getRequestGroupMan()->removeStaleServerStat
(option_->getAsInt(PREF_SERVER_STAT_TIMEOUT));
}
e->setStatCalc(statCalc_);
e_->setStatCalc(statCalc_);
if(uriListParser_) {
e->getRequestGroupMan()->setUriListParser(uriListParser_);
e_->getRequestGroupMan()->setUriListParser(uriListParser_);
}
#ifdef HAVE_SIGACTION
sigemptyset(&mask);
sigaddset(&mask, SIGINT);
sigaddset(&mask, SIGTERM);
#ifdef SIGHUP
sigaddset(&mask, SIGHUP);
#endif // SIGHUP
#else // !HAVE_SIGACTION
mask = 0;
#endif // !HAVE_SIGACTION
#ifdef SIGHUP
util::setGlobalSignalHandler(SIGHUP, &mask, handler, 0);
#endif // SIGHUP
util::setGlobalSignalHandler(SIGINT, &mask, handler, 0);
util::setGlobalSignalHandler(SIGTERM, &mask, handler, 0);
e->getRequestGroupMan()->getNetStat().downloadStart();
e->run();
if(useSignalHandler_) {
setupSignalHandlers();
}
e_->getRequestGroupMan()->getNetStat().downloadStart();
} catch(RecoverableException& e) {
SingletonHolder<Notifier>::clear();
if(useSignalHandler_) {
resetSignalHandlers();
}
return -1;
}
return 0;
}
error_code::Value MultiUrlRequestInfo::getResult()
{
error_code::Value returnValue = error_code::FINISHED;
if(!option_->blank(PREF_SAVE_COOKIES)) {
e->getCookieStorage()->saveNsFormat(option_->get(PREF_SAVE_COOKIES));
e_->getCookieStorage()->saveNsFormat(option_->get(PREF_SAVE_COOKIES));
}
const std::string& serverStatOf = option_->get(PREF_SERVER_STAT_OF);
if(!serverStatOf.empty()) {
e->getRequestGroupMan()->saveServerStat(serverStatOf);
e_->getRequestGroupMan()->saveServerStat(serverStatOf);
}
e->getRequestGroupMan()->showDownloadResults
e_->getRequestGroupMan()->showDownloadResults
(*summaryOut_, option_->get(PREF_DOWNLOAD_RESULT) == A2_V_FULL);
summaryOut_->flush();
RequestGroupMan::DownloadStat s =
e->getRequestGroupMan()->getDownloadStat();
e_->getRequestGroupMan()->getDownloadStat();
if(!s.allCompleted()) {
printMessageForContinue();
if(s.getLastErrorResult() == error_code::FINISHED &&
@ -280,7 +279,7 @@ error_code::Value MultiUrlRequestInfo::execute()
returnValue = s.getLastErrorResult();
}
}
SessionSerializer sessionSerializer(e->getRequestGroupMan());
SessionSerializer sessionSerializer(e_->getRequestGroupMan());
// TODO Add option: --save-session-status=error,inprogress,waiting
if(!option_->blank(PREF_SAVE_SESSION)) {
const std::string& filename = option_->get(PREF_SAVE_SESSION);
@ -292,23 +291,83 @@ error_code::Value MultiUrlRequestInfo::execute()
filename.c_str()));
}
}
} catch(RecoverableException& e) {
if(returnValue == error_code::FINISHED) {
returnValue = error_code::UNKNOWN_ERROR;
}
A2_LOG_ERROR_EX(EX_EXCEPTION_CAUGHT, e);
}
SingletonHolder<Notifier>::instance(0);
#ifdef HAVE_SIGACTION
sigemptyset(&mask);
#endif // HAVE_SIGACTION
#ifdef SIGHUP
util::setGlobalSignalHandler(SIGHUP, &mask, SIG_DFL, 0);
#endif // SIGHUP
util::setGlobalSignalHandler(SIGINT, &mask, SIG_DFL, 0);
util::setGlobalSignalHandler(SIGTERM, &mask, SIG_DFL, 0);
SingletonHolder<Notifier>::clear();
return returnValue;
}
error_code::Value MultiUrlRequestInfo::execute()
{
if(prepare() != 0) {
return error_code::UNKNOWN_ERROR;
}
// TODO Enclosed in try..catch block for just in case. Really need
// this?
try {
e_->run();
} catch(RecoverableException& e) {
A2_LOG_ERROR_EX(EX_EXCEPTION_CAUGHT, e);
}
error_code::Value returnValue = getResult();
if(useSignalHandler_) {
resetSignalHandlers();
}
return returnValue;
}
void MultiUrlRequestInfo::setupSignalHandlers()
{
#ifdef HAVE_SIGACTION
sigemptyset(&mask_);
#else // !HAVE_SIGACTION
mask_ = 0;
#endif // !HAVE_SIGACTION
#ifdef SIGPIPE
util::setGlobalSignalHandler(SIGPIPE, &mask_, SIG_IGN, 0);
#endif // SIGPIPE
#ifdef SIGCHLD
// Avoid to create zombie process when forked child processes are
// died.
util::setGlobalSignalHandler(SIGCHLD, &mask_, SIG_IGN, 0);
#endif // SIGCHILD
#ifdef HAVE_SIGACTION
sigaddset(&mask_, SIGINT);
sigaddset(&mask_, SIGTERM);
# ifdef SIGHUP
sigaddset(&mask_, SIGHUP);
# endif // SIGHUP
#endif // HAVE_SIGACTION
#ifdef SIGHUP
util::setGlobalSignalHandler(SIGHUP, &mask_, handler, 0);
#endif // SIGHUP
util::setGlobalSignalHandler(SIGINT, &mask_, handler, 0);
util::setGlobalSignalHandler(SIGTERM, &mask_, handler, 0);
}
void MultiUrlRequestInfo::resetSignalHandlers()
{
#ifdef HAVE_SIGACTION
sigemptyset(&mask_);
#endif // HAVE_SIGACTION
#ifdef SIGHUP
util::setGlobalSignalHandler(SIGHUP, &mask_, SIG_DFL, 0);
#endif // SIGHUP
util::setGlobalSignalHandler(SIGINT, &mask_, SIG_DFL, 0);
util::setGlobalSignalHandler(SIGTERM, &mask_, SIG_DFL, 0);
#ifdef SIGCHLD
util::setGlobalSignalHandler(SIGCHLD, &mask_, SIG_DFL, 0);
#endif // SIGCHILD
#ifdef SIGPIPE
util::setGlobalSignalHandler(SIGPIPE, &mask_, SIG_DFL, 0);
#endif // SIGPIPE
}
const SharedHandle<DownloadEngine>&
MultiUrlRequestInfo::getDownloadEngine() const
{
return e_;
}
} // namespace aria2

View File

@ -37,10 +37,13 @@
#include "common.h"
#include <signal.h>
#include <vector>
#include "SharedHandle.h"
#include "DownloadResult.h"
#include "util.h"
namespace aria2 {
@ -49,10 +52,11 @@ class Option;
class StatCalc;
class OutputFile;
class UriListParser;
class DownloadEngine;
class MultiUrlRequestInfo {
private:
std::vector<SharedHandle<RequestGroup> >& requestGroups_;
std::vector<SharedHandle<RequestGroup> > requestGroups_;
SharedHandle<Option> option_;
@ -62,7 +66,15 @@ private:
SharedHandle<UriListParser> uriListParser_;
SharedHandle<DownloadEngine> e_;
sigset_t mask_;
bool useSignalHandler_;
void printMessageForContinue();
void setupSignalHandlers();
void resetSignalHandlers();
public:
/*
* MultiRequestInfo effectively takes ownership of the
@ -77,11 +89,31 @@ public:
virtual ~MultiUrlRequestInfo();
/**
* Returns FINISHED if all downloads have completed, otherwise returns the
* last download result.
*/
// Returns FINISHED if all downloads have completed, otherwise returns the
// last download result.
//
// This method actually calls prepare() and
// getDownloadEngine()->run(true) and getResult().
error_code::Value execute();
// Performs preparations for downloads, including creating
// DownloadEngine instance. This function returns 0 if it succeeds,
// or -1.
int prepare();
// Performs finalization of download process, including saving
// sessions. This function returns last error code in this session,
// in particular, this function returns FINISHED if all downloads
// have completed.
error_code::Value getResult();
const SharedHandle<DownloadEngine>& getDownloadEngine() const;
// Signal handlers are not prepared if false is given.
void setUseSignalHandler(bool useSignalHandler)
{
useSignalHandler_ = useSignalHandler;
}
};
} // namespace aria2

View File

@ -43,8 +43,8 @@ class NullOutputFile:public OutputFile {
public:
virtual ~NullOutputFile() {}
virtual size_t write(const char* str) { return 0; }
virtual int vprintf(const char* format, va_list va) { return 0; }
virtual int flush() { return 0; }
virtual int vprintf(const char* format, va_list va) { return 0; }
virtual bool supportsColor() { return false; }
};

View File

@ -245,6 +245,20 @@ void OptionParser::parse(Option& option, std::istream& is) const
}
}
void OptionParser::parse(Option& option, const KeyVals& options) const
{
for(KeyVals::const_iterator i = options.begin(), eoi = options.end();
i != eoi; ++i) {
const Pref* pref = option::k2p((*i).first);
const OptionHandler* handler = find(pref);
if(handler) {
handler->parse(option, (*i).second);
} else {
A2_LOG_WARN(fmt("Unknown option: %s", (*i).first.c_str()));
}
}
}
void OptionParser::setOptionHandlers
(const std::vector<OptionHandler*>& handlers)
{

View File

@ -41,6 +41,7 @@
#include <vector>
#include <iosfwd>
#include <aria2/aria2.h>
#include "SharedHandle.h"
namespace aria2 {
@ -67,6 +68,8 @@ public:
void parse(Option& option, std::istream& ios) const;
void parse(Option& option, const KeyVals& options) const;
void parseDefaultValues(Option& option) const;
void setOptionHandlers

View File

@ -107,7 +107,7 @@ RequestGroupMan::RequestGroupMan
(option->getAsInt(PREF_MAX_OVERALL_DOWNLOAD_LIMIT)),
maxOverallUploadSpeedLimit_(option->getAsInt
(PREF_MAX_OVERALL_UPLOAD_LIMIT)),
rpc_(option->getAsBool(PREF_ENABLE_RPC)),
keepRunning_(option->getAsBool(PREF_ENABLE_RPC)),
queueCheck_(true),
removedErrorResult_(0),
removedLastErrorResult_(error_code::FINISHED),
@ -125,7 +125,7 @@ RequestGroupMan::~RequestGroupMan()
bool RequestGroupMan::downloadFinished()
{
if(rpc_) {
if(keepRunning_) {
return false;
}
return requestGroups_.empty() && reservedGroups_.empty();
@ -214,9 +214,8 @@ void notifyDownloadEvent
(const std::string& event, const SharedHandle<RequestGroup>& group)
{
// Check NULL to make unit test easier.
Notifier* notifier = SingletonHolder<Notifier>::instance();
if(notifier) {
notifier->notifyDownloadEvent(event, group);
if(SingletonHolder<Notifier>::instance()) {
SingletonHolder<Notifier>::instance()->notifyDownloadEvent(event, group);
}
}
@ -475,7 +474,7 @@ void RequestGroupMan::fillRequestGroupFromReserver(DownloadEngine* e)
}
SharedHandle<RequestGroup> groupToAdd = *reservedGroups_.begin();
reservedGroups_.pop_front();
if((rpc_ && groupToAdd->isPauseRequested()) ||
if((keepRunning_ && groupToAdd->isPauseRequested()) ||
!groupToAdd->isDependencyResolved()) {
pending.push_back(groupToAdd);
continue;

View File

@ -84,8 +84,9 @@ private:
NetStat netStat_;
// true if JSON-RPC/XML-RPC is enabled.
bool rpc_;
// true if download engine should keep running even if there is no
// download to perform.
bool keepRunning_;
bool queueCheck_;
@ -335,6 +336,16 @@ public:
// Initializes WrDiskCache according to PREF_DISK_CACHE option. If
// its value is 0, cache storage will not be initialized.
void initWrDiskCache();
void setKeepRunning(bool flag)
{
keepRunning_ = flag;
}
bool getKeepRunning() const
{
return keepRunning_;
}
};
} // namespace aria2

View File

@ -421,32 +421,6 @@ SharedHandle<ValueBase> ForceRemoveRpcMethod::process
return removeDownload(req, e, true);
}
namespace {
bool pauseRequestGroup
(const SharedHandle<RequestGroup>& group, bool reserved, bool forcePause)
{
if((reserved && !group->isPauseRequested()) ||
(!reserved &&
!group->isForceHaltRequested() &&
((forcePause && group->isHaltRequested() && group->isPauseRequested()) ||
(!group->isHaltRequested() && !group->isPauseRequested())))) {
if(!reserved) {
// Call setHaltRequested before setPauseRequested because
// setHaltRequested calls setPauseRequested(false) internally.
if(forcePause) {
group->setForceHaltRequested(true, RequestGroup::NONE);
} else {
group->setHaltRequested(true, RequestGroup::NONE);
}
}
group->setPauseRequested(true);
return true;
} else {
return false;
}
}
} // namespace
namespace {
SharedHandle<ValueBase> pauseDownload
(const RpcRequest& req, DownloadEngine* e, bool forcePause)
@ -1546,4 +1520,28 @@ SharedHandle<ValueBase> NoSuchMethodRpcMethod::process
} // namespace rpc
bool pauseRequestGroup
(const SharedHandle<RequestGroup>& group, bool reserved, bool forcePause)
{
if((reserved && !group->isPauseRequested()) ||
(!reserved &&
!group->isForceHaltRequested() &&
((forcePause && group->isHaltRequested() && group->isPauseRequested()) ||
(!group->isHaltRequested() && !group->isPauseRequested())))) {
if(!reserved) {
// Call setHaltRequested before setPauseRequested because
// setHaltRequested calls setPauseRequested(false) internally.
if(forcePause) {
group->setForceHaltRequested(true, RequestGroup::NONE);
} else {
group->setHaltRequested(true, RequestGroup::NONE);
}
}
group->setPauseRequested(true);
return true;
} else {
return false;
}
}
} // namespace aria2

View File

@ -605,6 +605,9 @@ void gatherBitTorrentMetadata
} // namespace rpc
bool pauseRequestGroup
(const SharedHandle<RequestGroup>& group, bool reserved, bool forcePause);
} // namespace aria2
#endif // D_RPC_METHOD_IMPL_H

View File

@ -35,30 +35,37 @@
#ifndef D_SINGLETON_HOLDER_H
#define D_SINGLETON_HOLDER_H
#include "SharedHandle.h"
namespace aria2 {
template<typename T>
class SingletonHolder {
private:
static T* instance_;
static SharedHandle<T> instance_;
SingletonHolder() {}
public:
~SingletonHolder() {}
static T* instance()
static const SharedHandle<T>& instance()
{
return instance_;
}
static void instance(T* instance)
static void instance(const SharedHandle<T>& instance)
{
instance_ = instance;
}
static void clear()
{
instance_.reset();
}
};
template<typename T>
T* SingletonHolder<T>::instance_ = 0;
SharedHandle<T> SingletonHolder<T>::instance_;
} // namespace aria2

637
src/aria2api.cc Normal file
View File

@ -0,0 +1,637 @@
/* <!-- copyright */
/*
* aria2 - The high speed download utility
*
* Copyright (C) 2013 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* 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.
*/
/* copyright --> */
#include "aria2api.h"
#include <functional>
#include "Platform.h"
#include "Context.h"
#include "DownloadEngine.h"
#include "OptionParser.h"
#include "Option.h"
#include "DlAbortEx.h"
#include "fmt.h"
#include "OptionHandler.h"
#include "RequestGroupMan.h"
#include "RequestGroup.h"
#include "MultiUrlRequestInfo.h"
#include "prefs.h"
#include "download_helper.h"
#include "LogFactory.h"
#include "PieceStorage.h"
#include "DownloadContext.h"
#include "FileEntry.h"
#include "BitfieldMan.h"
#include "DownloadContext.h"
#include "RpcMethodImpl.h"
#include "console.h"
#include "KeepRunningCommand.h"
namespace aria2 {
Session::Session(const KeyVals& options)
: context(new Context(false, 0, 0, options))
{}
Session::~Session()
{}
SessionConfig::SessionConfig()
: keepRunning(false),
useSignalHandler(true)
{}
namespace {
Platform* platform = 0;
} // namespace
int libraryInit()
{
global::initConsole(true);
try {
platform = new Platform();
} catch(RecoverableException& e) {
A2_LOG_ERROR_EX(EX_EXCEPTION_CAUGHT, e);
return -1;
}
LogFactory::setConsoleOutput(false);
return 0;
}
int libraryDeinit()
{
delete platform;
return 0;
}
Session* sessionNew(const KeyVals& options, const SessionConfig& config)
{
int rv;
Session* session;
try {
session = new Session(options);
} catch(RecoverableException& e) {
return 0;
}
if(session->context->reqinfo) {
if(!config.useSignalHandler) {
session->context->reqinfo->setUseSignalHandler(false);
}
rv = session->context->reqinfo->prepare();
if(rv != 0) {
delete session;
session = 0;
}
const SharedHandle<DownloadEngine>& e =
session->context->reqinfo->getDownloadEngine();
if(config.keepRunning) {
e->getRequestGroupMan()->setKeepRunning(true);
// Add command to make aria2 keep event polling
e->addCommand(new KeepRunningCommand(e->newCUID(), e.get()));
}
} else {
delete session;
session = 0;
}
return session;
}
int sessionFinal(Session* session)
{
error_code::Value rv = session->context->reqinfo->getResult();
delete session;
return rv;
}
int run(Session* session, RUN_MODE mode)
{
const SharedHandle<DownloadEngine>& e =
session->context->reqinfo->getDownloadEngine();
return e->run(mode == RUN_ONCE);
}
int shutdown(Session* session, bool force)
{
const SharedHandle<DownloadEngine>& e =
session->context->reqinfo->getDownloadEngine();
if(force) {
e->requestForceHalt();
} else {
e->requestHalt();
}
// Skip next polling timeout. This avoids 1 second delay when there
// is no Command other than KeepRunningCommand in the queue.
e->setNoWait(true);
return 0;
}
std::string gidToHex(const A2Gid& gid)
{
return GroupId::toHex(gid);
}
A2Gid hexToGid(const std::string& hex)
{
A2Gid gid;
if(GroupId::toNumericId(gid, hex.c_str()) == 0) {
return gid;
} else {
return 0;
}
}
bool isNull(const A2Gid& gid)
{
return gid == 0;
}
namespace {
template<typename InputIterator, typename Pred>
void apiGatherOption
(InputIterator first, InputIterator last,
Pred pred,
Option* option,
const SharedHandle<OptionParser>& optionParser)
{
for(; first != last; ++first) {
const std::string& optionName = (*first).first;
const Pref* pref = option::k2p(optionName);
if(!pref) {
throw DL_ABORT_EX
(fmt("We don't know how to deal with %s option", optionName.c_str()));
}
const OptionHandler* handler = optionParser->find(pref);
if(!handler || !pred(handler)) {
// Just ignore the unacceptable options in this context.
continue;
}
handler->parse(*option, (*first).second);
}
}
} // namespace
namespace {
void apiGatherRequestOption(Option* option, const KeyVals& options,
const SharedHandle<OptionParser>& optionParser)
{
apiGatherOption(options.begin(), options.end(),
std::mem_fun(&OptionHandler::getInitialOption),
option, optionParser);
}
} // namespace
namespace {
void addRequestGroup(const SharedHandle<RequestGroup>& group,
const SharedHandle<DownloadEngine>& e,
int position)
{
if(position >= 0) {
e->getRequestGroupMan()->insertReservedGroup(position, group);
} else {
e->getRequestGroupMan()->addReservedGroup(group);
}
}
} // namespace
int addUri(Session* session,
A2Gid& gid,
const std::vector<std::string>& uris,
const KeyVals& options,
int position)
{
const SharedHandle<DownloadEngine>& e =
session->context->reqinfo->getDownloadEngine();
SharedHandle<Option> requestOption(new Option(*e->getOption()));
try {
apiGatherRequestOption(requestOption.get(), options,
OptionParser::getInstance());
} catch(RecoverableException& e) {
A2_LOG_INFO_EX(EX_EXCEPTION_CAUGHT, e);
return -1;
}
std::vector<SharedHandle<RequestGroup> > result;
createRequestGroupForUri(result, requestOption, uris,
/* ignoreForceSeq = */ true,
/* ignoreLocalPath = */ true);
if(!result.empty()) {
gid = result.front()->getGID();
addRequestGroup(result.front(), e, position);
}
return 0;
}
int addMetalink(Session* session,
std::vector<A2Gid>& gids,
const std::string& metalinkFile,
const KeyVals& options,
int position)
{
#ifdef ENABLE_METALINK
const SharedHandle<DownloadEngine>& e =
session->context->reqinfo->getDownloadEngine();
SharedHandle<Option> requestOption(new Option(*e->getOption()));
std::vector<SharedHandle<RequestGroup> > result;
try {
apiGatherRequestOption(requestOption.get(), options,
OptionParser::getInstance());
requestOption->put(PREF_METALINK_FILE, metalinkFile);
createRequestGroupForMetalink(result, requestOption);
} catch(RecoverableException& e) {
A2_LOG_INFO_EX(EX_EXCEPTION_CAUGHT, e);
return -1;
}
if(!result.empty()) {
if(position >= 0) {
e->getRequestGroupMan()->insertReservedGroup(position, result);
} else {
e->getRequestGroupMan()->addReservedGroup(result);
}
for(std::vector<SharedHandle<RequestGroup> >::const_iterator i =
result.begin(), eoi = result.end(); i != eoi; ++i) {
gids.push_back((*i)->getGID());
}
}
return 0;
#else // !ENABLE_METALINK
return -1;
#endif // !ENABLE_METALINK
}
int removeDownload(Session* session, const A2Gid& gid, bool force)
{
const SharedHandle<DownloadEngine>& e =
session->context->reqinfo->getDownloadEngine();
SharedHandle<RequestGroup> group = e->getRequestGroupMan()->findGroup(gid);
if(group) {
if(group->getState() == RequestGroup::STATE_ACTIVE) {
if(force) {
group->setForceHaltRequested(true, RequestGroup::USER_REQUEST);
} else {
group->setHaltRequested(true, RequestGroup::USER_REQUEST);
}
e->setRefreshInterval(0);
} else {
if(group->isDependencyResolved()) {
e->getRequestGroupMan()->removeReservedGroup(gid);
} else {
return -1;
}
}
} else {
return -1;
}
return 0;
}
int pauseDownload(Session* session, const A2Gid& gid, bool force)
{
const SharedHandle<DownloadEngine>& e =
session->context->reqinfo->getDownloadEngine();
SharedHandle<RequestGroup> group = e->getRequestGroupMan()->findGroup(gid);
if(group) {
bool reserved = group->getState() == RequestGroup::STATE_WAITING;
if(pauseRequestGroup(group, reserved, force)) {
e->setRefreshInterval(0);
return 0;
}
}
return -1;
}
int unpauseDownload(Session* session, const A2Gid& gid)
{
const SharedHandle<DownloadEngine>& e =
session->context->reqinfo->getDownloadEngine();
SharedHandle<RequestGroup> group = e->getRequestGroupMan()->findGroup(gid);
if(!group ||
group->getState() != RequestGroup::STATE_WAITING ||
!group->isPauseRequested()) {
return -1;
} else {
group->setPauseRequested(false);
e->getRequestGroupMan()->requestQueueCheck();
}
return 0;
}
std::vector<A2Gid> getActiveDownload(Session* session)
{
const SharedHandle<DownloadEngine>& e =
session->context->reqinfo->getDownloadEngine();
const RequestGroupList& groups = e->getRequestGroupMan()->getRequestGroups();
std::vector<A2Gid> res;
for(RequestGroupList::const_iterator i = groups.begin(),
eoi = groups.end(); i != eoi; ++i) {
res.push_back((*i)->getGID());
}
return res;
}
namespace {
template<typename OutputIterator, typename InputIterator>
void createUriEntry
(OutputIterator out,
InputIterator first, InputIterator last,
UriStatus status)
{
for(; first != last; ++first) {
UriData uriData;
uriData.uri = *first;
uriData.status = status;
out++ = uriData;
}
}
} // namespace
namespace {
template<typename OutputIterator>
void createUriEntry
(OutputIterator out, const SharedHandle<FileEntry>& file)
{
createUriEntry(out,
file->getSpentUris().begin(),
file->getSpentUris().end(),
URI_USED);
createUriEntry(out,
file->getRemainingUris().begin(),
file->getRemainingUris().end(),
URI_WAITING);
}
} // namespace
namespace {
template<typename OutputIterator, typename InputIterator>
void createFileEntry
(OutputIterator out,
InputIterator first, InputIterator last,
const BitfieldMan* bf)
{
size_t index = 1;
for(; first != last; ++first) {
FileData file;
file.index = index++;
file.path = (*first)->getPath();
file.length = (*first)->getLength();
file.completedLength = bf->getOffsetCompletedLength
((*first)->getOffset(), (*first)->getLength());
file.selected = (*first)->isRequested();
createUriEntry(std::back_inserter(file.uris), *first);
out++ = file;
}
}
} // namespace
namespace {
template<typename OutputIterator, typename InputIterator>
void createFileEntry
(OutputIterator out,
InputIterator first, InputIterator last,
int64_t totalLength,
int32_t pieceLength,
const std::string& bitfield)
{
BitfieldMan bf(pieceLength, totalLength);
bf.setBitfield(reinterpret_cast<const unsigned char*>(bitfield.data()),
bitfield.size());
createFileEntry(out, first, last, &bf);
}
} // namespace
namespace {
template<typename OutputIterator, typename InputIterator>
void createFileEntry
(OutputIterator out,
InputIterator first, InputIterator last,
int64_t totalLength,
int32_t pieceLength,
const SharedHandle<PieceStorage>& ps)
{
BitfieldMan bf(pieceLength, totalLength);
if(ps) {
bf.setBitfield(ps->getBitfield(), ps->getBitfieldLength());
}
createFileEntry(out, first, last, &bf);
}
} // namespace
namespace {
struct RequestGroupDH : public DownloadHandle {
RequestGroupDH(const SharedHandle<RequestGroup>& group)
: group(group),
ts(group->calculateStat())
{}
virtual ~RequestGroupDH() {}
virtual DownloadStatus getStatus()
{
if(group->getState() == RequestGroup::STATE_ACTIVE) {
return DOWNLOAD_ACTIVE;
} else {
if(group->isPauseRequested()) {
return DOWNLOAD_PAUSED;
} else {
return DOWNLOAD_WAITING;
}
}
}
virtual int64_t getTotalLength()
{
return group->getTotalLength();
}
virtual int64_t getCompletedLength()
{
return group->getCompletedLength();
}
virtual int64_t getUploadLength()
{
return ts.allTimeUploadLength;
}
virtual std::string getBitfield()
{
const SharedHandle<PieceStorage>& ps = group->getPieceStorage();
if(ps) {
return std::string(reinterpret_cast<const char*>(ps->getBitfield()),
ps->getBitfieldLength());
} else {
return "";
}
}
virtual int getDownloadSpeed()
{
return ts.downloadSpeed;
}
virtual int getUploadSpeed()
{
return ts.uploadSpeed;
}
virtual size_t getNumPieces()
{
return group->getDownloadContext()->getNumPieces();
}
virtual int getConnections()
{
return group->getNumConnection();
}
virtual int getErrorCode()
{
return 0;
}
virtual const std::vector<A2Gid>& getFollowedBy()
{
return group->followedBy();
}
virtual A2Gid getBelongsTo()
{
return group->belongsTo();
}
virtual const std::string& getDir()
{
return group->getOption()->get(PREF_DIR);
}
virtual std::vector<FileData> getFiles()
{
std::vector<FileData> res;
const SharedHandle<DownloadContext>& dctx = group->getDownloadContext();
createFileEntry(std::back_inserter(res),
dctx->getFileEntries().begin(),
dctx->getFileEntries().end(),
dctx->getTotalLength(), dctx->getPieceLength(),
group->getPieceStorage());
return res;
}
SharedHandle<RequestGroup> group;
TransferStat ts;
};
} // namespace
namespace {
struct DownloadResultDH : public DownloadHandle {
DownloadResultDH(const SharedHandle<DownloadResult>& dr)
: dr(dr)
{}
virtual ~DownloadResultDH() {}
virtual DownloadStatus getStatus()
{
switch(dr->result) {
case error_code::FINISHED:
return DOWNLOAD_COMPLETE;
case error_code::REMOVED:
return DOWNLOAD_REMOVED;
default:
return DOWNLOAD_ERROR;
}
}
virtual int64_t getTotalLength()
{
return dr->totalLength;
}
virtual int64_t getCompletedLength()
{
return dr->completedLength;
}
virtual int64_t getUploadLength()
{
return dr->uploadLength;
}
virtual std::string getBitfield()
{
return dr->bitfield;
}
virtual int getDownloadSpeed()
{
return 0;
}
virtual int getUploadSpeed()
{
return 0;
}
virtual size_t getNumPieces()
{
return dr->numPieces;
}
virtual int getConnections()
{
return 0;
}
virtual int getErrorCode()
{
return dr->result;
}
virtual const std::vector<A2Gid>& getFollowedBy()
{
return dr->followedBy;
}
virtual A2Gid getBelongsTo()
{
return dr->belongsTo;
}
virtual const std::string& getDir()
{
return dr->dir;
}
virtual std::vector<FileData> getFiles()
{
std::vector<FileData> res;
createFileEntry(std::back_inserter(res),
dr->fileEntries.begin(), dr->fileEntries.end(),
dr->totalLength, dr->pieceLength, dr->bitfield);
return res;
}
SharedHandle<DownloadResult> dr;
};
} // namespace
DownloadHandle* getDownloadHandle(Session* session, const A2Gid& gid)
{
const SharedHandle<DownloadEngine>& e =
session->context->reqinfo->getDownloadEngine();
const SharedHandle<RequestGroupMan>& rgman = e->getRequestGroupMan();
SharedHandle<RequestGroup> group = rgman->findGroup(gid);
if(group) {
return new RequestGroupDH(group);
} else {
SharedHandle<DownloadResult> ds = rgman->findDownloadResult(gid);
if(ds) {
return new DownloadResultDH(ds);
}
}
return 0;
}
void deleteDownloadHandle(DownloadHandle* dh)
{
delete dh;
}
} // namespace aria2

55
src/aria2api.h Normal file
View File

@ -0,0 +1,55 @@
/* <!-- copyright */
/*
* aria2 - The high speed download utility
*
* Copyright (C) 2013 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* 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.
*/
/* copyright --> */
#ifndef ARIA2_API_H
#define ARIA2_API_H
#include "common.h"
#include <aria2/aria2.h>
#include "SharedHandle.h"
namespace aria2 {
struct Context;
struct Session {
Session(const KeyVals& options);
~Session();
SharedHandle<Context> context;
};
} // namespace aria2
#endif // ARIA2_API_H

View File

@ -33,38 +33,47 @@
*/
/* copyright --> */
#include "console.h"
#include "NullOutputFile.h"
#ifdef __MINGW32__
# include "WinConsoleFile.h"
#else // !__MINGW32__
# include "BufferedFile.h"
#endif // !__MINGW32__
namespace aria2 {
namespace global {
#ifdef __MINGW32__
const SharedHandle<WinConsoleFile>& cout()
{
static SharedHandle<WinConsoleFile> f(new WinConsoleFile(STD_OUTPUT_HANDLE));
return f;
}
#else // !__MINGW32__
const SharedHandle<BufferedFile>& cout()
{
static SharedHandle<BufferedFile> f(new BufferedFile(stdout));
return f;
}
#endif // !__MINGW32__
namespace {
Console consoleCout;
Console consoleCerr;
};
void initConsole(bool suppress)
{
if(suppress) {
consoleCerr.reset(new NullOutputFile());
consoleCout.reset(new NullOutputFile());
} else {
#ifdef __MINGW32__
const SharedHandle<WinConsoleFile>& cerr()
{
static SharedHandle<WinConsoleFile> f(new WinConsoleFile(STD_ERROR_HANDLE));
return f;
}
consoleCout.reset(new WinConsoleFile(STD_OUTPUT_HANDLE));
consoleCerr.reset(new WinConsoleFile(STD_ERROR_HANDLE));
#else // !__MINGW32__
const SharedHandle<BufferedFile>& cerr()
{
static SharedHandle<BufferedFile> f(new BufferedFile(stderr));
return f;
}
consoleCout.reset(new BufferedFile(stdout));
consoleCerr.reset(new BufferedFile(stderr));
#endif // !__MINGW32__
}
}
const Console& cout()
{
return consoleCout;
}
const Console& cerr()
{
return consoleCerr;
}
} // namespace global

View File

@ -37,22 +37,18 @@
#include "common.h"
#include "SharedHandle.h"
#ifdef __MINGW32__
# include "WinConsoleFile.h"
#else // !__MINGW32__
# include "BufferedFile.h"
#endif // !__MINGW32__
#include "OutputFile.h"
namespace aria2 {
#ifdef __MINGW32__
typedef SharedHandle<WinConsoleFile> Console;
#else // !__MINGW32__
typedef SharedHandle<BufferedFile> Console;
#endif // !__MINGW32__
typedef SharedHandle<OutputFile> Console;
namespace global {
// Initialize console output facility. If the |suppress| is true, all
// output sent to the console objects are discarded.
void initConsole(bool suppress);
const Console& cout();
const Console& cerr();

1
src/includes/Makefile.am Normal file
View File

@ -0,0 +1 @@
nobase_include_HEADERS = aria2/aria2.h

283
src/includes/aria2/aria2.h Normal file
View File

@ -0,0 +1,283 @@
/* <!-- copyright */
/*
* aria2 - The high speed download utility
*
* Copyright (C) 2013 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* 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.
*/
/* copyright --> */
#ifndef ARIA2_H
#define ARIA2_H
#include <stdlib.h>
#include <stdint.h>
#include <sys/types.h>
#include <string>
#include <vector>
// Libaria2: The aim of this library is provide same functionality
// available in RPC methods. The function signatures are not
// necessarily the same, because we can take advantage of the direct,
// no latency, access to the aria2 core.
//
// Therefore, this library is not meant to be the fine-grained,
// customizable, complete HTTP/FTP/BitTorrent library. If you are
// looking for such library for HTTP/FTP access, consider libcurl.
namespace aria2 {
struct Session;
// Initializes the global data. It also initializes
// underlying libraries libaria2 depends on. This function returns 0
// if it succeeds, or -1.
//
// Call this function only once before calling any other API functions.
int libraryInit();
// Releases the global data. This function returns 0 if
// it succeeds, or -1.
//
// Call this function only once at the end of the application.
int libraryDeinit();
// type of GID
typedef uint64_t A2Gid;
// type of Key/Value pairs
typedef std::vector<std::pair<std::string, std::string> > KeyVals;
struct SessionConfig {
// The constructor fills default values for all members.
SessionConfig();
// If the |keepRunning| member is true, run(session, RUN_ONCE) will
// return 1 even if there are no download to perform. The behavior
// is very similar to RPC server, except that this option does not
// enable RPC functionality. To stop aria2, use shutdown() function.
// The default value is false.
bool keepRunning;
// If the |useSignalHandler| is true, the library setups following
// signal handlers in sessionNew(). These signal handlers are
// removed in sessionFinal(). The default value is true. If the
// application sets this member to false, it must handle these
// signals and ensure that run() is repeatedly called until it
// returns 0 and sessionFinal() is called after that. Failing these
// steps will lead to not saving .aria2 control file and no session
// serialization.
//
// SIGPIPE, SIGCHLD: ignored
// SIGHUP, SIGTERM: handled like shutdown(session, true) is called.
// SIGINT: handled like shutdown(session, false) is called.
bool useSignalHandler;
};
// Creates new Session object using the |options| as additional
// parameters. The |options| is treated as if they are specified in
// command-line to aria2c(1). This function returns the pointer to the
// newly created Session object if it succeeds, or NULL.
//
// Please note that only one Session object can be created per
// process.
Session* sessionNew(const KeyVals& options, const SessionConfig& config);
// Performs post-download action, including saving sessions etc and
// destroys the |session| object, releasing the allocated resources
// for it. This function returns the last error code and it is the
// equivalent to the exit status of aria2c(1).
int sessionFinal(Session* session);
enum RUN_MODE {
RUN_DEFAULT,
RUN_ONCE
};
// Performs event polling and actions for them. If the |mode| is
// RUN_DEFAULT, this function returns when no downloads are left to be
// processed. In this case, this function returns 0.
//
// If the |mode| is RUN_ONCE, this function returns after one event
// polling. In the current implementation, event polling timeouts in 1
// second, so this function returns at most 1 second. On return, when
// no downloads are left to be processed, this function returns
// 0. Otherwise, returns 1, indicating that the caller must call this
// function one or more time to complete downloads.
int run(Session* session, RUN_MODE mode);
// Returns textual representation of the |gid|.
std::string gidToHex(const A2Gid& gid);
// Returns GID converted from the textual representation |hex|.
A2Gid hexToGid(const std::string& hex);
// Returns true if the |gid| is invalid.
bool isNull(const A2Gid& gid);
// Adds new HTTP(S)/FTP/BitTorrent Magnet URI. On successful return,
// the |gid| includes the GID of newly added download. The |uris|
// includes URI to be downloaded. For BitTorrent Magnet URI, the
// |uris| must have only one element and it should be BitTorrent
// Magnet URI. URIs in uris must point to the same file. If you mix
// other URIs which point to another file, aria2 does not complain but
// download may fail. The |options| is a pair of option name and
// value. If the |position| is not negative integer, the new download
// is inserted at position in the waiting queue. If the |position| is
// negative or the |position| is larger than the size of the queue, it
// is appended at the end of the queue. This function returns 0 if it
// succeeds, or -1.
int addUri(Session* session,
A2Gid& gid,
const std::vector<std::string>& uris,
const KeyVals& options,
int position = -1);
// Adds Metalink download. The path to Metalink file is specified by
// the |metalinkFile|. On successful return, the GID of added
// download is appended to the |gids|. The |options| is a pair of
// option name and value. If the |position| is not negative integer,
// the new download is inserted at position in the waiting queue. If
// the |position| is negative or the |position| is larger than the
// size of the queue, it is appended at the end of the queue. This
// function returns 0 if it succeeds, or -1.
int addMetalink(Session* session,
std::vector<A2Gid>& gids,
const std::string& metalinkFile,
const KeyVals& options,
int position = -1);
// Returns the array of active download GID.
std::vector<A2Gid> getActiveDownload(Session* session);
// Removes the download denoted by the |gid|. If the specified
// download is in progress, it is stopped at first. The status of
// removed download becomes DOWNLOAD_REMOVED. If the |force| is true,
// removal will take place without any action which takes time such as
// contacting BitTorrent tracker. This function returns 0 if it
// succeeds, or -1.
int removeDownload(Session* session, const A2Gid& gid, bool force = false);
// Pauses the download denoted by the |gid|. The status of paused
// download becomes DOWNLOAD_PAUSED. If the download is active, the
// download is placed on the first position of waiting queue. As long
// as the status is DOWNLOAD_PAUSED, the download will not start. To
// change status to DOWNLOAD_WAITING, use unpauseDownload() function.
// If the |force| is true, pause will take place without any action
// which takes time such as contacting BitTorrent tracker. This
// function returns 0 if it succeeds, or -1. Please note that, to
// make pause work, the application must call
// sessionConfigSetKeepRunning() function with the |flag| argument to
// true. Without this call, download may be paused at first, but it
// will be restarted automatically.
int pauseDownload(Session* session, const A2Gid& gid, bool force = false);
// Changes the status of the download denoted by the |gid| from
// DOWNLOAD_PAUSED to DOWNLOAD_WAITING. This makes the download
// eligible to restart. This function returns 0 if it succeeds, or -1.
int unpauseDownload(Session* session, const A2Gid& gid);
// Schedules shutdown. If the |force| is true, shutdown will take
// place without any action which takes time such as contacting
// BitTorrent tracker. After this call, the application must keep
// calling run() method until it returns 0. This function returns 0
// if it succeeds, or -1.
int shutdown(Session* session, bool force = false);
enum UriStatus {
URI_USED,
URI_WAITING
};
struct UriData {
std::string uri;
UriStatus status;
};
struct FileData {
int index;
std::string path;
int64_t length;
int64_t completedLength;
bool selected;
std::vector<UriData> uris;
};
enum DownloadStatus {
DOWNLOAD_ACTIVE,
DOWNLOAD_WAITING,
DOWNLOAD_PAUSED,
DOWNLOAD_COMPLETE,
DOWNLOAD_ERROR,
DOWNLOAD_REMOVED
};
struct DownloadHandle {
virtual ~DownloadHandle() {}
virtual DownloadStatus getStatus() = 0;
virtual int64_t getTotalLength() = 0;
virtual int64_t getCompletedLength() = 0;
virtual int64_t getUploadLength() = 0;
virtual std::string getBitfield() = 0;
virtual int getDownloadSpeed() = 0;
virtual int getUploadSpeed() = 0;
virtual size_t getNumPieces() = 0;
virtual int getConnections() = 0;
// Returns the last error code occurred in this download. The error
// codes are defined in EXIT STATUS section of aria2c(1) man
// page. This value is only available for stopped/completed
// downloads.
virtual int getErrorCode() = 0;
// Returns array of GIDs which are generated by the consequence of
// this download. For example, when aria2 downloaded Metalink file,
// it generates downloads described in it (see --follow-metalink
// option). This value is useful to track these auto generated
// downloads. If there is no such downloads, this function returns
// empty array.
virtual const std::vector<A2Gid>& getFollowedBy() = 0;
// Returns the GID of a parent download. Some downloads are a part
// of another download. For example, if a file in Metalink has
// BitTorrent resource, the download of ".torrent" is a part of that
// file. If this download has no parent, the invalid GID is returned
// (isNull(gid) is true).
virtual A2Gid getBelongsTo() = 0;
virtual const std::string& getDir() = 0;
virtual std::vector<FileData> getFiles() = 0;
};
// Returns handle for the download denoted by the |gid|. The caller
// can retrieve various information of the download via returned
// handle. The lifetime of the returned handle is before the next call
// of run() or sessionFinal(). This function returns NULL if no
// download denoted by the |gid| is present. The caller must call
// deleteDownloadHandle() to delete the acquired handle.
DownloadHandle* getDownloadHandle(Session* session, const A2Gid& gid);
// Deallocates the |dh|. Calling this function with NULL is safe.
void deleteDownloadHandle(DownloadHandle* dh);
} // namespace aria2
#endif // ARIA2_H

42
src/libaria2.pc.in Normal file
View File

@ -0,0 +1,42 @@
# aria2 - The high speed download utility
#
# Copyright (C) 2013 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#
# 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.
prefix=@prefix@
exec_prefix=@exec_prefix@
libdir=@libdir@
includedir=@includedir@
Name: aria2
Description: High speed download utility library
URL: http://aria2.sourceforge.net/
Version: @VERSION@
Libs: -L${libdir} -laria2
# TODO Add Requires.private: here
Cflags: -I${includedir}

View File

@ -34,270 +34,35 @@
/* copyright --> */
#include "common.h"
#include <signal.h>
#include <unistd.h>
#include <getopt.h>
#include <numeric>
#include <vector>
#include <iostream>
#include <aria2/aria2.h>
#include "SharedHandle.h"
#include "LogFactory.h"
#include "Logger.h"
#include "util.h"
#include "FeatureConfig.h"
#include "Context.h"
#include "MultiUrlRequestInfo.h"
#include "SimpleRandomizer.h"
#include "File.h"
#include "message.h"
#include "prefs.h"
#include "Option.h"
#include "a2algo.h"
#include "a2io.h"
#include "a2time.h"
#include "Platform.h"
#include "FileEntry.h"
#include "RequestGroup.h"
#include "ConsoleStatCalc.h"
#include "NullStatCalc.h"
#include "download_helper.h"
#include "Exception.h"
#include "ProtocolDetector.h"
#include "RecoverableException.h"
#include "SocketCore.h"
#include "DownloadContext.h"
#include "fmt.h"
#include "NullOutputFile.h"
#include "console.h"
#include "UriListParser.h"
#ifdef ENABLE_BITTORRENT
# include "bittorrent_helper.h"
#endif // ENABLE_BITTORRENT
#ifdef ENABLE_METALINK
# include "metalink_helper.h"
# include "MetalinkEntry.h"
#endif // ENABLE_METALINK
#ifdef ENABLE_MESSAGE_DIGEST
# include "message_digest_helper.h"
#endif // ENABLE_MESSAGE_DIGEST
extern char* optarg;
extern int optind, opterr, optopt;
namespace aria2 {
SharedHandle<StatCalc> getStatCalc(const SharedHandle<Option>& op)
error_code::Value main(int argc, char** argv)
{
SharedHandle<StatCalc> statCalc;
if(op->getAsBool(PREF_QUIET)) {
statCalc.reset(new NullStatCalc());
} else {
SharedHandle<ConsoleStatCalc> impl
(new ConsoleStatCalc(op->getAsInt(PREF_SUMMARY_INTERVAL),
op->getAsBool(PREF_HUMAN_READABLE)));
impl->setReadoutVisibility(op->getAsBool(PREF_SHOW_CONSOLE_READOUT));
impl->setTruncate(op->getAsBool(PREF_TRUNCATE_CONSOLE_READOUT));
statCalc = impl;
}
return statCalc;
}
SharedHandle<OutputFile> getSummaryOut(const SharedHandle<Option>& op)
{
if(op->getAsBool(PREF_QUIET)) {
return SharedHandle<OutputFile>(new NullOutputFile());
} else {
return global::cout();
}
}
#ifdef ENABLE_BITTORRENT
namespace {
void showTorrentFile(const std::string& uri)
{
SharedHandle<Option> op(new Option());
SharedHandle<DownloadContext> dctx(new DownloadContext());
bittorrent::load(uri, dctx, op);
bittorrent::print(*global::cout(), dctx);
}
} // namespace
#endif // ENABLE_BITTORRENT
#ifdef ENABLE_METALINK
namespace {
void showMetalinkFile
(const std::string& uri, const SharedHandle<Option>& op)
{
std::vector<SharedHandle<MetalinkEntry> > metalinkEntries;
metalink::parseAndQuery(metalinkEntries, uri, op.get(),
op->get(PREF_METALINK_BASE_URI));
std::vector<SharedHandle<FileEntry> > fileEntries;
MetalinkEntry::toFileEntry(fileEntries, metalinkEntries);
util::toStream(fileEntries.begin(), fileEntries.end(), *global::cout());
global::cout()->write("\n");
global::cout()->flush();
}
} // namespace
#endif // ENABLE_METALINK
#if defined ENABLE_BITTORRENT || defined ENABLE_METALINK
namespace {
void showFiles
(const std::vector<std::string>& uris, const SharedHandle<Option>& op)
{
ProtocolDetector dt;
for(std::vector<std::string>::const_iterator i = uris.begin(),
eoi = uris.end(); i != eoi; ++i) {
printf(">>> ");
printf(MSG_SHOW_FILES, (*i).c_str());
printf("\n");
try {
#ifdef ENABLE_BITTORRENT
if(dt.guessTorrentFile(*i)) {
showTorrentFile(*i);
} else
#endif // ENABLE_BITTORRENT
#ifdef ENABLE_METALINK
if(dt.guessMetalinkFile(*i)) {
showMetalinkFile(*i, op);
} else
#endif // ENABLE_METALINK
{
printf(MSG_NOT_TORRENT_METALINK);
printf("\n\n");
}
} catch(RecoverableException& e) {
global::cout()->printf("%s\n", e.stackTrace().c_str());
}
}
}
} // namespace
#endif // ENABLE_BITTORRENT || ENABLE_METALINK
extern void option_processing(Option& option, std::vector<std::string>& uris,
int argc, char* argv[]);
error_code::Value main(int argc, char* argv[])
{
std::vector<std::string> args;
SharedHandle<Option> op(new Option());
option_processing(*op.get(), args, argc, argv);
SimpleRandomizer::init();
#ifdef ENABLE_BITTORRENT
bittorrent::generateStaticPeerId(op->get(PREF_PEER_ID_PREFIX));
#endif // ENABLE_BITTORRENT
LogFactory::setLogFile(op->get(PREF_LOG));
LogFactory::setLogLevel(op->get(PREF_LOG_LEVEL));
LogFactory::setConsoleLogLevel(op->get(PREF_CONSOLE_LOG_LEVEL));
if(op->getAsBool(PREF_QUIET)) {
LogFactory::setConsoleOutput(false);
}
LogFactory::reconfigure();
Context context(true, argc, argv, KeyVals());
error_code::Value exitStatus = error_code::FINISHED;
A2_LOG_INFO("<<--- --- --- ---");
A2_LOG_INFO(" --- --- --- ---");
A2_LOG_INFO(" --- --- --- --->>");
A2_LOG_INFO(fmt("%s %s %s", PACKAGE, PACKAGE_VERSION, TARGET));
A2_LOG_INFO(MSG_LOGGING_STARTED);
if(op->getAsBool(PREF_DISABLE_IPV6)) {
SocketCore::setProtocolFamily(AF_INET);
// Get rid of AI_ADDRCONFIG. It causes name resolution error
// when none of network interface has IPv4 address.
setDefaultAIFlags(0);
}
net::checkAddrconfig();
// Bind interface
if(!op->get(PREF_INTERFACE).empty()) {
std::string iface = op->get(PREF_INTERFACE);
SocketCore::bindAddress(iface);
}
sigset_t mask;
#ifdef HAVE_SIGACTION
sigemptyset(&mask);
#else // !HAVE_SIGACTION
mask = 0;
#endif // !HAVE_SIGACTION
#ifdef SIGPIPE
util::setGlobalSignalHandler(SIGPIPE, &mask, SIG_IGN, 0);
#endif
#ifdef SIGCHLD
// Avoid to create zombie process when forked child processes are
// died.
util::setGlobalSignalHandler(SIGCHLD, &mask, SIG_IGN, 0);
#endif // SIGCHILD
std::vector<SharedHandle<RequestGroup> > requestGroups;
SharedHandle<UriListParser> uriListParser;
#ifdef ENABLE_BITTORRENT
if(!op->blank(PREF_TORRENT_FILE)) {
if(op->get(PREF_SHOW_FILES) == A2_V_TRUE) {
showTorrentFile(op->get(PREF_TORRENT_FILE));
return exitStatus;
} else {
createRequestGroupForBitTorrent(requestGroups, op, args,
op->get(PREF_TORRENT_FILE));
}
}
else
#endif // ENABLE_BITTORRENT
#ifdef ENABLE_METALINK
if(!op->blank(PREF_METALINK_FILE)) {
if(op->get(PREF_SHOW_FILES) == A2_V_TRUE) {
showMetalinkFile(op->get(PREF_METALINK_FILE), op);
return exitStatus;
} else {
createRequestGroupForMetalink(requestGroups, op);
}
}
else
#endif // ENABLE_METALINK
if(!op->blank(PREF_INPUT_FILE)) {
if(op->getAsBool(PREF_DEFERRED_INPUT)) {
uriListParser = openUriListParser(op->get(PREF_INPUT_FILE));
} else {
createRequestGroupForUriList(requestGroups, op);
}
#if defined ENABLE_BITTORRENT || defined ENABLE_METALINK
} else if(op->get(PREF_SHOW_FILES) == A2_V_TRUE) {
showFiles(args, op);
return exitStatus;
#endif // ENABLE_METALINK || ENABLE_METALINK
} else {
createRequestGroupForUri(requestGroups, op, args, false, false, true);
}
// Remove option values which is only valid for URIs specified in
// command-line. If they are left, because op is used as a template
// for new RequestGroup(such as created in RPC command), they causes
// unintentional effect.
for(SharedHandle<Option> i = op; i; i = i->getParent()) {
i->remove(PREF_OUT);
i->remove(PREF_FORCE_SEQUENTIAL);
i->remove(PREF_INPUT_FILE);
i->remove(PREF_INDEX_OUT);
i->remove(PREF_SELECT_FILE);
i->remove(PREF_PAUSE);
i->remove(PREF_CHECKSUM);
i->remove(PREF_GID);
}
if(!op->getAsBool(PREF_ENABLE_RPC) && requestGroups.empty() &&
!uriListParser) {
global::cout()->printf("%s\n", MSG_NO_FILES_TO_DOWNLOAD);
} else {
exitStatus = MultiUrlRequestInfo(requestGroups, op, getStatCalc(op),
getSummaryOut(op),
uriListParser).execute();
if(context.reqinfo) {
exitStatus = context.reqinfo->execute();
}
return exitStatus;
}
} // namespace aria2
int main(int argc, char* argv[])
int main(int argc, char** argv)
{
aria2::error_code::Value r;
aria2::global::initConsole(false);
try {
aria2::Platform platform;
r = aria2::main(argc, argv);

View File

@ -38,6 +38,7 @@
#include <cstring>
#include <sstream>
#include <aria2/aria2.h>
#include "Option.h"
#include "prefs.h"
#include "OptionParser.h"
@ -189,8 +190,10 @@ void optionNativeToUtf8(Option& op)
} // namespace
#endif // __MINGW32__
void option_processing(Option& op, std::vector<std::string>& uris,
int argc, char* argv[])
error_code::Value option_processing(Option& op, bool standalone,
std::vector<std::string>& uris,
int argc, char** argv,
const KeyVals& options)
{
const SharedHandle<OptionParser>& oparser = OptionParser::getInstance();
try {
@ -204,7 +207,7 @@ void option_processing(Option& op, std::vector<std::string>& uris,
oparser->parse(op, cmdstream);
noConf = op.getAsBool(PREF_NO_CONF);
ucfname = op.get(PREF_CONF_PATH);
if(standalone) {
if(op.defined(PREF_VERSION)) {
showVersion();
exit(error_code::FINISHED);
@ -227,6 +230,7 @@ void option_processing(Option& op, std::vector<std::string>& uris,
exit(error_code::FINISHED);
}
}
}
SharedHandle<Option> confOption(new Option());
oparser->parseDefaultValues(*confOption);
if(!noConf) {
@ -252,18 +256,18 @@ void option_processing(Option& op, std::vector<std::string>& uris,
global::cerr()->printf(_("Usage:"));
global::cerr()->printf("\n%s\n", h->getDescription());
}
exit(e.getErrorCode());
return e.getErrorCode();
} catch(Exception& e) {
global::cerr()->printf(_("Parse error in %s"), cfname.c_str());
global::cerr()->printf("\n%s", e.stackTrace().c_str());
exit(e.getErrorCode());
return e.getErrorCode();
}
} else if(!ucfname.empty()) {
global::cerr()->printf(_("Configuration file %s is not found."),
cfname.c_str());
global::cerr()->printf("\n");
showUsage(strHelpTag(TAG_HELP), oparser, global::cerr());
exit(error_code::UNKNOWN_ERROR);
return error_code::UNKNOWN_ERROR;
}
}
// Override configuration with environment variables.
@ -272,6 +276,12 @@ void option_processing(Option& op, std::vector<std::string>& uris,
overrideWithEnv(*confOption, oparser, PREF_FTP_PROXY, "ftp_proxy");
overrideWithEnv(*confOption, oparser, PREF_ALL_PROXY, "all_proxy");
overrideWithEnv(*confOption, oparser, PREF_NO_PROXY, "no_proxy");
if(!standalone) {
// For non-standalone mode, set PREF_QUIET to true to suppress
// output. The caller can override this by including PREF_QUIET
// in options argument.
confOption->put(PREF_QUIET, A2_V_TRUE);
}
// we must clear eof bit and seek to the beginning of the buffer.
cmdstream.clear();
@ -279,6 +289,7 @@ void option_processing(Option& op, std::vector<std::string>& uris,
// finaly let's parse and store command-iine options.
op.setParent(confOption);
oparser->parse(op, cmdstream);
oparser->parse(op, options);
#ifdef __MINGW32__
optionNativeToUtf8(op);
optionNativeToUtf8(*confOption);
@ -291,17 +302,18 @@ void option_processing(Option& op, std::vector<std::string>& uris,
global::cerr()->printf("\n");
write(global::cerr(), *h);
}
exit(e.getErrorCode());
return e.getErrorCode();
} catch(UnknownOptionException& e) {
showUsage("", oparser, global::cerr());
showCandidates(e.getUnknownOption(), oparser);
exit(e.getErrorCode());
return e.getErrorCode();
} catch(Exception& e) {
global::cerr()->printf("%s", e.stackTrace().c_str());
showUsage("", oparser, global::cerr());
exit(e.getErrorCode());
return e.getErrorCode();
}
if(!op.getAsBool(PREF_ENABLE_RPC) &&
if(standalone &&
!op.getAsBool(PREF_ENABLE_RPC) &&
#ifdef ENABLE_BITTORRENT
op.blank(PREF_TORRENT_FILE) &&
#endif // ENABLE_BITTORRENT
@ -313,15 +325,16 @@ void option_processing(Option& op, std::vector<std::string>& uris,
global::cerr()->printf(MSG_URI_REQUIRED);
global::cerr()->printf("\n");
showUsage("", oparser, global::cerr());
exit(error_code::UNKNOWN_ERROR);
return error_code::UNKNOWN_ERROR;
}
}
if(op.getAsBool(PREF_DAEMON)) {
if(standalone && op.getAsBool(PREF_DAEMON)) {
if(daemon(0, 0) < 0) {
perror(MSG_DAEMON_FAILED);
exit(error_code::UNKNOWN_ERROR);
return error_code::UNKNOWN_ERROR;
}
}
return error_code::FINISHED;
}
} // namespace aria2

View File

@ -9,8 +9,10 @@
#include "Platform.h"
#include "SocketCore.h"
#include "util.h"
#include "console.h"
int main(int argc, char* argv[]) {
aria2::global::initConsole(false);
aria2::Platform platform;
#ifdef ENABLE_NLS

View File

@ -237,9 +237,10 @@ if !HAVE_TIMEGM
aria2c_SOURCES += TimegmTest.cc
endif # !HAVE_TIMEGM
aria2c_LDADD = ../src/libaria2c.a @LIBINTL@ @CPPUNIT_LIBS@
aria2c_LDADD = ../src/libaria2.la @LIBINTL@ @CPPUNIT_LIBS@
AM_CPPFLAGS = -Wall\
-I$(top_srcdir)/src\
-I$(top_srcdir)/src/includes -I$(top_builddir)/src/includes\
-I$(top_srcdir)/lib -I$(top_srcdir)/intl\
-DLOCALEDIR=\"$(localedir)\"\
-DA2_TEST_DIR=\"$(top_srcdir)/test\"\

View File

@ -27,6 +27,7 @@ class OptionParserTest:public CppUnit::TestFixture {
CPPUNIT_TEST(testParseDefaultValues);
CPPUNIT_TEST(testParseArg);
CPPUNIT_TEST(testParse);
CPPUNIT_TEST(testParseKeyVals);
CPPUNIT_TEST_SUITE_END();
private:
SharedHandle<OptionParser> oparser_;
@ -73,6 +74,7 @@ public:
void testParseDefaultValues();
void testParseArg();
void testParse();
void testParseKeyVals();
};
@ -195,4 +197,16 @@ void OptionParserTest::testParse()
CPPUNIT_ASSERT_EQUAL(std::string("World"), option.get(PREF_DIR));
}
void OptionParserTest::testParseKeyVals()
{
Option option;
KeyVals kv;
kv.push_back(std::make_pair("timeout", "Hello"));
kv.push_back(std::make_pair("UNKNOWN", "x"));
kv.push_back(std::make_pair("dir", "World"));
oparser_->parse(option, kv);
CPPUNIT_ASSERT_EQUAL(std::string("Hello"), option.get(PREF_TIMEOUT));
CPPUNIT_ASSERT_EQUAL(std::string("World"), option.get(PREF_DIR));
}
} // namespace aria2

View File

@ -40,8 +40,8 @@ public:
void SingletonHolderTest::testInstance()
{
M m("Hello world.");
SingletonHolder<M>::instance(&m);
SharedHandle<M> m(new M("Hello world."));
SingletonHolder<M>::instance(m);
CPPUNIT_ASSERT_EQUAL(std::string("Hello world."),
SingletonHolder<M>::instance()->greeting());