Add initialization function and addUri API function for libaria2

libaria2
Tatsuhiro Tsujikawa 2013-04-26 23:55:51 +09:00
parent 90abec8a36
commit 6fcf274f27
17 changed files with 461 additions and 55 deletions

View File

@ -175,14 +175,16 @@ void showFiles
} // namespace
#endif // ENABLE_BITTORRENT || ENABLE_METALINK
extern void option_processing(Option& option, std::vector<std::string>& uris,
int argc, char* argv[]);
extern void option_processing(Option& option, bool standalone,
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;
SharedHandle<Option> op(new Option());
option_processing(*op.get(), args, argc, argv);
option_processing(*op.get(), standalone, args, argc, argv, options);
SimpleRandomizer::init();
#ifdef ENABLE_BITTORRENT
@ -281,7 +283,8 @@ Context::Context(int argc, char** argv)
i->remove(PREF_CHECKSUM);
i->remove(PREF_GID);
}
if(!op->getAsBool(PREF_ENABLE_RPC) && requestGroups.empty() &&
if(standalone &&
!op->getAsBool(PREF_ENABLE_RPC) && requestGroups.empty() &&
!uriListParser) {
global::cout()->printf("%s\n", MSG_NO_FILES_TO_DOWNLOAD);
} else {

View File

@ -36,6 +36,7 @@
#define CONTEXT_H
#include "common.h"
#include <aria2/aria2.h>
#include "SharedHandle.h"
namespace aria2 {
@ -43,7 +44,12 @@ namespace aria2 {
class MultiUrlRequestInfo;
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();
SharedHandle<MultiUrlRequestInfo> reqinfo;
};

View File

@ -88,6 +88,7 @@ DownloadEngine::DownloadEngine(const SharedHandle<EventPoll>& eventPoll)
haltRequested_(0),
noWait_(true),
refreshInterval_(DEFAULT_REFRESH_INTERVAL),
lastRefresh_(0),
cookieStorage_(new CookieStorage()),
#ifdef ENABLE_BITTORRENT
btRegistry_(new BtRegistry()),
@ -141,8 +142,6 @@ void executeCommand(std::deque<Command*>& commands,
int DownloadEngine::run(bool oneshot)
{
Timer cp;
cp.reset(0);
while(!commands_.empty() || !routineCommands_.empty()) {
if(!commands_.empty()) {
waitData();
@ -150,17 +149,17 @@ int DownloadEngine::run(bool oneshot)
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(oneshot) {
if(!noWait_ && oneshot) {
return 1;
}
}

View File

@ -124,6 +124,7 @@ private:
// Milliseconds
int64_t refreshInterval_;
Timer lastRefresh_;
std::deque<Command*> routineCommands_;

View File

@ -1,9 +1,10 @@
SUBDIRS = includes
bin_PROGRAMS = aria2c
aria2c_SOURCES = main.cc\
version_usage.cc
aria2c_SOURCES = main.cc
SRCS = option_processing.cc\
version_usage.cc\
Context.cc Context.h\
aria2api.cc aria2api.h\
SocketCore.cc SocketCore.h\
BinaryStream.h\
Command.cc Command.h\
@ -632,15 +633,18 @@ SRCS += KqueueEventPoll.cc KqueueEventPoll.h
endif # HAVE_KQUEUE
AR = @AR@
noinst_LTLIBRARIES = libaria2c.la
libaria2c_la_SOURCES = $(SRCS)
libaria2c_la_LIBADD = @WSLAY_LIBS@
aria2c_LDADD = libaria2c.la @LIBINTL@ @ALLOCA@ #-lprofiler
pkgconfigdir = $(libdir)/pkgconfig
pkgconfig_DATA = libaria2.pc
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
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
lib_LTLIBRARIES = libaria2.la
libaria2_la_SOURCES =
libaria2_la_LIBADD = libaria2c.la

View File

@ -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."));
}
error_code::Value MultiUrlRequestInfo::prepare()
int MultiUrlRequestInfo::prepare()
{
try {
SharedHandle<rpc::WebSocketSessionMan> wsSessionMan;
@ -259,9 +259,9 @@ error_code::Value MultiUrlRequestInfo::prepare()
} catch(RecoverableException& e) {
SingletonHolder<Notifier>::clear();
resetSignalHandlers();
return error_code::UNKNOWN_ERROR;
return -1;
}
return error_code::FINISHED;
return 0;
}
error_code::Value MultiUrlRequestInfo::getResult()
@ -306,30 +306,19 @@ error_code::Value MultiUrlRequestInfo::getResult()
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 returnValue;
returnValue = prepare();
if(returnValue != error_code::FINISHED) {
return returnValue;
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);
}
returnValue = getResult();
error_code::Value returnValue = getResult();
resetSignalHandlers();
return returnValue;
}
@ -346,4 +335,10 @@ void MultiUrlRequestInfo::resetSignalHandlers()
util::setGlobalSignalHandler(SIGTERM, &mask_, SIG_DFL, 0);
}
const SharedHandle<DownloadEngine>&
MultiUrlRequestInfo::getDownloadEngine() const
{
return e_;
}
} // namespace aria2

View File

@ -86,19 +86,25 @@ 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();
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();
// Returns 1 if the caller needs to call this function one or more
// time. Returns 0 if the function succeeds. Returns -1 on error.
// For return value 0 and -1, the caller must call tearDown() to get
// final error code.
int run();
const SharedHandle<DownloadEngine>& getDownloadEngine() const;
};
} // namespace aria2

View File

@ -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
(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

185
src/aria2api.cc Normal file
View File

@ -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

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

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

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

@ -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

View File

@ -36,6 +36,7 @@
#include <unistd.h>
#include <aria2/aria2.h>
#include "SharedHandle.h"
#include "Context.h"
#include "MultiUrlRequestInfo.h"
@ -48,7 +49,7 @@ namespace aria2 {
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;
if(context.reqinfo) {
exitStatus = context.reqinfo->execute();

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,9 @@ void optionNativeToUtf8(Option& op)
} // namespace
#endif // __MINGW32__
void option_processing(Option& op, std::vector<std::string>& uris,
int argc, char* argv[])
void 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 {
@ -279,6 +281,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);
@ -301,7 +304,8 @@ void option_processing(Option& op, std::vector<std::string>& uris,
showUsage("", oparser, global::cerr());
exit(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

View File

@ -237,9 +237,10 @@ if !HAVE_TIMEGM
aria2c_SOURCES += TimegmTest.cc
endif # !HAVE_TIMEGM
aria2c_LDADD = ../src/libaria2c.la @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