aria2/src/RequestGroupMan.cc

961 lines
28 KiB
C++
Raw Normal View History

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
* 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"
#include <unistd.h>
#include <cstring>
#include <iomanip>
#include <sstream>
#include <numeric>
#include <algorithm>
#include <utility>
#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"
#include "RequestGroup.h"
#include "LogFactory.h"
#include "Logger.h"
2007-05-20 13:57:56 +00:00
#include "DownloadEngine.h"
#include "message.h"
#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"
#include "DownloadContext.h"
#include "ServerStatMan.h"
#include "ServerStat.h"
#include "SegmentMan.h"
#include "FeedbackURISelector.h"
2011-06-11 12:49:09 +00:00
#include "InorderURISelector.h"
#include "AdaptiveURISelector.h"
#include "Option.h"
#include "prefs.h"
#include "File.h"
#include "util.h"
#include "Command.h"
#include "FileEntry.h"
#include "fmt.h"
#include "FileAllocationEntry.h"
#include "CheckIntegrityEntry.h"
#include "Segment.h"
2010-06-18 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net> Introduced ValueBase class, which is a replacement of BDE. In this change ValueBase is used instead of BDE except DHT messages, UTMetadata messages and XML-RPC. They'll be replaced in the later commits. DownloadContext::_attrs is now ContextAttribute rather than BDE. * src/ActivePeerConnectionCommand.cc * src/AnnounceList.cc * src/AnnounceList.h * src/BtDependency.cc * src/BtRegistry.cc * src/BtSetup.cc * src/ConsoleStatCalc.cc * src/ContextAttribute.h * src/DefaultBtAnnounce.cc * src/DefaultBtInteractive.cc * src/DownloadContext.cc * src/DownloadContext.h * src/HandshakeExtensionMessage.cc * src/InitiateConnectionCommand.cc * src/LpdReceiveMessageCommand.cc * src/MSEHandshake.cc * src/Makefile.am * src/Makefile.in * src/PeerInteractionCommand.cc * src/PeerListProcessor.h * src/ProtocolDetector.cc * src/RequestGroup.cc * src/RequestGroupMan.cc * src/TorrentAttribute.h * src/TrackerWatcherCommand.cc * src/UTMetadataDataExtensionMessage.cc * src/UTMetadataPostDownloadHandler.cc * src/UTMetadataRequestExtensionMessage.cc * src/ValueBase.cc * src/ValueBase.h * src/XmlRpcMethodImpl.cc * src/XmlRpcMethodImpl.h * src/bencode2.cc * src/bencode2.h * src/bittorrent_helper.cc * src/bittorrent_helper.h * src/download_helper.cc * src/magnet.cc * src/magnet.h * test/AnnounceListTest.cc * test/Bencode2Test.cc * test/BencodeTest.cc * test/BittorrentHelperTest.cc * test/BtDependencyTest.cc * test/BtRegistryTest.cc * test/DefaultBtAnnounceTest.cc * test/DefaultBtProgressInfoFileTest.cc * test/HandshakeExtensionMessageTest.cc * test/MSEHandshakeTest.cc * test/MagnetTest.cc * test/Makefile.am * test/Makefile.in * test/RequestGroupManTest.cc * test/UTMetadataDataExtensionMessageTest.cc * test/UTMetadataPostDownloadHandlerTest.cc * test/UTMetadataRequestExtensionMessageTest.cc * test/ValueBaseTest.cc * test/XmlRpcMethodTest.cc
2010-06-18 14:47:09 +00:00
#include "DlAbortEx.h"
#include "uri.h"
#include "Signature.h"
#include "OutputFile.h"
#include "download_helper.h"
#include "UriListParser.h"
#include "SingletonHolder.h"
#include "Notifier.h"
#include "PeerStat.h"
#include "WrDiskCache.h"
#ifdef ENABLE_BITTORRENT
# include "bittorrent_helper.h"
#endif // ENABLE_BITTORRENT
namespace aria2 {
namespace {
template<typename InputIterator>
void appendReservedGroup(RequestGroupList& list,
InputIterator first, InputIterator last)
{
for(; first != last; ++first) {
list.push_back((*first)->getGID(), *first);
}
}
} // namespace
RequestGroupMan::RequestGroupMan
(std::vector<std::shared_ptr<RequestGroup> > requestGroups,
int maxSimultaneousDownloads,
const Option* option)
: maxSimultaneousDownloads_(maxSimultaneousDownloads),
option_(option),
serverStatMan_(new ServerStatMan()),
maxOverallDownloadSpeedLimit_
(option->getAsInt(PREF_MAX_OVERALL_DOWNLOAD_LIMIT)),
maxOverallUploadSpeedLimit_(option->getAsInt
(PREF_MAX_OVERALL_UPLOAD_LIMIT)),
keepRunning_(option->getAsBool(PREF_ENABLE_RPC)),
queueCheck_(true),
removedErrorResult_(0),
removedLastErrorResult_(error_code::FINISHED),
maxDownloadResult_(option->getAsInt(PREF_MAX_DOWNLOAD_RESULT)),
wrDiskCache_(nullptr)
{
appendReservedGroup(reservedGroups_,
requestGroups.begin(), requestGroups.end());
}
RequestGroupMan::~RequestGroupMan()
{
delete wrDiskCache_;
}
bool RequestGroupMan::downloadFinished()
{
if(keepRunning_) {
return false;
}
return requestGroups_.empty() && reservedGroups_.empty();
}
void RequestGroupMan::addRequestGroup
(const std::shared_ptr<RequestGroup>& group)
{
requestGroups_.push_back(group->getGID(), group);
}
void RequestGroupMan::addReservedGroup
(const std::vector<std::shared_ptr<RequestGroup> >& groups)
{
requestQueueCheck();
appendReservedGroup(reservedGroups_, groups.begin(), groups.end());
}
void RequestGroupMan::addReservedGroup
(const std::shared_ptr<RequestGroup>& group)
{
requestQueueCheck();
reservedGroups_.push_back(group->getGID(), group);
}
namespace {
struct RequestGroupKeyFunc {
a2_gid_t operator()(const std::shared_ptr<RequestGroup>& rg) const
{
return rg->getGID();
}
};
} // namespace
void RequestGroupMan::insertReservedGroup
(size_t pos, const std::vector<std::shared_ptr<RequestGroup> >& groups)
{
requestQueueCheck();
pos = std::min(reservedGroups_.size(), pos);
reservedGroups_.insert(pos, RequestGroupKeyFunc(),
groups.begin(), groups.end());
}
void RequestGroupMan::insertReservedGroup
(size_t pos, const std::shared_ptr<RequestGroup>& group)
{
requestQueueCheck();
pos = std::min(reservedGroups_.size(), pos);
reservedGroups_.insert(pos, group->getGID(), group);
}
size_t RequestGroupMan::countRequestGroup() const
{
return requestGroups_.size();
}
std::shared_ptr<RequestGroup> RequestGroupMan::findGroup(a2_gid_t gid) const
{
std::shared_ptr<RequestGroup> rg = requestGroups_.get(gid);
if(!rg) {
rg = reservedGroups_.get(gid);
}
return rg;
}
size_t RequestGroupMan::changeReservedGroupPosition
2013-05-14 15:50:55 +00:00
(a2_gid_t gid, int pos, OffsetMode how)
{
ssize_t dest = reservedGroups_.move(gid, pos, how);
if(dest == -1) {
throw DL_ABORT_EX(fmt("GID#%s not found in the waiting queue.",
GroupId::toHex(gid).c_str()));
} else {
return dest;
}
}
bool RequestGroupMan::removeReservedGroup(a2_gid_t gid)
{
return reservedGroups_.remove(gid);
}
2010-10-30 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net> Use unnamed namespace instead of static keyword. * src/AbstractCommand.cc * src/AdaptiveURISelector.cc * src/Base64.cc * src/BitfieldMan.cc * src/BtDependency.cc * src/ConsoleStatCalc.cc * src/ContentTypeRequestGroupCriteria.cc * src/CookieStorage.cc * src/DHTMessageFactoryImpl.cc * src/DHTRoutingTableDeserializer.cc * src/DefaultBtAnnounce.cc * src/DefaultBtProgressInfoFile.cc * src/DefaultPeerStorage.cc * src/DefaultPieceStorage.cc * src/DownloadCommand.cc * src/DownloadEngine.cc * src/EpollEventPoll.cc * src/ExpatMetalinkProcessor.cc * src/ExpatXmlRpcRequestProcessor.cc * src/FileEntry.cc * src/HttpRequest.cc * src/HttpRequestCommand.cc * src/HttpResponseCommand.cc * src/KqueueEventPoll.cc * src/LongestSequencePieceSelector.cc * src/MetalinkParserStateV3Impl.cc * src/MetalinkParserStateV4Impl.cc * src/MultiDiskAdaptor.cc * src/MultiUrlRequestInfo.cc * src/OptionParser.cc * src/PeerSessionResource.cc * src/PortEventPoll.cc * src/Request.cc * src/RequestGroupMan.cc * src/SelectEventPoll.cc * src/SessionSerializer.cc * src/SimpleLogFormatter.cc * src/Sqlite3CookieParser.cc * src/TrackerWatcherCommand.cc * src/XML2SAXMetalinkProcessor.cc * src/Xml2XmlRpcRequestProcessor.cc * src/XmlRpcMethod.cc * src/XmlRpcMethodImpl.cc * src/XmlRpcResponse.cc * src/base32.cc * src/bencode2.cc * src/bittorrent_helper.cc * src/download_helper.cc * src/main.cc * src/messageDigest.cc * src/option_processing.cc * src/util.cc * test/AnnounceListTest.cc * test/BtRegistryTest.cc * test/DHTBucketTest.cc * test/DHTRoutingTableTest.cc * test/DefaultBtAnnounceTest.cc * test/FileEntryTest.cc * test/FtpConnectionTest.cc * test/MSEHandshakeTest.cc * test/MagnetTest.cc * test/XmlRpcMethodTest.cc * test/array_funTest.cc
2010-10-30 16:02:15 +00:00
namespace {
void notifyDownloadEvent
(DownloadEvent event, const std::shared_ptr<RequestGroup>& group)
{
// Check NULL to make unit test easier.
if(SingletonHolder<Notifier>::instance()) {
SingletonHolder<Notifier>::instance()->notifyDownloadEvent(event, group);
}
}
} // namespace
namespace {
2010-10-30 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net> Use unnamed namespace instead of static keyword. * src/AbstractCommand.cc * src/AdaptiveURISelector.cc * src/Base64.cc * src/BitfieldMan.cc * src/BtDependency.cc * src/ConsoleStatCalc.cc * src/ContentTypeRequestGroupCriteria.cc * src/CookieStorage.cc * src/DHTMessageFactoryImpl.cc * src/DHTRoutingTableDeserializer.cc * src/DefaultBtAnnounce.cc * src/DefaultBtProgressInfoFile.cc * src/DefaultPeerStorage.cc * src/DefaultPieceStorage.cc * src/DownloadCommand.cc * src/DownloadEngine.cc * src/EpollEventPoll.cc * src/ExpatMetalinkProcessor.cc * src/ExpatXmlRpcRequestProcessor.cc * src/FileEntry.cc * src/HttpRequest.cc * src/HttpRequestCommand.cc * src/HttpResponseCommand.cc * src/KqueueEventPoll.cc * src/LongestSequencePieceSelector.cc * src/MetalinkParserStateV3Impl.cc * src/MetalinkParserStateV4Impl.cc * src/MultiDiskAdaptor.cc * src/MultiUrlRequestInfo.cc * src/OptionParser.cc * src/PeerSessionResource.cc * src/PortEventPoll.cc * src/Request.cc * src/RequestGroupMan.cc * src/SelectEventPoll.cc * src/SessionSerializer.cc * src/SimpleLogFormatter.cc * src/Sqlite3CookieParser.cc * src/TrackerWatcherCommand.cc * src/XML2SAXMetalinkProcessor.cc * src/Xml2XmlRpcRequestProcessor.cc * src/XmlRpcMethod.cc * src/XmlRpcMethodImpl.cc * src/XmlRpcResponse.cc * src/base32.cc * src/bencode2.cc * src/bittorrent_helper.cc * src/download_helper.cc * src/main.cc * src/messageDigest.cc * src/option_processing.cc * src/util.cc * test/AnnounceListTest.cc * test/BtRegistryTest.cc * test/DHTBucketTest.cc * test/DHTRoutingTableTest.cc * test/DefaultBtAnnounceTest.cc * test/FileEntryTest.cc * test/FtpConnectionTest.cc * test/MSEHandshakeTest.cc * test/MagnetTest.cc * test/XmlRpcMethodTest.cc * test/array_funTest.cc
2010-10-30 16:02:15 +00:00
void executeStopHook
(const std::shared_ptr<RequestGroup>& group,
const Option* option,
error_code::Value result)
{
if(result == error_code::FINISHED &&
!option->blank(PREF_ON_DOWNLOAD_COMPLETE)) {
util::executeHookByOptName(group, option, PREF_ON_DOWNLOAD_COMPLETE);
} else if(result != error_code::IN_PROGRESS &&
result != error_code::REMOVED &&
!option->blank(PREF_ON_DOWNLOAD_ERROR)) {
util::executeHookByOptName(group, option, PREF_ON_DOWNLOAD_ERROR);
} else if(!option->blank(PREF_ON_DOWNLOAD_STOP)) {
util::executeHookByOptName(group, option, PREF_ON_DOWNLOAD_STOP);
}
if(result == error_code::FINISHED) {
notifyDownloadEvent(EVENT_ON_DOWNLOAD_COMPLETE, group);
} else if(result != error_code::IN_PROGRESS &&
result != error_code::REMOVED) {
notifyDownloadEvent(EVENT_ON_DOWNLOAD_ERROR, group);
} else {
notifyDownloadEvent(EVENT_ON_DOWNLOAD_STOP, group);
}
}
2010-10-30 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net> Use unnamed namespace instead of static keyword. * src/AbstractCommand.cc * src/AdaptiveURISelector.cc * src/Base64.cc * src/BitfieldMan.cc * src/BtDependency.cc * src/ConsoleStatCalc.cc * src/ContentTypeRequestGroupCriteria.cc * src/CookieStorage.cc * src/DHTMessageFactoryImpl.cc * src/DHTRoutingTableDeserializer.cc * src/DefaultBtAnnounce.cc * src/DefaultBtProgressInfoFile.cc * src/DefaultPeerStorage.cc * src/DefaultPieceStorage.cc * src/DownloadCommand.cc * src/DownloadEngine.cc * src/EpollEventPoll.cc * src/ExpatMetalinkProcessor.cc * src/ExpatXmlRpcRequestProcessor.cc * src/FileEntry.cc * src/HttpRequest.cc * src/HttpRequestCommand.cc * src/HttpResponseCommand.cc * src/KqueueEventPoll.cc * src/LongestSequencePieceSelector.cc * src/MetalinkParserStateV3Impl.cc * src/MetalinkParserStateV4Impl.cc * src/MultiDiskAdaptor.cc * src/MultiUrlRequestInfo.cc * src/OptionParser.cc * src/PeerSessionResource.cc * src/PortEventPoll.cc * src/Request.cc * src/RequestGroupMan.cc * src/SelectEventPoll.cc * src/SessionSerializer.cc * src/SimpleLogFormatter.cc * src/Sqlite3CookieParser.cc * src/TrackerWatcherCommand.cc * src/XML2SAXMetalinkProcessor.cc * src/Xml2XmlRpcRequestProcessor.cc * src/XmlRpcMethod.cc * src/XmlRpcMethodImpl.cc * src/XmlRpcResponse.cc * src/base32.cc * src/bencode2.cc * src/bittorrent_helper.cc * src/download_helper.cc * src/main.cc * src/messageDigest.cc * src/option_processing.cc * src/util.cc * test/AnnounceListTest.cc * test/BtRegistryTest.cc * test/DHTBucketTest.cc * test/DHTRoutingTableTest.cc * test/DefaultBtAnnounceTest.cc * test/FileEntryTest.cc * test/FtpConnectionTest.cc * test/MSEHandshakeTest.cc * test/MagnetTest.cc * test/XmlRpcMethodTest.cc * test/array_funTest.cc
2010-10-30 16:02:15 +00:00
} // namespace
namespace {
class ProcessStoppedRequestGroup {
private:
DownloadEngine* e_;
RequestGroupList& reservedGroups_;
void saveSignature(const std::shared_ptr<RequestGroup>& group)
{
auto& sig = group->getDownloadContext()->getSignature();
if(sig && !sig->getBody().empty()) {
// filename of signature file is the path to download file followed by
// ".sig".
2009-06-28 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net> Rewritten DownloadContext and removed SingleFileDownloadContext and BtContext and its derived classes. BitTorrent specific attributes are now set by DownloadContext::setAttribute() with key bittorrent::BITTORRENT. * src/AbstractBtMessage.cc * src/AbstractBtMessage.h * src/AbstractCommand.cc * src/AbstractCommand.h * src/AbstractProxyRequestCommand.cc * src/AbstractProxyResponseCommand.cc * src/ActivePeerConnectionCommand.cc * src/ActivePeerConnectionCommand.h * src/AnnounceList.cc * src/BtCheckIntegrityEntry.cc * src/BtConstants.h * src/BtContext.h: Removed * src/BtDependency.cc * src/BtExtendedMessage.cc * src/BtPieceMessage.cc * src/BtPieceMessage.h * src/BtPostDownloadHandler.cc * src/BtRegistry.cc * src/BtRegistry.h * src/BtSetup.cc * src/CheckIntegrityCommand.cc * src/CheckIntegrityEntry.cc * src/ChecksumCheckIntegrityEntry.cc * src/ConsoleStatCalc.cc * src/ContentTypeRequestGroupCriteria.cc * src/DHTGetPeersCommand.cc * src/DHTGetPeersCommand.h * src/DHTPeerLookupTask.cc * src/DHTPeerLookupTask.h * src/DHTTaskFactory.h * src/DHTTaskFactoryImpl.cc * src/DHTTaskFactoryImpl.h * src/DefaultBtAnnounce.cc * src/DefaultBtAnnounce.h * src/DefaultBtContext.cc: Removed * src/DefaultBtContext.h: Removed * src/DefaultBtInteractive.cc * src/DefaultBtInteractive.h * src/DefaultBtMessageDispatcher.cc * src/DefaultBtMessageDispatcher.h * src/DefaultBtMessageFactory.cc * src/DefaultBtMessageFactory.h * src/DefaultBtMessageReceiver.cc * src/DefaultBtMessageReceiver.h * src/DefaultBtProgressInfoFile.cc * src/DefaultBtRequestFactory.cc * src/DefaultBtRequestFactory.h * src/DefaultExtensionMessageFactory.cc * src/DefaultExtensionMessageFactory.h * src/DefaultPeerStorage.cc * src/DefaultPeerStorage.h * src/DefaultPieceStorage.cc * src/DefaultSegmentManFactory.cc * src/DiskAdaptor.cc * src/DiskAdaptor.h * src/DownloadContext.cc * src/DownloadContext.h * src/DownloadEngine.cc * src/DownloadEngineFactory.cc * src/DownloadHandler.cc * src/DownloadResult.h * src/FileAllocationCommand.cc * src/FileAllocationDispatcherCommand.cc * src/FileAllocationEntry.cc * src/FileEntry.h * src/FillRequestGroupCommand.cc * src/FtpDownloadCommand.cc * src/FtpFinishDownloadCommand.cc * src/FtpInitiateConnectionCommand.cc * src/FtpNegotiationCommand.cc * src/FtpTunnelRequestCommand.cc * src/FtpTunnelResponseCommand.cc * src/HandshakeExtensionMessage.cc * src/HandshakeExtensionMessage.h * src/HaveEraseCommand.cc * src/HttpDownloadCommand.cc * src/HttpInitiateConnectionCommand.cc * src/HttpProxyRequestCommand.cc * src/HttpProxyResponseCommand.cc * src/HttpRequest.cc * src/HttpRequest.h * src/HttpRequestCommand.cc * src/HttpResponseCommand.cc * src/HttpServerBodyCommand.cc * src/HttpServerCommand.cc * src/HttpSkipResponseCommand.cc * src/InitiateConnectionCommandFactory.cc * src/InitiatorMSEHandshakeCommand.cc * src/InitiatorMSEHandshakeCommand.h * src/IteratableChecksumValidator.cc * src/IteratableChecksumValidator.h * src/IteratableChunkChecksumValidator.cc * src/MSEHandshake.cc * src/MSEHandshake.h * src/Makefile.am * src/Makefile.in * src/Metalink2RequestGroup.cc * src/MetalinkPostDownloadHandler.cc * src/MultiDiskAdaptor.cc * src/MultiDiskAdaptor.h * src/MultiFileAllocationIterator.cc * src/MultiUrlRequestInfo.cc * src/PeerChokeCommand.cc * src/PeerChokeCommand.h * src/PeerInitiateConnectionCommand.cc * src/PeerInitiateConnectionCommand.h * src/PeerInteractionCommand.cc * src/PeerInteractionCommand.h * src/PeerReceiveHandshakeCommand.cc * src/PieceHashCheckIntegrityEntry.cc * src/RealtimeCommand.cc * src/ReceiverMSEHandshakeCommand.cc * src/RequestGroup.cc * src/RequestGroup.h * src/RequestGroupEntry.cc * src/RequestGroupMan.cc * src/SeedCheckCommand.cc * src/SeedCheckCommand.h * src/SegmentMan.cc * src/ShareRatioSeedCriteria.h * src/SingleFileDownloadContext.cc: Removed * src/SingleFileDownloadContext.h: Removed * src/SleepCommand.cc * src/StreamCheckIntegrityEntry.cc * src/TrackerWatcherCommand.cc * src/TrackerWatcherCommand.h * src/UnknownLengthPieceStorage.cc * src/Util.cc * src/Util.h * src/XmlRpcMethodImpl.cc * src/bittorrent_helper.cc * src/bittorrent_helper.h * src/download_helper.cc * src/main.cc * src/option_processing.cc * test/BitfieldManTest.cc * test/BittorrentHelperTest.cc * test/BtCancelMessageTest.cc * test/BtChokeMessageTest.cc * test/BtDependencyTest.cc * test/BtPieceMessageTest.cc * test/BtPostDownloadHandlerTest.cc * test/BtRegistryTest.cc * test/BtRejectMessageTest.cc * test/BtRequestMessageTest.cc * test/DHTPeerAnnounceStorageTest.cc * test/DefaultBtAnnounceTest.cc * test/DefaultBtContextTest.cc: Removed * test/DefaultBtMessageDispatcherTest.cc * test/DefaultBtMessageFactoryTest.cc * test/DefaultBtProgressInfoFileTest.cc * test/DefaultBtRequestFactoryTest.cc * test/DefaultExtensionMessageFactoryTest.cc * test/DefaultPeerStorageTest.cc * test/DefaultPieceStorageTest.cc * test/DirectDiskAdaptorTest.cc * test/DownloadContextTest.cc * test/DownloadHandlerFactoryTest.cc * test/DownloadHelperTest.cc * test/HandshakeExtensionMessageTest.cc * test/HttpRequestTest.cc * test/HttpResponseTest.cc * test/IteratableChecksumValidatorTest.cc * test/IteratableChunkChecksumValidatorTest.cc * test/MSEHandshakeTest.cc * test/Makefile.am * test/Makefile.in * test/Metalink2RequestGroupTest.cc * test/MetalinkPostDownloadHandlerTest.cc * test/MockBtContext.h: Removed * test/MockDHTTaskFactory.h * test/MockDownloadContext.h: Removed * test/MockPieceStorage.h * test/MultiDiskAdaptorTest.cc * test/MultiFileAllocationIteratorTest.cc * test/RequestGroupManTest.cc * test/RequestGroupTest.cc * test/SegmentManTest.cc * test/ShareRatioSeedCriteriaTest.cc * test/SingleFileDownloadContextTest.cc: Removed * test/UtilTest.cc * test/XmlRpcMethodTest.cc
2009-06-28 10:37:15 +00:00
std::string signatureFile = group->getFirstFilePath()+".sig";
if(sig->save(signatureFile)) {
A2_LOG_NOTICE(fmt(MSG_SIGNATURE_SAVED, signatureFile.c_str()));
} else {
A2_LOG_NOTICE(fmt(MSG_SIGNATURE_NOT_SAVED, signatureFile.c_str()));
}
}
}
// Collect statistics during download in PeerStats and update/register
// ServerStatMan
void collectStat(const std::shared_ptr<RequestGroup>& group)
{
if(group->getSegmentMan()) {
bool singleConnection =
group->getSegmentMan()->getPeerStats().size() == 1;
const std::vector<std::shared_ptr<PeerStat> >& peerStats =
group->getSegmentMan()->getFastestPeerStats();
2013-08-21 03:06:53 +00:00
for(auto & stat : peerStats) {
if(stat->getHostname().empty() || stat->getProtocol().empty()) {
continue;
}
2013-08-21 03:06:53 +00:00
int speed = stat->getAvgDownloadSpeed();
if (speed == 0) continue;
std::shared_ptr<ServerStat> ss =
2013-08-21 03:06:53 +00:00
e_->getRequestGroupMan()->getOrCreateServerStat(stat->getHostname(),
stat->getProtocol());
ss->increaseCounter();
ss->updateDownloadSpeed(speed);
if(singleConnection) {
ss->updateSingleConnectionAvgSpeed(speed);
}
else {
ss->updateMultiConnectionAvgSpeed(speed);
}
}
}
}
public:
ProcessStoppedRequestGroup
2008-11-03 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com> Made BtRegistry non-static object. Now DownloadEngine has a reference to its object. * src/AbstractBtMessage.cc * src/ActivePeerConnectionCommand.cc * src/ActivePeerConnectionCommand.h * src/BtContextAwareCommand.cc * src/BtContextAwareCommand.h * src/BtExtendedMessage.cc * src/BtExtendedMessage.h * src/BtInterestedMessage.cc * src/BtInterestedMessage.h * src/BtNotInterestedMessage.cc * src/BtNotInterestedMessage.h * src/BtRegistry.cc * src/BtRegistry.h * src/BtSeederStateChoke.cc * src/BtSetup.cc * src/DHTGetPeersCommand.cc * src/DHTGetPeersCommand.h * src/DHTPeerAnnounceEntry.cc * src/DHTPeerAnnounceEntry.h * src/DHTPeerAnnounceStorage.cc * src/DHTPeerAnnounceStorage.h * src/DHTPeerLookupTask.cc * src/DHTPeerLookupTask.h * src/DHTSetup.cc * src/DHTSetup.h * src/DHTTaskFactory.h * src/DHTTaskFactoryImpl.cc * src/DHTTaskFactoryImpl.h * src/DefaultBtAnnounce.cc * src/DefaultBtInteractive.cc * src/DefaultBtInteractive.h * src/DefaultBtMessageDispatcher.cc * src/DefaultBtMessageDispatcher.h * src/DefaultBtMessageFactory.cc * src/DefaultBtMessageFactory.h * src/DefaultBtMessageReceiver.cc * src/DefaultBtProgressInfoFile.cc * src/DefaultBtProgressInfoFile.h * src/DefaultBtRequestFactory.cc * src/DefaultBtRequestFactory.h * src/DefaultExtensionMessageFactory.cc * src/DefaultExtensionMessageFactory.h * src/DefaultPeerStorage.cc * src/DefaultPeerStorage.h * src/DownloadEngine.cc * src/DownloadEngine.h * src/InitiatorMSEHandshakeCommand.cc * src/InitiatorMSEHandshakeCommand.h * src/MSEHandshake.cc * src/MSEHandshake.h * src/Makefile.am * src/Makefile.in * src/PeerChokeCommand.cc * src/PeerChokeCommand.h * src/PeerInitiateConnectionCommand.cc * src/PeerInitiateConnectionCommand.h * src/PeerInteractionCommand.cc * src/PeerInteractionCommand.h * src/PeerObject.h * src/PeerReceiveHandshakeCommand.cc * src/PeerReceiveHandshakeCommand.h * src/ReceiverMSEHandshakeCommand.cc * src/RequestGroup.cc * src/RequestGroup.h * src/RequestGroupMan.cc * src/RequestGroupMan.h * src/SeedCheckCommand.cc * src/SeedCheckCommand.h * src/ShareRatioSeedCriteria.h * src/SimpleBtMessage.cc * src/TrackerWatcherCommand.cc * src/TrackerWatcherCommand.h * src/UTPexExtensionMessage.cc * src/UTPexExtensionMessage.h * test/BtCancelMessageTest.cc * test/BtChokeMessageTest.cc * test/BtExtendedMessageTest.cc * test/BtInterestedMessageTest.cc * test/BtNotInterestedMessageTest.cc * test/BtPieceMessageTest.cc * test/BtPortMessageTest.cc * test/BtRegistryTest.cc * test/BtRejectMessageTest.cc * test/BtRequestMessageTest.cc * test/DHTPeerAnnounceEntryTest.cc * test/DHTPeerAnnounceStorageTest.cc * test/DefaultBtMessageDispatcherTest.cc * test/DefaultBtMessageFactoryTest.cc * test/DefaultBtProgressInfoFileTest.cc * test/DefaultBtRequestFactoryTest.cc * test/DefaultExtensionMessageFactoryTest.cc * test/MSEHandshakeTest.cc * test/MockDHTTaskFactory.h * test/ShareRatioSeedCriteriaTest.cc * test/UTPexExtensionMessageTest.cc
2008-11-03 06:49:02 +00:00
(DownloadEngine* e,
RequestGroupList& reservedGroups)
: e_(e),
reservedGroups_(reservedGroups)
{}
bool operator()(const RequestGroupList::value_type& group)
{
if(group->getNumCommand() == 0) {
collectStat(group);
const std::shared_ptr<DownloadContext>& dctx = group->getDownloadContext();
// 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 {
group->closeFile();
if(group->isPauseRequested()) {
A2_LOG_NOTICE
(fmt(_("Download GID#%s paused"),
GroupId::toHex(group->getGID()).c_str()));
group->saveControlFile();
} else if(group->downloadFinished() &&
!group->getDownloadContext()->isChecksumVerificationNeeded()) {
group->applyLastModifiedTimeToLocalFiles();
group->reportDownloadFinished();
if(group->allDownloadFinished() &&
!group->getOption()->getAsBool(PREF_FORCE_SAVE)) {
group->removeControlFile();
saveSignature(group);
} else {
group->saveControlFile();
}
std::vector<std::shared_ptr<RequestGroup> > nextGroups;
group->postDownloadProcessing(nextGroups);
if(!nextGroups.empty()) {
A2_LOG_DEBUG
(fmt("Adding %lu RequestGroups as a result of"
" PostDownloadHandler.",
static_cast<unsigned long>(nextGroups.size())));
e_->getRequestGroupMan()->insertReservedGroup(0, nextGroups);
}
#ifdef ENABLE_BITTORRENT
// 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() &&
dctx->hasAttribute(CTX_ATTR_BT)) {
A2_LOG_INFO(fmt(MSG_REMOVING_UNSELECTED_FILE,
GroupId::toHex(group->getGID()).c_str()));
const std::vector<std::shared_ptr<FileEntry> >& files =
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()));
} else {
A2_LOG_INFO(fmt(MSG_FILE_COULD_NOT_REMOVED,
2013-08-21 03:06:53 +00:00
file->getPath().c_str()));
}
}
}
}
#endif // ENABLE_BITTORRENT
} else {
A2_LOG_NOTICE
(fmt(_("Download GID#%s not complete: %s"),
GroupId::toHex(group->getGID()).c_str(),
group->getDownloadContext()->getBasePath().c_str()));
group->saveControlFile();
}
} catch(RecoverableException& ex) {
A2_LOG_ERROR_EX(EX_EXCEPTION_CAUGHT, ex);
2007-05-20 13:57:56 +00:00
}
if(group->isPauseRequested()) {
group->setState(RequestGroup::STATE_WAITING);
reservedGroups_.push_front(group->getGID(), group);
group->releaseRuntimeResource(e_);
group->setForceHaltRequested(false);
util::executeHookByOptName(group, e_->getOption(),
PREF_ON_DOWNLOAD_PAUSE);
notifyDownloadEvent(EVENT_ON_DOWNLOAD_PAUSE, group);
// TODO Should we have to prepend spend uris to remaining uris
// in case PREF_REUSE_URI is disabed?
} else {
std::shared_ptr<DownloadResult> dr = group->createDownloadResult();
e_->getRequestGroupMan()->addDownloadResult(dr);
executeStopHook(group, e_->getOption(), dr->result);
group->releaseRuntimeResource(e_);
}
return true;
} else {
return false;
2007-05-20 13:57:56 +00:00
}
}
};
} // namespace
2008-11-03 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com> Made BtRegistry non-static object. Now DownloadEngine has a reference to its object. * src/AbstractBtMessage.cc * src/ActivePeerConnectionCommand.cc * src/ActivePeerConnectionCommand.h * src/BtContextAwareCommand.cc * src/BtContextAwareCommand.h * src/BtExtendedMessage.cc * src/BtExtendedMessage.h * src/BtInterestedMessage.cc * src/BtInterestedMessage.h * src/BtNotInterestedMessage.cc * src/BtNotInterestedMessage.h * src/BtRegistry.cc * src/BtRegistry.h * src/BtSeederStateChoke.cc * src/BtSetup.cc * src/DHTGetPeersCommand.cc * src/DHTGetPeersCommand.h * src/DHTPeerAnnounceEntry.cc * src/DHTPeerAnnounceEntry.h * src/DHTPeerAnnounceStorage.cc * src/DHTPeerAnnounceStorage.h * src/DHTPeerLookupTask.cc * src/DHTPeerLookupTask.h * src/DHTSetup.cc * src/DHTSetup.h * src/DHTTaskFactory.h * src/DHTTaskFactoryImpl.cc * src/DHTTaskFactoryImpl.h * src/DefaultBtAnnounce.cc * src/DefaultBtInteractive.cc * src/DefaultBtInteractive.h * src/DefaultBtMessageDispatcher.cc * src/DefaultBtMessageDispatcher.h * src/DefaultBtMessageFactory.cc * src/DefaultBtMessageFactory.h * src/DefaultBtMessageReceiver.cc * src/DefaultBtProgressInfoFile.cc * src/DefaultBtProgressInfoFile.h * src/DefaultBtRequestFactory.cc * src/DefaultBtRequestFactory.h * src/DefaultExtensionMessageFactory.cc * src/DefaultExtensionMessageFactory.h * src/DefaultPeerStorage.cc * src/DefaultPeerStorage.h * src/DownloadEngine.cc * src/DownloadEngine.h * src/InitiatorMSEHandshakeCommand.cc * src/InitiatorMSEHandshakeCommand.h * src/MSEHandshake.cc * src/MSEHandshake.h * src/Makefile.am * src/Makefile.in * src/PeerChokeCommand.cc * src/PeerChokeCommand.h * src/PeerInitiateConnectionCommand.cc * src/PeerInitiateConnectionCommand.h * src/PeerInteractionCommand.cc * src/PeerInteractionCommand.h * src/PeerObject.h * src/PeerReceiveHandshakeCommand.cc * src/PeerReceiveHandshakeCommand.h * src/ReceiverMSEHandshakeCommand.cc * src/RequestGroup.cc * src/RequestGroup.h * src/RequestGroupMan.cc * src/RequestGroupMan.h * src/SeedCheckCommand.cc * src/SeedCheckCommand.h * src/ShareRatioSeedCriteria.h * src/SimpleBtMessage.cc * src/TrackerWatcherCommand.cc * src/TrackerWatcherCommand.h * src/UTPexExtensionMessage.cc * src/UTPexExtensionMessage.h * test/BtCancelMessageTest.cc * test/BtChokeMessageTest.cc * test/BtExtendedMessageTest.cc * test/BtInterestedMessageTest.cc * test/BtNotInterestedMessageTest.cc * test/BtPieceMessageTest.cc * test/BtPortMessageTest.cc * test/BtRegistryTest.cc * test/BtRejectMessageTest.cc * test/BtRequestMessageTest.cc * test/DHTPeerAnnounceEntryTest.cc * test/DHTPeerAnnounceStorageTest.cc * test/DefaultBtMessageDispatcherTest.cc * test/DefaultBtMessageFactoryTest.cc * test/DefaultBtProgressInfoFileTest.cc * test/DefaultBtRequestFactoryTest.cc * test/DefaultExtensionMessageFactoryTest.cc * test/MSEHandshakeTest.cc * test/MockDHTTaskFactory.h * test/ShareRatioSeedCriteriaTest.cc * test/UTPexExtensionMessageTest.cc
2008-11-03 06:49:02 +00:00
void RequestGroupMan::removeStoppedGroup(DownloadEngine* e)
{
size_t numPrev = requestGroups_.size();
requestGroups_.remove_if(ProcessStoppedRequestGroup(e, reservedGroups_));
size_t numRemoved = numPrev-requestGroups_.size();
if(numRemoved > 0) {
A2_LOG_DEBUG(fmt("%lu RequestGroup(s) deleted.",
static_cast<unsigned long>(numRemoved)));
2007-05-20 13:57:56 +00:00
}
}
void RequestGroupMan::configureRequestGroup
(const std::shared_ptr<RequestGroup>& requestGroup) const
{
const std::string& uriSelectorValue =
requestGroup->getOption()->get(PREF_URI_SELECTOR);
if(uriSelectorValue == V_FEEDBACK) {
requestGroup->setURISelector(make_unique<FeedbackURISelector>
(serverStatMan_));
} else if(uriSelectorValue == V_INORDER) {
requestGroup->setURISelector(make_unique<InorderURISelector>());
} else if(uriSelectorValue == V_ADAPTIVE) {
requestGroup->setURISelector(make_unique<AdaptiveURISelector>
(serverStatMan_, requestGroup.get()));
}
}
2010-10-30 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net> Use unnamed namespace instead of static keyword. * src/AbstractCommand.cc * src/AdaptiveURISelector.cc * src/Base64.cc * src/BitfieldMan.cc * src/BtDependency.cc * src/ConsoleStatCalc.cc * src/ContentTypeRequestGroupCriteria.cc * src/CookieStorage.cc * src/DHTMessageFactoryImpl.cc * src/DHTRoutingTableDeserializer.cc * src/DefaultBtAnnounce.cc * src/DefaultBtProgressInfoFile.cc * src/DefaultPeerStorage.cc * src/DefaultPieceStorage.cc * src/DownloadCommand.cc * src/DownloadEngine.cc * src/EpollEventPoll.cc * src/ExpatMetalinkProcessor.cc * src/ExpatXmlRpcRequestProcessor.cc * src/FileEntry.cc * src/HttpRequest.cc * src/HttpRequestCommand.cc * src/HttpResponseCommand.cc * src/KqueueEventPoll.cc * src/LongestSequencePieceSelector.cc * src/MetalinkParserStateV3Impl.cc * src/MetalinkParserStateV4Impl.cc * src/MultiDiskAdaptor.cc * src/MultiUrlRequestInfo.cc * src/OptionParser.cc * src/PeerSessionResource.cc * src/PortEventPoll.cc * src/Request.cc * src/RequestGroupMan.cc * src/SelectEventPoll.cc * src/SessionSerializer.cc * src/SimpleLogFormatter.cc * src/Sqlite3CookieParser.cc * src/TrackerWatcherCommand.cc * src/XML2SAXMetalinkProcessor.cc * src/Xml2XmlRpcRequestProcessor.cc * src/XmlRpcMethod.cc * src/XmlRpcMethodImpl.cc * src/XmlRpcResponse.cc * src/base32.cc * src/bencode2.cc * src/bittorrent_helper.cc * src/download_helper.cc * src/main.cc * src/messageDigest.cc * src/option_processing.cc * src/util.cc * test/AnnounceListTest.cc * test/BtRegistryTest.cc * test/DHTBucketTest.cc * test/DHTRoutingTableTest.cc * test/DefaultBtAnnounceTest.cc * test/FileEntryTest.cc * test/FtpConnectionTest.cc * test/MSEHandshakeTest.cc * test/MagnetTest.cc * test/XmlRpcMethodTest.cc * test/array_funTest.cc
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)
{
2013-06-23 12:55:52 +00:00
std::vector<std::unique_ptr<Command>> res;
requestGroup->createInitialCommand(res, e);
return res;
}
2010-10-30 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net> Use unnamed namespace instead of static keyword. * src/AbstractCommand.cc * src/AdaptiveURISelector.cc * src/Base64.cc * src/BitfieldMan.cc * src/BtDependency.cc * src/ConsoleStatCalc.cc * src/ContentTypeRequestGroupCriteria.cc * src/CookieStorage.cc * src/DHTMessageFactoryImpl.cc * src/DHTRoutingTableDeserializer.cc * src/DefaultBtAnnounce.cc * src/DefaultBtProgressInfoFile.cc * src/DefaultPeerStorage.cc * src/DefaultPieceStorage.cc * src/DownloadCommand.cc * src/DownloadEngine.cc * src/EpollEventPoll.cc * src/ExpatMetalinkProcessor.cc * src/ExpatXmlRpcRequestProcessor.cc * src/FileEntry.cc * src/HttpRequest.cc * src/HttpRequestCommand.cc * src/HttpResponseCommand.cc * src/KqueueEventPoll.cc * src/LongestSequencePieceSelector.cc * src/MetalinkParserStateV3Impl.cc * src/MetalinkParserStateV4Impl.cc * src/MultiDiskAdaptor.cc * src/MultiUrlRequestInfo.cc * src/OptionParser.cc * src/PeerSessionResource.cc * src/PortEventPoll.cc * src/Request.cc * src/RequestGroupMan.cc * src/SelectEventPoll.cc * src/SessionSerializer.cc * src/SimpleLogFormatter.cc * src/Sqlite3CookieParser.cc * src/TrackerWatcherCommand.cc * src/XML2SAXMetalinkProcessor.cc * src/Xml2XmlRpcRequestProcessor.cc * src/XmlRpcMethod.cc * src/XmlRpcMethodImpl.cc * src/XmlRpcResponse.cc * src/base32.cc * src/bencode2.cc * src/bittorrent_helper.cc * src/download_helper.cc * src/main.cc * src/messageDigest.cc * src/option_processing.cc * src/util.cc * test/AnnounceListTest.cc * test/BtRegistryTest.cc * test/DHTBucketTest.cc * test/DHTRoutingTableTest.cc * test/DefaultBtAnnounceTest.cc * test/FileEntryTest.cc * test/FtpConnectionTest.cc * test/MSEHandshakeTest.cc * test/MagnetTest.cc * test/XmlRpcMethodTest.cc * test/array_funTest.cc
2010-10-30 16:02:15 +00:00
} // namespace
2007-05-20 13:57:56 +00:00
void RequestGroupMan::fillRequestGroupFromReserver(DownloadEngine* e)
{
2008-11-03 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com> Made BtRegistry non-static object. Now DownloadEngine has a reference to its object. * src/AbstractBtMessage.cc * src/ActivePeerConnectionCommand.cc * src/ActivePeerConnectionCommand.h * src/BtContextAwareCommand.cc * src/BtContextAwareCommand.h * src/BtExtendedMessage.cc * src/BtExtendedMessage.h * src/BtInterestedMessage.cc * src/BtInterestedMessage.h * src/BtNotInterestedMessage.cc * src/BtNotInterestedMessage.h * src/BtRegistry.cc * src/BtRegistry.h * src/BtSeederStateChoke.cc * src/BtSetup.cc * src/DHTGetPeersCommand.cc * src/DHTGetPeersCommand.h * src/DHTPeerAnnounceEntry.cc * src/DHTPeerAnnounceEntry.h * src/DHTPeerAnnounceStorage.cc * src/DHTPeerAnnounceStorage.h * src/DHTPeerLookupTask.cc * src/DHTPeerLookupTask.h * src/DHTSetup.cc * src/DHTSetup.h * src/DHTTaskFactory.h * src/DHTTaskFactoryImpl.cc * src/DHTTaskFactoryImpl.h * src/DefaultBtAnnounce.cc * src/DefaultBtInteractive.cc * src/DefaultBtInteractive.h * src/DefaultBtMessageDispatcher.cc * src/DefaultBtMessageDispatcher.h * src/DefaultBtMessageFactory.cc * src/DefaultBtMessageFactory.h * src/DefaultBtMessageReceiver.cc * src/DefaultBtProgressInfoFile.cc * src/DefaultBtProgressInfoFile.h * src/DefaultBtRequestFactory.cc * src/DefaultBtRequestFactory.h * src/DefaultExtensionMessageFactory.cc * src/DefaultExtensionMessageFactory.h * src/DefaultPeerStorage.cc * src/DefaultPeerStorage.h * src/DownloadEngine.cc * src/DownloadEngine.h * src/InitiatorMSEHandshakeCommand.cc * src/InitiatorMSEHandshakeCommand.h * src/MSEHandshake.cc * src/MSEHandshake.h * src/Makefile.am * src/Makefile.in * src/PeerChokeCommand.cc * src/PeerChokeCommand.h * src/PeerInitiateConnectionCommand.cc * src/PeerInitiateConnectionCommand.h * src/PeerInteractionCommand.cc * src/PeerInteractionCommand.h * src/PeerObject.h * src/PeerReceiveHandshakeCommand.cc * src/PeerReceiveHandshakeCommand.h * src/ReceiverMSEHandshakeCommand.cc * src/RequestGroup.cc * src/RequestGroup.h * src/RequestGroupMan.cc * src/RequestGroupMan.h * src/SeedCheckCommand.cc * src/SeedCheckCommand.h * src/ShareRatioSeedCriteria.h * src/SimpleBtMessage.cc * src/TrackerWatcherCommand.cc * src/TrackerWatcherCommand.h * src/UTPexExtensionMessage.cc * src/UTPexExtensionMessage.h * test/BtCancelMessageTest.cc * test/BtChokeMessageTest.cc * test/BtExtendedMessageTest.cc * test/BtInterestedMessageTest.cc * test/BtNotInterestedMessageTest.cc * test/BtPieceMessageTest.cc * test/BtPortMessageTest.cc * test/BtRegistryTest.cc * test/BtRejectMessageTest.cc * test/BtRequestMessageTest.cc * test/DHTPeerAnnounceEntryTest.cc * test/DHTPeerAnnounceStorageTest.cc * test/DefaultBtMessageDispatcherTest.cc * test/DefaultBtMessageFactoryTest.cc * test/DefaultBtProgressInfoFileTest.cc * test/DefaultBtRequestFactoryTest.cc * test/DefaultExtensionMessageFactoryTest.cc * test/MSEHandshakeTest.cc * test/MockDHTTaskFactory.h * test/ShareRatioSeedCriteriaTest.cc * test/UTPexExtensionMessageTest.cc
2008-11-03 06:49:02 +00:00
removeStoppedGroup(e);
if(static_cast<size_t>(maxSimultaneousDownloads_) <= requestGroups_.size()) {
return;
}
int count = 0;
int num = maxSimultaneousDownloads_-requestGroups_.size();
std::vector<std::shared_ptr<RequestGroup> > pending;
while(count < num && (uriListParser_ || !reservedGroups_.empty())) {
if(uriListParser_ && reservedGroups_.empty()) {
std::vector<std::shared_ptr<RequestGroup> > groups;
// May throw exception
bool ok = createRequestGroupFromUriListParser(groups, option_,
uriListParser_.get());
if(ok) {
appendReservedGroup(reservedGroups_, groups.begin(), groups.end());
} else {
uriListParser_.reset();
if(reservedGroups_.empty()) {
break;
}
}
}
std::shared_ptr<RequestGroup> groupToAdd = *reservedGroups_.begin();
reservedGroups_.pop_front();
if((keepRunning_ && groupToAdd->isPauseRequested()) ||
!groupToAdd->isDependencyResolved()) {
pending.push_back(groupToAdd);
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);
++count;
2013-06-23 12:55:52 +00:00
if(res.empty()) {
requestQueueCheck();
} else {
e->addCommand(std::move(res));
}
} catch(RecoverableException& ex) {
A2_LOG_ERROR_EX(EX_EXCEPTION_CAUGHT, ex);
A2_LOG_DEBUG("Deleting temporal commands.");
groupToAdd->setLastErrorCode(ex.getErrorCode());
// We add groupToAdd to e later in order to it is processed in
// removeStoppedGroup().
requestQueueCheck();
}
groupToAdd->setState(RequestGroup::STATE_ACTIVE);
requestGroups_.push_back(groupToAdd->getGID(), groupToAdd);
util::executeHookByOptName(groupToAdd, e->getOption(),
PREF_ON_DOWNLOAD_START);
notifyDownloadEvent(EVENT_ON_DOWNLOAD_START, groupToAdd);
2007-05-20 13:57:56 +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) {
e->setNoWait(true);
e->setRefreshInterval(0);
A2_LOG_DEBUG(fmt("%d RequestGroup(s) added.", count));
2007-05-20 13:57:56 +00:00
}
}
void RequestGroupMan::save()
{
for(auto& rg : requestGroups_) {
if(rg->allDownloadFinished() &&
!rg->getDownloadContext()->isChecksumVerificationNeeded() &&
!rg->getOption()->getAsBool(PREF_FORCE_SAVE)) {
rg->removeControlFile();
} else {
try {
rg->saveControlFile();
} catch(RecoverableException& e) {
A2_LOG_ERROR_EX(EX_EXCEPTION_CAUGHT, e);
}
}
}
}
void RequestGroupMan::closeFile()
{
2013-08-21 03:06:53 +00:00
for(auto & elem : requestGroups_) {
elem->closeFile();
}
}
RequestGroupMan::DownloadStat RequestGroupMan::getDownloadStat() const
{
int finished = 0;
int error = removedErrorResult_;
int inprogress = 0;
int removed = 0;
error_code::Value lastError = removedLastErrorResult_;
2013-08-21 03:06:53 +00:00
for(auto & dr : downloadResults_) {
if(dr->belongsTo != 0) {
continue;
}
if(dr->result == error_code::FINISHED) {
++finished;
} else if(dr->result == error_code::IN_PROGRESS) {
++inprogress;
} else if(dr->result == error_code::REMOVED) {
++removed;
} else {
++error;
lastError = dr->result;
}
}
return DownloadStat(error, inprogress,
reservedGroups_.size(),
lastError);
}
enum DownloadResultStatus {
A2_STATUS_OK,
A2_STATUS_INPR,
A2_STATUS_RM,
A2_STATUS_ERR
};
namespace {
const char* getStatusStr(DownloadResultStatus status, bool useColor)
{
// 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
void RequestGroupMan::showDownloadResults(OutputFile& o, bool full) const
2007-05-20 13:57:56 +00:00
{
int pathRowSize = 55;
// Download Results:
// idx|stat|path/length
// ===+====+=======================================================================
o.printf("\n%s"
"\ngid |stat|avg speed |",
_("Download Results:"));
if(full) {
o.write(" %|path/URI"
"\n======+====+===========+===+");
pathRowSize -= 4;
} else {
o.write("path/URI"
"\n======+====+===========+");
}
std::string line(pathRowSize, '=');
o.printf("%s\n", line.c_str());
bool useColor = o.supportsColor();
int ok = 0;
int err = 0;
int inpr = 0;
int rm = 0;
2013-08-21 03:06:53 +00:00
for(auto & dr : downloadResults_) {
if(dr->belongsTo != 0) {
continue;
}
const char* status;
switch(dr->result) {
case error_code::FINISHED:
status = getStatusStr(A2_STATUS_OK, useColor);
++ok;
break;
case error_code::IN_PROGRESS:
status = getStatusStr(A2_STATUS_INPR, useColor);
++inpr;
break;
case error_code::REMOVED:
status = getStatusStr(A2_STATUS_RM, useColor);
++rm;
break;
default:
status = getStatusStr(A2_STATUS_ERR, useColor);
++err;
}
if(full) {
formatDownloadResultFull(o, status, dr);
} else {
o.write(formatDownloadResult(status, dr).c_str());
o.write("\n");
}
}
if(ok > 0 || err > 0 || inpr > 0 || rm > 0) {
o.printf("\n%s\n", _("Status Legend:"));
if(ok > 0) {
2011-12-02 13:00:48 +00:00
o.write(_("(OK):download completed."));
}
if(err > 0) {
2011-12-02 13:00:48 +00:00
o.write(_("(ERR):error occurred."));
}
if(inpr > 0) {
2011-12-02 13:00:48 +00:00
o.write(_("(INPR):download in-progress."));
}
if(rm > 0) {
2011-12-02 13:00:48 +00:00
o.write(_("(RM):download removed."));
}
o.write("\n");
}
}
namespace {
void formatDownloadResultCommon
(std::ostream& o,
const char* status,
const std::shared_ptr<DownloadResult>& downloadResult)
{
o << std::setw(3) << downloadResult->gid->toAbbrevHex() << "|"
<< std::setw(4) << status << "|"
<< std::setw(11);
if(downloadResult->sessionTime > 0) {
o << util::abbrevSize
(downloadResult->sessionDownloadLength*1000/downloadResult->sessionTime)+
"B/s";
} else {
o << "n/a";
}
o << "|";
}
} // namespace
void RequestGroupMan::formatDownloadResultFull
(OutputFile& out,
const char* status,
const std::shared_ptr<DownloadResult>& downloadResult) const
{
BitfieldMan bt(downloadResult->pieceLength, downloadResult->totalLength);
bt.setBitfield(reinterpret_cast<const unsigned char*>
(downloadResult->bitfield.data()),
downloadResult->bitfield.size());
bool head = true;
const std::vector<std::shared_ptr<FileEntry> >& fileEntries =
downloadResult->fileEntries;
2013-08-21 03:06:53 +00:00
for(auto & f: fileEntries) {
if(!f->isRequested()) {
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()) {
o << " -|";
} else {
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() << "|";
}
2013-08-21 03:06:53 +00:00
writeFilePath(o, f, downloadResult->inMemoryDownload);
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
(const char* status,
const std::shared_ptr<DownloadResult>& downloadResult) const
{
std::stringstream o;
formatDownloadResultCommon(o, status, downloadResult);
const std::vector<std::shared_ptr<FileEntry> >& fileEntries =
downloadResult->fileEntries;
2009-06-29 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net> Removed _uris from RequestGroup. All functions that refer to _uris were moved to FileEntry. Exit status code are now defined in DownloadResultCode.h. * src/AbstractCommand.cc * src/AdaptiveURISelector.cc * src/AdaptiveURISelector.h * src/AutoSaveCommand.cc * src/BtPostDownloadHandler.cc * src/CheckIntegrityDispatcherCommand.cc * src/CookieStorage.cc * src/DHTAutoSaveCommand.cc * src/DHTBucketRefreshCommand.cc * src/DHTEntryPointNameResolveCommand.cc * src/DHTInteractionCommand.cc * src/DHTPeerAnnounceCommand.cc * src/DHTTokenUpdateCommand.cc * src/DlAbortEx.h * src/DlRetryEx.h * src/DownloadCommand.cc * src/DownloadContext.h * src/DownloadFailureException.h * src/DownloadResult.h * src/DownloadResultCode.h * src/FeedbackURISelector.cc * src/FeedbackURISelector.h * src/FileEntry.cc * src/FileEntry.h * src/FtpNegotiationCommand.cc * src/HttpListenCommand.cc * src/HttpResponseCommand.cc * src/HttpServerResponseCommand.cc * src/HttpSkipResponseCommand.cc * src/InOrderURISelector.cc * src/InOrderURISelector.h * src/Makefile.am * src/Makefile.in * src/Metalink2RequestGroup.cc * src/MultiUrlRequestInfo.cc * src/MultiUrlRequestInfo.h * src/OptionHandlerFactory.cc * src/PeerListenCommand.cc * src/RecoverableException.h * src/RequestGroup.cc * src/RequestGroup.h * src/RequestGroupMan.cc * src/RequestGroupMan.h * src/TimedHaltCommand.cc * src/TrackerWatcherCommand.cc * src/URIResult.cc * src/URIResult.h * src/URISelector.h * src/XmlRpcMethodImpl.cc * src/bittorrent_helper.cc * src/bittorrent_helper.h * src/download_helper.cc * src/main.cc * src/option_processing.cc * test/BtDependencyTest.cc * test/BtPostDownloadHandlerTest.cc * test/CookieStorageTest.cc * test/DefaultBtMessageDispatcherTest.cc * test/DownloadHandlerFactoryTest.cc * test/DownloadHelperTest.cc * test/FeedbackURISelectorTest.cc * test/FileEntryTest.cc * test/InOrderURISelectorTest.cc * test/Metalink2RequestGroupTest.cc * test/MetalinkPostDownloadHandlerTest.cc * test/RequestGroupManTest.cc * test/RequestGroupTest.cc * test/XmlRpcMethodTest.cc
2009-06-29 08:42:58 +00:00
writeFilePath(fileEntries.begin(), fileEntries.end(), o,
downloadResult->inMemoryDownload);
return o.str();
2007-05-20 13:57:56 +00:00
}
2010-10-30 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net> Use unnamed namespace instead of static keyword. * src/AbstractCommand.cc * src/AdaptiveURISelector.cc * src/Base64.cc * src/BitfieldMan.cc * src/BtDependency.cc * src/ConsoleStatCalc.cc * src/ContentTypeRequestGroupCriteria.cc * src/CookieStorage.cc * src/DHTMessageFactoryImpl.cc * src/DHTRoutingTableDeserializer.cc * src/DefaultBtAnnounce.cc * src/DefaultBtProgressInfoFile.cc * src/DefaultPeerStorage.cc * src/DefaultPieceStorage.cc * src/DownloadCommand.cc * src/DownloadEngine.cc * src/EpollEventPoll.cc * src/ExpatMetalinkProcessor.cc * src/ExpatXmlRpcRequestProcessor.cc * src/FileEntry.cc * src/HttpRequest.cc * src/HttpRequestCommand.cc * src/HttpResponseCommand.cc * src/KqueueEventPoll.cc * src/LongestSequencePieceSelector.cc * src/MetalinkParserStateV3Impl.cc * src/MetalinkParserStateV4Impl.cc * src/MultiDiskAdaptor.cc * src/MultiUrlRequestInfo.cc * src/OptionParser.cc * src/PeerSessionResource.cc * src/PortEventPoll.cc * src/Request.cc * src/RequestGroupMan.cc * src/SelectEventPoll.cc * src/SessionSerializer.cc * src/SimpleLogFormatter.cc * src/Sqlite3CookieParser.cc * src/TrackerWatcherCommand.cc * src/XML2SAXMetalinkProcessor.cc * src/Xml2XmlRpcRequestProcessor.cc * src/XmlRpcMethod.cc * src/XmlRpcMethodImpl.cc * src/XmlRpcResponse.cc * src/base32.cc * src/bencode2.cc * src/bittorrent_helper.cc * src/download_helper.cc * src/main.cc * src/messageDigest.cc * src/option_processing.cc * src/util.cc * test/AnnounceListTest.cc * test/BtRegistryTest.cc * test/DHTBucketTest.cc * test/DHTRoutingTableTest.cc * test/DefaultBtAnnounceTest.cc * test/FileEntryTest.cc * test/FtpConnectionTest.cc * test/MSEHandshakeTest.cc * test/MagnetTest.cc * test/XmlRpcMethodTest.cc * test/array_funTest.cc
2010-10-30 16:02:15 +00:00
namespace {
template<typename StringInputIterator, typename FileEntryInputIterator>
2010-10-30 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net> Use unnamed namespace instead of static keyword. * src/AbstractCommand.cc * src/AdaptiveURISelector.cc * src/Base64.cc * src/BitfieldMan.cc * src/BtDependency.cc * src/ConsoleStatCalc.cc * src/ContentTypeRequestGroupCriteria.cc * src/CookieStorage.cc * src/DHTMessageFactoryImpl.cc * src/DHTRoutingTableDeserializer.cc * src/DefaultBtAnnounce.cc * src/DefaultBtProgressInfoFile.cc * src/DefaultPeerStorage.cc * src/DefaultPieceStorage.cc * src/DownloadCommand.cc * src/DownloadEngine.cc * src/EpollEventPoll.cc * src/ExpatMetalinkProcessor.cc * src/ExpatXmlRpcRequestProcessor.cc * src/FileEntry.cc * src/HttpRequest.cc * src/HttpRequestCommand.cc * src/HttpResponseCommand.cc * src/KqueueEventPoll.cc * src/LongestSequencePieceSelector.cc * src/MetalinkParserStateV3Impl.cc * src/MetalinkParserStateV4Impl.cc * src/MultiDiskAdaptor.cc * src/MultiUrlRequestInfo.cc * src/OptionParser.cc * src/PeerSessionResource.cc * src/PortEventPoll.cc * src/Request.cc * src/RequestGroupMan.cc * src/SelectEventPoll.cc * src/SessionSerializer.cc * src/SimpleLogFormatter.cc * src/Sqlite3CookieParser.cc * src/TrackerWatcherCommand.cc * src/XML2SAXMetalinkProcessor.cc * src/Xml2XmlRpcRequestProcessor.cc * src/XmlRpcMethod.cc * src/XmlRpcMethodImpl.cc * src/XmlRpcResponse.cc * src/base32.cc * src/bencode2.cc * src/bittorrent_helper.cc * src/download_helper.cc * src/main.cc * src/messageDigest.cc * src/option_processing.cc * src/util.cc * test/AnnounceListTest.cc * test/BtRegistryTest.cc * test/DHTBucketTest.cc * test/DHTRoutingTableTest.cc * test/DefaultBtAnnounceTest.cc * test/FileEntryTest.cc * test/FtpConnectionTest.cc * test/MSEHandshakeTest.cc * test/MagnetTest.cc * test/XmlRpcMethodTest.cc * test/array_funTest.cc
2010-10-30 16:02:15 +00:00
bool sameFilePathExists(StringInputIterator sfirst,
StringInputIterator slast,
FileEntryInputIterator ffirst,
FileEntryInputIterator flast)
{
for(; ffirst != flast; ++ffirst) {
if(std::binary_search(sfirst, slast, (*ffirst)->getPath())) {
return true;
}
}
return false;
}
2010-10-30 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net> Use unnamed namespace instead of static keyword. * src/AbstractCommand.cc * src/AdaptiveURISelector.cc * src/Base64.cc * src/BitfieldMan.cc * src/BtDependency.cc * src/ConsoleStatCalc.cc * src/ContentTypeRequestGroupCriteria.cc * src/CookieStorage.cc * src/DHTMessageFactoryImpl.cc * src/DHTRoutingTableDeserializer.cc * src/DefaultBtAnnounce.cc * src/DefaultBtProgressInfoFile.cc * src/DefaultPeerStorage.cc * src/DefaultPieceStorage.cc * src/DownloadCommand.cc * src/DownloadEngine.cc * src/EpollEventPoll.cc * src/ExpatMetalinkProcessor.cc * src/ExpatXmlRpcRequestProcessor.cc * src/FileEntry.cc * src/HttpRequest.cc * src/HttpRequestCommand.cc * src/HttpResponseCommand.cc * src/KqueueEventPoll.cc * src/LongestSequencePieceSelector.cc * src/MetalinkParserStateV3Impl.cc * src/MetalinkParserStateV4Impl.cc * src/MultiDiskAdaptor.cc * src/MultiUrlRequestInfo.cc * src/OptionParser.cc * src/PeerSessionResource.cc * src/PortEventPoll.cc * src/Request.cc * src/RequestGroupMan.cc * src/SelectEventPoll.cc * src/SessionSerializer.cc * src/SimpleLogFormatter.cc * src/Sqlite3CookieParser.cc * src/TrackerWatcherCommand.cc * src/XML2SAXMetalinkProcessor.cc * src/Xml2XmlRpcRequestProcessor.cc * src/XmlRpcMethod.cc * src/XmlRpcMethodImpl.cc * src/XmlRpcResponse.cc * src/base32.cc * src/bencode2.cc * src/bittorrent_helper.cc * src/download_helper.cc * src/main.cc * src/messageDigest.cc * src/option_processing.cc * src/util.cc * test/AnnounceListTest.cc * test/BtRegistryTest.cc * test/DHTBucketTest.cc * test/DHTRoutingTableTest.cc * test/DefaultBtAnnounceTest.cc * test/FileEntryTest.cc * test/FtpConnectionTest.cc * test/MSEHandshakeTest.cc * test/MagnetTest.cc * test/XmlRpcMethodTest.cc * test/array_funTest.cc
2010-10-30 16:02:15 +00:00
} // namespace
bool RequestGroupMan::isSameFileBeingDownloaded(RequestGroup* requestGroup) const
{
// TODO it may be good to use dedicated method rather than use
// isPreLocalFileCheckEnabled
if(!requestGroup->isPreLocalFileCheckEnabled()) {
return false;
}
std::vector<std::string> files;
2013-08-21 03:06:53 +00:00
for(auto & rg : requestGroups_) {
if(rg.get() != requestGroup) {
const std::vector<std::shared_ptr<FileEntry> >& entries =
rg->getDownloadContext()->getFileEntries();
std::transform(entries.begin(), entries.end(),
std::back_inserter(files),
std::mem_fn(&FileEntry::getPath));
}
}
std::sort(files.begin(), files.end());
const std::vector<std::shared_ptr<FileEntry> >& entries =
requestGroup->getDownloadContext()->getFileEntries();
return sameFilePathExists(files.begin(), files.end(),
entries.begin(), entries.end());
}
void RequestGroupMan::halt()
{
2013-08-21 03:06:53 +00:00
for(auto & elem : requestGroups_) {
elem->setHaltRequested(true);
}
}
void RequestGroupMan::forceHalt()
{
2013-08-21 03:06:53 +00:00
for(auto & elem : requestGroups_) {
elem->setForceHaltRequested(true);
}
}
TransferStat RequestGroupMan::calculateStat()
{
// TODO Currently, all time upload length is not set.
return netStat_.toTransferStat();
}
std::shared_ptr<DownloadResult>
RequestGroupMan::findDownloadResult(a2_gid_t gid) const
{
return downloadResults_.get(gid);
}
bool RequestGroupMan::removeDownloadResult(a2_gid_t gid)
{
return downloadResults_.remove(gid);
}
void RequestGroupMan::addDownloadResult(const std::shared_ptr<DownloadResult>& dr)
{
bool rv = downloadResults_.push_back(dr->gid->getNumericId(), dr);
assert(rv);
while(downloadResults_.size() > maxDownloadResult_){
DownloadResultList::iterator i = downloadResults_.begin();
// Save last encountered error code so that we can report it
// later.
const std::shared_ptr<DownloadResult>& dr = *i;
if(dr->belongsTo == 0 && dr->result != error_code::FINISHED) {
removedLastErrorResult_ = dr->result;
++removedErrorResult_;
}
downloadResults_.pop_front();
}
}
void RequestGroupMan::purgeDownloadResult()
{
downloadResults_.clear();
}
std::shared_ptr<ServerStat>
RequestGroupMan::findServerStat(const std::string& hostname,
const std::string& protocol) const
{
return serverStatMan_->find(hostname, protocol);
}
std::shared_ptr<ServerStat>
RequestGroupMan::getOrCreateServerStat(const std::string& hostname,
const std::string& protocol)
{
std::shared_ptr<ServerStat> ss = findServerStat(hostname, protocol);
if(!ss) {
ss.reset(new ServerStat(hostname, protocol));
addServerStat(ss);
}
return ss;
}
bool RequestGroupMan::addServerStat(const std::shared_ptr<ServerStat>& serverStat)
{
return serverStatMan_->add(serverStat);
}
bool RequestGroupMan::loadServerStat(const std::string& filename)
{
return serverStatMan_->load(filename);
}
bool RequestGroupMan::saveServerStat(const std::string& filename) const
{
return serverStatMan_->save(filename);
}
void RequestGroupMan::removeStaleServerStat(time_t timeout)
{
serverStatMan_->removeStaleServerStat(timeout);
}
bool RequestGroupMan::doesOverallDownloadSpeedExceed()
{
return maxOverallDownloadSpeedLimit_ > 0 &&
maxOverallDownloadSpeedLimit_ < netStat_.calculateDownloadSpeed();
}
bool RequestGroupMan::doesOverallUploadSpeedExceed()
{
return maxOverallUploadSpeedLimit_ > 0 &&
maxOverallUploadSpeedLimit_ < netStat_.calculateUploadSpeed();
}
void RequestGroupMan::getUsedHosts
(std::vector<std::pair<size_t, std::string> >& usedHosts)
{
2013-06-22 12:08:50 +00:00
// vector of tuple which consists of use count, -download speed,
// 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 =
rg->getDownloadContext()->getFirstFileEntry()->getInFlightRequests();
2013-06-22 12:08:50 +00:00
for(const auto& req : inFlightReqs) {
uri_split_result us;
2013-06-22 12:08:50 +00:00
if(uri_split(&us, req->getUri().c_str()) == 0) {
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);
break;
}
}
if(k == eok) {
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);
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);
}
}
}
}
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));
});
}
void RequestGroupMan::setUriListParser
(const std::shared_ptr<UriListParser>& uriListParser)
{
uriListParser_ = uriListParser;
}
void RequestGroupMan::initWrDiskCache()
{
assert(wrDiskCache_ == nullptr);
size_t limit = option_->getAsInt(PREF_DISK_CACHE);
if(limit > 0) {
wrDiskCache_ = new WrDiskCache(limit);
}
}
} // namespace aria2