mirror of https://github.com/aria2/aria2
Add initialization function and addUri API function for libaria2
parent
90abec8a36
commit
6fcf274f27
|
@ -175,14 +175,16 @@ void showFiles
|
||||||
} // namespace
|
} // namespace
|
||||||
#endif // ENABLE_BITTORRENT || ENABLE_METALINK
|
#endif // ENABLE_BITTORRENT || ENABLE_METALINK
|
||||||
|
|
||||||
extern void option_processing(Option& option, std::vector<std::string>& uris,
|
extern void option_processing(Option& option, bool standalone,
|
||||||
int argc, char* argv[]);
|
std::vector<std::string>& uris,
|
||||||
|
int argc, char** argv, const KeyVals& options);
|
||||||
|
|
||||||
Context::Context(int argc, char** argv)
|
Context::Context(bool standalone,
|
||||||
|
int argc, char** argv, const KeyVals& options)
|
||||||
{
|
{
|
||||||
std::vector<std::string> args;
|
std::vector<std::string> args;
|
||||||
SharedHandle<Option> op(new Option());
|
SharedHandle<Option> op(new Option());
|
||||||
option_processing(*op.get(), args, argc, argv);
|
option_processing(*op.get(), standalone, args, argc, argv, options);
|
||||||
|
|
||||||
SimpleRandomizer::init();
|
SimpleRandomizer::init();
|
||||||
#ifdef ENABLE_BITTORRENT
|
#ifdef ENABLE_BITTORRENT
|
||||||
|
@ -281,7 +283,8 @@ Context::Context(int argc, char** argv)
|
||||||
i->remove(PREF_CHECKSUM);
|
i->remove(PREF_CHECKSUM);
|
||||||
i->remove(PREF_GID);
|
i->remove(PREF_GID);
|
||||||
}
|
}
|
||||||
if(!op->getAsBool(PREF_ENABLE_RPC) && requestGroups.empty() &&
|
if(standalone &&
|
||||||
|
!op->getAsBool(PREF_ENABLE_RPC) && requestGroups.empty() &&
|
||||||
!uriListParser) {
|
!uriListParser) {
|
||||||
global::cout()->printf("%s\n", MSG_NO_FILES_TO_DOWNLOAD);
|
global::cout()->printf("%s\n", MSG_NO_FILES_TO_DOWNLOAD);
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -36,6 +36,7 @@
|
||||||
#define CONTEXT_H
|
#define CONTEXT_H
|
||||||
|
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
|
#include <aria2/aria2.h>
|
||||||
#include "SharedHandle.h"
|
#include "SharedHandle.h"
|
||||||
|
|
||||||
namespace aria2 {
|
namespace aria2 {
|
||||||
|
@ -43,7 +44,12 @@ namespace aria2 {
|
||||||
class MultiUrlRequestInfo;
|
class MultiUrlRequestInfo;
|
||||||
|
|
||||||
struct Context {
|
struct Context {
|
||||||
Context(int argc, char** argv);
|
// 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();
|
~Context();
|
||||||
SharedHandle<MultiUrlRequestInfo> reqinfo;
|
SharedHandle<MultiUrlRequestInfo> reqinfo;
|
||||||
};
|
};
|
||||||
|
|
|
@ -88,6 +88,7 @@ DownloadEngine::DownloadEngine(const SharedHandle<EventPoll>& eventPoll)
|
||||||
haltRequested_(0),
|
haltRequested_(0),
|
||||||
noWait_(true),
|
noWait_(true),
|
||||||
refreshInterval_(DEFAULT_REFRESH_INTERVAL),
|
refreshInterval_(DEFAULT_REFRESH_INTERVAL),
|
||||||
|
lastRefresh_(0),
|
||||||
cookieStorage_(new CookieStorage()),
|
cookieStorage_(new CookieStorage()),
|
||||||
#ifdef ENABLE_BITTORRENT
|
#ifdef ENABLE_BITTORRENT
|
||||||
btRegistry_(new BtRegistry()),
|
btRegistry_(new BtRegistry()),
|
||||||
|
@ -141,8 +142,6 @@ void executeCommand(std::deque<Command*>& commands,
|
||||||
|
|
||||||
int DownloadEngine::run(bool oneshot)
|
int DownloadEngine::run(bool oneshot)
|
||||||
{
|
{
|
||||||
Timer cp;
|
|
||||||
cp.reset(0);
|
|
||||||
while(!commands_.empty() || !routineCommands_.empty()) {
|
while(!commands_.empty() || !routineCommands_.empty()) {
|
||||||
if(!commands_.empty()) {
|
if(!commands_.empty()) {
|
||||||
waitData();
|
waitData();
|
||||||
|
@ -150,17 +149,17 @@ int DownloadEngine::run(bool oneshot)
|
||||||
noWait_ = false;
|
noWait_ = false;
|
||||||
global::wallclock().reset();
|
global::wallclock().reset();
|
||||||
calculateStatistics();
|
calculateStatistics();
|
||||||
if(cp.differenceInMillis(global::wallclock())+A2_DELTA_MILLIS >=
|
if(lastRefresh_.differenceInMillis(global::wallclock())+A2_DELTA_MILLIS >=
|
||||||
refreshInterval_) {
|
refreshInterval_) {
|
||||||
refreshInterval_ = DEFAULT_REFRESH_INTERVAL;
|
refreshInterval_ = DEFAULT_REFRESH_INTERVAL;
|
||||||
cp = global::wallclock();
|
lastRefresh_ = global::wallclock();
|
||||||
executeCommand(commands_, Command::STATUS_ALL);
|
executeCommand(commands_, Command::STATUS_ALL);
|
||||||
} else {
|
} else {
|
||||||
executeCommand(commands_, Command::STATUS_ACTIVE);
|
executeCommand(commands_, Command::STATUS_ACTIVE);
|
||||||
}
|
}
|
||||||
executeCommand(routineCommands_, Command::STATUS_ALL);
|
executeCommand(routineCommands_, Command::STATUS_ALL);
|
||||||
afterEachIteration();
|
afterEachIteration();
|
||||||
if(oneshot) {
|
if(!noWait_ && oneshot) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -124,6 +124,7 @@ private:
|
||||||
|
|
||||||
// Milliseconds
|
// Milliseconds
|
||||||
int64_t refreshInterval_;
|
int64_t refreshInterval_;
|
||||||
|
Timer lastRefresh_;
|
||||||
|
|
||||||
std::deque<Command*> routineCommands_;
|
std::deque<Command*> routineCommands_;
|
||||||
|
|
||||||
|
|
|
@ -1,9 +1,10 @@
|
||||||
SUBDIRS = includes
|
SUBDIRS = includes
|
||||||
bin_PROGRAMS = aria2c
|
bin_PROGRAMS = aria2c
|
||||||
aria2c_SOURCES = main.cc\
|
aria2c_SOURCES = main.cc
|
||||||
version_usage.cc
|
|
||||||
SRCS = option_processing.cc\
|
SRCS = option_processing.cc\
|
||||||
|
version_usage.cc\
|
||||||
Context.cc Context.h\
|
Context.cc Context.h\
|
||||||
|
aria2api.cc aria2api.h\
|
||||||
SocketCore.cc SocketCore.h\
|
SocketCore.cc SocketCore.h\
|
||||||
BinaryStream.h\
|
BinaryStream.h\
|
||||||
Command.cc Command.h\
|
Command.cc Command.h\
|
||||||
|
@ -632,15 +633,18 @@ SRCS += KqueueEventPoll.cc KqueueEventPoll.h
|
||||||
endif # HAVE_KQUEUE
|
endif # HAVE_KQUEUE
|
||||||
|
|
||||||
AR = @AR@
|
AR = @AR@
|
||||||
noinst_LTLIBRARIES = libaria2c.la
|
|
||||||
libaria2c_la_SOURCES = $(SRCS)
|
pkgconfigdir = $(libdir)/pkgconfig
|
||||||
libaria2c_la_LIBADD = @WSLAY_LIBS@
|
pkgconfig_DATA = libaria2.pc
|
||||||
aria2c_LDADD = libaria2c.la @LIBINTL@ @ALLOCA@ #-lprofiler
|
DISTCLEANFILES = $(pkgconfig_DATA)
|
||||||
|
|
||||||
|
lib_LTLIBRARIES = libaria2.la
|
||||||
|
libaria2_la_SOURCES = $(SRCS)
|
||||||
|
libaria2_la_LIBADD = @WSLAY_LIBS@
|
||||||
|
|
||||||
|
LDADD = libaria2.la @LIBINTL@ @ALLOCA@ #-lprofiler
|
||||||
#aria2c_LDFLAGS = -pg
|
#aria2c_LDFLAGS = -pg
|
||||||
AM_CPPFLAGS = -Wall\
|
AM_CPPFLAGS = -Wall\
|
||||||
-I$(top_srcdir)/lib -I$(top_srcdir)/intl\
|
-I$(top_srcdir)/lib -I$(top_srcdir)/intl\
|
||||||
|
-I$(srcdir)/includes -I$(builddir)/includes\
|
||||||
-DLOCALEDIR=\"@localedir@\" -DCA_BUNDLE=\"$(ca_bundle)\" @DEFS@ #-pg
|
-DLOCALEDIR=\"@localedir@\" -DCA_BUNDLE=\"$(ca_bundle)\" @DEFS@ #-pg
|
||||||
|
|
||||||
lib_LTLIBRARIES = libaria2.la
|
|
||||||
libaria2_la_SOURCES =
|
|
||||||
libaria2_la_LIBADD = libaria2c.la
|
|
||||||
|
|
|
@ -128,7 +128,7 @@ void MultiUrlRequestInfo::printMessageForContinue()
|
||||||
_("If there are any errors, then see the log file. See '-l' option in help/man page for details."));
|
_("If there are any errors, then see the log file. See '-l' option in help/man page for details."));
|
||||||
}
|
}
|
||||||
|
|
||||||
error_code::Value MultiUrlRequestInfo::prepare()
|
int MultiUrlRequestInfo::prepare()
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
SharedHandle<rpc::WebSocketSessionMan> wsSessionMan;
|
SharedHandle<rpc::WebSocketSessionMan> wsSessionMan;
|
||||||
|
@ -259,9 +259,9 @@ error_code::Value MultiUrlRequestInfo::prepare()
|
||||||
} catch(RecoverableException& e) {
|
} catch(RecoverableException& e) {
|
||||||
SingletonHolder<Notifier>::clear();
|
SingletonHolder<Notifier>::clear();
|
||||||
resetSignalHandlers();
|
resetSignalHandlers();
|
||||||
return error_code::UNKNOWN_ERROR;
|
return -1;
|
||||||
}
|
}
|
||||||
return error_code::FINISHED;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
error_code::Value MultiUrlRequestInfo::getResult()
|
error_code::Value MultiUrlRequestInfo::getResult()
|
||||||
|
@ -306,30 +306,19 @@ error_code::Value MultiUrlRequestInfo::getResult()
|
||||||
return returnValue;
|
return returnValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
int MultiUrlRequestInfo::run()
|
|
||||||
{
|
|
||||||
int rv;
|
|
||||||
try {
|
|
||||||
rv = e_->run(true);
|
|
||||||
} catch(RecoverableException& e) {
|
|
||||||
rv = -1;
|
|
||||||
}
|
|
||||||
return rv;
|
|
||||||
}
|
|
||||||
|
|
||||||
error_code::Value MultiUrlRequestInfo::execute()
|
error_code::Value MultiUrlRequestInfo::execute()
|
||||||
{
|
{
|
||||||
error_code::Value returnValue;
|
if(prepare() != 0) {
|
||||||
returnValue = prepare();
|
return error_code::UNKNOWN_ERROR;
|
||||||
if(returnValue != error_code::FINISHED) {
|
|
||||||
return returnValue;
|
|
||||||
}
|
}
|
||||||
|
// TODO Enclosed in try..catch block for just in case. Really need
|
||||||
|
// this?
|
||||||
try {
|
try {
|
||||||
e_->run();
|
e_->run();
|
||||||
} catch(RecoverableException& e) {
|
} catch(RecoverableException& e) {
|
||||||
A2_LOG_ERROR_EX(EX_EXCEPTION_CAUGHT, e);
|
A2_LOG_ERROR_EX(EX_EXCEPTION_CAUGHT, e);
|
||||||
}
|
}
|
||||||
returnValue = getResult();
|
error_code::Value returnValue = getResult();
|
||||||
resetSignalHandlers();
|
resetSignalHandlers();
|
||||||
return returnValue;
|
return returnValue;
|
||||||
}
|
}
|
||||||
|
@ -346,4 +335,10 @@ void MultiUrlRequestInfo::resetSignalHandlers()
|
||||||
util::setGlobalSignalHandler(SIGTERM, &mask_, SIG_DFL, 0);
|
util::setGlobalSignalHandler(SIGTERM, &mask_, SIG_DFL, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const SharedHandle<DownloadEngine>&
|
||||||
|
MultiUrlRequestInfo::getDownloadEngine() const
|
||||||
|
{
|
||||||
|
return e_;
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace aria2
|
} // namespace aria2
|
||||||
|
|
|
@ -86,19 +86,25 @@ public:
|
||||||
|
|
||||||
virtual ~MultiUrlRequestInfo();
|
virtual ~MultiUrlRequestInfo();
|
||||||
|
|
||||||
/**
|
// Returns FINISHED if all downloads have completed, otherwise returns the
|
||||||
* Returns FINISHED if all downloads have completed, otherwise returns the
|
// last download result.
|
||||||
* last download result.
|
//
|
||||||
*/
|
// This method actually calls prepare() and
|
||||||
|
// getDownloadEngine()->run(true) and getResult().
|
||||||
error_code::Value execute();
|
error_code::Value execute();
|
||||||
|
|
||||||
error_code::Value prepare();
|
// 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();
|
error_code::Value getResult();
|
||||||
// Returns 1 if the caller needs to call this function one or more
|
|
||||||
// time. Returns 0 if the function succeeds. Returns -1 on error.
|
const SharedHandle<DownloadEngine>& getDownloadEngine() const;
|
||||||
// For return value 0 and -1, the caller must call tearDown() to get
|
|
||||||
// final error code.
|
|
||||||
int run();
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace aria2
|
} // namespace aria2
|
||||||
|
|
|
@ -245,6 +245,21 @@ void OptionParser::parse(Option& option, std::istream& is) const
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void OptionParser::parse(Option& option, const KeyVals& options) const
|
||||||
|
{
|
||||||
|
std::string line;
|
||||||
|
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", line.c_str()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void OptionParser::setOptionHandlers
|
void OptionParser::setOptionHandlers
|
||||||
(const std::vector<OptionHandler*>& handlers)
|
(const std::vector<OptionHandler*>& handlers)
|
||||||
{
|
{
|
||||||
|
|
|
@ -41,6 +41,7 @@
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <iosfwd>
|
#include <iosfwd>
|
||||||
|
|
||||||
|
#include <aria2/aria2.h>
|
||||||
#include "SharedHandle.h"
|
#include "SharedHandle.h"
|
||||||
|
|
||||||
namespace aria2 {
|
namespace aria2 {
|
||||||
|
@ -67,6 +68,8 @@ public:
|
||||||
|
|
||||||
void parse(Option& option, std::istream& ios) const;
|
void parse(Option& option, std::istream& ios) const;
|
||||||
|
|
||||||
|
void parse(Option& option, const KeyVals& options) const;
|
||||||
|
|
||||||
void parseDefaultValues(Option& option) const;
|
void parseDefaultValues(Option& option) const;
|
||||||
|
|
||||||
void setOptionHandlers
|
void setOptionHandlers
|
||||||
|
|
|
@ -0,0 +1,185 @@
|
||||||
|
/* <!-- 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"
|
||||||
|
|
||||||
|
namespace aria2 {
|
||||||
|
|
||||||
|
Session::Session(const KeyVals& options)
|
||||||
|
: context(new Context(false, 0, 0, options))
|
||||||
|
{}
|
||||||
|
|
||||||
|
Session::~Session()
|
||||||
|
{}
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
Platform* platform = 0;
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
int libraryInit()
|
||||||
|
{
|
||||||
|
platform = new Platform();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int libraryDeinit()
|
||||||
|
{
|
||||||
|
delete platform;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
Session* sessionNew(const KeyVals& options)
|
||||||
|
{
|
||||||
|
int rv;
|
||||||
|
Session* session = new Session(options);
|
||||||
|
if(session->context->reqinfo) {
|
||||||
|
rv = session->context->reqinfo->prepare();
|
||||||
|
if(rv != 0) {
|
||||||
|
delete session;
|
||||||
|
session = 0;
|
||||||
|
}
|
||||||
|
} 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace aria2
|
|
@ -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
|
|
@ -0,0 +1 @@
|
||||||
|
nobase_include_HEADERS = aria2/aria2.h
|
|
@ -0,0 +1,113 @@
|
||||||
|
/* <!-- 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>
|
||||||
|
|
||||||
|
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.
|
||||||
|
int libraryInit();
|
||||||
|
|
||||||
|
// Releases the global data. This function returns 0 if
|
||||||
|
// it succeeds, or -1.
|
||||||
|
int libraryDeinit();
|
||||||
|
|
||||||
|
// type of GID
|
||||||
|
typedef uint64_t A2Gid;
|
||||||
|
|
||||||
|
// type of Key/Value pairs
|
||||||
|
typedef std::vector<std::pair<std::string, std::string> > KeyVals;
|
||||||
|
|
||||||
|
// 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.
|
||||||
|
Session* sessionNew(const KeyVals& options);
|
||||||
|
|
||||||
|
// 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);
|
||||||
|
|
||||||
|
// This method 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);
|
||||||
|
|
||||||
|
} // namespace aria2
|
||||||
|
|
||||||
|
#endif // ARIA2_H
|
|
@ -36,6 +36,7 @@
|
||||||
|
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#include <aria2/aria2.h>
|
||||||
#include "SharedHandle.h"
|
#include "SharedHandle.h"
|
||||||
#include "Context.h"
|
#include "Context.h"
|
||||||
#include "MultiUrlRequestInfo.h"
|
#include "MultiUrlRequestInfo.h"
|
||||||
|
@ -48,7 +49,7 @@ namespace aria2 {
|
||||||
|
|
||||||
error_code::Value main(int argc, char** argv)
|
error_code::Value main(int argc, char** argv)
|
||||||
{
|
{
|
||||||
Context context(argc, argv);
|
Context context(true, argc, argv, KeyVals());
|
||||||
error_code::Value exitStatus = error_code::FINISHED;
|
error_code::Value exitStatus = error_code::FINISHED;
|
||||||
if(context.reqinfo) {
|
if(context.reqinfo) {
|
||||||
exitStatus = context.reqinfo->execute();
|
exitStatus = context.reqinfo->execute();
|
||||||
|
|
|
@ -38,6 +38,7 @@
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
|
||||||
|
#include <aria2/aria2.h>
|
||||||
#include "Option.h"
|
#include "Option.h"
|
||||||
#include "prefs.h"
|
#include "prefs.h"
|
||||||
#include "OptionParser.h"
|
#include "OptionParser.h"
|
||||||
|
@ -189,8 +190,9 @@ void optionNativeToUtf8(Option& op)
|
||||||
} // namespace
|
} // namespace
|
||||||
#endif // __MINGW32__
|
#endif // __MINGW32__
|
||||||
|
|
||||||
void option_processing(Option& op, std::vector<std::string>& uris,
|
void option_processing(Option& op, bool standalone,
|
||||||
int argc, char* argv[])
|
std::vector<std::string>& uris,
|
||||||
|
int argc, char** argv, const KeyVals& options)
|
||||||
{
|
{
|
||||||
const SharedHandle<OptionParser>& oparser = OptionParser::getInstance();
|
const SharedHandle<OptionParser>& oparser = OptionParser::getInstance();
|
||||||
try {
|
try {
|
||||||
|
@ -279,6 +281,7 @@ void option_processing(Option& op, std::vector<std::string>& uris,
|
||||||
// finaly let's parse and store command-iine options.
|
// finaly let's parse and store command-iine options.
|
||||||
op.setParent(confOption);
|
op.setParent(confOption);
|
||||||
oparser->parse(op, cmdstream);
|
oparser->parse(op, cmdstream);
|
||||||
|
oparser->parse(op, options);
|
||||||
#ifdef __MINGW32__
|
#ifdef __MINGW32__
|
||||||
optionNativeToUtf8(op);
|
optionNativeToUtf8(op);
|
||||||
optionNativeToUtf8(*confOption);
|
optionNativeToUtf8(*confOption);
|
||||||
|
@ -301,7 +304,8 @@ void option_processing(Option& op, std::vector<std::string>& uris,
|
||||||
showUsage("", oparser, global::cerr());
|
showUsage("", oparser, global::cerr());
|
||||||
exit(e.getErrorCode());
|
exit(e.getErrorCode());
|
||||||
}
|
}
|
||||||
if(!op.getAsBool(PREF_ENABLE_RPC) &&
|
if(standalone &&
|
||||||
|
!op.getAsBool(PREF_ENABLE_RPC) &&
|
||||||
#ifdef ENABLE_BITTORRENT
|
#ifdef ENABLE_BITTORRENT
|
||||||
op.blank(PREF_TORRENT_FILE) &&
|
op.blank(PREF_TORRENT_FILE) &&
|
||||||
#endif // ENABLE_BITTORRENT
|
#endif // ENABLE_BITTORRENT
|
||||||
|
|
|
@ -237,9 +237,10 @@ if !HAVE_TIMEGM
|
||||||
aria2c_SOURCES += TimegmTest.cc
|
aria2c_SOURCES += TimegmTest.cc
|
||||||
endif # !HAVE_TIMEGM
|
endif # !HAVE_TIMEGM
|
||||||
|
|
||||||
aria2c_LDADD = ../src/libaria2c.la @LIBINTL@ @CPPUNIT_LIBS@
|
aria2c_LDADD = ../src/libaria2.la @LIBINTL@ @CPPUNIT_LIBS@
|
||||||
AM_CPPFLAGS = -Wall\
|
AM_CPPFLAGS = -Wall\
|
||||||
-I$(top_srcdir)/src\
|
-I$(top_srcdir)/src\
|
||||||
|
-I$(top_srcdir)/src/includes -I$(top_builddir)/src/includes\
|
||||||
-I$(top_srcdir)/lib -I$(top_srcdir)/intl\
|
-I$(top_srcdir)/lib -I$(top_srcdir)/intl\
|
||||||
-DLOCALEDIR=\"$(localedir)\"\
|
-DLOCALEDIR=\"$(localedir)\"\
|
||||||
-DA2_TEST_DIR=\"$(top_srcdir)/test\"\
|
-DA2_TEST_DIR=\"$(top_srcdir)/test\"\
|
||||||
|
|
|
@ -27,6 +27,7 @@ class OptionParserTest:public CppUnit::TestFixture {
|
||||||
CPPUNIT_TEST(testParseDefaultValues);
|
CPPUNIT_TEST(testParseDefaultValues);
|
||||||
CPPUNIT_TEST(testParseArg);
|
CPPUNIT_TEST(testParseArg);
|
||||||
CPPUNIT_TEST(testParse);
|
CPPUNIT_TEST(testParse);
|
||||||
|
CPPUNIT_TEST(testParseKeyVals);
|
||||||
CPPUNIT_TEST_SUITE_END();
|
CPPUNIT_TEST_SUITE_END();
|
||||||
private:
|
private:
|
||||||
SharedHandle<OptionParser> oparser_;
|
SharedHandle<OptionParser> oparser_;
|
||||||
|
@ -73,6 +74,7 @@ public:
|
||||||
void testParseDefaultValues();
|
void testParseDefaultValues();
|
||||||
void testParseArg();
|
void testParseArg();
|
||||||
void testParse();
|
void testParse();
|
||||||
|
void testParseKeyVals();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
@ -195,4 +197,16 @@ void OptionParserTest::testParse()
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string("World"), option.get(PREF_DIR));
|
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
|
} // namespace aria2
|
||||||
|
|
Loading…
Reference in New Issue