Skip to content

Commit

Permalink
Add --bt-detach-seed-only option
Browse files Browse the repository at this point in the history
This option excludes seed only downloads when counting concurrent
active downloads (-j option).  This means that if -j3 is given and
this option is turned on and 3 downloads are active and one of those
enters seed mode, then it is excluded from active download count (thus
it becomes 2), and the next download waiting in queue gets started.
But be aware that seeding item is still recognized as active download
in RPC method.
  • Loading branch information
tatsuhiro-t committed Oct 13, 2014
1 parent 09d7956 commit f72a303
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 7 deletions.
10 changes: 10 additions & 0 deletions doc/manual-src/en/aria2c.rst
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,16 @@ BitTorrent/Metalink Options
BitTorrent Specific Options
~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. option:: --bt-detach-seed-only[=true|false]

Exclude seed only downloads when counting concurrent active
downloads (See :option:`-j` option). This means that if ``-j3`` is
given and this option is turned on and 3 downloads are active and
one of those enters seed mode, then it is excluded from active
download count (thus it becomes 2), and the next download waiting in
queue gets started. But be aware that seeding item is still
recognized as active download in RPC method. Default: ``false``

.. option:: --bt-enable-lpd[=true|false]

Enable Local Peer Discovery. If a private flag is set in a torrent,
Expand Down
1 change: 1 addition & 0 deletions src/BtFileAllocationEntry.cc
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ void BtFileAllocationEntry::prepareForNextAction
diskAdaptor->openFile();
}
#endif // __MINGW32__
getRequestGroup()->enableSeedOnly();
}
}

Expand Down
11 changes: 7 additions & 4 deletions src/DefaultPieceStorage.cc
Original file line number Diff line number Diff line change
Expand Up @@ -490,11 +490,14 @@ void DefaultPieceStorage::completePiece(const std::shared_ptr<Piece>& piece)
diskAdaptor_->enableReadOnly();
diskAdaptor_->openFile();
#endif // __MINGW32__
util::executeHookByOptName(downloadContext_->getOwnerRequestGroup(),
option_, PREF_ON_BT_DOWNLOAD_COMPLETE);
auto group = downloadContext_->getOwnerRequestGroup();

util::executeHookByOptName(group, option_,
PREF_ON_BT_DOWNLOAD_COMPLETE);
SingletonHolder<Notifier>::instance()->
notifyDownloadEvent(EVENT_ON_BT_DOWNLOAD_COMPLETE,
downloadContext_->getOwnerRequestGroup());
notifyDownloadEvent(EVENT_ON_BT_DOWNLOAD_COMPLETE, group);

group->enableSeedOnly();
}
}
#endif // ENABLE_BITTORRENT
Expand Down
9 changes: 9 additions & 0 deletions src/OptionHandlerFactory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1700,6 +1700,15 @@ std::vector<OptionHandler*> OptionHandlerFactory::createOptionHandlers()
#endif // ENABLE_BITTORRENT || ENABLE_METALINK
// BitTorrent Specific Options
#ifdef ENABLE_BITTORRENT
{
OptionHandler* op(new BooleanOptionHandler
(PREF_BT_DETACH_SEED_ONLY,
TEXT_BT_DETACH_SEED_ONLY,
A2_V_FALSE,
OptionHandler::OPT_ARG));
op->addTag(TAG_BITTORRENT);
handlers.push_back(op);
}
{
OptionHandler* op(new BooleanOptionHandler
(PREF_BT_ENABLE_LPD,
Expand Down
17 changes: 16 additions & 1 deletion src/RequestGroup.cc
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,8 @@ RequestGroup::RequestGroup(const std::shared_ptr<GroupId>& gid,
haltRequested_(false),
forceHaltRequested_(false),
pauseRequested_(false),
inMemoryDownload_(false)
inMemoryDownload_(false),
seedOnly_(false)
{
fileAllocationEnabled_ = option_->get(PREF_FILE_ALLOCATION) != V_NONE;
if(!option_->getAsBool(PREF_DRY_RUN)) {
Expand Down Expand Up @@ -1265,4 +1266,18 @@ bool RequestGroup::p2pInvolved() const
#endif // !ENABLE_BITTORRENT
}

void RequestGroup::enableSeedOnly()
{
if(seedOnly_ || !option_->getAsBool(PREF_BT_DETACH_SEED_ONLY)) {
return;
}

if(requestGroupMan_) {
seedOnly_ = true;

requestGroupMan_->decreaseNumActive();
requestGroupMan_->requestQueueCheck();
}
}

} // namespace aria2
8 changes: 8 additions & 0 deletions src/RequestGroup.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ class RequestGroup {
// just sits in memory.
bool inMemoryDownload_;

bool seedOnly_;

void validateFilename(const std::string& expectedFilename,
const std::string& actualFilename) const;
Expand Down Expand Up @@ -563,6 +564,13 @@ class RequestGroup {
{
state_ = state;
}

bool isSeedOnlyEnabled()
{
return seedOnly_;
}

void enableSeedOnly();
};

} // namespace aria2
Expand Down
18 changes: 16 additions & 2 deletions src/RequestGroupMan.cc
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ RequestGroupMan::RequestGroupMan
int maxSimultaneousDownloads,
const Option* option)
: maxSimultaneousDownloads_(maxSimultaneousDownloads),
numActive_(0),
option_(option),
serverStatMan_(std::make_shared<ServerStatMan>()),
maxOverallDownloadSpeedLimit_
Expand Down Expand Up @@ -140,6 +141,7 @@ bool RequestGroupMan::downloadFinished()
void RequestGroupMan::addRequestGroup
(const std::shared_ptr<RequestGroup>& group)
{
++numActive_;
requestGroups_.push_back(group->getGID(), group);
}

Expand Down Expand Up @@ -320,6 +322,11 @@ class ProcessStoppedRequestGroup {
if(group->getNumCommand() == 0) {
collectStat(group);
const std::shared_ptr<DownloadContext>& dctx = group->getDownloadContext();

if(!group->isSeedOnlyEnabled()) {
e_->getRequestGroupMan()->decreaseNumActive();
}

// DownloadContext::resetDownloadStopTime() is only called when
// download completed. If
// DownloadContext::getDownloadStopTime().isZero() is true, then
Expand Down Expand Up @@ -453,11 +460,11 @@ std::vector<std::unique_ptr<Command>> createInitialCommand
void RequestGroupMan::fillRequestGroupFromReserver(DownloadEngine* e)
{
removeStoppedGroup(e);
if(static_cast<size_t>(maxSimultaneousDownloads_) <= requestGroups_.size()) {
if(static_cast<size_t>(maxSimultaneousDownloads_) <= numActive_) {
return;
}
int count = 0;
int num = maxSimultaneousDownloads_-requestGroups_.size();
int num = maxSimultaneousDownloads_ - numActive_;
std::vector<std::shared_ptr<RequestGroup> > pending;

while(count < num && (uriListParser_ || !reservedGroups_.empty())) {
Expand Down Expand Up @@ -488,6 +495,7 @@ void RequestGroupMan::fillRequestGroupFromReserver(DownloadEngine* e)
configureRequestGroup(groupToAdd);
groupToAdd->setRequestGroupMan(this);
groupToAdd->setState(RequestGroup::STATE_ACTIVE);
++numActive_;
requestGroups_.push_back(groupToAdd->getGID(), groupToAdd);
try {
auto res = createInitialCommand(groupToAdd, e);
Expand Down Expand Up @@ -965,4 +973,10 @@ void RequestGroupMan::initWrDiskCache()
}
}

void RequestGroupMan::decreaseNumActive()
{
assert(numActive_ > 0);
--numActive_;
}

} // namespace aria2
9 changes: 9 additions & 0 deletions src/RequestGroupMan.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ class RequestGroupMan {

int maxSimultaneousDownloads_;

// The number of simultaneous active downloads, excluding seed only
// item if PREF_BT_DETACH_SEED_ONLY is true. We rely on this
// variable to maintain the number of concurrent downloads. If
// PREF_BT_DETACH_SEED_ONLY is false, this variable is equal to
// requestGroups_.size().
size_t numActive_;

const Option* option_;

std::shared_ptr<ServerStatMan> serverStatMan_;
Expand Down Expand Up @@ -376,6 +383,8 @@ class RequestGroupMan {
{
return openedFileCounter_;
}

void decreaseNumActive();
};

} // namespace aria2
Expand Down
1 change: 1 addition & 0 deletions src/prefs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,7 @@ PrefPtr PREF_BT_EXCLUDE_TRACKER = makePref("bt-exclude-tracker");
// values: true | false
PrefPtr PREF_BT_REMOVE_UNSELECTED_FILE =
makePref("bt-remove-unselected-file");
PrefPtr PREF_BT_DETACH_SEED_ONLY = makePref("bt-detach-seed-only");

/**
* Metalink related preferences
Expand Down
2 changes: 2 additions & 0 deletions src/prefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,8 @@ extern PrefPtr PREF_BT_TRACKER;
extern PrefPtr PREF_BT_EXCLUDE_TRACKER;
// values: true | false
extern PrefPtr PREF_BT_REMOVE_UNSELECTED_FILE;
// values: true |false
extern PrefPtr PREF_BT_DETACH_SEED_ONLY;

/**
* Metalink related preferences
Expand Down
11 changes: 11 additions & 0 deletions src/usage_text.h
Original file line number Diff line number Diff line change
Expand Up @@ -1000,3 +1000,14 @@
" using their metadata. This option pauses these\n" \
" subsequent downloads. This option is effective\n" \
" only when --enable-rpc=true is given.")
#define TEXT_BT_DETACH_SEED_ONLY \
_(" --bt-detach-seed-only[=true|false]\n" \
" Exclude seed only downloads when counting\n" \
" concurrent active downloads (See -j option).\n" \
" This means that if -j3 is given and this option\n" \
" is turned on and 3 downloads are active and one\n" \
" of those enters seed mode, then it is excluded\n" \
" from active download count (thus it becomes 2),\n" \
" and the next download waiting in queue gets\n" \
" started. But be aware that seeding item is still\n" \
" recognized as active download in RPC method.")

0 comments on commit f72a303

Please sign in to comment.