2007-05-20 13:57:56 +00:00
|
|
|
/* <!-- copyright */
|
|
|
|
/*
|
|
|
|
* aria2 - The high speed download utility
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006 Tatsuhiro Tsujikawa
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
2010-01-05 16:01:46 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2007-05-20 13:57:56 +00:00
|
|
|
*
|
|
|
|
* In addition, as a special exception, the copyright holders give
|
|
|
|
* permission to link the code of portions of this program with the
|
|
|
|
* OpenSSL library under certain conditions as described in each
|
|
|
|
* individual source file, and distribute linked combinations
|
|
|
|
* including the two.
|
|
|
|
* You must obey the GNU General Public License in all respects
|
|
|
|
* for all of the code used other than OpenSSL. If you modify
|
|
|
|
* file(s) with this exception, you may extend this exception to your
|
|
|
|
* version of the file(s), but you are not obligated to do so. If you
|
|
|
|
* do not wish to do so, delete this exception statement from your
|
|
|
|
* version. If you delete this exception statement from all source
|
|
|
|
* files in the program, then also delete it here.
|
|
|
|
*/
|
|
|
|
/* copyright --> */
|
|
|
|
#include "RequestGroupMan.h"
|
2008-11-10 16:10:31 +00:00
|
|
|
|
2009-05-22 13:43:07 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <cstring>
|
2008-11-10 16:10:31 +00:00
|
|
|
#include <iomanip>
|
|
|
|
#include <sstream>
|
|
|
|
#include <numeric>
|
|
|
|
#include <algorithm>
|
2011-05-07 09:48:58 +00:00
|
|
|
#include <utility>
|
2008-11-10 16:10:31 +00:00
|
|
|
|
2007-10-11 16:58:24 +00:00
|
|
|
#include "BtProgressInfoFile.h"
|
2007-11-10 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Don't connect server before checking file integrity at startup,
if
filesize and output file path are known.
* src/AbstractCommand.cc
* src/StreamFileAllocationEntry.cc
* src/Metalink2RequestGroup.cc
* src/RequestGroup.{h, cc}
* src/HttpResponseCommand.cc
* src/FtpNegotiationCommand.cc
Added DownloadFailureException. If it is thrown, RequestGroup
should
halt.
* src/AbstractCommand.cc
* src/DownloadFailureException.h
* src/RequestGroup.cc
Catch RecoverableException, instead of DlAbortEx.
* src/RequestGroupMan.cc
* src/FillRequestGroupCommand.cc
* src/MetaFileUtil.cc
* src/IteratableChunkChecksumValidator.cc
Now first parameter of MSG_DOWNLOAD_ABORTED is
gid(RequestGroup::
getGID())
* src/CheckIntegrityCommand.cc
* src/message.h
Print gid instead of idx.
* src/RequestGroupMan.cc
Removed exception throwers declaration.
* src/DirectDiskAdaptor.{h, cc}
* src/SocketCore.{h, cc}
* src/MultiDiskAdaptor.{h, cc}
* src/HttpConnection.{h, cc}
* src/HttpResponse.{h, cc}
* src/DiskAdaptor.{h, cc}
* src/CopyDiskAdaptor.{h, cc}
* src/MultiDiskAdaptor.{h, cc}
* src/HttpHeaderProcessor.{h, cc}
* src/AbstractSingleDiskAdaptor.{h, cc}
* src/Util.{h, cc}
* test/UtilTest.cc
* src/DefaultDiskWriter.{h, cc}
* src/FtpConnection.{h, cc}
* src/AbstractDiskWriter.{h, cc}
Removed duplicate code.
* src/StreamCheckIntegrityEntry.cc
Removed unnecessary include.
* src/DiskWriter.h
Included Exception.h
* src/option_processing.cc
Included 2 files and added doc
* src/TrackerWatcherCommand.cc
* src/SocketCore.cc (writeData): Fixed send error with GnuTLS.
2007-11-09 18:01:12 +00:00
|
|
|
#include "RecoverableException.h"
|
2007-10-11 16:58:24 +00:00
|
|
|
#include "RequestGroup.h"
|
|
|
|
#include "LogFactory.h"
|
2008-02-08 15:53:45 +00:00
|
|
|
#include "Logger.h"
|
2007-05-20 13:57:56 +00:00
|
|
|
#include "DownloadEngine.h"
|
2007-07-20 17:06:21 +00:00
|
|
|
#include "message.h"
|
2007-11-07 12:36:33 +00:00
|
|
|
#include "a2functional.h"
|
2007-11-27 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Rewritten to add content-type support.
* src/DownloadHandler.{h, cc}
* src/BtPostDownloadHandler.{h, cc}
* test/BtPostDownloadHandlerTest.cc
* src/MetalinkPostDownloadHandler.{h, cc}
* test/MetalinkPostDownloadHandlerTest.cc
* src/PostDownloadHandler.{h, cc}
* src/DownloadHandlerConstants.{h, cc}
* src/RequestGroup.cc
* src/HttpResponseCommand.cc
* src/FtpNegotiationCommand.cc
* src/SingleFileDownloadContext.{h, cc}
* src/RequestGroup.h
* src/RequestGroupCriteria.h
* src/ContentTypeRequestGroupCriteria.h
Added 'mem' option value for --follow-metalink,
--follow-torrent.
If it is give, metalink/torrent file is not written to the disk,
but
just is kept in memory. Parsing is occurred on memory.
* src/MetalinkHelper.{h, cc}
* src/MetalinkProcessor.h
* src/Xml2MetalinkProcessor.{h, cc}
* test/Xml2MetalinkProcessorTest.cc
* src/DownloadHandlerFactory.{h, cc}
* test/DownloadHandlerFactoryTest.cc
* src/PreDownloadHandler.{h, cc}
* src/OptionHandlerFactory.cc
* src/DefaultBtContext.{h, cc}
* test/DefaultBtContextTest.cc
* src/version_usage.cc
* src/Metalink2RequestGroup.{h, cc}
* src/RequestGroup.{h, cc}
* src/a2functional.h
* test/a2functionalTest.cc
* src/MemoryBufferPreDownloadHandler.{h, cc}
* src/OptionHandlerImpl.h
* src/prefs.h
* src/Util.{h, cc}
* test/UtilTest.cc
Keep DownloadResult rather than RequestGroup after downloads to
reduce
memory usage.
* src/RequestGroupMan.{h, cc}
* src/DownloadEngine.cc
* src/BtDependency.{h, cc}: Changed the type of dependee from
WeakHandle to SharedHandle because WeakHandle could be null.
* src/RequestGroup.{h, cc}
* src/DownloadEngineFactory.cc
* src/DownloadResult.h
Set totalLength after download finished
* src/UnknownLengthPieceStorage.{h, cc}
Keep torrent file specified in metalink in memory.
* src/Metalink2RequestGroup.cc
* src/BtDependency.cc
* src/TrueRequestGroupCriteria.h
Fixed the bug: seekg is used where seekp should be used.
* src/ByteArrayDiskWriter.cc
* test/ByteArraydiskWriterTest.cc
2007-11-27 12:27:10 +00:00
|
|
|
#include "DownloadResult.h"
|
2008-07-12 15:09:03 +00:00
|
|
|
#include "DownloadContext.h"
|
2008-08-04 17:06:47 +00:00
|
|
|
#include "ServerStatMan.h"
|
|
|
|
#include "ServerStat.h"
|
|
|
|
#include "SegmentMan.h"
|
2009-03-13 12:44:58 +00:00
|
|
|
#include "FeedbackURISelector.h"
|
2011-06-11 12:49:09 +00:00
|
|
|
#include "InorderURISelector.h"
|
2009-01-06 13:13:42 +00:00
|
|
|
#include "AdaptiveURISelector.h"
|
2008-08-04 17:06:47 +00:00
|
|
|
#include "Option.h"
|
|
|
|
#include "prefs.h"
|
2008-08-23 15:42:06 +00:00
|
|
|
#include "File.h"
|
2009-10-22 15:35:33 +00:00
|
|
|
#include "util.h"
|
2009-01-15 15:23:16 +00:00
|
|
|
#include "Command.h"
|
2009-03-10 12:58:38 +00:00
|
|
|
#include "FileEntry.h"
|
2010-11-20 08:21:36 +00:00
|
|
|
#include "fmt.h"
|
2010-06-08 14:11:36 +00:00
|
|
|
#include "FileAllocationEntry.h"
|
|
|
|
#include "CheckIntegrityEntry.h"
|
2010-06-12 13:49:39 +00:00
|
|
|
#include "Segment.h"
|
2010-06-18 14:47:09 +00:00
|
|
|
#include "DlAbortEx.h"
|
2010-09-11 12:09:19 +00:00
|
|
|
#include "uri.h"
|
2010-11-14 07:17:55 +00:00
|
|
|
#include "Signature.h"
|
2011-08-09 14:33:55 +00:00
|
|
|
#include "OutputFile.h"
|
2012-02-05 09:57:16 +00:00
|
|
|
#include "download_helper.h"
|
|
|
|
#include "UriListParser.h"
|
2012-03-20 12:42:09 +00:00
|
|
|
#include "SingletonHolder.h"
|
|
|
|
#include "Notifier.h"
|
2012-12-01 12:33:25 +00:00
|
|
|
#include "PeerStat.h"
|
2012-11-27 13:04:59 +00:00
|
|
|
#include "WrDiskCache.h"
|
2012-03-25 10:25:43 +00:00
|
|
|
#ifdef ENABLE_BITTORRENT
|
|
|
|
# include "bittorrent_helper.h"
|
|
|
|
#endif // ENABLE_BITTORRENT
|
2007-10-11 16:58:24 +00:00
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
namespace aria2 {
|
|
|
|
|
2012-12-19 14:03:48 +00:00
|
|
|
namespace {
|
|
|
|
template<typename InputIterator>
|
|
|
|
void appendReservedGroup(RequestGroupList& list,
|
|
|
|
InputIterator first, InputIterator last)
|
|
|
|
{
|
|
|
|
for(; first != last; ++first) {
|
|
|
|
list.push_back((*first)->getGID(), *first);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2010-02-28 12:30:11 +00:00
|
|
|
RequestGroupMan::RequestGroupMan
|
2013-06-23 07:35:14 +00:00
|
|
|
(std::vector<std::shared_ptr<RequestGroup> > requestGroups,
|
2011-12-08 12:38:00 +00:00
|
|
|
int maxSimultaneousDownloads,
|
2010-11-20 08:21:36 +00:00
|
|
|
const Option* option)
|
2012-12-19 14:03:48 +00:00
|
|
|
: maxSimultaneousDownloads_(maxSimultaneousDownloads),
|
2010-11-20 08:21:36 +00:00
|
|
|
option_(option),
|
|
|
|
serverStatMan_(new ServerStatMan()),
|
|
|
|
maxOverallDownloadSpeedLimit_
|
|
|
|
(option->getAsInt(PREF_MAX_OVERALL_DOWNLOAD_LIMIT)),
|
|
|
|
maxOverallUploadSpeedLimit_(option->getAsInt
|
|
|
|
(PREF_MAX_OVERALL_UPLOAD_LIMIT)),
|
2013-05-01 12:28:04 +00:00
|
|
|
keepRunning_(option->getAsBool(PREF_ENABLE_RPC)),
|
2010-11-20 08:21:36 +00:00
|
|
|
queueCheck_(true),
|
|
|
|
removedErrorResult_(0),
|
2010-11-28 07:52:02 +00:00
|
|
|
removedLastErrorResult_(error_code::FINISHED),
|
2012-11-27 13:04:59 +00:00
|
|
|
maxDownloadResult_(option->getAsInt(PREF_MAX_DOWNLOAD_RESULT)),
|
2013-08-20 16:57:17 +00:00
|
|
|
wrDiskCache_(nullptr)
|
2012-07-31 14:55:51 +00:00
|
|
|
{
|
2012-12-19 14:03:48 +00:00
|
|
|
appendReservedGroup(reservedGroups_,
|
|
|
|
requestGroups.begin(), requestGroups.end());
|
2012-07-31 14:55:51 +00:00
|
|
|
}
|
2007-10-11 16:58:24 +00:00
|
|
|
|
2012-11-27 13:04:59 +00:00
|
|
|
RequestGroupMan::~RequestGroupMan()
|
|
|
|
{
|
|
|
|
delete wrDiskCache_;
|
|
|
|
}
|
2010-11-14 07:17:55 +00:00
|
|
|
|
2007-10-11 16:58:24 +00:00
|
|
|
bool RequestGroupMan::downloadFinished()
|
|
|
|
{
|
2013-05-01 12:28:04 +00:00
|
|
|
if(keepRunning_) {
|
2009-05-08 08:18:23 +00:00
|
|
|
return false;
|
|
|
|
}
|
2010-06-21 13:51:56 +00:00
|
|
|
return requestGroups_.empty() && reservedGroups_.empty();
|
2007-10-11 16:58:24 +00:00
|
|
|
}
|
|
|
|
|
2010-02-28 12:30:11 +00:00
|
|
|
void RequestGroupMan::addRequestGroup
|
2013-06-21 16:10:38 +00:00
|
|
|
(const std::shared_ptr<RequestGroup>& group)
|
2007-10-11 16:58:24 +00:00
|
|
|
{
|
2012-12-19 14:03:48 +00:00
|
|
|
requestGroups_.push_back(group->getGID(), group);
|
2007-10-11 16:58:24 +00:00
|
|
|
}
|
|
|
|
|
2010-02-28 12:30:11 +00:00
|
|
|
void RequestGroupMan::addReservedGroup
|
2013-06-21 16:10:38 +00:00
|
|
|
(const std::vector<std::shared_ptr<RequestGroup> >& groups)
|
2007-10-11 16:58:24 +00:00
|
|
|
{
|
2011-06-16 13:01:14 +00:00
|
|
|
requestQueueCheck();
|
2012-12-19 14:03:48 +00:00
|
|
|
appendReservedGroup(reservedGroups_, groups.begin(), groups.end());
|
2007-10-11 16:58:24 +00:00
|
|
|
}
|
|
|
|
|
2010-02-28 12:30:11 +00:00
|
|
|
void RequestGroupMan::addReservedGroup
|
2013-06-21 16:10:38 +00:00
|
|
|
(const std::shared_ptr<RequestGroup>& group)
|
2007-10-11 16:58:24 +00:00
|
|
|
{
|
2011-06-16 13:01:14 +00:00
|
|
|
requestQueueCheck();
|
2012-12-19 14:03:48 +00:00
|
|
|
reservedGroups_.push_back(group->getGID(), group);
|
2007-10-11 16:58:24 +00:00
|
|
|
}
|
|
|
|
|
2012-12-22 10:29:27 +00:00
|
|
|
namespace {
|
|
|
|
struct RequestGroupKeyFunc {
|
2013-06-21 16:10:38 +00:00
|
|
|
a2_gid_t operator()(const std::shared_ptr<RequestGroup>& rg) const
|
2012-12-22 10:29:27 +00:00
|
|
|
{
|
|
|
|
return rg->getGID();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
2009-05-30 13:55:14 +00:00
|
|
|
void RequestGroupMan::insertReservedGroup
|
2013-06-21 16:10:38 +00:00
|
|
|
(size_t pos, const std::vector<std::shared_ptr<RequestGroup> >& groups)
|
2009-05-30 13:55:14 +00:00
|
|
|
{
|
2011-06-16 13:01:14 +00:00
|
|
|
requestQueueCheck();
|
2012-12-19 14:03:48 +00:00
|
|
|
pos = std::min(reservedGroups_.size(), pos);
|
2013-03-01 14:13:24 +00:00
|
|
|
reservedGroups_.insert(pos, RequestGroupKeyFunc(),
|
2012-12-22 10:29:27 +00:00
|
|
|
groups.begin(), groups.end());
|
2009-05-30 13:55:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RequestGroupMan::insertReservedGroup
|
2013-06-21 16:10:38 +00:00
|
|
|
(size_t pos, const std::shared_ptr<RequestGroup>& group)
|
2009-05-30 13:55:14 +00:00
|
|
|
{
|
2011-06-16 13:36:20 +00:00
|
|
|
requestQueueCheck();
|
2012-12-19 14:03:48 +00:00
|
|
|
pos = std::min(reservedGroups_.size(), pos);
|
|
|
|
reservedGroups_.insert(pos, group->getGID(), group);
|
2012-12-16 09:50:45 +00:00
|
|
|
}
|
|
|
|
|
2008-03-09 12:24:01 +00:00
|
|
|
size_t RequestGroupMan::countRequestGroup() const
|
2007-10-11 16:58:24 +00:00
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
return requestGroups_.size();
|
2007-10-11 16:58:24 +00:00
|
|
|
}
|
|
|
|
|
2013-06-21 16:10:38 +00:00
|
|
|
std::shared_ptr<RequestGroup> RequestGroupMan::findGroup(a2_gid_t gid) const
|
2012-07-31 14:55:51 +00:00
|
|
|
{
|
2013-06-21 16:10:38 +00:00
|
|
|
std::shared_ptr<RequestGroup> rg = requestGroups_.get(gid);
|
2012-12-19 14:03:48 +00:00
|
|
|
if(!rg) {
|
|
|
|
rg = reservedGroups_.get(gid);
|
2009-05-09 12:41:37 +00:00
|
|
|
}
|
2012-12-19 14:03:48 +00:00
|
|
|
return rg;
|
2009-05-09 12:41:37 +00:00
|
|
|
}
|
|
|
|
|
2009-12-21 15:17:34 +00:00
|
|
|
size_t RequestGroupMan::changeReservedGroupPosition
|
2013-05-14 15:50:55 +00:00
|
|
|
(a2_gid_t gid, int pos, OffsetMode how)
|
2009-12-21 15:17:34 +00:00
|
|
|
{
|
2012-12-19 14:03:48 +00:00
|
|
|
ssize_t dest = reservedGroups_.move(gid, pos, how);
|
|
|
|
if(dest == -1) {
|
2012-12-16 07:00:18 +00:00
|
|
|
throw DL_ABORT_EX(fmt("GID#%s not found in the waiting queue.",
|
|
|
|
GroupId::toHex(gid).c_str()));
|
2009-12-23 04:19:49 +00:00
|
|
|
} else {
|
2012-12-19 14:03:48 +00:00
|
|
|
return dest;
|
2009-12-23 04:19:49 +00:00
|
|
|
}
|
2009-12-21 15:17:34 +00:00
|
|
|
}
|
|
|
|
|
2011-03-17 03:17:46 +00:00
|
|
|
bool RequestGroupMan::removeReservedGroup(a2_gid_t gid)
|
2009-05-09 12:41:37 +00:00
|
|
|
{
|
2013-02-03 09:08:39 +00:00
|
|
|
return reservedGroups_.remove(gid);
|
2009-05-09 12:41:37 +00:00
|
|
|
}
|
|
|
|
|
2010-10-30 16:02:15 +00:00
|
|
|
namespace {
|
2010-12-04 09:14:27 +00:00
|
|
|
|
2012-03-20 12:42:09 +00:00
|
|
|
void notifyDownloadEvent
|
2013-06-21 16:10:38 +00:00
|
|
|
(DownloadEvent event, const std::shared_ptr<RequestGroup>& group)
|
2012-03-20 12:42:09 +00:00
|
|
|
{
|
2012-12-22 14:37:44 +00:00
|
|
|
// Check NULL to make unit test easier.
|
2013-04-25 12:46:31 +00:00
|
|
|
if(SingletonHolder<Notifier>::instance()) {
|
|
|
|
SingletonHolder<Notifier>::instance()->notifyDownloadEvent(event, group);
|
2012-12-22 14:37:44 +00:00
|
|
|
}
|
2012-03-20 12:42:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2010-10-30 16:02:15 +00:00
|
|
|
void executeStopHook
|
2013-06-21 16:10:38 +00:00
|
|
|
(const std::shared_ptr<RequestGroup>& group,
|
2010-12-04 09:14:27 +00:00
|
|
|
const Option* option,
|
|
|
|
error_code::Value result)
|
2009-05-22 13:43:07 +00:00
|
|
|
{
|
2010-12-04 09:14:27 +00:00
|
|
|
if(result == error_code::FINISHED &&
|
2009-05-22 13:43:07 +00:00
|
|
|
!option->blank(PREF_ON_DOWNLOAD_COMPLETE)) {
|
2010-12-04 09:14:27 +00:00
|
|
|
util::executeHookByOptName(group, option, PREF_ON_DOWNLOAD_COMPLETE);
|
|
|
|
} else if(result != error_code::IN_PROGRESS &&
|
2011-05-29 11:59:45 +00:00
|
|
|
result != error_code::REMOVED &&
|
2010-01-05 16:01:46 +00:00
|
|
|
!option->blank(PREF_ON_DOWNLOAD_ERROR)) {
|
2010-12-04 09:14:27 +00:00
|
|
|
util::executeHookByOptName(group, option, PREF_ON_DOWNLOAD_ERROR);
|
2009-05-22 13:43:07 +00:00
|
|
|
} else if(!option->blank(PREF_ON_DOWNLOAD_STOP)) {
|
2010-12-04 09:14:27 +00:00
|
|
|
util::executeHookByOptName(group, option, PREF_ON_DOWNLOAD_STOP);
|
2009-05-22 13:43:07 +00:00
|
|
|
}
|
2012-03-20 12:42:09 +00:00
|
|
|
if(result == error_code::FINISHED) {
|
2013-05-11 02:04:47 +00:00
|
|
|
notifyDownloadEvent(EVENT_ON_DOWNLOAD_COMPLETE, group);
|
2012-03-20 12:42:09 +00:00
|
|
|
} else if(result != error_code::IN_PROGRESS &&
|
|
|
|
result != error_code::REMOVED) {
|
2013-05-11 02:04:47 +00:00
|
|
|
notifyDownloadEvent(EVENT_ON_DOWNLOAD_ERROR, group);
|
2012-03-20 12:42:09 +00:00
|
|
|
} else {
|
2013-05-11 02:04:47 +00:00
|
|
|
notifyDownloadEvent(EVENT_ON_DOWNLOAD_STOP, group);
|
2012-03-20 12:42:09 +00:00
|
|
|
}
|
2009-05-22 13:43:07 +00:00
|
|
|
}
|
2010-12-04 09:14:27 +00:00
|
|
|
|
2010-10-30 16:02:15 +00:00
|
|
|
} // namespace
|
2009-05-22 13:43:07 +00:00
|
|
|
|
2010-08-31 13:33:05 +00:00
|
|
|
namespace {
|
2008-05-24 12:00:51 +00:00
|
|
|
class ProcessStoppedRequestGroup {
|
|
|
|
private:
|
2010-06-21 13:51:56 +00:00
|
|
|
DownloadEngine* e_;
|
2012-12-19 14:03:48 +00:00
|
|
|
RequestGroupList& reservedGroups_;
|
2008-07-12 15:09:03 +00:00
|
|
|
|
2013-06-21 16:10:38 +00:00
|
|
|
void saveSignature(const std::shared_ptr<RequestGroup>& group)
|
2008-07-12 15:09:03 +00:00
|
|
|
{
|
2013-07-12 13:59:49 +00:00
|
|
|
auto& sig = group->getDownloadContext()->getSignature();
|
2010-11-12 12:48:48 +00:00
|
|
|
if(sig && !sig->getBody().empty()) {
|
2008-07-12 15:09:03 +00:00
|
|
|
// filename of signature file is the path to download file followed by
|
|
|
|
// ".sig".
|
2009-06-28 10:37:15 +00:00
|
|
|
std::string signatureFile = group->getFirstFilePath()+".sig";
|
2008-07-12 15:09:03 +00:00
|
|
|
if(sig->save(signatureFile)) {
|
2010-11-20 08:21:36 +00:00
|
|
|
A2_LOG_NOTICE(fmt(MSG_SIGNATURE_SAVED, signatureFile.c_str()));
|
2008-07-12 15:09:03 +00:00
|
|
|
} else {
|
2010-11-20 08:21:36 +00:00
|
|
|
A2_LOG_NOTICE(fmt(MSG_SIGNATURE_NOT_SAVED, signatureFile.c_str()));
|
2008-07-12 15:09:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-03-03 09:08:19 +00:00
|
|
|
|
|
|
|
// Collect statistics during download in PeerStats and update/register
|
|
|
|
// ServerStatMan
|
2013-06-21 16:10:38 +00:00
|
|
|
void collectStat(const std::shared_ptr<RequestGroup>& group)
|
2013-03-03 09:08:19 +00:00
|
|
|
{
|
|
|
|
if(group->getSegmentMan()) {
|
|
|
|
bool singleConnection =
|
|
|
|
group->getSegmentMan()->getPeerStats().size() == 1;
|
2013-06-21 16:10:38 +00:00
|
|
|
const std::vector<std::shared_ptr<PeerStat> >& peerStats =
|
2013-03-03 09:08:19 +00:00
|
|
|
group->getSegmentMan()->getFastestPeerStats();
|
2013-08-21 03:06:53 +00:00
|
|
|
for(auto & stat : peerStats) {
|
|
|
|
if(stat->getHostname().empty() || stat->getProtocol().empty()) {
|
2013-03-03 09:08:19 +00:00
|
|
|
continue;
|
|
|
|
}
|
2013-08-21 03:06:53 +00:00
|
|
|
int speed = stat->getAvgDownloadSpeed();
|
2013-03-03 09:08:19 +00:00
|
|
|
if (speed == 0) continue;
|
|
|
|
|
2013-06-21 16:10:38 +00:00
|
|
|
std::shared_ptr<ServerStat> ss =
|
2013-08-21 03:06:53 +00:00
|
|
|
e_->getRequestGroupMan()->getOrCreateServerStat(stat->getHostname(),
|
|
|
|
stat->getProtocol());
|
2013-03-03 09:08:19 +00:00
|
|
|
ss->increaseCounter();
|
|
|
|
ss->updateDownloadSpeed(speed);
|
|
|
|
if(singleConnection) {
|
|
|
|
ss->updateSingleConnectionAvgSpeed(speed);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ss->updateMultiConnectionAvgSpeed(speed);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-05-24 12:00:51 +00:00
|
|
|
public:
|
|
|
|
ProcessStoppedRequestGroup
|
2008-11-03 06:49:02 +00:00
|
|
|
(DownloadEngine* e,
|
2012-12-19 14:03:48 +00:00
|
|
|
RequestGroupList& reservedGroups)
|
2010-11-20 08:21:36 +00:00
|
|
|
: e_(e),
|
2012-12-19 14:03:48 +00:00
|
|
|
reservedGroups_(reservedGroups)
|
2010-11-20 08:21:36 +00:00
|
|
|
{}
|
2008-05-24 12:00:51 +00:00
|
|
|
|
2013-03-03 09:08:19 +00:00
|
|
|
bool operator()(const RequestGroupList::value_type& group)
|
2008-05-24 12:00:51 +00:00
|
|
|
{
|
|
|
|
if(group->getNumCommand() == 0) {
|
2013-03-03 09:08:19 +00:00
|
|
|
collectStat(group);
|
2013-06-21 16:10:38 +00:00
|
|
|
const std::shared_ptr<DownloadContext>& dctx = group->getDownloadContext();
|
2010-03-06 14:21:43 +00:00
|
|
|
// DownloadContext::resetDownloadStopTime() is only called when
|
|
|
|
// download completed. If
|
|
|
|
// DownloadContext::getDownloadStopTime().isZero() is true, then
|
|
|
|
// there is a possibility that the download is error or
|
|
|
|
// in-progress and resetDownloadStopTime() is not called. So
|
|
|
|
// call it here.
|
|
|
|
if(dctx->getDownloadStopTime().isZero()) {
|
|
|
|
dctx->resetDownloadStopTime();
|
|
|
|
}
|
2007-11-27 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Rewritten to add content-type support.
* src/DownloadHandler.{h, cc}
* src/BtPostDownloadHandler.{h, cc}
* test/BtPostDownloadHandlerTest.cc
* src/MetalinkPostDownloadHandler.{h, cc}
* test/MetalinkPostDownloadHandlerTest.cc
* src/PostDownloadHandler.{h, cc}
* src/DownloadHandlerConstants.{h, cc}
* src/RequestGroup.cc
* src/HttpResponseCommand.cc
* src/FtpNegotiationCommand.cc
* src/SingleFileDownloadContext.{h, cc}
* src/RequestGroup.h
* src/RequestGroupCriteria.h
* src/ContentTypeRequestGroupCriteria.h
Added 'mem' option value for --follow-metalink,
--follow-torrent.
If it is give, metalink/torrent file is not written to the disk,
but
just is kept in memory. Parsing is occurred on memory.
* src/MetalinkHelper.{h, cc}
* src/MetalinkProcessor.h
* src/Xml2MetalinkProcessor.{h, cc}
* test/Xml2MetalinkProcessorTest.cc
* src/DownloadHandlerFactory.{h, cc}
* test/DownloadHandlerFactoryTest.cc
* src/PreDownloadHandler.{h, cc}
* src/OptionHandlerFactory.cc
* src/DefaultBtContext.{h, cc}
* test/DefaultBtContextTest.cc
* src/version_usage.cc
* src/Metalink2RequestGroup.{h, cc}
* src/RequestGroup.{h, cc}
* src/a2functional.h
* test/a2functionalTest.cc
* src/MemoryBufferPreDownloadHandler.{h, cc}
* src/OptionHandlerImpl.h
* src/prefs.h
* src/Util.{h, cc}
* test/UtilTest.cc
Keep DownloadResult rather than RequestGroup after downloads to
reduce
memory usage.
* src/RequestGroupMan.{h, cc}
* src/DownloadEngine.cc
* src/BtDependency.{h, cc}: Changed the type of dependee from
WeakHandle to SharedHandle because WeakHandle could be null.
* src/RequestGroup.{h, cc}
* src/DownloadEngineFactory.cc
* src/DownloadResult.h
Set totalLength after download finished
* src/UnknownLengthPieceStorage.{h, cc}
Keep torrent file specified in metalink in memory.
* src/Metalink2RequestGroup.cc
* src/BtDependency.cc
* src/TrueRequestGroupCriteria.h
Fixed the bug: seekg is used where seekp should be used.
* src/ByteArrayDiskWriter.cc
* test/ByteArraydiskWriterTest.cc
2007-11-27 12:27:10 +00:00
|
|
|
try {
|
2010-01-05 16:01:46 +00:00
|
|
|
group->closeFile();
|
2012-09-06 16:40:40 +00:00
|
|
|
if(group->isPauseRequested()) {
|
|
|
|
A2_LOG_NOTICE
|
2012-12-16 07:00:18 +00:00
|
|
|
(fmt(_("Download GID#%s paused"),
|
|
|
|
GroupId::toHex(group->getGID()).c_str()));
|
2012-09-06 16:40:40 +00:00
|
|
|
group->saveControlFile();
|
|
|
|
} else if(group->downloadFinished() &&
|
2010-06-22 11:57:31 +00:00
|
|
|
!group->getDownloadContext()->isChecksumVerificationNeeded()) {
|
2010-01-05 16:01:46 +00:00
|
|
|
group->applyLastModifiedTimeToLocalFiles();
|
|
|
|
group->reportDownloadFinished();
|
2013-07-28 09:07:29 +00:00
|
|
|
if(group->allDownloadFinished() &&
|
|
|
|
!group->getOption()->getAsBool(PREF_FORCE_SAVE)) {
|
2010-01-05 16:01:46 +00:00
|
|
|
group->removeControlFile();
|
|
|
|
saveSignature(group);
|
|
|
|
} else {
|
|
|
|
group->saveControlFile();
|
|
|
|
}
|
2013-06-21 16:10:38 +00:00
|
|
|
std::vector<std::shared_ptr<RequestGroup> > nextGroups;
|
2010-01-05 16:01:46 +00:00
|
|
|
group->postDownloadProcessing(nextGroups);
|
|
|
|
if(!nextGroups.empty()) {
|
2010-11-20 08:21:36 +00:00
|
|
|
A2_LOG_DEBUG
|
|
|
|
(fmt("Adding %lu RequestGroups as a result of"
|
|
|
|
" PostDownloadHandler.",
|
|
|
|
static_cast<unsigned long>(nextGroups.size())));
|
2010-06-21 13:51:56 +00:00
|
|
|
e_->getRequestGroupMan()->insertReservedGroup(0, nextGroups);
|
2010-01-05 16:01:46 +00:00
|
|
|
}
|
2012-04-07 15:05:42 +00:00
|
|
|
#ifdef ENABLE_BITTORRENT
|
2012-03-25 10:25:43 +00:00
|
|
|
// For in-memory download (e.g., Magnet URI), the
|
|
|
|
// FileEntry::getPath() does not return actual file path, so
|
|
|
|
// we don't remove it.
|
|
|
|
if(group->getOption()->getAsBool(PREF_BT_REMOVE_UNSELECTED_FILE) &&
|
|
|
|
!group->inMemoryDownload() &&
|
2012-09-22 08:37:30 +00:00
|
|
|
dctx->hasAttribute(CTX_ATTR_BT)) {
|
2012-12-16 07:00:18 +00:00
|
|
|
A2_LOG_INFO(fmt(MSG_REMOVING_UNSELECTED_FILE,
|
|
|
|
GroupId::toHex(group->getGID()).c_str()));
|
2013-06-21 16:10:38 +00:00
|
|
|
const std::vector<std::shared_ptr<FileEntry> >& files =
|
2012-03-25 10:25:43 +00:00
|
|
|
dctx->getFileEntries();
|
2013-08-21 03:06:53 +00:00
|
|
|
for(auto & file : files) {
|
|
|
|
if(!file->isRequested()) {
|
|
|
|
if(File(file->getPath()).remove()) {
|
|
|
|
A2_LOG_INFO(fmt(MSG_FILE_REMOVED, file->getPath().c_str()));
|
2012-03-25 10:25:43 +00:00
|
|
|
} else {
|
|
|
|
A2_LOG_INFO(fmt(MSG_FILE_COULD_NOT_REMOVED,
|
2013-08-21 03:06:53 +00:00
|
|
|
file->getPath().c_str()));
|
2012-03-25 10:25:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-04-07 15:05:42 +00:00
|
|
|
#endif // ENABLE_BITTORRENT
|
2010-01-05 16:01:46 +00:00
|
|
|
} else {
|
2010-11-20 08:21:36 +00:00
|
|
|
A2_LOG_NOTICE
|
2012-12-16 07:00:18 +00:00
|
|
|
(fmt(_("Download GID#%s not complete: %s"),
|
|
|
|
GroupId::toHex(group->getGID()).c_str(),
|
2011-08-08 12:46:10 +00:00
|
|
|
group->getDownloadContext()->getBasePath().c_str()));
|
2010-01-05 16:01:46 +00:00
|
|
|
group->saveControlFile();
|
|
|
|
}
|
2008-04-27 02:22:14 +00:00
|
|
|
} catch(RecoverableException& ex) {
|
2010-11-20 08:21:36 +00:00
|
|
|
A2_LOG_ERROR_EX(EX_EXCEPTION_CAUGHT, ex);
|
2007-05-20 13:57:56 +00:00
|
|
|
}
|
2010-04-08 16:02:22 +00:00
|
|
|
if(group->isPauseRequested()) {
|
2012-07-31 14:55:51 +00:00
|
|
|
group->setState(RequestGroup::STATE_WAITING);
|
2012-12-19 14:03:48 +00:00
|
|
|
reservedGroups_.push_front(group->getGID(), group);
|
2010-09-10 14:13:50 +00:00
|
|
|
group->releaseRuntimeResource(e_);
|
2010-04-08 16:02:22 +00:00
|
|
|
group->setForceHaltRequested(false);
|
2012-03-20 12:42:09 +00:00
|
|
|
util::executeHookByOptName(group, e_->getOption(),
|
|
|
|
PREF_ON_DOWNLOAD_PAUSE);
|
2013-05-11 02:04:47 +00:00
|
|
|
notifyDownloadEvent(EVENT_ON_DOWNLOAD_PAUSE, group);
|
2010-04-08 16:02:22 +00:00
|
|
|
// TODO Should we have to prepend spend uris to remaining uris
|
|
|
|
// in case PREF_REUSE_URI is disabed?
|
|
|
|
} else {
|
2013-06-21 16:10:38 +00:00
|
|
|
std::shared_ptr<DownloadResult> dr = group->createDownloadResult();
|
2010-09-10 14:13:50 +00:00
|
|
|
e_->getRequestGroupMan()->addDownloadResult(dr);
|
2010-12-04 09:14:27 +00:00
|
|
|
executeStopHook(group, e_->getOption(), dr->result);
|
2010-09-10 14:13:50 +00:00
|
|
|
group->releaseRuntimeResource(e_);
|
2010-04-08 16:02:22 +00:00
|
|
|
}
|
2013-03-03 09:08:19 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
2007-05-20 13:57:56 +00:00
|
|
|
}
|
|
|
|
}
|
2008-05-24 12:00:51 +00:00
|
|
|
};
|
2010-10-30 14:53:40 +00:00
|
|
|
} // namespace
|
2008-05-24 12:00:51 +00:00
|
|
|
|
2008-11-03 06:49:02 +00:00
|
|
|
void RequestGroupMan::removeStoppedGroup(DownloadEngine* e)
|
2008-05-24 12:00:51 +00:00
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
size_t numPrev = requestGroups_.size();
|
2013-03-03 09:08:19 +00:00
|
|
|
requestGroups_.remove_if(ProcessStoppedRequestGroup(e, reservedGroups_));
|
2010-06-21 13:51:56 +00:00
|
|
|
size_t numRemoved = numPrev-requestGroups_.size();
|
2008-05-24 12:00:51 +00:00
|
|
|
if(numRemoved > 0) {
|
2010-11-20 08:21:36 +00:00
|
|
|
A2_LOG_DEBUG(fmt("%lu RequestGroup(s) deleted.",
|
|
|
|
static_cast<unsigned long>(numRemoved)));
|
2007-05-20 13:57:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-04 17:06:47 +00:00
|
|
|
void RequestGroupMan::configureRequestGroup
|
2013-06-21 16:10:38 +00:00
|
|
|
(const std::shared_ptr<RequestGroup>& requestGroup) const
|
2008-08-04 17:06:47 +00:00
|
|
|
{
|
2011-10-22 12:49:46 +00:00
|
|
|
const std::string& uriSelectorValue =
|
|
|
|
requestGroup->getOption()->get(PREF_URI_SELECTOR);
|
2008-08-04 17:06:47 +00:00
|
|
|
if(uriSelectorValue == V_FEEDBACK) {
|
2013-07-03 16:16:48 +00:00
|
|
|
requestGroup->setURISelector(make_unique<FeedbackURISelector>
|
|
|
|
(serverStatMan_));
|
2008-08-04 17:06:47 +00:00
|
|
|
} else if(uriSelectorValue == V_INORDER) {
|
2013-07-03 16:16:48 +00:00
|
|
|
requestGroup->setURISelector(make_unique<InorderURISelector>());
|
2009-01-06 13:13:42 +00:00
|
|
|
} else if(uriSelectorValue == V_ADAPTIVE) {
|
2013-07-03 16:16:48 +00:00
|
|
|
requestGroup->setURISelector(make_unique<AdaptiveURISelector>
|
|
|
|
(serverStatMan_, requestGroup.get()));
|
2008-08-04 17:06:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-30 16:02:15 +00:00
|
|
|
namespace {
|
2013-06-23 12:55:52 +00:00
|
|
|
std::vector<std::unique_ptr<Command>> createInitialCommand
|
|
|
|
(const std::shared_ptr<RequestGroup>& requestGroup, DownloadEngine* e)
|
2008-12-29 14:05:39 +00:00
|
|
|
{
|
2013-06-23 12:55:52 +00:00
|
|
|
std::vector<std::unique_ptr<Command>> res;
|
|
|
|
requestGroup->createInitialCommand(res, e);
|
|
|
|
return res;
|
2008-12-29 14:05:39 +00:00
|
|
|
}
|
2010-10-30 16:02:15 +00:00
|
|
|
} // namespace
|
2008-12-29 14:05:39 +00:00
|
|
|
|
2007-05-20 13:57:56 +00:00
|
|
|
void RequestGroupMan::fillRequestGroupFromReserver(DownloadEngine* e)
|
|
|
|
{
|
2008-11-03 06:49:02 +00:00
|
|
|
removeStoppedGroup(e);
|
2011-12-08 12:38:00 +00:00
|
|
|
if(static_cast<size_t>(maxSimultaneousDownloads_) <= requestGroups_.size()) {
|
2009-05-30 14:28:18 +00:00
|
|
|
return;
|
|
|
|
}
|
2011-12-08 12:38:00 +00:00
|
|
|
int count = 0;
|
|
|
|
int num = maxSimultaneousDownloads_-requestGroups_.size();
|
2013-06-21 16:10:38 +00:00
|
|
|
std::vector<std::shared_ptr<RequestGroup> > pending;
|
2012-12-22 10:29:27 +00:00
|
|
|
|
|
|
|
while(count < num && (uriListParser_ || !reservedGroups_.empty())) {
|
|
|
|
if(uriListParser_ && reservedGroups_.empty()) {
|
2013-06-21 16:10:38 +00:00
|
|
|
std::vector<std::shared_ptr<RequestGroup> > groups;
|
2012-12-22 14:37:44 +00:00
|
|
|
// May throw exception
|
2012-02-05 09:57:16 +00:00
|
|
|
bool ok = createRequestGroupFromUriListParser(groups, option_,
|
|
|
|
uriListParser_.get());
|
|
|
|
if(ok) {
|
2012-12-19 14:03:48 +00:00
|
|
|
appendReservedGroup(reservedGroups_, groups.begin(), groups.end());
|
2012-02-05 09:57:16 +00:00
|
|
|
} else {
|
|
|
|
uriListParser_.reset();
|
2012-12-22 10:29:27 +00:00
|
|
|
if(reservedGroups_.empty()) {
|
2012-02-05 09:57:16 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-06-21 16:10:38 +00:00
|
|
|
std::shared_ptr<RequestGroup> groupToAdd = *reservedGroups_.begin();
|
2012-12-22 10:29:27 +00:00
|
|
|
reservedGroups_.pop_front();
|
2013-05-01 12:28:04 +00:00
|
|
|
if((keepRunning_ && groupToAdd->isPauseRequested()) ||
|
2012-12-22 14:37:44 +00:00
|
|
|
!groupToAdd->isDependencyResolved()) {
|
2012-12-22 10:29:27 +00:00
|
|
|
pending.push_back(groupToAdd);
|
2012-12-22 14:37:44 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Drop pieceStorage here because paused download holds its
|
|
|
|
// reference.
|
|
|
|
groupToAdd->dropPieceStorage();
|
|
|
|
configureRequestGroup(groupToAdd);
|
|
|
|
groupToAdd->setRequestGroupMan(this);
|
|
|
|
try {
|
2013-06-23 12:55:52 +00:00
|
|
|
auto res = createInitialCommand(groupToAdd, e);
|
2007-10-11 16:58:24 +00:00
|
|
|
++count;
|
2013-06-23 12:55:52 +00:00
|
|
|
if(res.empty()) {
|
|
|
|
requestQueueCheck();
|
|
|
|
} else {
|
|
|
|
e->addCommand(std::move(res));
|
|
|
|
}
|
2008-04-27 02:22:14 +00:00
|
|
|
} catch(RecoverableException& ex) {
|
2010-11-20 08:21:36 +00:00
|
|
|
A2_LOG_ERROR_EX(EX_EXCEPTION_CAUGHT, ex);
|
|
|
|
A2_LOG_DEBUG("Deleting temporal commands.");
|
2010-11-28 16:32:25 +00:00
|
|
|
groupToAdd->setLastErrorCode(ex.getErrorCode());
|
2012-12-22 14:37:44 +00:00
|
|
|
// We add groupToAdd to e later in order to it is processed in
|
2010-11-29 12:06:39 +00:00
|
|
|
// removeStoppedGroup().
|
|
|
|
requestQueueCheck();
|
2007-10-11 16:58:24 +00:00
|
|
|
}
|
2012-12-22 14:37:44 +00:00
|
|
|
groupToAdd->setState(RequestGroup::STATE_ACTIVE);
|
|
|
|
requestGroups_.push_back(groupToAdd->getGID(), groupToAdd);
|
|
|
|
|
2012-03-20 12:42:09 +00:00
|
|
|
util::executeHookByOptName(groupToAdd, e->getOption(),
|
|
|
|
PREF_ON_DOWNLOAD_START);
|
2013-05-11 02:04:47 +00:00
|
|
|
notifyDownloadEvent(EVENT_ON_DOWNLOAD_START, groupToAdd);
|
2007-05-20 13:57:56 +00:00
|
|
|
}
|
2012-12-22 10:29:27 +00:00
|
|
|
if(!pending.empty()) {
|
|
|
|
reservedGroups_.insert(reservedGroups_.begin(), RequestGroupKeyFunc(),
|
|
|
|
pending.begin(), pending.end());
|
|
|
|
}
|
2007-05-20 13:57:56 +00:00
|
|
|
if(count > 0) {
|
2008-04-20 05:42:15 +00:00
|
|
|
e->setNoWait(true);
|
2011-05-15 14:35:06 +00:00
|
|
|
e->setRefreshInterval(0);
|
2010-11-20 08:21:36 +00:00
|
|
|
A2_LOG_DEBUG(fmt("%d RequestGroup(s) added.", count));
|
2007-05-20 13:57:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-11 16:58:24 +00:00
|
|
|
void RequestGroupMan::save()
|
|
|
|
{
|
2013-07-28 09:07:29 +00:00
|
|
|
for(auto& rg : requestGroups_) {
|
2012-12-19 14:03:48 +00:00
|
|
|
if(rg->allDownloadFinished() &&
|
2013-07-28 09:07:29 +00:00
|
|
|
!rg->getDownloadContext()->isChecksumVerificationNeeded() &&
|
|
|
|
!rg->getOption()->getAsBool(PREF_FORCE_SAVE)) {
|
2012-12-19 14:03:48 +00:00
|
|
|
rg->removeControlFile();
|
2007-10-29 14:06:59 +00:00
|
|
|
} else {
|
2007-10-11 16:58:24 +00:00
|
|
|
try {
|
2012-12-19 14:03:48 +00:00
|
|
|
rg->saveControlFile();
|
2008-04-27 02:22:14 +00:00
|
|
|
} catch(RecoverableException& e) {
|
2010-11-20 08:21:36 +00:00
|
|
|
A2_LOG_ERROR_EX(EX_EXCEPTION_CAUGHT, e);
|
2007-10-11 16:58:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RequestGroupMan::closeFile()
|
|
|
|
{
|
2013-08-21 03:06:53 +00:00
|
|
|
for(auto & elem : requestGroups_) {
|
|
|
|
elem->closeFile();
|
2007-10-11 16:58:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-20 16:46:56 +00:00
|
|
|
RequestGroupMan::DownloadStat RequestGroupMan::getDownloadStat() const
|
|
|
|
{
|
2011-12-08 14:26:51 +00:00
|
|
|
int finished = 0;
|
|
|
|
int error = removedErrorResult_;
|
|
|
|
int inprogress = 0;
|
|
|
|
int removed = 0;
|
2010-11-28 07:52:02 +00:00
|
|
|
error_code::Value lastError = removedLastErrorResult_;
|
2013-08-21 03:06:53 +00:00
|
|
|
for(auto & dr : downloadResults_) {
|
|
|
|
|
2012-12-19 14:03:48 +00:00
|
|
|
if(dr->belongsTo != 0) {
|
2011-01-16 13:20:29 +00:00
|
|
|
continue;
|
|
|
|
}
|
2012-12-19 14:03:48 +00:00
|
|
|
if(dr->result == error_code::FINISHED) {
|
2008-02-20 16:46:56 +00:00
|
|
|
++finished;
|
2012-12-19 14:03:48 +00:00
|
|
|
} else if(dr->result == error_code::IN_PROGRESS) {
|
2011-05-29 11:59:45 +00:00
|
|
|
++inprogress;
|
2012-12-19 14:03:48 +00:00
|
|
|
} else if(dr->result == error_code::REMOVED) {
|
2011-05-29 11:59:45 +00:00
|
|
|
++removed;
|
2008-02-20 16:46:56 +00:00
|
|
|
} else {
|
|
|
|
++error;
|
2012-12-19 14:03:48 +00:00
|
|
|
lastError = dr->result;
|
2008-02-20 16:46:56 +00:00
|
|
|
}
|
|
|
|
}
|
2013-03-01 12:17:52 +00:00
|
|
|
return DownloadStat(error, inprogress,
|
2011-05-29 11:59:45 +00:00
|
|
|
reservedGroups_.size(),
|
2010-01-05 16:01:46 +00:00
|
|
|
lastError);
|
2008-02-20 16:46:56 +00:00
|
|
|
}
|
|
|
|
|
2013-05-11 02:04:47 +00:00
|
|
|
enum DownloadResultStatus {
|
2012-12-08 12:48:18 +00:00
|
|
|
A2_STATUS_OK,
|
|
|
|
A2_STATUS_INPR,
|
|
|
|
A2_STATUS_RM,
|
|
|
|
A2_STATUS_ERR
|
|
|
|
};
|
|
|
|
|
|
|
|
namespace {
|
2013-05-11 02:04:47 +00:00
|
|
|
const char* getStatusStr(DownloadResultStatus status, bool useColor)
|
2012-12-08 12:48:18 +00:00
|
|
|
{
|
|
|
|
// status string is formatted in 4 characters wide.
|
|
|
|
switch(status) {
|
|
|
|
case(A2_STATUS_OK):
|
|
|
|
if(useColor) {
|
|
|
|
return "\033[1;32mOK\033[0m ";
|
|
|
|
} else {
|
|
|
|
return "OK ";
|
|
|
|
}
|
|
|
|
case(A2_STATUS_INPR):
|
|
|
|
if(useColor) {
|
|
|
|
return "\033[1;34mINPR\033[0m";
|
|
|
|
} else {
|
|
|
|
return "INPR";
|
|
|
|
}
|
|
|
|
case(A2_STATUS_RM):
|
|
|
|
if(useColor) {
|
|
|
|
return "\033[1mRM\033[0m ";
|
|
|
|
} else {
|
|
|
|
return "RM ";
|
|
|
|
}
|
|
|
|
case(A2_STATUS_ERR):
|
|
|
|
if(useColor) {
|
|
|
|
return "\033[1;31mERR\033[0m ";
|
|
|
|
} else {
|
|
|
|
return "ERR ";
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2011-08-22 14:05:06 +00:00
|
|
|
void RequestGroupMan::showDownloadResults(OutputFile& o, bool full) const
|
2007-05-20 13:57:56 +00:00
|
|
|
{
|
2012-12-16 07:00:18 +00:00
|
|
|
int pathRowSize = 55;
|
2011-08-22 14:05:06 +00:00
|
|
|
// Download Results:
|
|
|
|
// idx|stat|path/length
|
|
|
|
// ===+====+=======================================================================
|
|
|
|
o.printf("\n%s"
|
2012-12-16 07:00:18 +00:00
|
|
|
"\ngid |stat|avg speed |",
|
2011-08-22 14:05:06 +00:00
|
|
|
_("Download Results:"));
|
|
|
|
if(full) {
|
|
|
|
o.write(" %|path/URI"
|
2012-12-16 07:00:18 +00:00
|
|
|
"\n======+====+===========+===+");
|
2011-08-22 14:05:06 +00:00
|
|
|
pathRowSize -= 4;
|
|
|
|
} else {
|
|
|
|
o.write("path/URI"
|
2012-12-16 07:00:18 +00:00
|
|
|
"\n======+====+===========+");
|
2011-08-22 14:05:06 +00:00
|
|
|
}
|
2011-08-09 14:33:55 +00:00
|
|
|
std::string line(pathRowSize, '=');
|
|
|
|
o.printf("%s\n", line.c_str());
|
2012-12-08 12:48:18 +00:00
|
|
|
bool useColor = o.supportsColor();
|
2008-10-21 16:26:43 +00:00
|
|
|
int ok = 0;
|
|
|
|
int err = 0;
|
|
|
|
int inpr = 0;
|
2011-05-29 11:59:45 +00:00
|
|
|
int rm = 0;
|
2013-08-21 03:06:53 +00:00
|
|
|
for(auto & dr : downloadResults_) {
|
|
|
|
|
2012-12-19 14:03:48 +00:00
|
|
|
if(dr->belongsTo != 0) {
|
2011-01-16 13:20:29 +00:00
|
|
|
continue;
|
|
|
|
}
|
2012-09-24 14:20:43 +00:00
|
|
|
const char* status;
|
2012-12-19 14:03:48 +00:00
|
|
|
switch(dr->result) {
|
|
|
|
case error_code::FINISHED:
|
2012-12-08 12:48:18 +00:00
|
|
|
status = getStatusStr(A2_STATUS_OK, useColor);
|
2008-10-21 16:26:43 +00:00
|
|
|
++ok;
|
2012-12-19 14:03:48 +00:00
|
|
|
break;
|
|
|
|
case error_code::IN_PROGRESS:
|
2012-12-08 12:48:18 +00:00
|
|
|
status = getStatusStr(A2_STATUS_INPR, useColor);
|
2009-05-09 04:11:31 +00:00
|
|
|
++inpr;
|
2012-12-19 14:03:48 +00:00
|
|
|
break;
|
|
|
|
case error_code::REMOVED:
|
2012-12-08 12:48:18 +00:00
|
|
|
status = getStatusStr(A2_STATUS_RM, useColor);
|
2011-05-29 11:59:45 +00:00
|
|
|
++rm;
|
2012-12-19 14:03:48 +00:00
|
|
|
break;
|
|
|
|
default:
|
2012-12-08 12:48:18 +00:00
|
|
|
status = getStatusStr(A2_STATUS_ERR, useColor);
|
2008-10-21 16:26:43 +00:00
|
|
|
++err;
|
|
|
|
}
|
2011-08-22 14:05:06 +00:00
|
|
|
if(full) {
|
2012-12-19 14:03:48 +00:00
|
|
|
formatDownloadResultFull(o, status, dr);
|
2011-08-22 14:05:06 +00:00
|
|
|
} else {
|
2012-12-19 14:03:48 +00:00
|
|
|
o.write(formatDownloadResult(status, dr).c_str());
|
2011-08-22 14:05:06 +00:00
|
|
|
o.write("\n");
|
|
|
|
}
|
2007-10-11 16:58:24 +00:00
|
|
|
}
|
2011-05-29 11:59:45 +00:00
|
|
|
if(ok > 0 || err > 0 || inpr > 0 || rm > 0) {
|
2011-08-09 14:33:55 +00:00
|
|
|
o.printf("\n%s\n", _("Status Legend:"));
|
2008-10-21 16:26:43 +00:00
|
|
|
if(ok > 0) {
|
2011-12-02 13:00:48 +00:00
|
|
|
o.write(_("(OK):download completed."));
|
2008-10-21 16:26:43 +00:00
|
|
|
}
|
|
|
|
if(err > 0) {
|
2011-12-02 13:00:48 +00:00
|
|
|
o.write(_("(ERR):error occurred."));
|
2008-10-21 16:26:43 +00:00
|
|
|
}
|
|
|
|
if(inpr > 0) {
|
2011-12-02 13:00:48 +00:00
|
|
|
o.write(_("(INPR):download in-progress."));
|
2008-10-21 16:26:43 +00:00
|
|
|
}
|
2011-05-29 11:59:45 +00:00
|
|
|
if(rm > 0) {
|
2011-12-02 13:00:48 +00:00
|
|
|
o.write(_("(RM):download removed."));
|
2011-05-29 11:59:45 +00:00
|
|
|
}
|
2011-08-09 14:33:55 +00:00
|
|
|
o.write("\n");
|
2007-10-11 16:58:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-22 14:05:06 +00:00
|
|
|
namespace {
|
|
|
|
void formatDownloadResultCommon
|
|
|
|
(std::ostream& o,
|
2012-09-24 14:20:43 +00:00
|
|
|
const char* status,
|
2013-06-21 16:10:38 +00:00
|
|
|
const std::shared_ptr<DownloadResult>& downloadResult)
|
2007-10-11 16:58:24 +00:00
|
|
|
{
|
2012-12-16 07:00:18 +00:00
|
|
|
o << std::setw(3) << downloadResult->gid->toAbbrevHex() << "|"
|
2008-11-10 16:10:31 +00:00
|
|
|
<< std::setw(4) << status << "|"
|
|
|
|
<< std::setw(11);
|
|
|
|
if(downloadResult->sessionTime > 0) {
|
2009-10-22 15:09:00 +00:00
|
|
|
o << util::abbrevSize
|
2008-11-10 16:10:31 +00:00
|
|
|
(downloadResult->sessionDownloadLength*1000/downloadResult->sessionTime)+
|
|
|
|
"B/s";
|
|
|
|
} else {
|
|
|
|
o << "n/a";
|
|
|
|
}
|
|
|
|
o << "|";
|
2011-08-22 14:05:06 +00:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
void RequestGroupMan::formatDownloadResultFull
|
|
|
|
(OutputFile& out,
|
2012-09-24 14:20:43 +00:00
|
|
|
const char* status,
|
2013-06-21 16:10:38 +00:00
|
|
|
const std::shared_ptr<DownloadResult>& downloadResult) const
|
2011-08-22 14:05:06 +00:00
|
|
|
{
|
|
|
|
BitfieldMan bt(downloadResult->pieceLength, downloadResult->totalLength);
|
|
|
|
bt.setBitfield(reinterpret_cast<const unsigned char*>
|
2011-10-13 12:57:33 +00:00
|
|
|
(downloadResult->bitfield.data()),
|
|
|
|
downloadResult->bitfield.size());
|
2011-08-22 14:05:06 +00:00
|
|
|
bool head = true;
|
2013-06-21 16:10:38 +00:00
|
|
|
const std::vector<std::shared_ptr<FileEntry> >& fileEntries =
|
2011-08-22 14:05:06 +00:00
|
|
|
downloadResult->fileEntries;
|
2013-08-21 03:06:53 +00:00
|
|
|
for(auto & f: fileEntries) {
|
|
|
|
if(!f->isRequested()) {
|
2011-08-22 14:05:06 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
std::stringstream o;
|
|
|
|
if(head) {
|
|
|
|
formatDownloadResultCommon(o, status, downloadResult);
|
|
|
|
head = false;
|
|
|
|
} else {
|
|
|
|
o << " | | |";
|
|
|
|
}
|
2013-08-21 03:06:53 +00:00
|
|
|
if(f->getLength() == 0 || downloadResult->bitfield.empty()) {
|
2011-08-22 14:05:06 +00:00
|
|
|
o << " -|";
|
|
|
|
} else {
|
2012-06-25 14:35:24 +00:00
|
|
|
int64_t completedLength =
|
2013-08-21 03:06:53 +00:00
|
|
|
bt.getOffsetCompletedLength(f->getOffset(), f->getLength());
|
|
|
|
o << std::setw(3) << 100*completedLength/f->getLength() << "|";
|
2011-08-22 14:05:06 +00:00
|
|
|
}
|
2013-08-21 03:06:53 +00:00
|
|
|
writeFilePath(o, f, downloadResult->inMemoryDownload);
|
2011-08-22 14:05:06 +00:00
|
|
|
o << "\n";
|
|
|
|
out.write(o.str().c_str());
|
|
|
|
}
|
|
|
|
if(head) {
|
|
|
|
std::stringstream o;
|
|
|
|
formatDownloadResultCommon(o, status, downloadResult);
|
|
|
|
o << " -|n/a\n";
|
|
|
|
out.write(o.str().c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string RequestGroupMan::formatDownloadResult
|
2012-09-24 14:20:43 +00:00
|
|
|
(const char* status,
|
2013-06-21 16:10:38 +00:00
|
|
|
const std::shared_ptr<DownloadResult>& downloadResult) const
|
2011-08-22 14:05:06 +00:00
|
|
|
{
|
|
|
|
std::stringstream o;
|
|
|
|
formatDownloadResultCommon(o, status, downloadResult);
|
2013-06-21 16:10:38 +00:00
|
|
|
const std::vector<std::shared_ptr<FileEntry> >& fileEntries =
|
2009-05-12 15:16:11 +00:00
|
|
|
downloadResult->fileEntries;
|
2009-06-29 08:42:58 +00:00
|
|
|
writeFilePath(fileEntries.begin(), fileEntries.end(), o,
|
2010-01-05 16:01:46 +00:00
|
|
|
downloadResult->inMemoryDownload);
|
2007-10-11 16:58:24 +00:00
|
|
|
return o.str();
|
2007-05-20 13:57:56 +00:00
|
|
|
}
|
2007-06-03 14:24:37 +00:00
|
|
|
|
2010-10-30 16:02:15 +00:00
|
|
|
namespace {
|
2009-03-10 12:58:38 +00:00
|
|
|
template<typename StringInputIterator, typename FileEntryInputIterator>
|
2010-10-30 16:02:15 +00:00
|
|
|
bool sameFilePathExists(StringInputIterator sfirst,
|
|
|
|
StringInputIterator slast,
|
|
|
|
FileEntryInputIterator ffirst,
|
|
|
|
FileEntryInputIterator flast)
|
2009-03-10 12:58:38 +00:00
|
|
|
{
|
|
|
|
for(; ffirst != flast; ++ffirst) {
|
|
|
|
if(std::binary_search(sfirst, slast, (*ffirst)->getPath())) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2010-10-30 16:02:15 +00:00
|
|
|
} // namespace
|
2009-03-10 12:58:38 +00:00
|
|
|
|
2007-06-03 14:24:37 +00:00
|
|
|
bool RequestGroupMan::isSameFileBeingDownloaded(RequestGroup* requestGroup) const
|
|
|
|
{
|
2009-03-10 12:58:38 +00:00
|
|
|
// TODO it may be good to use dedicated method rather than use
|
|
|
|
// isPreLocalFileCheckEnabled
|
2007-11-25 11:48:44 +00:00
|
|
|
if(!requestGroup->isPreLocalFileCheckEnabled()) {
|
|
|
|
return false;
|
|
|
|
}
|
2010-02-28 12:30:11 +00:00
|
|
|
std::vector<std::string> files;
|
2013-08-21 03:06:53 +00:00
|
|
|
for(auto & rg : requestGroups_) {
|
2012-12-19 14:03:48 +00:00
|
|
|
if(rg.get() != requestGroup) {
|
2013-06-21 16:10:38 +00:00
|
|
|
const std::vector<std::shared_ptr<FileEntry> >& entries =
|
2012-12-19 14:03:48 +00:00
|
|
|
rg->getDownloadContext()->getFileEntries();
|
2009-03-10 12:58:38 +00:00
|
|
|
std::transform(entries.begin(), entries.end(),
|
2010-01-05 16:01:46 +00:00
|
|
|
std::back_inserter(files),
|
2013-06-22 08:53:39 +00:00
|
|
|
std::mem_fn(&FileEntry::getPath));
|
2007-06-03 14:24:37 +00:00
|
|
|
}
|
|
|
|
}
|
2009-03-10 12:58:38 +00:00
|
|
|
std::sort(files.begin(), files.end());
|
2013-06-21 16:10:38 +00:00
|
|
|
const std::vector<std::shared_ptr<FileEntry> >& entries =
|
2009-03-10 12:58:38 +00:00
|
|
|
requestGroup->getDownloadContext()->getFileEntries();
|
|
|
|
return sameFilePathExists(files.begin(), files.end(),
|
2010-01-05 16:01:46 +00:00
|
|
|
entries.begin(), entries.end());
|
2007-06-03 14:24:37 +00:00
|
|
|
}
|
2007-10-11 16:58:24 +00:00
|
|
|
|
|
|
|
void RequestGroupMan::halt()
|
|
|
|
{
|
2013-08-21 03:06:53 +00:00
|
|
|
for(auto & elem : requestGroups_) {
|
|
|
|
elem->setHaltRequested(true);
|
2007-10-11 16:58:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-07 13:33:59 +00:00
|
|
|
void RequestGroupMan::forceHalt()
|
|
|
|
{
|
2013-08-21 03:06:53 +00:00
|
|
|
for(auto & elem : requestGroups_) {
|
|
|
|
elem->setForceHaltRequested(true);
|
2007-12-07 13:33:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-11 16:58:24 +00:00
|
|
|
TransferStat RequestGroupMan::calculateStat()
|
|
|
|
{
|
2012-10-25 14:33:45 +00:00
|
|
|
// TODO Currently, all time upload length is not set.
|
|
|
|
return netStat_.toTransferStat();
|
2007-10-11 16:58:24 +00:00
|
|
|
}
|
2008-02-08 15:53:45 +00:00
|
|
|
|
2013-06-21 16:10:38 +00:00
|
|
|
std::shared_ptr<DownloadResult>
|
2011-03-17 03:17:46 +00:00
|
|
|
RequestGroupMan::findDownloadResult(a2_gid_t gid) const
|
2009-05-09 12:41:37 +00:00
|
|
|
{
|
2012-12-19 14:03:48 +00:00
|
|
|
return downloadResults_.get(gid);
|
2009-05-09 12:41:37 +00:00
|
|
|
}
|
|
|
|
|
2011-03-17 03:17:46 +00:00
|
|
|
bool RequestGroupMan::removeDownloadResult(a2_gid_t gid)
|
2011-01-19 15:56:34 +00:00
|
|
|
{
|
2013-02-03 09:08:39 +00:00
|
|
|
return downloadResults_.remove(gid);
|
2011-01-19 15:56:34 +00:00
|
|
|
}
|
|
|
|
|
2013-06-21 16:10:38 +00:00
|
|
|
void RequestGroupMan::addDownloadResult(const std::shared_ptr<DownloadResult>& dr)
|
2010-09-10 14:13:50 +00:00
|
|
|
{
|
2012-12-19 14:03:48 +00:00
|
|
|
bool rv = downloadResults_.push_back(dr->gid->getNumericId(), dr);
|
|
|
|
assert(rv);
|
|
|
|
while(downloadResults_.size() > maxDownloadResult_){
|
2013-03-01 14:13:24 +00:00
|
|
|
DownloadResultList::iterator i = downloadResults_.begin();
|
2013-02-25 12:42:54 +00:00
|
|
|
// Save last encountered error code so that we can report it
|
|
|
|
// later.
|
2013-06-21 16:10:38 +00:00
|
|
|
const std::shared_ptr<DownloadResult>& dr = *i;
|
2011-01-16 13:20:29 +00:00
|
|
|
if(dr->belongsTo == 0 && dr->result != error_code::FINISHED) {
|
2010-09-10 15:03:40 +00:00
|
|
|
removedLastErrorResult_ = dr->result;
|
|
|
|
++removedErrorResult_;
|
|
|
|
}
|
2013-02-25 12:42:54 +00:00
|
|
|
downloadResults_.pop_front();
|
2010-09-10 14:13:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-14 13:48:26 +00:00
|
|
|
void RequestGroupMan::purgeDownloadResult()
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
downloadResults_.clear();
|
2009-05-14 13:48:26 +00:00
|
|
|
}
|
|
|
|
|
2013-06-21 16:10:38 +00:00
|
|
|
std::shared_ptr<ServerStat>
|
2008-08-04 17:06:47 +00:00
|
|
|
RequestGroupMan::findServerStat(const std::string& hostname,
|
2010-01-05 16:01:46 +00:00
|
|
|
const std::string& protocol) const
|
2008-08-04 17:06:47 +00:00
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
return serverStatMan_->find(hostname, protocol);
|
2008-08-04 17:06:47 +00:00
|
|
|
}
|
|
|
|
|
2013-06-21 16:10:38 +00:00
|
|
|
std::shared_ptr<ServerStat>
|
2008-09-14 14:25:16 +00:00
|
|
|
RequestGroupMan::getOrCreateServerStat(const std::string& hostname,
|
2010-01-05 16:01:46 +00:00
|
|
|
const std::string& protocol)
|
2008-09-14 14:25:16 +00:00
|
|
|
{
|
2013-06-21 16:10:38 +00:00
|
|
|
std::shared_ptr<ServerStat> ss = findServerStat(hostname, protocol);
|
2010-11-12 12:48:48 +00:00
|
|
|
if(!ss) {
|
2008-09-14 14:25:16 +00:00
|
|
|
ss.reset(new ServerStat(hostname, protocol));
|
|
|
|
addServerStat(ss);
|
|
|
|
}
|
|
|
|
return ss;
|
|
|
|
}
|
|
|
|
|
2013-06-21 16:10:38 +00:00
|
|
|
bool RequestGroupMan::addServerStat(const std::shared_ptr<ServerStat>& serverStat)
|
2008-08-04 17:06:47 +00:00
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
return serverStatMan_->add(serverStat);
|
2008-08-04 17:06:47 +00:00
|
|
|
}
|
|
|
|
|
2008-08-10 15:22:55 +00:00
|
|
|
bool RequestGroupMan::loadServerStat(const std::string& filename)
|
|
|
|
{
|
2011-08-06 15:10:53 +00:00
|
|
|
return serverStatMan_->load(filename);
|
2008-08-10 15:22:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool RequestGroupMan::saveServerStat(const std::string& filename) const
|
|
|
|
{
|
2011-08-06 15:10:53 +00:00
|
|
|
return serverStatMan_->save(filename);
|
2008-08-10 15:22:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RequestGroupMan::removeStaleServerStat(time_t timeout)
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
serverStatMan_->removeStaleServerStat(timeout);
|
2008-08-10 15:22:55 +00:00
|
|
|
}
|
|
|
|
|
2009-02-28 11:48:26 +00:00
|
|
|
bool RequestGroupMan::doesOverallDownloadSpeedExceed()
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
return maxOverallDownloadSpeedLimit_ > 0 &&
|
2012-10-25 14:33:45 +00:00
|
|
|
maxOverallDownloadSpeedLimit_ < netStat_.calculateDownloadSpeed();
|
2009-02-28 11:48:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool RequestGroupMan::doesOverallUploadSpeedExceed()
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
return maxOverallUploadSpeedLimit_ > 0 &&
|
2012-10-25 14:33:45 +00:00
|
|
|
maxOverallUploadSpeedLimit_ < netStat_.calculateUploadSpeed();
|
2009-02-28 11:48:26 +00:00
|
|
|
}
|
|
|
|
|
2010-07-15 13:49:02 +00:00
|
|
|
void RequestGroupMan::getUsedHosts
|
|
|
|
(std::vector<std::pair<size_t, std::string> >& usedHosts)
|
2010-07-14 14:10:33 +00:00
|
|
|
{
|
2013-06-22 12:08:50 +00:00
|
|
|
// vector of tuple which consists of use count, -download speed,
|
2010-09-26 05:47:12 +00:00
|
|
|
// hostname. We want to sort by least used and faster download
|
|
|
|
// speed. We use -download speed so that we can sort them using
|
|
|
|
// operator<().
|
2013-06-22 12:08:50 +00:00
|
|
|
std::vector<std::tuple<size_t, int, std::string> > tempHosts;
|
|
|
|
for(const auto& rg : requestGroups_) {
|
|
|
|
const auto& inFlightReqs =
|
2012-12-19 14:03:48 +00:00
|
|
|
rg->getDownloadContext()->getFirstFileEntry()->getInFlightRequests();
|
2013-06-22 12:08:50 +00:00
|
|
|
for(const auto& req : inFlightReqs) {
|
2012-10-14 09:05:34 +00:00
|
|
|
uri_split_result us;
|
2013-06-22 12:08:50 +00:00
|
|
|
if(uri_split(&us, req->getUri().c_str()) == 0) {
|
2012-10-14 09:05:34 +00:00
|
|
|
std::string host = uri::getFieldString(us, USR_HOST,
|
2013-06-22 12:08:50 +00:00
|
|
|
req->getUri().c_str());
|
|
|
|
auto k = tempHosts.begin();
|
|
|
|
auto eok = tempHosts.end();
|
|
|
|
for(; k != eok; ++k) {
|
|
|
|
if(std::get<2>(*k) == host) {
|
|
|
|
++std::get<0>(*k);
|
2010-07-15 13:49:02 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(k == eok) {
|
2012-10-14 09:05:34 +00:00
|
|
|
std::string protocol = uri::getFieldString(us, USR_SCHEME,
|
2013-06-22 12:08:50 +00:00
|
|
|
req->getUri().c_str());
|
|
|
|
auto ss = findServerStat(host, protocol);
|
2010-11-12 12:48:48 +00:00
|
|
|
int invDlSpeed = (ss && ss->isOK()) ?
|
|
|
|
-(static_cast<int>(ss->getDownloadSpeed())) : 0;
|
2013-06-22 12:08:50 +00:00
|
|
|
tempHosts.emplace_back(1, invDlSpeed, host);
|
2010-07-15 13:49:02 +00:00
|
|
|
}
|
2010-07-14 14:10:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-09-26 05:47:12 +00:00
|
|
|
std::sort(tempHosts.begin(), tempHosts.end());
|
|
|
|
std::transform(tempHosts.begin(), tempHosts.end(),
|
2013-06-22 12:08:50 +00:00
|
|
|
std::back_inserter(usedHosts),
|
|
|
|
[](const std::tuple<size_t, int, std::string>& x) {
|
|
|
|
return std::make_pair(std::get<0>(x), std::get<2>(x));
|
|
|
|
});
|
2010-07-14 14:10:33 +00:00
|
|
|
}
|
|
|
|
|
2012-02-05 09:57:16 +00:00
|
|
|
void RequestGroupMan::setUriListParser
|
2013-06-21 16:10:38 +00:00
|
|
|
(const std::shared_ptr<UriListParser>& uriListParser)
|
2012-02-05 09:57:16 +00:00
|
|
|
{
|
|
|
|
uriListParser_ = uriListParser;
|
|
|
|
}
|
|
|
|
|
2012-11-27 13:04:59 +00:00
|
|
|
void RequestGroupMan::initWrDiskCache()
|
|
|
|
{
|
2013-08-20 16:57:17 +00:00
|
|
|
assert(wrDiskCache_ == nullptr);
|
2012-11-27 13:04:59 +00:00
|
|
|
size_t limit = option_->getAsInt(PREF_DISK_CACHE);
|
|
|
|
if(limit > 0) {
|
|
|
|
wrDiskCache_ = new WrDiskCache(limit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
} // namespace aria2
|