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
pull/1/head
Tatsuhiro Tsujikawa 2009-06-28 10:37:15 +00:00
parent 90471d6805
commit 5699b67526
183 changed files with 2986 additions and 3449 deletions

189
ChangeLog
View File

@ -1,3 +1,192 @@
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-24 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
Added experimental support of WEB-Seeding for multi-file torrent.

View File

@ -34,7 +34,6 @@
/* copyright --> */
#include "AbstractBtMessage.h"
#include "Peer.h"
#include "BtContext.h"
#include "PieceStorage.h"
#include "BtMessageValidator.h"
#include "LogFactory.h"
@ -73,11 +72,6 @@ AbstractBtMessage::setBtMessageValidator(const SharedHandle<BtMessageValidator>&
this->validator = validator;
}
void AbstractBtMessage::setBtContext(const SharedHandle<BtContext>& btContext)
{
this->btContext = btContext;
}
void AbstractBtMessage::setPieceStorage(const SharedHandle<PieceStorage>& pieceStorage)
{
this->pieceStorage = pieceStorage;

View File

@ -41,7 +41,6 @@
namespace aria2 {
class BtContext;
class PieceStorage;
class Peer;
class BtMessageDispatcher;
@ -60,8 +59,6 @@ protected:
std::string _name;
SharedHandle<BtContext> btContext;
SharedHandle<PieceStorage> pieceStorage;
SharedHandle<Peer> peer;
@ -137,13 +134,6 @@ public:
void setBtMessageValidator(const SharedHandle<BtMessageValidator>& validator);
void setBtContext(const SharedHandle<BtContext>& btContext);
const SharedHandle<BtContext>& getBtContext() const
{
return btContext;
}
void setPieceStorage(const SharedHandle<PieceStorage>& pieceStorage);
void setBtMessageDispatcher(const WeakHandle<BtMessageDispatcher>& dispatcher);

View File

@ -36,7 +36,6 @@
#include <algorithm>
#include "RequestGroup.h"
#include "Request.h"
#include "DownloadEngine.h"
#include "Option.h"
@ -231,9 +230,9 @@ bool AbstractCommand::execute() {
void AbstractCommand::tryReserved() {
_requestGroup->removeServerHost(cuid);
if(_requestGroup->getDownloadContext()->getFileMode() == DownloadContext::SINGLE) {
if(_requestGroup->getDownloadContext()->getFileEntries().size() == 1) {
const SharedHandle<FileEntry>& entry =
_requestGroup->getDownloadContext()->getFileEntries().front();
_requestGroup->getDownloadContext()->getFirstFileEntry();
// Don't create new command if currently file length is unknown
// and there are no URI left. Because file length is unknown, we
// can assume that there are no in-flight request object.

View File

@ -39,12 +39,12 @@
#include "SharedHandle.h"
#include "TimeA2.h"
#include "FileEntry.h"
#include "RequestGroup.h"
namespace aria2 {
class Request;
class DownloadEngine;
class RequestGroup;
class Segment;
class Exception;
class SocketCore;
@ -122,6 +122,11 @@ protected:
const std::string& resolveProxyMethod(const std::string& protocol) const;
const SharedHandle<Option>& getOption() const;
const SharedHandle<DownloadContext>& getDownloadContext() const
{
return _requestGroup->getDownloadContext();
}
private:
bool checkSocketIsReadable;
bool checkSocketIsWritable;

View File

@ -45,6 +45,7 @@
#include "CookieStorage.h"
#include "AuthConfigFactory.h"
#include "AuthConfig.h"
#include "DownloadContext.h"
namespace aria2 {

View File

@ -45,6 +45,7 @@
#include "DlRetryEx.h"
#include "message.h"
#include "HttpHeader.h"
#include "DownloadContext.h"
namespace aria2 {

View File

@ -36,7 +36,6 @@
#include "PeerInitiateConnectionCommand.h"
#include "message.h"
#include "DownloadEngine.h"
#include "BtContext.h"
#include "PeerStorage.h"
#include "PieceStorage.h"
#include "BtRuntime.h"
@ -48,6 +47,7 @@
#include "SocketCore.h"
#include "BtAnnounce.h"
#include "RequestGroup.h"
#include "DownloadContext.h"
namespace aria2 {
@ -55,12 +55,10 @@ ActivePeerConnectionCommand::ActivePeerConnectionCommand
(int cuid,
RequestGroup* requestGroup,
DownloadEngine* e,
const BtContextHandle& btContext,
time_t interval)
:
Command(cuid),
_requestGroup(requestGroup),
_btContext(btContext),
interval(interval),
e(e),
_numNewConnection(5)
@ -130,7 +128,7 @@ void ActivePeerConnectionCommand::connectToPeer(const PeerHandle& peer)
peer->usedBy(e->newCUID());
PeerInitiateConnectionCommand* command =
new PeerInitiateConnectionCommand(peer->usedBy(), _requestGroup, peer, e,
_btContext, _btRuntime);
_btRuntime);
command->setPeerStorage(_peerStorage);
command->setPieceStorage(_pieceStorage);
e->commands.push_back(command);

View File

@ -44,7 +44,6 @@ namespace aria2 {
class RequestGroup;
class DownloadEngine;
class Peer;
class BtContext;
class BtRuntime;
class PieceStorage;
class PeerStorage;
@ -53,7 +52,6 @@ class BtAnnounce;
class ActivePeerConnectionCommand : public Command {
private:
RequestGroup* _requestGroup;
SharedHandle<BtContext> _btContext;
SharedHandle<BtRuntime> _btRuntime;
SharedHandle<PieceStorage> _pieceStorage;
SharedHandle<PeerStorage> _peerStorage;
@ -67,7 +65,6 @@ public:
ActivePeerConnectionCommand(int cuid,
RequestGroup* requestGroup,
DownloadEngine* e,
const SharedHandle<BtContext>& btContext,
time_t interval);
virtual ~ActivePeerConnectionCommand();

View File

@ -39,6 +39,7 @@
#include "A2STR.h"
#include "SimpleRandomizer.h"
#include "bencode.h"
#include "Util.h"
namespace aria2 {
@ -72,7 +73,7 @@ void AnnounceList::reconfigure(const BDE& announceList)
elemItr != elemList.listEnd(); ++elemItr) {
const BDE& data = *elemItr;
if(data.isString()) {
urls.push_back(data.s());
urls.push_back(Util::trim(data.s()));
}
}
if(!urls.empty()) {

View File

@ -40,6 +40,7 @@
#include "DiskAdaptor.h"
#include "prefs.h"
#include "Option.h"
#include "DownloadContext.h"
namespace aria2 {

View File

@ -41,6 +41,10 @@
typedef std::map<std::string, uint8_t> Extensions;
#define INFO_HASH_LENGTH 20
#define PIECE_HASH_LENGTH 20
#define PEER_ID_LENGTH 20
#define INFO_HASH_LENGTH 20

View File

@ -1,97 +0,0 @@
/* <!-- 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
*
* In addition, as a special exception, the copyright holders give
* permission to link the code of portions of this program with the
* OpenSSL library under certain conditions as described in each
* individual source file, and distribute linked combinations
* including the two.
* You must obey the GNU General Public License in all respects
* for all of the code used other than OpenSSL. If you modify
* file(s) with this exception, you may extend this exception to your
* version of the file(s), but you are not obligated to do so. If you
* do not wish to do so, delete this exception statement from your
* version. If you delete this exception statement from all source
* files in the program, then also delete it here.
*/
/* copyright --> */
#ifndef _D_BT_CONTEXT_H_
#define _D_BT_CONTEXT_H_
#include "DownloadContext.h"
#include <utility>
#include <deque>
#include "IntSequence.h"
namespace aria2 {
class AnnounceTier;
class RequestGroup;
class BtContext:public DownloadContext {
protected:
bool _private;
public:
BtContext():_private(false) {}
virtual ~BtContext() {}
virtual const unsigned char* getInfoHash() const = 0;
virtual size_t getInfoHashLength() const = 0;
virtual const std::string& getInfoHashAsString() const = 0;
virtual const std::deque<SharedHandle<AnnounceTier> >&
getAnnounceTiers() const = 0;
virtual void load(const std::string& torrentFile,
const std::string& overrideName = "") = 0;
/**
* Returns the peer id of localhost, 20 byte length
*/
virtual const unsigned char* getPeerId() = 0;
bool isPrivate() const
{
return _private;
}
virtual void computeFastSet
(std::deque<size_t>& fastSet, const std::string& ipaddr, size_t fastSetSize) = 0;
virtual RequestGroup* getOwnerRequestGroup() = 0;
virtual std::deque<std::pair<std::string, uint16_t> >& getNodes() = 0;
virtual const std::string& getName() const = 0;
virtual void setFileFilter(IntSequence seq) = 0;
};
class BtContext;
typedef SharedHandle<BtContext> BtContextHandle;
} // namespace aria2
#endif // _D_BT_CONTEXT_H_

View File

@ -37,7 +37,7 @@
#include "Option.h"
#include "LogFactory.h"
#include "Logger.h"
#include "DefaultBtContext.h"
#include "DownloadContext.h"
#include "RecoverableException.h"
#include "message.h"
#include "prefs.h"
@ -45,6 +45,8 @@
#include "PieceStorage.h"
#include "DiskAdaptor.h"
#include "File.h"
#include "bittorrent_helper.h"
#include "DlAbortEx.h"
namespace aria2 {
@ -62,39 +64,38 @@ bool BtDependency::resolve()
RequestGroupHandle dependee = _dependee;
// cut reference here
_dependee.reset();
DefaultBtContextHandle btContext(new DefaultBtContext());
btContext->setDir(_dependant->getDownloadContext()->getDir());
SharedHandle<DownloadContext> context(new DownloadContext());
context->setDir(_dependant->getDownloadContext()->getDir());
try {
DiskAdaptorHandle diskAdaptor = dependee->getPieceStorage()->getDiskAdaptor();
DiskAdaptorHandle diskAdaptor =
dependee->getPieceStorage()->getDiskAdaptor();
diskAdaptor->openExistingFile();
std::string content = Util::toString(diskAdaptor);
std::string overrideName;
if(Util::startsWith(_dependant->getDownloadContext()->getActualBasePath(),
_dependant->getDownloadContext()->getDir())) {
overrideName =
_dependant->getDownloadContext()->getActualBasePath().substr
(_dependant->getDownloadContext()->getDir().size());
if(Util::startsWith(overrideName, "/")) {
overrideName = overrideName.substr(1);
}
bittorrent::loadFromMemory
(content, context, File(dependee->getFirstFilePath()).getBasename());
if(context->getFileEntries().size() !=
_dependant->getDownloadContext()->getFileEntries().size()) {
throw DL_ABORT_EX("The number of file in torrent doesn't match to"
" the dependant.");
}
btContext->loadFromMemory(content,
File(dependee->getFilePath()).getBasename(),
overrideName);
if(_dependant->getOption()->defined(PREF_PEER_ID_PREFIX)) {
btContext->setPeerIdPrefix
(_dependant->getOption()->get(PREF_PEER_ID_PREFIX));
// Copy file path in _dependant's FileEntries to newly created
// context's FileEntries to endorse the path structure of
// _dependant.
for(std::vector<SharedHandle<FileEntry> >::const_iterator s =
_dependant->getDownloadContext()->getFileEntries().begin(),
d = context->getFileEntries().begin();
d != context->getFileEntries().end(); ++s, ++d) {
(*d)->setPath((*s)->getPath());
}
} catch(RecoverableException& e) {
_logger->error(EX_EXCEPTION_CAUGHT, e);
_logger->debug("BtDependency for GID#%d failed. Go without Bt.",
_dependant->getGID());
_logger->info("BtDependency for GID#%d failed. Go without Bt.",
_dependant->getGID());
return true;
}
_logger->debug("Dependency resolved for GID#%d", _dependant->getGID());
_dependant->setDownloadContext(btContext);
btContext->setOwnerRequestGroup(_dependant.get());
_dependant->setDownloadContext(context);
context->setOwnerRequestGroup(_dependant.get());
return true;
} else if(_dependee->getNumCommand() == 0) {
// _dependee's download failed.

View File

@ -41,7 +41,6 @@
#include "ExtensionMessageFactory.h"
#include "PeerMessageUtil.h"
#include "Peer.h"
#include "BtContext.h"
#include "DlAbortEx.h"
#include "message.h"
#include "Util.h"

View File

@ -47,13 +47,13 @@
#include "Logger.h"
#include "Peer.h"
#include "Piece.h"
#include "BtContext.h"
#include "PieceStorage.h"
#include "BtMessageDispatcher.h"
#include "BtMessageFactory.h"
#include "BtRequestFactory.h"
#include "PeerConnection.h"
#include "StringFormat.h"
#include "DownloadContext.h"
namespace aria2 {
@ -85,7 +85,7 @@ void BtPieceMessage::doReceivedAction() {
peer->snubbing(false);
peer->updateLatency(slot.getLatencyInMillis());
PieceHandle piece = pieceStorage->getPiece(index);
off_t offset = (off_t)index*btContext->getPieceLength()+begin;
off_t offset = (off_t)index*_downloadContext->getPieceLength()+begin;
logger->debug(MSG_PIECE_RECEIVED,
cuid, index, begin, blockLength, offset, slot.getBlockIndex());
pieceStorage->getDiskAdaptor()->writeData(block, blockLength, offset);
@ -143,7 +143,7 @@ void BtPieceMessage::send() {
getMessageHeader();
peerConnection->sendMessage(msgHeader, getMessageHeaderLength());
off_t pieceDataOffset =
(off_t)index*btContext->getPieceLength()+begin;
(off_t)index*_downloadContext->getPieceLength()+begin;
size_t writtenLength = sendPieceData(pieceDataOffset, blockLength);
logger->debug("msglength = %lu bytes",
static_cast<unsigned long>(getMessageHeaderLength()+
@ -176,12 +176,13 @@ bool BtPieceMessage::checkPieceHash(const PieceHandle& piece) {
if(piece->isHashCalculated()) {
logger->debug("Hash is available!! index=%lu",
static_cast<unsigned long>(piece->getIndex()));
return piece->getHashString() == btContext->getPieceHash(piece->getIndex());
return
piece->getHashString()==_downloadContext->getPieceHash(piece->getIndex());
} else {
off_t offset = (off_t)piece->getIndex()*btContext->getPieceLength();
off_t offset = (off_t)piece->getIndex()*_downloadContext->getPieceLength();
return MessageDigestHelper::staticSHA1Digest(pieceStorage->getDiskAdaptor(), offset, piece->getLength())
== btContext->getPieceHash(piece->getIndex());
== _downloadContext->getPieceHash(piece->getIndex());
}
}
@ -203,7 +204,7 @@ void BtPieceMessage::erasePieceOnDisk(const PieceHandle& piece) {
size_t BUFSIZE = 4096;
unsigned char buf[BUFSIZE];
memset(buf, 0, BUFSIZE);
off_t offset = (off_t)piece->getIndex()*btContext->getPieceLength();
off_t offset = (off_t)piece->getIndex()*_downloadContext->getPieceLength();
div_t res = div(piece->getLength(), BUFSIZE);
for(int i = 0; i < res.quot; ++i) {
pieceStorage->getDiskAdaptor()->writeData(buf, BUFSIZE, offset);
@ -255,4 +256,10 @@ void BtPieceMessage::onCancelSendingPieceEvent
}
}
void BtPieceMessage::setDownloadContext
(const SharedHandle<DownloadContext>& downloadContext)
{
_downloadContext = downloadContext;
}
} // namespace aria2

View File

@ -41,6 +41,7 @@ namespace aria2 {
class Piece;
class BtPieceMessage;
class DownloadContext;
typedef SharedHandle<BtPieceMessage> BtPieceMessageHandle;
@ -51,6 +52,7 @@ private:
uint32_t blockLength;
unsigned char* block;
unsigned char* msgHeader;
SharedHandle<DownloadContext> _downloadContext;
static size_t MESSAGE_HEADER_LENGTH;
@ -100,6 +102,8 @@ public:
void setBlockLength(size_t blockLength) { this->blockLength = blockLength; }
void setDownloadContext(const SharedHandle<DownloadContext>& downloadContext);
static BtPieceMessageHandle create(const unsigned char* data, size_t dataLength);
virtual void doReceivedAction();

View File

@ -33,7 +33,6 @@
*/
/* copyright --> */
#include "BtPostDownloadHandler.h"
#include "DefaultBtContext.h"
#include "prefs.h"
#include "RequestGroup.h"
#include "Option.h"
@ -45,6 +44,8 @@
#include "Util.h"
#include "ContentTypeRequestGroupCriteria.h"
#include "Exception.h"
#include "DownloadContext.h"
#include "bittorrent_helper.h"
namespace aria2 {
@ -64,7 +65,7 @@ void BtPostDownloadHandler::getNextRequestGroups
{
const SharedHandle<Option>& op = requestGroup->getOption();
_logger->debug("Generating RequestGroups for Torrent file %s",
requestGroup->getFilePath().c_str());
requestGroup->getFirstFilePath().c_str());
RequestGroupHandle rg(new RequestGroup(op, std::deque<std::string>()));
std::string content;
@ -76,14 +77,12 @@ void BtPostDownloadHandler::getNextRequestGroups
requestGroup->getPieceStorage()->getDiskAdaptor()->closeFile();
throw;
}
DefaultBtContextHandle btContext(new DefaultBtContext());
btContext->setDir(requestGroup->getDownloadContext()->getDir());
btContext->loadFromMemory(content, File(requestGroup->getFilePath()).getBasename());
if(op->defined(PREF_PEER_ID_PREFIX)) {
btContext->setPeerIdPrefix(op->get(PREF_PEER_ID_PREFIX));
}
rg->setDownloadContext(btContext);
btContext->setOwnerRequestGroup(rg.get());
SharedHandle<DownloadContext> context(new DownloadContext());
context->setDir(requestGroup->getDownloadContext()->getDir());
bittorrent::loadFromMemory(content, context,
File(requestGroup->getFirstFilePath()).getBasename());
rg->setDownloadContext(context);
context->setOwnerRequestGroup(rg.get());
groups.push_back(rg);
}

View File

@ -34,7 +34,7 @@
/* copyright --> */
#include "BtRegistry.h"
#include "DlAbortEx.h"
#include "BtContext.h"
#include "DownloadContext.h"
#include "PeerStorage.h"
#include "PieceStorage.h"
#include "BtAnnounce.h"
@ -43,10 +43,10 @@
namespace aria2 {
SharedHandle<BtContext>
BtRegistry::getBtContext(const std::string& infoHash) const
SharedHandle<DownloadContext>
BtRegistry::getDownloadContext(const std::string& infoHash) const
{
return get(infoHash)._btContext;
return get(infoHash)._downloadContext;
}
void BtRegistry::put(const std::string& infoHash, const BtObject& obj)

View File

@ -49,23 +49,23 @@ class PieceStorage;
class BtAnnounce;
class BtRuntime;
class BtProgressInfoFile;
class BtContext;
class DownloadContext;
struct BtObject {
SharedHandle<BtContext> _btContext;
SharedHandle<DownloadContext> _downloadContext;
SharedHandle<PieceStorage> _pieceStorage;
SharedHandle<PeerStorage> _peerStorage;
SharedHandle<BtAnnounce> _btAnnounce;
SharedHandle<BtRuntime> _btRuntime;
SharedHandle<BtProgressInfoFile> _btProgressInfoFile;
BtObject(const SharedHandle<BtContext>& btContext,
BtObject(const SharedHandle<DownloadContext>& downloadContext,
const SharedHandle<PieceStorage>& pieceStorage,
const SharedHandle<PeerStorage>& peerStorage,
const SharedHandle<BtAnnounce>& btAnnounce,
const SharedHandle<BtRuntime>& btRuntime,
const SharedHandle<BtProgressInfoFile>& btProgressInfoFile):
_btContext(btContext),
_downloadContext(downloadContext),
_pieceStorage(pieceStorage),
_peerStorage(peerStorage),
_btAnnounce(btAnnounce),
@ -76,7 +76,7 @@ struct BtObject {
bool isNull() const
{
return _btContext.isNull() &&
return _downloadContext.isNull() &&
_pieceStorage.isNull() &&
_peerStorage.isNull() &&
_btAnnounce.isNull() &&
@ -89,19 +89,21 @@ class BtRegistry {
private:
std::map<std::string, BtObject> _pool;
public:
SharedHandle<BtContext> getBtContext(const std::string& infoHash) const;
SharedHandle<DownloadContext>
getDownloadContext(const std::string& infoHash) const;
void put(const std::string& infoHash, const BtObject& obj);
BtObject get(const std::string& infoHash) const;
template<typename OutputIterator>
void getAllBtContext(OutputIterator dest)
OutputIterator getAllDownloadContext(OutputIterator dest)
{
for(std::map<std::string, BtObject>::const_iterator i = _pool.begin();
i != _pool.end(); ++i) {
*dest++ = (*i).second._btContext;
*dest++ = (*i).second._downloadContext;
}
return dest;
}
void removeAll();

View File

@ -58,6 +58,7 @@
#include "BtProgressInfoFile.h"
#include "BtAnnounce.h"
#include "BtRuntime.h"
#include "bittorrent_helper.h"
namespace aria2 {
@ -68,11 +69,13 @@ void BtSetup::setup(std::deque<Command*>& commands,
DownloadEngine* e,
const Option* option)
{
BtContextHandle btContext(dynamic_pointer_cast<BtContext>(requestGroup->getDownloadContext()));
if(btContext.isNull()) {
if(!requestGroup->getDownloadContext()->hasAttribute(bittorrent::BITTORRENT)) {
return;
}
BtObject btObject = e->getBtRegistry()->get(btContext->getInfoHashAsString());
const BDE& torrentAttrs =
requestGroup->getDownloadContext()->getAttribute(bittorrent::BITTORRENT);
BtObject btObject =
e->getBtRegistry()->get(torrentAttrs[bittorrent::INFO_HASH].s());
SharedHandle<PieceStorage> pieceStorage = btObject._pieceStorage;
SharedHandle<PeerStorage> peerStorage = btObject._peerStorage;
SharedHandle<BtRuntime> btRuntime = btObject._btRuntime;
@ -80,7 +83,7 @@ void BtSetup::setup(std::deque<Command*>& commands,
// commands
{
TrackerWatcherCommand* c =
new TrackerWatcherCommand(e->newCUID(), requestGroup, e, btContext);
new TrackerWatcherCommand(e->newCUID(), requestGroup, e);
c->setPeerStorage(peerStorage);
c->setPieceStorage(pieceStorage);
c->setBtRuntime(btRuntime);
@ -90,7 +93,7 @@ void BtSetup::setup(std::deque<Command*>& commands,
}
{
PeerChokeCommand* c =
new PeerChokeCommand(e->newCUID(), e, btContext);
new PeerChokeCommand(e->newCUID(), e);
c->setPeerStorage(peerStorage);
c->setBtRuntime(btRuntime);
@ -98,8 +101,7 @@ void BtSetup::setup(std::deque<Command*>& commands,
}
{
ActivePeerConnectionCommand* c =
new ActivePeerConnectionCommand(e->newCUID(), requestGroup, e, btContext,
10);
new ActivePeerConnectionCommand(e->newCUID(), requestGroup, e, 10);
c->setBtRuntime(btRuntime);
c->setPieceStorage(pieceStorage);
c->setPeerStorage(peerStorage);
@ -108,11 +110,11 @@ void BtSetup::setup(std::deque<Command*>& commands,
commands.push_back(c);
}
if(!btContext->isPrivate() && DHTSetup::initialized()) {
DHTRegistry::_peerAnnounceStorage->addPeerAnnounce(btContext->getInfoHash(),
peerStorage);
if(torrentAttrs[bittorrent::PRIVATE].i() == 0 && DHTSetup::initialized()) {
DHTRegistry::_peerAnnounceStorage->addPeerAnnounce
(torrentAttrs[bittorrent::INFO_HASH].uc(), peerStorage);
DHTGetPeersCommand* command =
new DHTGetPeersCommand(e->newCUID(), requestGroup, e, btContext);
new DHTGetPeersCommand(e->newCUID(), requestGroup, e);
command->setTaskQueue(DHTRegistry::_taskQueue);
command->setTaskFactory(DHTRegistry::_taskFactory);
command->setBtRuntime(btRuntime);
@ -129,7 +131,7 @@ void BtSetup::setup(std::deque<Command*>& commands,
if(ratio > 0.0) {
SharedHandle<ShareRatioSeedCriteria> cri
(new ShareRatioSeedCriteria(option->getAsDouble(PREF_SEED_RATIO),
btContext));
requestGroup->getDownloadContext()));
cri->setPieceStorage(pieceStorage);
cri->setPeerStorage(peerStorage);
@ -138,7 +140,7 @@ void BtSetup::setup(std::deque<Command*>& commands,
}
if(unionCri->getSeedCriterion().size() > 0) {
SeedCheckCommand* c =
new SeedCheckCommand(e->newCUID(), requestGroup, e, btContext, unionCri);
new SeedCheckCommand(e->newCUID(), requestGroup, e, unionCri);
c->setPieceStorage(pieceStorage);
c->setBtRuntime(btRuntime);
commands.push_back(c);

View File

@ -39,6 +39,7 @@
#include "Logger.h"
#include "message.h"
#include "prefs.h"
#include "DownloadContext.h"
namespace aria2 {
@ -62,13 +63,13 @@ bool CheckIntegrityCommand::executeInternal()
if(_requestGroup->downloadFinished()) {
logger->notice(MSG_VERIFICATION_SUCCESSFUL,
_requestGroup->getFilePath().c_str());
_requestGroup->getDownloadContext()->getBasePath().c_str());
std::deque<Command*> commands;
_entry->onDownloadFinished(commands, _e);
_e->addCommand(commands);
} else {
logger->error(MSG_VERIFICATION_FAILED,
_requestGroup->getFilePath().c_str());
_requestGroup->getDownloadContext()->getBasePath().c_str());
std::deque<Command*> commands;
_entry->onDownloadIncomplete(commands,_e);
_e->addCommand(commands);
@ -86,7 +87,7 @@ bool CheckIntegrityCommand::handleException(Exception& e)
_e->_checkIntegrityMan->dropPickedEntry();
logger->error(MSG_FILE_VALIDATION_FAILURE, e, cuid);
logger->error(MSG_DOWNLOAD_NOT_COMPLETE,
cuid, _requestGroup->getFilePath().c_str());
cuid, _requestGroup->getDownloadContext()->getBasePath().c_str());
return true;
}

View File

@ -41,6 +41,7 @@
#include "FileAllocationEntry.h"
#include "DownloadEngine.h"
#include "Option.h"
#include "DownloadContext.h"
namespace aria2 {

View File

@ -34,7 +34,7 @@
/* copyright --> */
#include "ChecksumCheckIntegrityEntry.h"
#include "RequestGroup.h"
#include "SingleFileDownloadContext.h"
#include "DownloadContext.h"
#include "FileEntry.h"
#include "IteratableChecksumValidator.h"
#include "DownloadEngine.h"
@ -49,18 +49,15 @@ ChecksumCheckIntegrityEntry::~ChecksumCheckIntegrityEntry() {}
bool ChecksumCheckIntegrityEntry::isValidationReady()
{
SingleFileDownloadContextHandle dctx
(dynamic_pointer_cast<SingleFileDownloadContext>(_requestGroup->getDownloadContext()));
return !dctx.isNull() && dctx->getChecksum().size() > 0 &&
dctx->getChecksumHashAlgo().size() > 0;
return !_requestGroup->getDownloadContext()->getChecksum().empty() &&
!_requestGroup->getDownloadContext()->getChecksumHashAlgo().empty();
}
void ChecksumCheckIntegrityEntry::initValidator()
{
SingleFileDownloadContextHandle dctx
(dynamic_pointer_cast<SingleFileDownloadContext>(_requestGroup->getDownloadContext()));
_validator.reset(new IteratableChecksumValidator(dctx,
_requestGroup->getPieceStorage()));
_validator.reset(new IteratableChecksumValidator
(_requestGroup->getDownloadContext(),
_requestGroup->getPieceStorage()));
_validator->init();
}

View File

@ -57,8 +57,9 @@
#include "CheckIntegrityMan.h"
#include "CheckIntegrityEntry.h"
#include "Util.h"
#include "DownloadContext.h"
#ifdef ENABLE_BITTORRENT
# include "BtContext.h"
# include "bittorrent_helper.h"
# include "Peer.h"
# include "PeerStorage.h"
# include "BtRegistry.h"
@ -83,10 +84,8 @@ static void printProgress
<< "#" << rg->getGID() << " ";
#ifdef ENABLE_BITTORRENT
SharedHandle<BtContext> btctx =
dynamic_pointer_cast<BtContext>(rg->getDownloadContext());
if(!btctx.isNull() && rg->downloadFinished()) {
if(rg->getDownloadContext()->hasAttribute(bittorrent::BITTORRENT) &&
rg->downloadFinished()) {
o << "SEEDING" << "(" << "ratio:";
if(rg->getCompletedLength() > 0) {
o << std::fixed << std::setprecision(1)
@ -114,9 +113,11 @@ static void printProgress
<< "CN:"
<< rg->getNumConnection();
#ifdef ENABLE_BITTORRENT
if(!btctx.isNull()) {
if(rg->getDownloadContext()->hasAttribute(bittorrent::BITTORRENT)) {
const BDE& torrentAttrs =
rg->getDownloadContext()->getAttribute(bittorrent::BITTORRENT);
SharedHandle<PeerStorage> ps =
e->getBtRegistry()->get(btctx->getInfoHashAsString())._peerStorage;
e->getBtRegistry()->get(torrentAttrs[bittorrent::INFO_HASH].s())._peerStorage;
std::deque<SharedHandle<Peer> > peers;
ps->getActivePeers(peers);
o << " " << "SEED:"
@ -155,7 +156,7 @@ public:
{
const char SEP_CHAR = '-';
printProgress(std::cout, rg, _e);
const std::deque<SharedHandle<FileEntry> >& fileEntries =
const std::vector<SharedHandle<FileEntry> >& fileEntries =
rg->getDownloadContext()->getFileEntries();
std::cout << "\n"
<< "FILE: ";

View File

@ -34,9 +34,9 @@
/* copyright --> */
#include "ContentTypeRequestGroupCriteria.h"
#include "RequestGroup.h"
#include "SingleFileDownloadContext.h"
#include "Util.h"
#include "FileEntry.h"
#include "DownloadContext.h"
namespace aria2 {
@ -49,15 +49,15 @@ ContentTypeRequestGroupCriteria::~ContentTypeRequestGroupCriteria() {}
bool ContentTypeRequestGroupCriteria::match(const RequestGroup* requestGroup) const
{
if(forwardMatch(requestGroup->getFilePath(), _extensions)) {
if(requestGroup->getDownloadContext()->getFileEntries().size() != 1) {
return false;
}
if(forwardMatch(requestGroup->getFirstFilePath(), _extensions)) {
return true;
} else {
SingleFileDownloadContextHandle dctx(dynamic_pointer_cast<SingleFileDownloadContext>(requestGroup->getDownloadContext()));
if(dctx.isNull()) {
return false;
} else {
return exactMatch(dctx->getContentType(), _contentTypes);
}
return exactMatch
(requestGroup->getDownloadContext()->getFirstFileEntry()->getContentType(),
_contentTypes);
}
}

View File

@ -40,21 +40,20 @@
#include "RequestGroup.h"
#include "DHTNode.h"
#include "DHTNodeLookupEntry.h"
#include "BtContext.h"
#include "BtRuntime.h"
#include "PeerStorage.h"
#include "Peer.h"
#include "Logger.h"
#include "bittorrent_helper.h"
#include "DownloadContext.h"
namespace aria2 {
DHTGetPeersCommand::DHTGetPeersCommand(int32_t cuid,
RequestGroup* requestGroup,
DownloadEngine* e,
const BtContextHandle& ctx):
DownloadEngine* e):
Command(cuid),
_requestGroup(requestGroup),
_btContext(ctx),
_e(e),
_numRetry(0),
_lastGetPeerTime(0)
@ -76,9 +75,9 @@ bool DHTGetPeersCommand::execute()
((_numRetry > 0 && _lastGetPeerTime.elapsed(RETRY_INTERVAL)) ||
_lastGetPeerTime.elapsed(GET_PEER_INTERVAL))) {
logger->debug("Issuing PeerLookup for infoHash=%s",
_btContext->getInfoHashAsString().c_str());
_task = _taskFactory->createPeerLookupTask(_btContext, _btRuntime,
_peerStorage);
bittorrent::getInfoHashString(_requestGroup->getDownloadContext()).c_str());
_task = _taskFactory->createPeerLookupTask
(_requestGroup->getDownloadContext(), _btRuntime, _peerStorage);
_taskQueue->addPeriodicTask2(_task);
} else if(!_task.isNull() && _task->finished()) {
_lastGetPeerTime.reset();

View File

@ -47,7 +47,6 @@ class DHTTaskFactory;
class DHTTask;
class DownloadEngine;
class RequestGroup;
class BtContext;
class BtRuntime;
class PeerStorage;
@ -61,8 +60,6 @@ private:
RequestGroup* _requestGroup;
SharedHandle<BtContext> _btContext;
SharedHandle<BtRuntime> _btRuntime;
SharedHandle<PeerStorage> _peerStorage;
@ -80,8 +77,7 @@ private:
Time _lastGetPeerTime;
public:
DHTGetPeersCommand(int32_t cuid, RequestGroup* requestGroup,
DownloadEngine* e,
const SharedHandle<BtContext>& ctx);
DownloadEngine* e);
virtual ~DHTGetPeersCommand();

View File

@ -43,15 +43,15 @@
#include "DHTMessageCallback.h"
#include "PeerStorage.h"
#include "BtRuntime.h"
#include "BtContext.h"
#include "Util.h"
#include "DHTBucket.h"
#include "bittorrent_helper.h"
namespace aria2 {
DHTPeerLookupTask::DHTPeerLookupTask(const SharedHandle<BtContext>& btContext):
DHTAbstractNodeLookupTask(btContext->getInfoHash()),
_ctx(btContext) {}
DHTPeerLookupTask::DHTPeerLookupTask
(const SharedHandle<DownloadContext>& downloadContext):
DHTAbstractNodeLookupTask(bittorrent::getInfoHash(downloadContext)) {}
void
DHTPeerLookupTask::getNodesFromMessage(std::deque<SharedHandle<DHTNode> >& nodes,
@ -92,11 +92,11 @@ void DHTPeerLookupTask::onFinish()
if((*i)->_used) {
const SharedHandle<DHTNode>& node = (*i)->_node;
SharedHandle<DHTMessage> m =
_factory->createAnnouncePeerMessage(node,
_ctx->getInfoHash(),
_btRuntime->getListenPort(),
_tokenStorage[Util::toHex(node->getID(),
DHT_ID_LENGTH)]);
_factory->createAnnouncePeerMessage
(node,
_targetID, // this is infoHash
_btRuntime->getListenPort(),
_tokenStorage[Util::toHex(node->getID(), DHT_ID_LENGTH)]);
_dispatcher->addMessageToQueue(m);
}
}

View File

@ -40,7 +40,7 @@
namespace aria2 {
class BtContext;
class DownloadContext;
class Peer;
class PeerStorage;
class BtRuntime;
@ -51,13 +51,11 @@ private:
std::deque<SharedHandle<Peer> > _peers;
SharedHandle<BtContext> _ctx;
SharedHandle<PeerStorage> _peerStorage;
SharedHandle<BtRuntime> _btRuntime;
public:
DHTPeerLookupTask(const SharedHandle<BtContext>& btContext);
DHTPeerLookupTask(const SharedHandle<DownloadContext>& downloadContext);
virtual void getNodesFromMessage(std::deque<SharedHandle<DHTNode> >& nodes,
const SharedHandle<DHTMessage>& message);

View File

@ -40,7 +40,7 @@
namespace aria2 {
class BtContext;
class DownloadContext;
class BtRuntime;
class PeerStorage;
class DHTTask;
@ -61,7 +61,7 @@ public:
virtual SharedHandle<DHTTask> createBucketRefreshTask() = 0;
virtual SharedHandle<DHTTask>
createPeerLookupTask(const SharedHandle<BtContext>& ctx,
createPeerLookupTask(const SharedHandle<DownloadContext>& ctx,
const SharedHandle<BtRuntime>& btRuntime,
const SharedHandle<PeerStorage>& peerStorage) = 0;

View File

@ -47,7 +47,6 @@
#include "DHTReplaceNodeTask.h"
#include "Peer.h"
#include "DHTNodeLookupEntry.h"
#include "BtContext.h"
#include "PeerStorage.h"
#include "BtRuntime.h"
#include "DHTMessageCallback.h"
@ -85,7 +84,7 @@ DHTTaskFactoryImpl::createBucketRefreshTask()
SharedHandle<DHTTask>
DHTTaskFactoryImpl::createPeerLookupTask
(const SharedHandle<BtContext>& ctx,
(const SharedHandle<DownloadContext>& ctx,
const SharedHandle<BtRuntime>& btRuntime,
const SharedHandle<PeerStorage>& peerStorage)
{

View File

@ -77,7 +77,7 @@ public:
virtual SharedHandle<DHTTask> createBucketRefreshTask();
virtual SharedHandle<DHTTask>
createPeerLookupTask(const SharedHandle<BtContext>& ctx,
createPeerLookupTask(const SharedHandle<DownloadContext>& ctx,
const SharedHandle<BtRuntime>& btRuntime,
const SharedHandle<PeerStorage>& peerStorage);

View File

@ -41,7 +41,7 @@
#include "DlAbortEx.h"
#include "message.h"
#include "SimpleRandomizer.h"
#include "BtContext.h"
#include "DownloadContext.h"
#include "PieceStorage.h"
#include "BtRuntime.h"
#include "PeerStorage.h"
@ -51,19 +51,21 @@
#include "A2STR.h"
#include "Request.h"
#include "bencode.h"
#include "bittorrent_helper.h"
namespace aria2 {
DefaultBtAnnounce::DefaultBtAnnounce(const BtContextHandle& btContext,
const Option* option):
btContext(btContext),
DefaultBtAnnounce::DefaultBtAnnounce
(const SharedHandle<DownloadContext>& downloadContext,
const Option* option):
_downloadContext(downloadContext),
trackers(0),
interval(DEFAULT_ANNOUNCE_INTERVAL),
minInterval(DEFAULT_ANNOUNCE_INTERVAL),
_userDefinedInterval(0),
complete(0),
incomplete(0),
announceList(btContext->getAnnounceTiers()),
announceList(downloadContext->getAttribute(bittorrent::BITTORRENT)[bittorrent::ANNOUNCE_LIST]),
option(option),
logger(LogFactory::getInstance()),
_randomizer(SimpleRandomizer::getInstance())
@ -145,10 +147,10 @@ std::string DefaultBtAnnounce::getAnnounceUrl() {
std::string url = announceList.getAnnounce();
url += uriHasQuery(url) ? "&" : "?";
url += "info_hash=";
url += Util::torrentUrlencode(btContext->getInfoHash(),
btContext->getInfoHashLength());
url += Util::torrentUrlencode(bittorrent::getInfoHash(_downloadContext),
INFO_HASH_LENGTH);
url += "&peer_id=";
url += Util::torrentUrlencode(btContext->getPeerId(), 20);
url += Util::torrentUrlencode(bittorrent::getStaticPeerId(), PEER_ID_LENGTH);
url += "&uploaded=";
url += Util::uitos(stat.getSessionUploadLength());
url += "&downloaded=";

View File

@ -41,7 +41,7 @@
namespace aria2 {
class BtContext;
class DownloadContext;
class Option;
class Logger;
class BtRuntime;
@ -51,7 +51,7 @@ class Randomizer;
class DefaultBtAnnounce : public BtAnnounce {
private:
SharedHandle<BtContext> btContext;
SharedHandle<DownloadContext> _downloadContext;
unsigned int trackers;
Time prevAnnounceTime;
time_t interval;
@ -69,7 +69,7 @@ private:
SharedHandle<PieceStorage> pieceStorage;
SharedHandle<PeerStorage> peerStorage;
public:
DefaultBtAnnounce(const SharedHandle<BtContext>& btContext,
DefaultBtAnnounce(const SharedHandle<DownloadContext>& downloadContext,
const Option* option);
virtual ~DefaultBtAnnounce();

View File

@ -1,577 +0,0 @@
/* <!-- 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
*
* 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 "DefaultBtContext.h"
#include <cstring>
#include <ostream>
#include <functional>
#include <algorithm>
#include <vector>
#include "DlAbortEx.h"
#include "Util.h"
#include "MessageDigestHelper.h"
#include "a2netcompat.h"
#include "AnnounceTier.h"
#include "SimpleRandomizer.h"
#include "LogFactory.h"
#include "Logger.h"
#include "FileEntry.h"
#include "message.h"
#include "PeerMessageUtil.h"
#include "StringFormat.h"
#include "A2STR.h"
#include "bencode.h"
#include "a2functional.h"
namespace aria2 {
const std::string DefaultBtContext::C_NAME("name");
const std::string DefaultBtContext::C_NAME_UTF8("name.utf-8");
const std::string DefaultBtContext::C_FILES("files");
const std::string DefaultBtContext::C_LENGTH("length");
const std::string DefaultBtContext::C_PATH("path");
const std::string DefaultBtContext::C_PATH_UTF8("path.utf-8");
const std::string DefaultBtContext::C_INFO("info");
const std::string DefaultBtContext::C_PIECES("pieces");
const std::string DefaultBtContext::C_PIECE_LENGTH("piece length");
const std::string DefaultBtContext::C_PRIVATE("private");
const std::string DefaultBtContext::C_PRIVATE_ON("1");
const std::string DefaultBtContext::C_URL_LIST("url-list");
const std::string DefaultBtContext::C_ANNOUNCE("announce");
const std::string DefaultBtContext::C_ANNOUNCE_LIST("announce-list");
const std::string DefaultBtContext::C_NODES("nodes");
const std::string DefaultBtContext::DEFAULT_PEER_ID_PREFIX("-aria2-");
DefaultBtContext::DefaultBtContext():_peerIdPrefix(DEFAULT_PEER_ID_PREFIX),
_randomizer(SimpleRandomizer::getInstance()),
_ownerRequestGroup(0),
_logger(LogFactory::getInstance()) {}
DefaultBtContext::~DefaultBtContext() {}
std::string DefaultBtContext::generatePeerId() const {
std::string peerId = _peerIdPrefix;
peerId += Util::randomAlpha(20-_peerIdPrefix.size(), _randomizer);
if(peerId.size() > 20) {
peerId.erase(20);
}
return peerId;
}
const unsigned char* DefaultBtContext::getInfoHash() const {
return infoHash;
}
size_t DefaultBtContext::getInfoHashLength() const {
return INFO_HASH_LENGTH;
}
const std::string& DefaultBtContext::getInfoHashAsString() const {
return infoHashString;
}
void DefaultBtContext::clear() {
memset(infoHash, 0, INFO_HASH_LENGTH);
infoHashString = A2STR::NIL;
pieceHashes.clear();
_fileEntries.clear();
totalLength = 0;
pieceLength = 0;
fileMode = BtContext::SINGLE;
numPieces = 0;
name = A2STR::NIL;
announceTiers.clear();
_private = false;
}
void DefaultBtContext::extractPieceHash(const std::string& hashData,
size_t hashLength)
{
size_t numPieces = hashData.size()/hashLength;
for(size_t i = 0; i < numPieces; ++i) {
pieceHashes.push_back(Util::toHex(hashData.data()+i*hashLength,
hashLength));
}
}
void DefaultBtContext::extractFileEntries(const BDE& infoDict,
const std::string& defaultName,
const std::string& overrideName,
const std::deque<std::string>& urlList)
{
if(overrideName.empty()) {
std::string nameKey;
if(infoDict.containsKey(C_NAME_UTF8)) {
nameKey = C_NAME_UTF8;
} else {
nameKey = C_NAME;
}
const BDE& nameData = infoDict[nameKey];
if(nameData.isString()) {
name = nameData.s();
} else {
name = strconcat(File(defaultName).getBasename(), ".file");
}
} else {
name = overrideName;
}
const BDE& filesList = infoDict[C_FILES];
if(filesList.isList()) {
uint64_t length = 0;
off_t offset = 0;
// multi-file mode
fileMode = BtContext::MULTI;
for(BDE::List::const_iterator itr = filesList.listBegin();
itr != filesList.listEnd(); ++itr) {
const BDE& fileDict = *itr;
if(!fileDict.isDict()) {
continue;
}
const BDE& fileLengthData = fileDict[C_LENGTH];
if(!fileLengthData.isInteger()) {
throw DL_ABORT_EX(StringFormat(MSG_MISSING_BT_INFO,
C_LENGTH.c_str()).str());
}
length += fileLengthData.i();
std::string pathKey;
if(fileDict.containsKey(C_PATH_UTF8)) {
pathKey = C_PATH_UTF8;
} else {
pathKey = C_PATH;
}
const BDE& pathList = fileDict[pathKey];
if(!pathList.isList() || pathList.empty()) {
throw DL_ABORT_EX("Path is empty.");
}
std::vector<std::string> pathelem(pathList.size());
std::transform(pathList.listBegin(), pathList.listEnd(), pathelem.begin(),
std::mem_fun_ref(&BDE::s));
std::string path = name;
strappend(path, "/", Util::joinPath(pathelem.begin(), pathelem.end()));
// Split path with '/' again because each pathList element can
// contain "/" inside.
std::deque<std::string> elements;
Util::slice(elements, path, '/');
path = Util::joinPath(elements.begin(), elements.end());
std::deque<std::string> uris;
std::transform(urlList.begin(), urlList.end(), std::back_inserter(uris),
std::bind2nd(std::plus<std::string>(), "/"+path));
FileEntryHandle fileEntry(new FileEntry(strconcat(_dir, "/", path),
fileLengthData.i(),
offset, uris));
_fileEntries.push_back(fileEntry);
offset += fileEntry->getLength();
}
totalLength = length;
} else {
// single-file mode;
fileMode = BtContext::SINGLE;
const BDE& lengthData = infoDict[C_LENGTH];
if(!lengthData.isInteger()) {
throw DL_ABORT_EX(StringFormat(MSG_MISSING_BT_INFO,
C_LENGTH.c_str()).str());
}
totalLength = lengthData.i();
// Slice path by '/' just in case nasty ".." is included in name
std::deque<std::string> pathelems;
Util::slice(pathelems, name, '/');
// For each uri in urlList, if it ends with '/', then
// concatenate name to it. Specification just says so.
std::deque<std::string> uris;
for(std::deque<std::string>::const_iterator i = urlList.begin();
i != urlList.end(); ++i) {
if(Util::endsWith(*i, "/")) {
uris.push_back((*i)+name);
} else {
uris.push_back(*i);
}
}
SharedHandle<FileEntry> fileEntry
(new FileEntry(strconcat(_dir, "/",
Util::joinPath(pathelems.begin(),
pathelems.end())),
totalLength, 0, uris));
_fileEntries.push_back(fileEntry);
}
}
void DefaultBtContext::extractAnnounceURI(const BDE& announceData)
{
// Assumed announceData is string
std::deque<std::string> urls;
urls.push_back(Util::trim(announceData.s()));
announceTiers.push_back(AnnounceTierHandle(new AnnounceTier(urls)));
}
void DefaultBtContext::extractAnnounceList(const BDE& announceList)
{
// Assumed announceList is string
for(BDE::List::const_iterator itr = announceList.listBegin();
itr != announceList.listEnd(); ++itr) {
const BDE& elemList = *itr;
if(!elemList.isList()) {
continue;
}
std::deque<std::string> urls;
for(BDE::List::const_iterator elemItr = elemList.listBegin();
elemItr != elemList.listEnd(); ++elemItr) {
const BDE& url = (*elemItr);
if(url.isString()) {
urls.push_back(Util::trim(url.s()));
}
}
if(!urls.empty()) {
AnnounceTierHandle tier(new AnnounceTier(urls));
announceTiers.push_back(tier);
}
}
}
void DefaultBtContext::extractAnnounce(const BDE& rootDict)
{
const BDE& announceList = rootDict[C_ANNOUNCE_LIST];
if(announceList.isList()) {
extractAnnounceList(announceList);
} else {
const BDE& announce = rootDict[C_ANNOUNCE];
if(announce.isString()) {
extractAnnounceURI(announce);
}
}
}
void DefaultBtContext::extractUrlList(std::deque<std::string>& uris,
const BDE& bde)
{
if(bde.isList()) {
for(BDE::List::const_iterator itr = bde.listBegin();
itr != bde.listEnd(); ++itr) {
if((*itr).isString()) {
uris.push_back((*itr).s());
}
}
} else if(bde.isString()) {
uris.push_back(bde.s());
}
}
void DefaultBtContext::extractNodes(const BDE& nodesList)
{
if(!nodesList.isList()) {
return;
}
for(BDE::List::const_iterator i = nodesList.listBegin();
i != nodesList.listEnd(); ++i) {
const BDE& addrPairList = (*i);
if(!addrPairList.isList() || addrPairList.size() != 2) {
continue;
}
const BDE& hostname = addrPairList[0];
if(!hostname.isString()) {
continue;
}
if(Util::trim(hostname.s()).empty()) {
continue;
}
const BDE& port = addrPairList[1];
if(!port.isInteger() || !(0 < port.i() && port.i() < 65536)) {
continue;
}
_nodes.push_back(std::pair<std::string, uint16_t>(hostname.s(), port.i()));
}
}
void DefaultBtContext::loadFromMemory(const unsigned char* content,
size_t length,
const std::string& defaultName,
const std::string& overrideName)
{
processRootDictionary(bencode::decode(content, length), defaultName,
overrideName);
}
void DefaultBtContext::load(const std::string& torrentFile,
const std::string& overrideName) {
processRootDictionary(bencode::decodeFromFile(torrentFile), torrentFile,
overrideName);
}
void DefaultBtContext::processRootDictionary(const BDE& rootDict,
const std::string& defaultName,
const std::string& overrideName)
{
clear();
if(!rootDict.isDict()) {
throw DL_ABORT_EX("torrent file does not contain a root dictionary.");
}
const BDE& infoDict = rootDict[C_INFO];
if(!infoDict.isDict()) {
throw DL_ABORT_EX(StringFormat(MSG_MISSING_BT_INFO,
C_INFO.c_str()).str());
}
// retrieve infoHash
std::string encodedInfoDict = bencode::encode(infoDict);
MessageDigestHelper::digest(infoHash, INFO_HASH_LENGTH,
MessageDigestContext::SHA1,
encodedInfoDict.data(),
encodedInfoDict.size());
infoHashString = Util::toHex(infoHash, INFO_HASH_LENGTH);
// calculate the number of pieces
const BDE& piecesData = infoDict[C_PIECES];
if(!piecesData.isString()) {
throw DL_ABORT_EX(StringFormat(MSG_MISSING_BT_INFO,
C_PIECES.c_str()).str());
}
if(piecesData.s().empty()) {
throw DL_ABORT_EX("The length of piece hash is 0.");
}
numPieces = piecesData.s().size()/PIECE_HASH_LENGTH;
if(numPieces == 0) {
throw DL_ABORT_EX("The number of pieces is 0.");
}
// retrieve piece length
const BDE& pieceLengthData = infoDict[C_PIECE_LENGTH];
if(!pieceLengthData.isInteger()) {
throw DL_ABORT_EX(StringFormat(MSG_MISSING_BT_INFO,
C_PIECE_LENGTH.c_str()).str());
}
pieceLength = pieceLengthData.i();
// retrieve piece hashes
extractPieceHash(piecesData.s(), PIECE_HASH_LENGTH);
// private flag
const BDE& privateData = infoDict[C_PRIVATE];
if(privateData.isInteger()) {
_private = (privateData.i() == 1);
}
// retrieve uri-list.
// This implemantation obeys HTTP-Seeding specification:
// see http://www.getright.com/seedtorrent.html
std::deque<std::string> urlList;
extractUrlList(urlList, rootDict[C_URL_LIST]);
// retrieve file entries
extractFileEntries(infoDict, defaultName, overrideName, urlList);
if((totalLength+pieceLength-1)/pieceLength != numPieces) {
throw DL_ABORT_EX("Too few/many piece hash.");
}
// retrieve announce
extractAnnounce(rootDict);
// retrieve nodes
extractNodes(rootDict[C_NODES]);
}
const std::string& DefaultBtContext::getPieceHash(size_t index) const {
if(index < numPieces) {
return pieceHashes[index];
} else {
return A2STR::NIL;
}
}
uint64_t DefaultBtContext::getTotalLength() const {
return totalLength;
}
bool DefaultBtContext::knowsTotalLength() const
{
return true;
}
BtContext::FILE_MODE DefaultBtContext::getFileMode() const {
return fileMode;
}
const std::string& DefaultBtContext::getPieceHashAlgo() const
{
return MessageDigestContext::SHA1;
}
const std::deque<SharedHandle<AnnounceTier> >&
DefaultBtContext::getAnnounceTiers() const
{
return announceTiers;
}
const std::string& DefaultBtContext::getName() const {
return name;
}
size_t DefaultBtContext::getPieceLength() const {
return pieceLength;
}
size_t DefaultBtContext::getNumPieces() const {
return numPieces;
}
std::string DefaultBtContext::getActualBasePath() const
{
if(_fileEntries.size() == 1) {
return _fileEntries.front()->getPath();
} else {
return strconcat(_dir, "/", name);
}
}
void DefaultBtContext::computeFastSet
(std::deque<size_t>& fastSet, const std::string& ipaddr, size_t fastSetSize)
{
unsigned char compact[6];
if(!PeerMessageUtil::createcompact(compact, ipaddr, 0)) {
return;
}
if(numPieces < fastSetSize) {
fastSetSize = numPieces;
}
unsigned char tx[24];
memcpy(tx, compact, 4);
if((tx[0] & 0x80) == 0 || (tx[0] & 0x40) == 0) {
tx[2] = 0x00;
tx[3] = 0x00;
} else {
tx[3] = 0x00;
}
memcpy(tx+4, infoHash, 20);
unsigned char x[20];
MessageDigestHelper::digest(x, sizeof(x), MessageDigestContext::SHA1, tx, 24);
while(fastSet.size() < fastSetSize) {
for(size_t i = 0; i < 5 && fastSet.size() < fastSetSize; ++i) {
size_t j = i*4;
uint32_t ny;
memcpy(&ny, x+j, 4);
uint32_t y = ntohl(ny);
size_t index = y%numPieces;
if(std::find(fastSet.begin(), fastSet.end(), index) == fastSet.end()) {
fastSet.push_back(index);
}
}
unsigned char temp[20];
MessageDigestHelper::digest(temp, sizeof(temp), MessageDigestContext::SHA1, x, sizeof(x));
memcpy(x, temp, sizeof(x));
}
}
std::ostream& operator<<(std::ostream& o, const DefaultBtContext& ctx)
{
o << "*** BitTorrent File Information ***" << "\n";
o << "Mode: " << (ctx.getFileMode() == DownloadContext::SINGLE ? "Single File Torrent":"Multi File Torrent") << "\n";
o << "Announce:" << "\n";
AnnounceTiers tiers = ctx.getAnnounceTiers();
for(AnnounceTiers::const_iterator itr = tiers.begin(); itr != tiers.end(); ++itr) {
const AnnounceTierHandle& tier = *itr;
for(std::deque<std::string>::const_iterator uriItr = tier->urls.begin(); uriItr != tier->urls.end(); ++uriItr) {
o << " " << *uriItr;
}
o << "\n";
}
o << "Info Hash: " << ctx.getInfoHashAsString() << "\n";
o << "Piece Length: " << Util::abbrevSize(ctx.getPieceLength()) << "B\n";
o << "The Number of Pieces: " << ctx.getNumPieces() << "\n";
o << "Total Length: " << Util::abbrevSize(ctx.getTotalLength()) << "B\n";
if(ctx.getFileMode() == DownloadContext::MULTI) {
o << "Name: " << ctx.getName() << "\n";
}
Util::toStream(o, ctx.getFileEntries());
return o;
}
void DefaultBtContext::setRandomizer(const RandomizerHandle& randomizer)
{
_randomizer = randomizer;
}
std::deque<std::pair<std::string, uint16_t> >&
DefaultBtContext::getNodes()
{
return _nodes;
}
void DefaultBtContext::setInfoHash(const unsigned char* infoHash)
{
memcpy(this->infoHash, infoHash, sizeof(this->infoHash));
}
void DefaultBtContext::setFilePathWithIndex
(size_t index, const std::string& path)
{
if(0 < index && index <= _fileEntries.size()) {
_fileEntries[index-1]->setPath(path);
} else {
throw DL_ABORT_EX(StringFormat("No such file with index=%u",
static_cast<unsigned int>(index)).str());
}
}
void DefaultBtContext::setFileFilter(IntSequence seq)
{
std::deque<int32_t> fileIndexes = seq.flush();
std::sort(fileIndexes.begin(), fileIndexes.end());
fileIndexes.erase(std::unique(fileIndexes.begin(), fileIndexes.end()),
fileIndexes.end());
bool selectAll = fileIndexes.empty() || _fileEntries.size() == 1;
int32_t index = 1;
for(std::deque<SharedHandle<FileEntry> >::const_iterator i =
_fileEntries.begin(); i != _fileEntries.end(); ++i, ++index) {
(*i)->setRequested
(selectAll ||
std::binary_search(fileIndexes.begin(), fileIndexes.end(), index));
}
}
} // namespace aria2

View File

@ -1,232 +0,0 @@
/* <!-- 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
*
* In addition, as a special exception, the copyright holders give
* permission to link the code of portions of this program with the
* OpenSSL library under certain conditions as described in each
* individual source file, and distribute linked combinations
* including the two.
* You must obey the GNU General Public License in all respects
* for all of the code used other than OpenSSL. If you modify
* file(s) with this exception, you may extend this exception to your
* version of the file(s), but you are not obligated to do so. If you
* do not wish to do so, delete this exception statement from your
* version. If you delete this exception statement from all source
* files in the program, then also delete it here.
*/
/* copyright --> */
#ifndef _D_DEFAULT_BT_CONTEXT_H_
#define _D_DEFAULT_BT_CONTEXT_H_
#include "BtContext.h"
#include <iosfwd>
#include "IntSequence.h"
namespace aria2 {
class Randomizer;
class Logger;
class BDE;
#define INFO_HASH_LENGTH 20
#define PIECE_HASH_LENGTH 20
class DefaultBtContext : public BtContext {
private:
unsigned char infoHash[INFO_HASH_LENGTH];
std::string infoHashString;
std::deque<std::string> pieceHashes;
FILE_MODE fileMode;
uint64_t totalLength;
size_t pieceLength;
std::string name;
size_t numPieces;
std::string peerId;
std::string _peerIdPrefix;
std::deque<SharedHandle<AnnounceTier> > announceTiers;
std::deque<std::pair<std::string, uint16_t> > _nodes;
SharedHandle<Randomizer> _randomizer;
RequestGroup* _ownerRequestGroup;
Logger* _logger;
static const std::string C_NAME;
static const std::string C_NAME_UTF8;
static const std::string C_FILES;
static const std::string C_LENGTH;
static const std::string C_PATH;
static const std::string C_PATH_UTF8;
static const std::string C_INFO;
static const std::string C_PIECES;
static const std::string C_PIECE_LENGTH;
static const std::string C_PRIVATE;
// This is just a string "1". Used as a value of "private" flag.
static const std::string C_PRIVATE_ON;
static const std::string C_URL_LIST;
static const std::string C_ANNOUNCE;
static const std::string C_ANNOUNCE_LIST;
static const std::string C_NODES;
void clear();
void extractPieceHash(const std::string& hashData, size_t hashLength);
void extractFileEntries(const BDE& infoDic,
const std::string& defaultName,
const std::string& overrideName,
const std::deque<std::string>& urlList);
void extractAnnounceURI(const BDE& announceData);
void extractAnnounceList(const BDE& announceListData);
void extractAnnounce(const BDE& rootDict);
void extractUrlList(std::deque<std::string>& uris, const BDE& obj);
void extractNodes(const BDE& nodes);
void processRootDictionary(const BDE& rootDic,
const std::string& defaultName,
const std::string& overrideName);
public:
DefaultBtContext();
virtual ~DefaultBtContext();
virtual const unsigned char* getInfoHash() const;
virtual size_t getInfoHashLength() const;
virtual const std::string& getInfoHashAsString() const;
virtual const std::string& getPieceHash(size_t index) const;
virtual const std::deque<std::string>& getPieceHashes() const
{
return pieceHashes;
}
virtual uint64_t getTotalLength() const;
virtual bool knowsTotalLength() const;
virtual FILE_MODE getFileMode() const;
virtual const std::string& getPieceHashAlgo() const;
virtual const std::deque<SharedHandle<AnnounceTier> >&
getAnnounceTiers() const;
// Note: Before calling load* function, call setDir() to specify
// directory. Setting directory after load* function doesn't change
// the output file path.
virtual void load(const std::string& torrentFile,
const std::string& overrideName = "");
void loadFromMemory(const unsigned char* content, size_t length,
const std::string& defaultName,
const std::string& overrideName = "");
void loadFromMemory(const std::string& context,
const std::string& defaultName,
const std::string& overrideName = "")
{
loadFromMemory(reinterpret_cast<const unsigned char*>(context.c_str()),
context.size(), defaultName, overrideName);
}
virtual const std::string& getName() const;
virtual size_t getPieceLength() const;
virtual size_t getNumPieces() const;
virtual std::string getActualBasePath() const;
virtual const unsigned char* getPeerId() {
if(peerId.empty()) {
peerId = generatePeerId();
}
return reinterpret_cast<const unsigned char*>(peerId.c_str());
}
virtual void computeFastSet
(std::deque<size_t>& fastSet, const std::string& ipaddr, size_t fastSetSize);
virtual RequestGroup* getOwnerRequestGroup()
{
return _ownerRequestGroup;
}
virtual std::deque<std::pair<std::string, uint16_t> >& getNodes();
virtual void setFileFilter(IntSequence seq);
std::string generatePeerId() const;
void setPeerIdPrefix(const std::string& peerIdPrefix)
{
_peerIdPrefix = peerIdPrefix;
}
// for unit test
void setInfoHash(const unsigned char* infoHash);
void setNumPieces(size_t numPieces)
{
this->numPieces = numPieces;
}
void setOwnerRequestGroup(RequestGroup* owner)
{
_ownerRequestGroup = owner;
}
void setRandomizer(const SharedHandle<Randomizer>& randomizer);
// Sets file path for spcified index. index starts from 1. The index
// is the same used in BtContext::setFileFilter(). Please note that
// path is not the actual file path. The actual file path is
// getDir()+"/"+path.
void setFilePathWithIndex(size_t index, const std::string& path);
friend std::ostream& operator<<(std::ostream& o, const DefaultBtContext& ctx);
static const std::string DEFAULT_PEER_ID_PREFIX;
};
typedef SharedHandle<DefaultBtContext> DefaultBtContextHandle;
} // namespace aria2
#endif // _D_DEFAULT_BT_CONTEXT_H_

View File

@ -54,7 +54,7 @@
#include "DHTNode.h"
#include "Peer.h"
#include "Piece.h"
#include "BtContext.h"
#include "DownloadContext.h"
#include "PieceStorage.h"
#include "PeerStorage.h"
#include "BtRuntime.h"
@ -68,13 +68,15 @@
#include "StringFormat.h"
#include "RequestGroup.h"
#include "RequestGroupMan.h"
#include "bittorrent_helper.h"
namespace aria2 {
DefaultBtInteractive::DefaultBtInteractive
(const SharedHandle<BtContext>& btContext, const SharedHandle<Peer>& peer)
(const SharedHandle<DownloadContext>& downloadContext,
const SharedHandle<Peer>& peer)
:
_btContext(btContext),
_downloadContext(downloadContext),
peer(peer),
logger(LogFactory::getInstance()),
allowedFastSetSize(10),
@ -88,8 +90,8 @@ DefaultBtInteractive::~DefaultBtInteractive() {}
void DefaultBtInteractive::initiateHandshake() {
SharedHandle<BtMessage> message =
messageFactory->createHandshakeMessage(_btContext->getInfoHash(),
_btContext->getPeerId());
messageFactory->createHandshakeMessage
(bittorrent::getInfoHash(_downloadContext), bittorrent::getStaticPeerId());
dispatcher->addMessageToQueue(message);
dispatcher->sendMessages();
}
@ -100,7 +102,7 @@ BtMessageHandle DefaultBtInteractive::receiveHandshake(bool quickReply) {
if(message.isNull()) {
return SharedHandle<BtMessage>();
}
if(memcmp(message->getPeerId(), _btContext->getPeerId(),
if(memcmp(message->getPeerId(), bittorrent::getStaticPeerId(),
PEER_ID_LENGTH) == 0) {
throw DL_ABORT_EX
(StringFormat
@ -187,9 +189,12 @@ void DefaultBtInteractive::addBitfieldMessageToQueue() {
void DefaultBtInteractive::addAllowedFastMessageToQueue() {
if(peer->isFastExtensionEnabled()) {
std::deque<size_t> fastSet;
_btContext->computeFastSet(fastSet, peer->ipaddr, allowedFastSetSize);
for(std::deque<size_t>::const_iterator itr = fastSet.begin();
std::vector<size_t> fastSet;
bittorrent::computeFastSet(fastSet, peer->ipaddr,
_downloadContext->getNumPieces(),
bittorrent::getInfoHash(_downloadContext),
allowedFastSetSize);
for(std::vector<size_t>::const_iterator itr = fastSet.begin();
itr != fastSet.end(); ++itr) {
dispatcher->addMessageToQueue
(messageFactory->createAllowedFastMessage(*itr));
@ -239,7 +244,7 @@ size_t DefaultBtInteractive::receiveMessages() {
size_t msgcount = 0;
for(int i = 0; i < 50; ++i) {
if(_requestGroupMan->doesOverallDownloadSpeedExceed() ||
_btContext->getOwnerRequestGroup()->doesDownloadSpeedExceed()) {
_downloadContext->getOwnerRequestGroup()->doesDownloadSpeedExceed()) {
break;
}
BtMessageHandle message = btMessageReceiver->receiveMessage();

View File

@ -43,7 +43,7 @@
namespace aria2 {
class BtContext;
class DownloadContext;
class BtRuntime;
class PieceStorage;
class PeerStorage;
@ -97,7 +97,7 @@ class DefaultBtInteractive : public BtInteractive {
private:
int32_t cuid;
SharedHandle<BtContext> _btContext;
SharedHandle<DownloadContext> _downloadContext;
SharedHandle<BtRuntime> _btRuntime;
@ -151,7 +151,7 @@ private:
void addPortMessageToQueue();
public:
DefaultBtInteractive(const SharedHandle<BtContext>& btContext,
DefaultBtInteractive(const SharedHandle<DownloadContext>& downloadContext,
const SharedHandle<Peer>& peer);
virtual ~DefaultBtInteractive();

View File

@ -42,7 +42,7 @@
#include "BtChokingEvent.h"
#include "BtMessageFactory.h"
#include "message.h"
#include "BtContext.h"
#include "DownloadContext.h"
#include "PeerStorage.h"
#include "PieceStorage.h"
#include "BtMessage.h"
@ -87,7 +87,7 @@ void DefaultBtMessageDispatcher::sendMessages() {
messageQueue.pop_front();
if(msg->isUploading() && !msg->isSendingInProgress()) {
if(_requestGroupMan->doesOverallUploadSpeedExceed() ||
btContext->getOwnerRequestGroup()->doesUploadSpeedExceed()) {
_downloadContext->getOwnerRequestGroup()->doesUploadSpeedExceed()) {
tempQueue.push_back(msg);
continue;
}
@ -412,9 +412,10 @@ void DefaultBtMessageDispatcher::setPeer(const SharedHandle<Peer>& peer)
this->peer = peer;
}
void DefaultBtMessageDispatcher::setBtContext(const BtContextHandle& btContext)
void DefaultBtMessageDispatcher::setDownloadContext
(const SharedHandle<DownloadContext>& downloadContext)
{
this->btContext = btContext;
_downloadContext = downloadContext;
}
void DefaultBtMessageDispatcher::setPieceStorage

View File

@ -40,7 +40,7 @@
namespace aria2 {
class BtContext;
class DownloadContext;
class PeerStorage;
class PieceStorage;
class BtMessage;
@ -55,7 +55,7 @@ private:
int32_t cuid;
std::deque<SharedHandle<BtMessage> > messageQueue;
std::deque<RequestSlot> requestSlots;
SharedHandle<BtContext> btContext;
SharedHandle<DownloadContext> _downloadContext;
SharedHandle<PeerStorage> _peerStorage;
SharedHandle<PieceStorage> _pieceStorage;
WeakHandle<BtMessageFactory> messageFactory;
@ -116,7 +116,7 @@ public:
void setPeer(const SharedHandle<Peer>& peer);
void setBtContext(const SharedHandle<BtContext>& btContext);
void setDownloadContext(const SharedHandle<DownloadContext>& downloadContext);
void setPieceStorage(const SharedHandle<PieceStorage>& pieceStorage);

View File

@ -61,11 +61,12 @@
#include "ExtensionMessage.h"
#include "Peer.h"
#include "Piece.h"
#include "BtContext.h"
#include "DownloadContext.h"
#include "PieceStorage.h"
#include "PeerStorage.h"
#include "StringFormat.h"
#include "ExtensionMessageFactory.h"
#include "bittorrent_helper.h"
namespace aria2 {
@ -112,7 +113,7 @@ DefaultBtMessageFactory::createBtMessage(const unsigned char* data, size_t dataL
{
SharedHandle<BtMessageValidator> v
(new IndexBtMessageValidator((BtHaveMessage*)msg.get(),
btContext->getNumPieces()));
_downloadContext->getNumPieces()));
msg->setBtMessageValidator(v);
}
break;
@ -121,7 +122,7 @@ DefaultBtMessageFactory::createBtMessage(const unsigned char* data, size_t dataL
{
SharedHandle<BtMessageValidator> v
(new BtBitfieldMessageValidator((BtBitfieldMessage*)msg.get(),
btContext->getNumPieces()));
_downloadContext->getNumPieces()));
msg->setBtMessageValidator(v);
}
break;
@ -130,7 +131,7 @@ DefaultBtMessageFactory::createBtMessage(const unsigned char* data, size_t dataL
SharedHandle<BtMessageValidator> validator
(new RangeBtMessageValidator
(temp.get(),
btContext->getNumPieces(),
_downloadContext->getNumPieces(),
_pieceStorage->getPieceLength(temp->getIndex())));
temp->setBtMessageValidator(validator);
msg = temp;
@ -141,7 +142,7 @@ DefaultBtMessageFactory::createBtMessage(const unsigned char* data, size_t dataL
SharedHandle<BtMessageValidator> validator
(new RangeBtMessageValidator
(temp.get(),
btContext->getNumPieces(),
_downloadContext->getNumPieces(),
_pieceStorage->getPieceLength(temp->getIndex())));
temp->setBtMessageValidator(validator);
msg = temp;
@ -151,9 +152,10 @@ DefaultBtMessageFactory::createBtMessage(const unsigned char* data, size_t dataL
BtPieceMessageHandle temp = BtPieceMessage::create(data, dataLength);
BtMessageValidatorHandle validator
(new BtPieceMessageValidator(temp.get(),
btContext->getNumPieces(),
_downloadContext->getNumPieces(),
_pieceStorage->getPieceLength(temp->getIndex())));
temp->setBtMessageValidator(validator);
temp->setDownloadContext(_downloadContext);
msg = temp;
break;
}
@ -168,7 +170,7 @@ DefaultBtMessageFactory::createBtMessage(const unsigned char* data, size_t dataL
SharedHandle<BtMessageValidator> validator
(new RangeBtMessageValidator
(temp.get(),
btContext->getNumPieces(),
_downloadContext->getNumPieces(),
_pieceStorage->getPieceLength(temp->getIndex())));
temp->setBtMessageValidator(validator);
msg = temp;
@ -177,7 +179,8 @@ DefaultBtMessageFactory::createBtMessage(const unsigned char* data, size_t dataL
case BtSuggestPieceMessage::ID: {
BtSuggestPieceMessageHandle temp = BtSuggestPieceMessage::create(data, dataLength);
SharedHandle<BtMessageValidator> validator
(new IndexBtMessageValidator(temp.get(), btContext->getNumPieces()));
(new IndexBtMessageValidator(temp.get(),
_downloadContext->getNumPieces()));
temp->setBtMessageValidator(validator);
msg = temp;
break;
@ -185,7 +188,8 @@ DefaultBtMessageFactory::createBtMessage(const unsigned char* data, size_t dataL
case BtAllowedFastMessage::ID: {
BtAllowedFastMessageHandle temp = BtAllowedFastMessage::create(data, dataLength);
SharedHandle<BtMessageValidator> validator
(new IndexBtMessageValidator(temp.get(), btContext->getNumPieces()));
(new IndexBtMessageValidator(temp.get(),
_downloadContext->getNumPieces()));
temp->setBtMessageValidator(validator);
msg = temp;
break;
@ -219,7 +223,6 @@ DefaultBtMessageFactory::createBtMessage(const unsigned char* data, size_t dataL
void DefaultBtMessageFactory::setCommonProperty(const AbstractBtMessageHandle& msg) {
msg->setCuid(cuid);
msg->setPeer(peer);
msg->setBtContext(btContext);
msg->setPieceStorage(_pieceStorage);
msg->setBtMessageDispatcher(dispatcher);
msg->setBtRequestFactory(requestFactory);
@ -232,8 +235,8 @@ DefaultBtMessageFactory::createHandshakeMessage(const unsigned char* data, size_
{
SharedHandle<BtHandshakeMessage> msg = BtHandshakeMessage::create(data, dataLength);
BtMessageValidatorHandle validator
(new BtHandshakeMessageValidator(msg.get(),
btContext->getInfoHash()));
(new BtHandshakeMessageValidator
(msg.get(), bittorrent::getInfoHash(_downloadContext)));
msg->setBtMessageValidator(validator);
setCommonProperty(msg);
return msg;
@ -245,8 +248,8 @@ DefaultBtMessageFactory::createHandshakeMessage(const unsigned char* infoHash,
{
SharedHandle<BtHandshakeMessage> msg(new BtHandshakeMessage(infoHash, peerId));
BtMessageValidatorHandle validator
(new BtHandshakeMessageValidator(msg.get(),
btContext->getInfoHash()));
(new BtHandshakeMessageValidator
(msg.get(), bittorrent::getInfoHash(_downloadContext)));
msg->setBtMessageValidator(validator);
msg->setDHTEnabled(_dhtEnabled);
setCommonProperty(msg);
@ -264,7 +267,7 @@ DefaultBtMessageFactory::createRequestMessage(const PieceHandle& piece, size_t b
SharedHandle<BtMessageValidator> validator
(new RangeBtMessageValidator
(msg.get(),
btContext->getNumPieces(),
_downloadContext->getNumPieces(),
_pieceStorage->getPieceLength(msg->getIndex())));
msg->setBtMessageValidator(validator);
setCommonProperty(msg);
@ -278,7 +281,7 @@ DefaultBtMessageFactory::createCancelMessage(size_t index, uint32_t begin, size_
SharedHandle<BtMessageValidator> validator
(new RangeBtMessageValidator
(msg.get(),
btContext->getNumPieces(),
_downloadContext->getNumPieces(),
_pieceStorage->getPieceLength(index)));
msg->setBtMessageValidator(validator);
setCommonProperty(msg);
@ -291,9 +294,10 @@ DefaultBtMessageFactory::createPieceMessage(size_t index, uint32_t begin, size_t
BtPieceMessageHandle msg(new BtPieceMessage(index, begin, length));
BtMessageValidatorHandle validator
(new BtPieceMessageValidator(msg.get(),
btContext->getNumPieces(),
_downloadContext->getNumPieces(),
_pieceStorage->getPieceLength(index)));
msg->setBtMessageValidator(validator);
msg->setDownloadContext(_downloadContext);
setCommonProperty(msg);
return msg;
}
@ -303,7 +307,7 @@ DefaultBtMessageFactory::createHaveMessage(size_t index)
{
BtHaveMessageHandle msg(new BtHaveMessage(index));
SharedHandle<BtMessageValidator> v
(new IndexBtMessageValidator(msg.get(), btContext->getNumPieces()));
(new IndexBtMessageValidator(msg.get(), _downloadContext->getNumPieces()));
msg->setBtMessageValidator(v);
setCommonProperty(msg);
return msg;
@ -349,7 +353,7 @@ DefaultBtMessageFactory::createBitfieldMessage()
_pieceStorage->getBitfieldLength()));
SharedHandle<BtMessageValidator> v
(new BtBitfieldMessageValidator(msg.get(),
btContext->getNumPieces()));
_downloadContext->getNumPieces()));
msg->setBtMessageValidator(v);
setCommonProperty(msg);
return msg;
@ -386,7 +390,7 @@ DefaultBtMessageFactory::createRejectMessage(size_t index, uint32_t begin, size_
SharedHandle<BtMessageValidator> validator
(new RangeBtMessageValidator
(msg.get(),
btContext->getNumPieces(),
_downloadContext->getNumPieces(),
_pieceStorage->getPieceLength(index)));
msg->setBtMessageValidator(validator);
setCommonProperty(msg);
@ -398,7 +402,7 @@ DefaultBtMessageFactory::createAllowedFastMessage(size_t index)
{
BtAllowedFastMessageHandle msg(new BtAllowedFastMessage(index));
SharedHandle<BtMessageValidator> validator
(new IndexBtMessageValidator(msg.get(), btContext->getNumPieces()));
(new IndexBtMessageValidator(msg.get(), _downloadContext->getNumPieces()));
msg->setBtMessageValidator(validator);
setCommonProperty(msg);
return msg;
@ -435,9 +439,10 @@ void DefaultBtMessageFactory::setPeer(const SharedHandle<Peer>& peer)
this->peer = peer;
}
void DefaultBtMessageFactory::setBtContext(const SharedHandle<BtContext>& btContext)
void DefaultBtMessageFactory::setDownloadContext
(const SharedHandle<DownloadContext>& downloadContext)
{
this->btContext = btContext;
_downloadContext = downloadContext;
}
void DefaultBtMessageFactory::setPieceStorage

View File

@ -39,7 +39,7 @@
namespace aria2 {
class BtContext;
class DownloadContext;
class PieceStorage;
class PeerStorage;
class Peer;
@ -56,7 +56,7 @@ class DHTTaskFactory;
class DefaultBtMessageFactory : public BtMessageFactory {
private:
int32_t cuid;
SharedHandle<BtContext> btContext;
SharedHandle<DownloadContext> _downloadContext;
SharedHandle<PieceStorage> _pieceStorage;
SharedHandle<PeerStorage> _peerStorage;
SharedHandle<Peer> peer;
@ -134,7 +134,7 @@ public:
void setPeer(const SharedHandle<Peer>& peer);
void setBtContext(const SharedHandle<BtContext>& btContext);
void setDownloadContext(const SharedHandle<DownloadContext>& downloadContext);
void setPieceStorage(const SharedHandle<PieceStorage>& pieceStorage);

View File

@ -38,13 +38,14 @@
#include "BtHandshakeMessage.h"
#include "message.h"
#include "BtContext.h"
#include "DownloadContext.h"
#include "Peer.h"
#include "PeerConnection.h"
#include "BtMessageDispatcher.h"
#include "BtMessageFactory.h"
#include "Logger.h"
#include "LogFactory.h"
#include "bittorrent_helper.h"
namespace aria2 {
@ -71,7 +72,8 @@ DefaultBtMessageReceiver::receiveHandshake(bool quickReply)
if(!handshakeSent && quickReply && dataLength >= 48) {
handshakeSent = true;
// check info_hash
if(memcmp(btContext->getInfoHash(), &data[28], INFO_HASH_LENGTH) == 0) {
if(memcmp(bittorrent::getInfoHash(_downloadContext), &data[28],
INFO_HASH_LENGTH) == 0) {
sendHandshake();
}
}
@ -92,8 +94,8 @@ DefaultBtMessageReceiver::receiveAndSendHandshake()
void DefaultBtMessageReceiver::sendHandshake() {
SharedHandle<BtMessage> msg =
messageFactory->createHandshakeMessage(btContext->getInfoHash(),
btContext->getPeerId());
messageFactory->createHandshakeMessage
(bittorrent::getInfoHash(_downloadContext), bittorrent::getStaticPeerId());
dispatcher->addMessageToQueue(msg);
dispatcher->sendMessages();
}
@ -114,9 +116,10 @@ BtMessageHandle DefaultBtMessageReceiver::receiveMessage() {
}
}
void DefaultBtMessageReceiver::setBtContext(const SharedHandle<BtContext>& btContext)
void DefaultBtMessageReceiver::setDownloadContext
(const SharedHandle<DownloadContext>& downloadContext)
{
this->btContext = btContext;
_downloadContext = downloadContext;
}
void DefaultBtMessageReceiver::setPeer(const SharedHandle<Peer>& peer)

View File

@ -39,7 +39,7 @@
namespace aria2 {
class BtContext;
class DownloadContext;
class Peer;
class PeerConnection;
class BtMessageDispatcher;
@ -50,7 +50,7 @@ class DefaultBtMessageReceiver : public BtMessageReceiver {
private:
int32_t cuid;
bool handshakeSent;
SharedHandle<BtContext> btContext;
SharedHandle<DownloadContext> _downloadContext;
SharedHandle<Peer> peer;
WeakHandle<PeerConnection> peerConnection;
WeakHandle<BtMessageDispatcher> dispatcher;
@ -77,7 +77,7 @@ public:
return cuid;
}
void setBtContext(const SharedHandle<BtContext>& btContext);
void setDownloadContext(const SharedHandle<DownloadContext>& downloadContext);
void setPeer(const SharedHandle<Peer>& peer);

View File

@ -38,13 +38,8 @@
#include <cstring>
#include <fstream>
#include "BtContext.h"
#include "PieceStorage.h"
#include "Piece.h"
#ifdef ENABLE_BITTORRENT
#include "PeerStorage.h"
#include "BtRuntime.h"
#endif // ENABLE_BITTORRENT
#include "BitfieldMan.h"
#include "Option.h"
#include "TransferStat.h"
@ -59,6 +54,12 @@
#include "DownloadFailureException.h"
#include "StringFormat.h"
#include "array_fun.h"
#include "DownloadContext.h"
#ifdef ENABLE_BITTORRENT
# include "PeerStorage.h"
# include "BtRuntime.h"
# include "bittorrent_helper.h"
#endif // ENABLE_BITTORRENT
namespace aria2 {
@ -67,13 +68,13 @@ const std::string DefaultBtProgressInfoFile::V0001("0001");
static std::string createFilename(const SharedHandle<DownloadContext>& dctx)
{
std::string t = dctx->getActualBasePath();
std::string t = dctx->getBasePath();
t += ".aria2";
return t;
}
DefaultBtProgressInfoFile::DefaultBtProgressInfoFile
(const DownloadContextHandle& dctx,
(const SharedHandle<DownloadContext>& dctx,
const PieceStorageHandle& pieceStorage,
const Option* option):
_dctx(dctx),
@ -123,13 +124,12 @@ void DefaultBtProgressInfoFile::save() {
if(torrentDownload) {
// infoHashLength:
// length: 32 bits
BtContextHandle btContext(dynamic_pointer_cast<BtContext>(_dctx));
uint32_t infoHashLengthNL = htonl(btContext->getInfoHashLength());
const unsigned char* infoHash = bittorrent::getInfoHash(_dctx);
uint32_t infoHashLengthNL = htonl(INFO_HASH_LENGTH);
o.write(reinterpret_cast<const char*>(&infoHashLengthNL),
sizeof(infoHashLengthNL));
// infoHash:
o.write(reinterpret_cast<const char*>(btContext->getInfoHash()),
btContext->getInfoHashLength());
o.write(reinterpret_cast<const char*>(infoHash), INFO_HASH_LENGTH);
} else {
// infoHashLength:
// length: 32 bits
@ -240,15 +240,16 @@ void DefaultBtProgressInfoFile::load()
array_ptr<unsigned char> savedInfoHash(new unsigned char[infoHashLength]);
in.read(reinterpret_cast<char*>
(static_cast<unsigned char*>(savedInfoHash)), infoHashLength);
BtContextHandle btContext(dynamic_pointer_cast<BtContext>(_dctx));
if(infoHashCheckEnabled &&
Util::toHex(savedInfoHash, infoHashLength) !=
btContext->getInfoHashAsString()) {
throw DL_ABORT_EX
(StringFormat("info hash mismatch. expected: %s, actual: %s",
btContext->getInfoHashAsString().c_str(),
Util::toHex(savedInfoHash,
infoHashLength).c_str()).str());
if(infoHashCheckEnabled) {
const unsigned char* infoHash = bittorrent::getInfoHash(_dctx);
if(infoHashLength != INFO_HASH_LENGTH ||
memcmp(savedInfoHash, infoHash, INFO_HASH_LENGTH) != 0) {
throw DL_ABORT_EX
(StringFormat("info hash mismatch. expected: %s, actual: %s",
Util::toHex(infoHash, INFO_HASH_LENGTH).c_str(),
Util::toHex(savedInfoHash, infoHashLength).c_str()
).str());
}
}
}

View File

@ -40,7 +40,6 @@
#include "Logger.h"
#include "Piece.h"
#include "Peer.h"
#include "BtContext.h"
#include "PieceStorage.h"
#include "BtMessageDispatcher.h"
#include "BtMessageFactory.h"
@ -235,11 +234,6 @@ void DefaultBtRequestFactory::getTargetPieceIndexes
mem_fun_sh(&Piece::getIndex));
}
void DefaultBtRequestFactory::setBtContext(const SharedHandle<BtContext>& btContext)
{
this->btContext = btContext;
}
void DefaultBtRequestFactory::setPieceStorage
(const SharedHandle<PieceStorage>& pieceStorage)
{

View File

@ -39,7 +39,6 @@
namespace aria2 {
class BtContext;
class PieceStorage;
class Peer;
class BtMessageDispatcher;
@ -50,7 +49,6 @@ class Logger;
class DefaultBtRequestFactory : public BtRequestFactory {
private:
int32_t cuid;
SharedHandle<BtContext> btContext;
SharedHandle<PieceStorage> _pieceStorage;
SharedHandle<Peer> peer;
WeakHandle<BtMessageDispatcher> dispatcher;
@ -96,8 +94,6 @@ public:
this->cuid = cuid;
}
void setBtContext(const SharedHandle<BtContext>& btContext);
void setPieceStorage(const SharedHandle<PieceStorage>& pieceStorage);
void setPeer(const SharedHandle<Peer>& peer);

View File

@ -33,7 +33,6 @@
*/
/* copyright --> */
#include "DefaultExtensionMessageFactory.h"
#include "BtContext.h"
#include "Peer.h"
#include "DlAbortEx.h"
#include "HandshakeExtensionMessage.h"
@ -50,10 +49,8 @@ DefaultExtensionMessageFactory::DefaultExtensionMessageFactory():
_logger(LogFactory::getInstance()) {}
DefaultExtensionMessageFactory::DefaultExtensionMessageFactory
(const BtContextHandle& btContext,
const PeerHandle& peer,
(const PeerHandle& peer,
const SharedHandle<ExtensionMessageRegistry>& registry):
_btContext(btContext),
_peer(peer),
_registry(registry),
_logger(LogFactory::getInstance()) {}
@ -67,7 +64,6 @@ DefaultExtensionMessageFactory::createMessage(const unsigned char* data, size_t
if(extensionMessageID == 0) {
// handshake
HandshakeExtensionMessageHandle m = HandshakeExtensionMessage::create(data, length);
m->setBtContext(_btContext);
m->setPeer(_peer);
return m;
} else {
@ -91,11 +87,6 @@ DefaultExtensionMessageFactory::createMessage(const unsigned char* data, size_t
}
}
void DefaultExtensionMessageFactory::setBtContext(const BtContextHandle& btContext)
{
_btContext = btContext;
}
void DefaultExtensionMessageFactory::setPeerStorage
(const SharedHandle<PeerStorage>& peerStorage)
{

View File

@ -39,7 +39,6 @@
namespace aria2 {
class BtContext;
class PeerStorage;
class Peer;
class Logger;
@ -47,8 +46,6 @@ class ExtensionMessageRegistry;
class DefaultExtensionMessageFactory:public ExtensionMessageFactory {
private:
SharedHandle<BtContext> _btContext;
SharedHandle<PeerStorage> _peerStorage;
SharedHandle<Peer> _peer;
@ -61,8 +58,7 @@ public:
DefaultExtensionMessageFactory();
DefaultExtensionMessageFactory
(const SharedHandle<BtContext>& btContext,
const SharedHandle<Peer>& peer,
(const SharedHandle<Peer>& peer,
const SharedHandle<ExtensionMessageRegistry>& registry);
virtual ~DefaultExtensionMessageFactory();
@ -70,8 +66,6 @@ public:
virtual SharedHandle<ExtensionMessage>
createMessage(const unsigned char* data, size_t length);
void setBtContext(const SharedHandle<BtContext>& btContext);
void setPeerStorage(const SharedHandle<PeerStorage>& peerStorage);
void setPeer(const SharedHandle<Peer>& peer);

View File

@ -41,7 +41,6 @@
#include "message.h"
#include "a2time.h"
#include "Peer.h"
#include "BtContext.h"
#include "BtRuntime.h"
#include "BtSeederStateChoke.h"
#include "BtLeecherStateChoke.h"
@ -51,9 +50,7 @@ namespace aria2 {
static const int MAX_PEER_LIST_SIZE = 1024;
DefaultPeerStorage::DefaultPeerStorage(const BtContextHandle& btContext,
const Option* option):
btContext(btContext),
DefaultPeerStorage::DefaultPeerStorage(const Option* option):
option(option),
logger(LogFactory::getInstance()),
removedPeerSessionDownloadLength(0),

View File

@ -43,7 +43,6 @@
namespace aria2 {
class BtContext;
class Option;
class Logger;
class BtRuntime;
@ -53,7 +52,6 @@ class PieceStorage;
class DefaultPeerStorage : public PeerStorage {
private:
SharedHandle<BtContext> btContext;
SharedHandle<BtRuntime> _btRuntime;
SharedHandle<PieceStorage> _pieceStorage;
const Option* option;
@ -73,8 +71,7 @@ private:
bool isPeerAlreadyAdded(const SharedHandle<Peer>& peer);
public:
DefaultPeerStorage(const SharedHandle<BtContext>& btContext,
const Option* option);
DefaultPeerStorage(const Option* option);
virtual ~DefaultPeerStorage();

View File

@ -63,7 +63,7 @@
namespace aria2 {
DefaultPieceStorage::DefaultPieceStorage
(const DownloadContextHandle& downloadContext, const Option* option):
(const SharedHandle<DownloadContext>& downloadContext, const Option* option):
downloadContext(downloadContext),
bitfieldMan(BitfieldManFactory::getFactoryInstance()->
createBitfieldMan(downloadContext->getPieceLength(),
@ -421,10 +421,10 @@ size_t DefaultPieceStorage::getInFlightPieceCompletedLength() const
// not unittested
void DefaultPieceStorage::setupFileFilter()
{
const std::deque<SharedHandle<FileEntry> >& fileEntries =
const std::vector<SharedHandle<FileEntry> >& fileEntries =
downloadContext->getFileEntries();
bool allSelected = true;
for(std::deque<SharedHandle<FileEntry> >::const_iterator i =
for(std::vector<SharedHandle<FileEntry> >::const_iterator i =
fileEntries.begin(); i != fileEntries.end(); ++i) {
if(!(*i)->isRequested()) {
allSelected = false;
@ -434,7 +434,7 @@ void DefaultPieceStorage::setupFileFilter()
if(allSelected) {
return;
}
for(std::deque<SharedHandle<FileEntry> >::const_iterator i =
for(std::vector<SharedHandle<FileEntry> >::const_iterator i =
fileEntries.begin(); i != fileEntries.end(); ++i) {
if((*i)->isRequested()) {
bitfieldMan->addFilter((*i)->getOffset(), (*i)->getLength());
@ -465,11 +465,12 @@ bool DefaultPieceStorage::allDownloadFinished()
// not unittested
void DefaultPieceStorage::initStorage()
{
if(downloadContext->getFileMode() == DownloadContext::SINGLE) {
if(downloadContext->getFileEntries().size() == 1) {
logger->debug("Instantiating DirectDiskAdaptor");
DirectDiskAdaptorHandle directDiskAdaptor(new DirectDiskAdaptor());
directDiskAdaptor->setTotalLength(downloadContext->getTotalLength());
directDiskAdaptor->setFileEntries(downloadContext->getFileEntries());
directDiskAdaptor->setFileEntries(downloadContext->getFileEntries().begin(),
downloadContext->getFileEntries().end());
DiskWriterHandle writer =
_diskWriterFactory->newDiskWriter(directDiskAdaptor->getFilePath());
@ -480,10 +481,10 @@ void DefaultPieceStorage::initStorage()
directDiskAdaptor->setDiskWriter(writer);
this->diskAdaptor = directDiskAdaptor;
} else {
// file mode == DownloadContext::MULTI
logger->debug("Instantiating MultiDiskAdaptor");
MultiDiskAdaptorHandle multiDiskAdaptor(new MultiDiskAdaptor());
multiDiskAdaptor->setFileEntries(downloadContext->getFileEntries());
multiDiskAdaptor->setFileEntries(downloadContext->getFileEntries().begin(),
downloadContext->getFileEntries().end());
if(option->getAsBool(PREF_ENABLE_DIRECT_IO)) {
multiDiskAdaptor->allowDirectIO();
}

View File

@ -42,8 +42,9 @@
namespace aria2 {
SegmentManHandle
DefaultSegmentManFactory::createNewInstance(const DownloadContextHandle& dctx,
const PieceStorageHandle& ps)
DefaultSegmentManFactory::createNewInstance
(const SharedHandle<DownloadContext>& dctx,
const PieceStorageHandle& ps)
{
SegmentManHandle segmentMan(new SegmentMan(_option, dctx, ps));
return segmentMan;

View File

@ -47,8 +47,4 @@ DiskAdaptor::DiskAdaptor():
DiskAdaptor::~DiskAdaptor() {}
void DiskAdaptor::setFileEntries(const FileEntries& fileEntries) {
this->fileEntries = fileEntries;
}
} // namespace aria2

View File

@ -38,7 +38,7 @@
#include "BinaryStream.h"
#include <string>
#include <deque>
#include <vector>
#include "TimeA2.h"
@ -50,7 +50,7 @@ class FileAllocationIterator;
class DiskAdaptor:public BinaryStream {
protected:
std::deque<SharedHandle<FileEntry> > fileEntries;
std::vector<SharedHandle<FileEntry> > fileEntries;
#ifdef HAVE_POSIX_FALLOCATE
bool _fallocate;
#endif // HAVE_POSIX_FALLOCATE
@ -73,9 +73,13 @@ public:
virtual uint64_t size() = 0;
void setFileEntries(const std::deque<SharedHandle<FileEntry> >& fileEntries);
template<typename InputIterator>
void setFileEntries(InputIterator first, InputIterator last)
{
fileEntries.assign(first, last);
}
const std::deque<SharedHandle<FileEntry> >& getFileEntries() const
const std::vector<SharedHandle<FileEntry> >& getFileEntries() const
{
return fileEntries;
}

View File

@ -37,29 +37,30 @@
#include <algorithm>
#include "FileEntry.h"
#include "StringFormat.h"
namespace aria2 {
DownloadContext::DownloadContext():
_dir("."),
_pieceLength(0),
_knowsTotalLength(true),
_ownerRequestGroup(0),
_downloadStartTime(0),
_downloadStopTime(_downloadStartTime) {}
DownloadContext::~DownloadContext() {}
void DownloadContext::setDir(const std::string& dir)
DownloadContext::DownloadContext(size_t pieceLength,
uint64_t totalLength,
const std::string& path):
_dir("."),
_pieceLength(pieceLength),
_knowsTotalLength(true),
_ownerRequestGroup(0),
_downloadStartTime(0),
_downloadStopTime(_downloadStartTime)
{
_dir = dir;
}
SharedHandle<Signature> DownloadContext::getSignature() const
{
return _signature;
}
void DownloadContext::setSignature(const SharedHandle<Signature>& signature)
{
_signature = signature;
SharedHandle<FileEntry> fileEntry(new FileEntry(path, totalLength, 0));
_fileEntries.push_back(fileEntry);
}
void DownloadContext::resetDownloadStartTime()
@ -94,7 +95,7 @@ DownloadContext::findFileEntryByOffset(off_t offset) const
SharedHandle<FileEntry> obj(new FileEntry());
obj->setOffset(offset);
std::deque<SharedHandle<FileEntry> >::const_iterator i =
std::vector<SharedHandle<FileEntry> >::const_iterator i =
std::upper_bound(_fileEntries.begin(), _fileEntries.end(), obj);
if(i != _fileEntries.end() && (*i)->getOffset() == offset) {
return *i;
@ -103,4 +104,65 @@ DownloadContext::findFileEntryByOffset(off_t offset) const
}
}
void DownloadContext::setFilePathWithIndex
(size_t index, const std::string& path)
{
if(0 < index && index <= _fileEntries.size()) {
_fileEntries[index-1]->setPath(path);
} else {
throw DL_ABORT_EX(StringFormat("No such file with index=%u",
static_cast<unsigned int>(index)).str());
}
}
void DownloadContext::setFileFilter(IntSequence seq)
{
std::deque<int32_t> fileIndexes = seq.flush();
std::sort(fileIndexes.begin(), fileIndexes.end());
fileIndexes.erase(std::unique(fileIndexes.begin(), fileIndexes.end()),
fileIndexes.end());
bool selectAll = fileIndexes.empty() || _fileEntries.size() == 1;
int32_t index = 1;
for(std::vector<SharedHandle<FileEntry> >::const_iterator i =
_fileEntries.begin(); i != _fileEntries.end(); ++i, ++index) {
(*i)->setRequested
(selectAll ||
std::binary_search(fileIndexes.begin(), fileIndexes.end(), index));
}
}
void DownloadContext::ensureAttrs()
{
if(_attrs.isNone()) {
_attrs = BDE::dict();
}
}
void DownloadContext::setAttribute(const std::string& key, const BDE& value)
{
ensureAttrs();
_attrs[key] = value;
}
BDE& DownloadContext::getAttribute(const std::string& key)
{
ensureAttrs();
if(_attrs.containsKey(key)) {
return _attrs[key];
} else {
throw DL_ABORT_EX(StringFormat("No attribute named %s", key.c_str()).str());
}
}
bool DownloadContext::hasAttribute(const std::string& key) const
{
if(_attrs.isNone()) {
return false;
} else {
return _attrs.containsKey(key);
}
}
} // namespace aria2

View File

@ -37,80 +37,194 @@
#include "common.h"
#include <cassert>
#include <string>
#include <deque>
#include <vector>
#include "SharedHandle.h"
#include "Signature.h"
#include "TimeA2.h"
#include "A2STR.h"
#include "BDE.h"
#include "IntSequence.h"
namespace aria2 {
class FileEntry;
class RequestGroup;
class DownloadContext
{
protected:
std::deque<SharedHandle<FileEntry> > _fileEntries;
private:
std::vector<SharedHandle<FileEntry> > _fileEntries;
std::string _dir;
private:
std::vector<std::string> _pieceHashes;
size_t _pieceLength;
std::string _pieceHashAlgo;
std::string _checksum;
std::string _checksumHashAlgo;
std::string _basePath;
bool _knowsTotalLength;
RequestGroup* _ownerRequestGroup;
BDE _attrs;
Time _downloadStartTime;
Time _downloadStopTime;
SharedHandle<Signature> _signature;
void ensureAttrs();
public:
DownloadContext();
virtual ~DownloadContext();
// Convenient constructor that creates single file download.
DownloadContext(size_t pieceLength,
uint64_t totalLength,
const std::string& path = A2STR::NIL);
enum FILE_MODE {
SINGLE,
MULTI
};
virtual const std::string& getPieceHash(size_t index) const = 0;
const std::string& getPieceHash(size_t index) const
{
if(index < _pieceHashes.size()) {
return _pieceHashes[index];
} else {
return A2STR::NIL;
}
}
virtual const std::deque<std::string>& getPieceHashes() const = 0;
const std::vector<std::string>& getPieceHashes() const
{
return _pieceHashes;
}
virtual uint64_t getTotalLength() const = 0;
template<typename InputIterator>
void setPieceHashes(InputIterator first, InputIterator last)
{
_pieceHashes.assign(first, last);
}
virtual bool knowsTotalLength() const = 0;
uint64_t getTotalLength() const
{
if(_fileEntries.empty()) {
return 0;
} else {
return _fileEntries.back()->getLastOffset();
}
}
virtual FILE_MODE getFileMode() const = 0;
bool knowsTotalLength() const { return _knowsTotalLength; }
const std::deque<SharedHandle<FileEntry> >& getFileEntries() const
void markTotalLengthIsUnknown() { _knowsTotalLength = false; }
const std::vector<SharedHandle<FileEntry> >& getFileEntries() const
{
return _fileEntries;
}
virtual size_t getPieceLength() const = 0;
virtual size_t getNumPieces() const = 0;
virtual const std::string& getPieceHashAlgo() const = 0;
/**
* Returns an actual file path.
* If this contains a single file entry, then returns its file path,
* for example, "/tmp/downloads/aria2.txt"
* If this contains multiple file entries(i,e /tmp/downloads/aria2.txt,
* /tmp/downloads/aria2.bin), then returns its base dir path,
* for example, "/tmp/downloads"
*/
virtual std::string getActualBasePath() const = 0;
const std::string& getDir() const
const SharedHandle<FileEntry>& getFirstFileEntry() const
{
return _dir;
assert(!_fileEntries.empty());
return _fileEntries[0];
}
void setDir(const std::string& dir);
template<typename InputIterator>
void setFileEntries(InputIterator first, InputIterator last)
{
_fileEntries.assign(first, last);
}
SharedHandle<Signature> getSignature() const;
size_t getPieceLength() const { return _pieceLength; }
void setSignature(const SharedHandle<Signature>& signature);
void setPieceLength(size_t length) { _pieceLength = length; }
size_t getNumPieces() const
{
if(_pieceLength == 0) {
return 0;
} else {
assert(!_fileEntries.empty());
return (_fileEntries.back()->getLastOffset()+_pieceLength-1)/_pieceLength;
}
}
const std::string& getPieceHashAlgo() const { return _pieceHashAlgo; }
void setPieceHashAlgo(const std::string& algo)
{
_pieceHashAlgo = algo;
}
const std::string& getChecksum() const { return _checksum; }
void setChecksum(const std::string& checksum)
{
_checksum = checksum;
}
const std::string& getChecksumHashAlgo() const { return _checksumHashAlgo; }
void setChecksumHashAlgo(const std::string& algo)
{
_checksumHashAlgo = algo;
}
// The representative path name for this context. It is used as a
// part of .aria2 control file. If _basePath is set, returns
// _basePath. Otherwise, the first FileEntry's getFilePath() is
// returned.
const std::string& getBasePath() const
{
if(_basePath.empty()) {
assert(!_fileEntries.empty());
return getFirstFileEntry()->getPath();
} else {
return _basePath;
}
}
void setBasePath(const std::string& basePath) { _basePath = basePath; }
const std::string& getDir() const { return _dir; }
void setDir(const std::string& dir) { _dir = dir; }
SharedHandle<Signature> getSignature() const { return _signature; }
void setSignature(const SharedHandle<Signature>& signature)
{
_signature = signature;
}
RequestGroup* getOwnerRequestGroup() { return _ownerRequestGroup; }
void setOwnerRequestGroup(RequestGroup* owner)
{
_ownerRequestGroup = owner;
}
void setFileFilter(IntSequence seq);
// Sets file path for spcified index. index starts from 1. The index
// is the same used in setFileFilter(). Please note that path is
// not the actual file path. The actual file path is
// getDir()+"/"+path.
void setFilePathWithIndex(size_t index, const std::string& path);
void setAttribute(const std::string& key, const BDE& value);
BDE& getAttribute(const std::string& key);
bool hasAttribute(const std::string& key) const;
void resetDownloadStartTime();
@ -123,8 +237,6 @@ public:
SharedHandle<FileEntry> findFileEntryByOffset(off_t offset) const;
};
typedef SharedHandle<DownloadContext> DownloadContextHandle;
} // namespace aria2
#endif // _D_DOWNLOAD_CONTEXT_H_

View File

@ -65,14 +65,15 @@
#include "Command.h"
#include "FileAllocationEntry.h"
#include "CheckIntegrityEntry.h"
#include "BtRegistry.h"
#include "BtContext.h"
#include "PeerStorage.h"
#include "PieceStorage.h"
#include "BtAnnounce.h"
#include "BtRuntime.h"
#include "BtProgressInfoFile.h"
#include "DownloadContext.h"
#ifdef ENABLE_BITTORRENT
# include "BtRegistry.h"
# include "PeerStorage.h"
# include "PieceStorage.h"
# include "BtAnnounce.h"
# include "BtRuntime.h"
#endif // ENABLE_BITTORRENT
namespace aria2 {

View File

@ -57,6 +57,7 @@
#include "DownloadResult.h"
#include "ServerStatMan.h"
#include "a2io.h"
#include "DownloadContext.h"
#ifdef HAVE_EPOLL
# include "EpollEventPoll.h"
#endif // HAVE_EPOLL

View File

@ -37,6 +37,7 @@
#include "Logger.h"
#include "RequestGroup.h"
#include "RequestGroupCriteria.h"
#include "DownloadContext.h"
namespace aria2 {

View File

@ -40,7 +40,7 @@
#include <stdint.h>
#include <string>
#include <deque>
#include <vector>
#include "SharedHandle.h"
#include "FileEntry.h"
@ -64,7 +64,7 @@ public:
int32_t gid;
std::deque<SharedHandle<FileEntry> > fileEntries;
std::vector<SharedHandle<FileEntry> > fileEntries;
bool inMemoryDownload;
@ -82,7 +82,7 @@ public:
RESULT result;
DownloadResult(int32_t gid,
const std::deque<SharedHandle<FileEntry> >& fileEntries,
const std::vector<SharedHandle<FileEntry> >& fileEntries,
bool inMemoryDownload,
uint64_t totalLength,
const std::string& uri,

View File

@ -42,6 +42,7 @@
#include "prefs.h"
#include "Util.h"
#include "DownloadEngine.h"
#include "DownloadContext.h"
namespace aria2 {
@ -78,7 +79,8 @@ bool FileAllocationCommand::handleException(Exception& e)
{
_e->_fileAllocationMan->dropPickedEntry();
logger->error(MSG_FILE_ALLOCATION_FAILURE, e, cuid);
logger->error(MSG_DOWNLOAD_NOT_COMPLETE, cuid, _requestGroup->getFilePath().c_str());
logger->error(MSG_DOWNLOAD_NOT_COMPLETE, cuid,
_requestGroup->getDownloadContext()->getBasePath().c_str());
return true;
}

View File

@ -37,6 +37,7 @@
#include "FileAllocationCommand.h"
#include "message.h"
#include "Logger.h"
#include "DownloadContext.h"
namespace aria2 {

View File

@ -38,6 +38,7 @@
#include "RequestGroup.h"
#include "PieceStorage.h"
#include "DiskAdaptor.h"
#include "DownloadContext.h"
namespace aria2 {

View File

@ -60,6 +60,7 @@ private:
bool requested;
std::deque<SharedHandle<Request> > _requestPool;
std::deque<SharedHandle<Request> > _inFlightRequests;
std::string _contentType;
public:
FileEntry():length(0), offset(0), extracted(false), requested(false) {}
@ -128,6 +129,13 @@ public:
// Inserts _uris and _spentUris into uris.
void getUris(std::deque<std::string>& uris) const;
void setContentType(const std::string& contentType)
{
_contentType = contentType;
}
const std::string& getContentType() const { return _contentType; }
std::string selectUri(const SharedHandle<URISelector>& uriSelector);
// If pooled Request object is available, one of them is removed

View File

@ -39,6 +39,7 @@
#include "RecoverableException.h"
#include "message.h"
#include "Logger.h"
#include "DownloadContext.h"
namespace aria2 {

View File

@ -43,6 +43,7 @@
#include "FtpFinishDownloadCommand.h"
#include "FtpConnection.h"
#include "Logger.h"
#include "DownloadContext.h"
namespace aria2 {

View File

@ -47,6 +47,7 @@
#include "SocketCore.h"
#include "RequestGroup.h"
#include "Logger.h"
#include "DownloadContext.h"
namespace aria2 {

View File

@ -50,6 +50,7 @@
#include "prefs.h"
#include "HttpConnection.h"
#include "Socket.h"
#include "DownloadContext.h"
namespace aria2 {

View File

@ -53,7 +53,7 @@
#include "Option.h"
#include "Logger.h"
#include "Segment.h"
#include "SingleFileDownloadContext.h"
#include "DownloadContext.h"
#include "DefaultBtProgressInfoFile.h"
#include "RequestGroupMan.h"
#include "DownloadFailureException.h"
@ -328,17 +328,17 @@ bool FtpNegotiationCommand::sendSize() {
bool FtpNegotiationCommand::onFileSizeDetermined(uint64_t totalLength)
{
SingleFileDownloadContextHandle dctx =
dynamic_pointer_cast<SingleFileDownloadContext>(_requestGroup->getDownloadContext());
dctx->setTotalLength(totalLength);
_fileEntry->setLength(totalLength);
dctx->setFilename
(strconcat(dctx->getDir(), "/", Util::urldecode(req->getFile())));
if(getOption()->get(PREF_OUT).empty()) {
_fileEntry->setPath
(strconcat(getDownloadContext()->getDir(),
"/", Util::urldecode(req->getFile())));
}
_requestGroup->preDownloadProcessing();
if(e->_requestGroupMan->isSameFileBeingDownloaded(_requestGroup)) {
throw DOWNLOAD_FAILURE_EXCEPTION
(StringFormat(EX_DUPLICATE_FILE_DOWNLOAD,
_requestGroup->getFilePath().c_str()).str());
_requestGroup->getFirstFilePath().c_str()).str());
}
if(totalLength == 0) {
@ -361,7 +361,7 @@ bool FtpNegotiationCommand::onFileSizeDetermined(uint64_t totalLength)
logger->notice(MSG_DOWNLOAD_ALREADY_COMPLETED,
_requestGroup->getGID(),
_requestGroup->getFilePath().c_str());
_requestGroup->getFirstFilePath().c_str());
poolConnection();
@ -372,7 +372,7 @@ bool FtpNegotiationCommand::onFileSizeDetermined(uint64_t totalLength)
_requestGroup->initPieceStorage();
_requestGroup->getPieceStorage()->getDiskAdaptor()->initAndOpenFile();
if(dctx->knowsTotalLength()) {
if(getDownloadContext()->knowsTotalLength()) {
sequence = SEQ_DOWNLOAD_ALREADY_COMPLETED;
poolConnection();
return false;
@ -400,7 +400,7 @@ bool FtpNegotiationCommand::onFileSizeDetermined(uint64_t totalLength)
logger->notice(MSG_DOWNLOAD_ALREADY_COMPLETED,
_requestGroup->getGID(),
_requestGroup->getFilePath().c_str());
_requestGroup->getFirstFilePath().c_str());
poolConnection();
@ -444,10 +444,7 @@ bool FtpNegotiationCommand::recvSize() {
// command, resuming and segmented downloading are disabled when the first
// contacted FTP server doesn't support it.
if(_requestGroup->getPieceStorage().isNull()) {
SingleFileDownloadContextHandle dctx =
dynamic_pointer_cast<SingleFileDownloadContext>
(_requestGroup->getDownloadContext());
dctx->markTotalLengthIsUnknown();
getDownloadContext()->markTotalLengthIsUnknown();
return onFileSizeDetermined(0);
}

View File

@ -36,6 +36,7 @@
#include "FtpTunnelResponseCommand.h"
#include "Request.h"
#include "Socket.h"
#include "DownloadContext.h"
namespace aria2 {

View File

@ -39,6 +39,7 @@
#include "HttpRequest.h"
#include "Segment.h"
#include "Socket.h"
#include "DownloadContext.h"
namespace aria2 {

View File

@ -34,7 +34,6 @@
/* copyright --> */
#include "HandshakeExtensionMessage.h"
#include "Peer.h"
#include "BtContext.h"
#include "Util.h"
#include "DlAbortEx.h"
#include "LogFactory.h"
@ -107,11 +106,6 @@ void HandshakeExtensionMessage::setPeer(const PeerHandle& peer)
_peer = peer;
}
void HandshakeExtensionMessage::setBtContext(const BtContextHandle& btContext)
{
_btContext = btContext;
}
uint8_t HandshakeExtensionMessage::getExtensionMessageID(const std::string& name) const
{
std::map<std::string, uint8_t>::const_iterator i = _extensions.find(name);

View File

@ -43,7 +43,6 @@
namespace aria2 {
class BtContext;
class Peer;
class Logger;
class HandshakeExtensionMessage;
@ -57,8 +56,6 @@ private:
std::map<std::string, uint8_t> _extensions;
SharedHandle<BtContext> _btContext;
SharedHandle<Peer> _peer;
Logger* _logger;
@ -120,8 +117,6 @@ public:
void setPeer(const SharedHandle<Peer>& peer);
void setBtContext(const SharedHandle<BtContext>& btContext);
static HandshakeExtensionMessageHandle create(const unsigned char* data,
size_t dataLength);
};

View File

@ -37,6 +37,7 @@
#include "RequestGroupMan.h"
#include "PieceStorage.h"
#include "RequestGroup.h"
#include "DownloadContext.h"
namespace aria2 {

View File

@ -46,6 +46,7 @@
#include "HttpResponse.h"
#include "HttpHeader.h"
#include "Range.h"
#include "DownloadContext.h"
namespace aria2 {

View File

@ -47,6 +47,7 @@
#include "message.h"
#include "prefs.h"
#include "A2STR.h"
#include "DownloadContext.h"
namespace aria2 {

View File

@ -36,6 +36,7 @@
#include "HttpProxyResponseCommand.h"
#include "Request.h"
#include "Socket.h"
#include "DownloadContext.h"
namespace aria2 {

View File

@ -39,6 +39,7 @@
#include "HttpRequest.h"
#include "Segment.h"
#include "Socket.h"
#include "DownloadContext.h"
namespace aria2 {

View File

@ -53,8 +53,7 @@ namespace aria2 {
const std::string HttpRequest::USER_AGENT("aria2");
HttpRequest::HttpRequest():entityLength(0),
_contentEncodingEnabled(true),
HttpRequest::HttpRequest():_contentEncodingEnabled(true),
userAgent(USER_AGENT)
{}
@ -83,7 +82,8 @@ off_t HttpRequest::getEndByte() const
return 0;
} else {
if(request->isPipeliningEnabled()) {
return _fileEntry->gtoloff(segment->getPosition()+segment->getLength()-1);
off_t endByte = _fileEntry->gtoloff(segment->getPosition()+segment->getLength()-1);
return std::min(endByte, _fileEntry->getLastOffset()-1);
} else {
return 0;
}
@ -96,7 +96,8 @@ RangeHandle HttpRequest::getRange() const
if(segment.isNull()) {
return SharedHandle<Range>(new Range());
} else {
return SharedHandle<Range>(new Range(getStartByte(), getEndByte(), entityLength));
return SharedHandle<Range>(new Range(getStartByte(), getEndByte(),
_fileEntry->getLength()));
}
}
@ -107,9 +108,9 @@ bool HttpRequest::isRangeSatisfied(const RangeHandle& range) const
}
if((getStartByte() == range->getStartByte()) &&
((getEndByte() == 0) ||
((getEndByte() > 0) && (getEndByte() == range->getEndByte()))) &&
((entityLength == 0) ||
((entityLength > 0) && (entityLength == range->getEntityLength())))) {
(getEndByte() == range->getEndByte())) &&
((_fileEntry->getLength() == 0) ||
(_fileEntry->getLength() == range->getEntityLength()))) {
return true;
} else {
return false;

View File

@ -37,6 +37,7 @@
#include "common.h"
#include <cassert>
#include <string>
#include <deque>
@ -64,8 +65,6 @@ private:
SharedHandle<Segment> segment;
uint64_t entityLength;
bool _contentEncodingEnabled;
std::string userAgent;
@ -97,17 +96,10 @@ public:
void setRequest(const SharedHandle<Request>& request);
/**
* entityLength is used in isRangeSatisfied() method.
*/
void setEntityLength(uint64_t entityLength)
{
this->entityLength = entityLength;
}
uint64_t getEntityLength() const
{
return entityLength;
assert(!_fileEntry.isNull());
return _fileEntry->getLength();
}
const std::string& getHost() const

View File

@ -52,6 +52,7 @@
#include "CookieStorage.h"
#include "AuthConfigFactory.h"
#include "AuthConfig.h"
#include "DownloadContext.h"
namespace aria2 {
@ -89,7 +90,6 @@ createHttpRequest(const SharedHandle<Request>& req,
httpRequest->setRequest(req);
httpRequest->setFileEntry(fileEntry);
httpRequest->setSegment(segment);
httpRequest->setEntityLength(fileEntry->getLength());
httpRequest->addHeader(option->get(PREF_HEADER));
httpRequest->setCookieStorage(cookieStorage);
httpRequest->setAuthConfigFactory(authConfigFactory);

View File

@ -34,7 +34,7 @@
/* copyright --> */
#include "HttpResponseCommand.h"
#include "DownloadEngine.h"
#include "SingleFileDownloadContext.h"
#include "DownloadContext.h"
#include "FileEntry.h"
#include "RequestGroup.h"
#include "ServerHost.h"
@ -136,18 +136,20 @@ bool HttpResponseCommand::executeInternal()
}
if(_requestGroup->getPieceStorage().isNull()) {
uint64_t totalLength = httpResponse->getEntityLength();
SingleFileDownloadContextHandle dctx =
dynamic_pointer_cast<SingleFileDownloadContext>(_requestGroup->getDownloadContext());
dctx->setTotalLength(totalLength);
_fileEntry->setLength(totalLength);
dctx->setFilename
(strconcat(dctx->getDir(), "/", httpResponse->determinFilename()));
dctx->setContentType(httpResponse->getContentType());
// We assume that in this context
// DownloadContext::getFileEntries().size() == 1
if(getOption()->get(PREF_OUT).empty()) {
_fileEntry->setPath
(strconcat(getDownloadContext()->getDir(),
"/", httpResponse->determinFilename()));
}
_fileEntry->setContentType(httpResponse->getContentType());
_requestGroup->preDownloadProcessing();
if(e->_requestGroupMan->isSameFileBeingDownloaded(_requestGroup)) {
throw DOWNLOAD_FAILURE_EXCEPTION
(StringFormat(EX_DUPLICATE_FILE_DOWNLOAD,
_requestGroup->getFilePath().c_str()).str());
_requestGroup->getFirstFilePath().c_str()).str());
}
// update last modified time
updateLastModifiedTime(httpResponse->getLastModifiedTime());
@ -156,13 +158,13 @@ bool HttpResponseCommand::executeInternal()
// assume we can do segmented downloading
if(totalLength == 0 || shouldInflateContentEncoding(httpResponse)) {
// we ignore content-length when inflate is required
dctx->setTotalLength(0);
_fileEntry->setLength(0);
if(req->getMethod() == Request::METHOD_GET &&
(totalLength != 0 ||
!httpResponse->getHttpHeader()->defined(HttpHeader::CONTENT_LENGTH))){
// dctx->knowsTotalLength() == true only when server says the
// size of file is 0 explicitly.
dctx->markTotalLengthIsUnknown();
// DownloadContext::knowsTotalLength() == true only when
// server says the size of file is 0 explicitly.
getDownloadContext()->markTotalLengthIsUnknown();
}
return handleOtherEncoding(httpResponse);
} else {
@ -241,7 +243,7 @@ bool HttpResponseCommand::handleDefaultEncoding(const HttpResponseHandle& httpRe
logger->notice(MSG_DOWNLOAD_ALREADY_COMPLETED,
_requestGroup->getGID(),
_requestGroup->getFilePath().c_str());
_requestGroup->getFirstFilePath().c_str());
return true;
}
@ -249,7 +251,7 @@ bool HttpResponseCommand::handleDefaultEncoding(const HttpResponseHandle& httpRe
DownloadCommand* command = 0;
try {
_requestGroup->loadAndOpenFile(infoFile);
File file(_requestGroup->getFilePath());
File file(_requestGroup->getFirstFilePath());
SegmentHandle segment = _requestGroup->getSegmentMan()->getSegment(cuid, 0);
// pipelining requires implicit range specified. But the request for
@ -335,7 +337,7 @@ bool HttpResponseCommand::handleOtherEncoding(const HttpResponseHandle& httpResp
logger->notice(MSG_DOWNLOAD_ALREADY_COMPLETED,
_requestGroup->getGID(),
_requestGroup->getFilePath().c_str());
_requestGroup->getFirstFilePath().c_str());
poolConnection();
return true;

View File

@ -49,6 +49,7 @@
#include "XmlRpcMethod.h"
#include "XmlRpcMethodFactory.h"
#include "XmlRpcResponse.h"
#include "DownloadContext.h"
namespace aria2 {

View File

@ -46,6 +46,7 @@
#include "prefs.h"
#include "Option.h"
#include "Util.h"
#include "DownloadContext.h"
namespace aria2 {

View File

@ -53,6 +53,7 @@
#include "CookieStorage.h"
#include "AuthConfigFactory.h"
#include "AuthConfig.h"
#include "DownloadContext.h"
namespace aria2 {

View File

@ -43,6 +43,7 @@
#include "Option.h"
#include "prefs.h"
#include "SocketCore.h"
#include "DownloadContext.h"
namespace aria2 {

View File

@ -43,7 +43,6 @@
#include "Logger.h"
#include "Peer.h"
#include "PeerConnection.h"
#include "BtContext.h"
#include "BtRuntime.h"
#include "PeerStorage.h"
#include "PieceStorage.h"
@ -52,6 +51,8 @@
#include "ARC4Encryptor.h"
#include "ARC4Decryptor.h"
#include "RequestGroup.h"
#include "DownloadContext.h"
#include "bittorrent_helper.h"
namespace aria2 {
@ -60,13 +61,11 @@ InitiatorMSEHandshakeCommand::InitiatorMSEHandshakeCommand
RequestGroup* requestGroup,
const SharedHandle<Peer>& p,
DownloadEngine* e,
const SharedHandle<BtContext>& btContext,
const SharedHandle<BtRuntime>& btRuntime,
const SharedHandle<SocketCore>& s):
PeerAbstractCommand(cuid, p, e, s),
_requestGroup(requestGroup),
_btContext(btContext),
_btRuntime(btRuntime),
_sequence(INITIATOR_SEND_KEY),
_mseHandshake(new MSEHandshake(cuid, socket, getOption().get()))
@ -114,7 +113,8 @@ bool InitiatorMSEHandshakeCommand::executeInternal() {
break;
case INITIATOR_WAIT_KEY: {
if(_mseHandshake->receivePublicKey()) {
_mseHandshake->initCipher(_btContext->getInfoHash());
_mseHandshake->initCipher
(bittorrent::getInfoHash(_requestGroup->getDownloadContext()));;
if(_mseHandshake->sendInitiatorStep2()) {
_sequence = INITIATOR_FIND_VC_MARKER;
} else {
@ -151,11 +151,11 @@ bool InitiatorMSEHandshakeCommand::executeInternal() {
_mseHandshake->getDecryptor());
}
PeerInteractionCommand* c =
new PeerInteractionCommand(cuid, _requestGroup, peer, e, _btContext,
_btRuntime, _pieceStorage,
socket,
PeerInteractionCommand::INITIATOR_SEND_HANDSHAKE,
peerConnection);
new PeerInteractionCommand
(cuid, _requestGroup, peer, e, _btRuntime, _pieceStorage,
socket,
PeerInteractionCommand::INITIATOR_SEND_HANDSHAKE,
peerConnection);
c->setPeerStorage(_peerStorage);
e->commands.push_back(c);
return true;
@ -176,7 +176,7 @@ bool InitiatorMSEHandshakeCommand::prepareForNextPeer(time_t wait)
peer->usedBy(e->newCUID());
PeerInitiateConnectionCommand* command =
new PeerInitiateConnectionCommand(peer->usedBy(), _requestGroup, peer,
e, _btContext, _btRuntime);
e, _btRuntime);
command->setPeerStorage(_peerStorage);
command->setPieceStorage(_pieceStorage);
e->commands.push_back(command);
@ -187,7 +187,7 @@ bool InitiatorMSEHandshakeCommand::prepareForNextPeer(time_t wait)
logger->info("CUID#%d - Retry using legacy BitTorrent handshake.", cuid);
PeerInitiateConnectionCommand* command =
new PeerInitiateConnectionCommand(cuid, _requestGroup, peer, e,
_btContext, _btRuntime, false);
_btRuntime, false);
command->setPeerStorage(_peerStorage);
command->setPieceStorage(_pieceStorage);
e->commands.push_back(command);

View File

@ -40,7 +40,6 @@
namespace aria2 {
class RequestGroup;
class BtContext;
class PeerStorage;
class PieceStorage;
class BtRuntime;
@ -61,8 +60,6 @@ public:
private:
RequestGroup* _requestGroup;
SharedHandle<BtContext> _btContext;
SharedHandle<PeerStorage> _peerStorage;
SharedHandle<PieceStorage> _pieceStorage;
@ -79,13 +76,13 @@ protected:
virtual void onAbort();
virtual bool exitBeforeExecute();
public:
InitiatorMSEHandshakeCommand(int32_t cuid,
RequestGroup* requestGroup,
const SharedHandle<Peer>& peer,
DownloadEngine* e,
const SharedHandle<BtContext>& btContext,
const SharedHandle<BtRuntime>& btRuntime,
const SharedHandle<SocketCore>& s);
InitiatorMSEHandshakeCommand
(int32_t cuid,
RequestGroup* requestGroup,
const SharedHandle<Peer>& peer,
DownloadEngine* e,
const SharedHandle<BtRuntime>& btRuntime,
const SharedHandle<SocketCore>& s);
virtual ~InitiatorMSEHandshakeCommand();

View File

@ -38,7 +38,6 @@
#include "Util.h"
#include "message.h"
#include "SingleFileDownloadContext.h"
#include "PieceStorage.h"
#include "messageDigest.h"
#include "LogFactory.h"
@ -46,14 +45,16 @@
#include "DiskAdaptor.h"
#include "FileEntry.h"
#include "BitfieldMan.h"
#include "DownloadContext.h"
namespace aria2 {
#define BUFSIZE (256*1024)
#define ALIGNMENT 512
IteratableChecksumValidator::IteratableChecksumValidator(const SingleFileDownloadContextHandle& dctx,
const PieceStorageHandle& pieceStorage):
IteratableChecksumValidator::IteratableChecksumValidator
(const SharedHandle<DownloadContext>& dctx,
const PieceStorageHandle& pieceStorage):
_dctx(dctx),
_pieceStorage(pieceStorage),
_currentOffset(0),

View File

@ -39,7 +39,7 @@
namespace aria2 {
class SingleFileDownloadContext;
class DownloadContext;
class PieceStorage;
class Logger;
class MessageDigestContext;
@ -47,7 +47,7 @@ class MessageDigestContext;
class IteratableChecksumValidator:public IteratableValidator
{
private:
SharedHandle<SingleFileDownloadContext> _dctx;
SharedHandle<DownloadContext> _dctx;
SharedHandle<PieceStorage> _pieceStorage;
@ -60,7 +60,7 @@ private:
unsigned char* _buffer;
public:
IteratableChecksumValidator(const SharedHandle<SingleFileDownloadContext>& dctx,
IteratableChecksumValidator(const SharedHandle<DownloadContext>& dctx,
const SharedHandle<PieceStorage>& pieceStorage);
virtual ~IteratableChecksumValidator();

View File

@ -58,7 +58,7 @@ namespace aria2 {
#define ALIGNMENT 512
IteratableChunkChecksumValidator::
IteratableChunkChecksumValidator(const DownloadContextHandle& dctx,
IteratableChunkChecksumValidator(const SharedHandle<DownloadContext>& dctx,
const PieceStorageHandle& pieceStorage):
_dctx(dctx),
_pieceStorage(pieceStorage),
@ -153,7 +153,7 @@ std::string IteratableChunkChecksumValidator::digest(off_t offset, size_t length
curoffset);
if(r == 0 || r < static_cast<size_t>(woffset)) {
throw DL_ABORT_EX
(StringFormat(EX_FILE_READ, _dctx->getActualBasePath().c_str(),
(StringFormat(EX_FILE_READ, _dctx->getBasePath().c_str(),
strerror(errno)).str());
}
size_t wlength;

View File

@ -50,10 +50,11 @@
#include "MessageDigestHelper.h"
#include "SimpleRandomizer.h"
#include "Util.h"
#include "BtContext.h"
#include "DownloadContext.h"
#include "prefs.h"
#include "Option.h"
#include "StringFormat.h"
#include "bittorrent_helper.h"
namespace aria2 {
@ -435,7 +436,7 @@ bool MSEHandshake::findReceiverHashMarker()
}
bool MSEHandshake::receiveReceiverHashAndPadCLength
(const std::deque<SharedHandle<BtContext> >& btContexts)
(const std::deque<SharedHandle<DownloadContext> >& downloadContexts)
{
size_t r = 20+VC_LENGTH+CRYPTO_BITFIELD_LENGTH+2/*PadC length*/-_rbufLength;
if(r > receiveNBytes(r)) {
@ -444,22 +445,23 @@ bool MSEHandshake::receiveReceiverHashAndPadCLength
// resolve info hash
// pointing to the position of HASH('req2', SKEY) xor HASH('req3', S)
unsigned char* rbufptr = _rbuf;
SharedHandle<BtContext> btContext;
for(std::deque<SharedHandle<BtContext> >::const_iterator i = btContexts.begin();
i != btContexts.end(); ++i) {
SharedHandle<DownloadContext> downloadContext;
for(std::deque<SharedHandle<DownloadContext> >::const_iterator i =
downloadContexts.begin(); i != downloadContexts.end(); ++i) {
unsigned char md[20];
createReq23Hash(md, (*i)->getInfoHash());
const BDE& torrentAttrs = (*i)->getAttribute(bittorrent::BITTORRENT);
createReq23Hash(md, torrentAttrs[bittorrent::INFO_HASH].uc());
if(memcmp(md, rbufptr, sizeof(md)) == 0) {
_logger->debug("CUID#%d - info hash found: %s", _cuid, (*i)->getInfoHashAsString().c_str());
btContext = *i;
_logger->debug("CUID#%d - info hash found: %s", _cuid,
Util::toHex(torrentAttrs[bittorrent::INFO_HASH].s()).c_str());
downloadContext = *i;
break;
}
}
if(btContext.isNull()) {
if(downloadContext.isNull()) {
throw DL_ABORT_EX("Unknown info hash.");
}
initCipher(btContext->getInfoHash());
initCipher(bittorrent::getInfoHash(downloadContext));
// decrypt VC
rbufptr += 20;
verifyVC(rbufptr);

View File

@ -51,7 +51,7 @@ class SocketCore;
class DHKeyExchange;
class ARC4Encryptor;
class ARC4Decryptor;
class BtContext;
class DownloadContext;
class MSEHandshake {
public:
@ -158,7 +158,7 @@ public:
bool findReceiverHashMarker();
bool receiveReceiverHashAndPadCLength
(const std::deque<SharedHandle<BtContext> >& btContexts);
(const std::deque<SharedHandle<DownloadContext> >& downloadContexts);
bool receiveReceiverIALength();

View File

@ -152,7 +152,6 @@ SRCS = Socket.h\
ByteArrayDiskWriterFactory.cc ByteArrayDiskWriterFactory.h\
ServerHost.cc ServerHost.h\
DownloadContext.cc DownloadContext.h\
SingleFileDownloadContext.cc SingleFileDownloadContext.h\
TimedHaltCommand.cc TimedHaltCommand.h\
CUIDCounter.h\
DNSCache.h\
@ -294,8 +293,6 @@ SRCS += PeerMessageUtil.cc PeerMessageUtil.h\
PeerListProcessor.h\
AnnounceTier.h\
AnnounceList.h AnnounceList.cc\
BtContext.h\
DefaultBtContext.cc DefaultBtContext.h\
PeerStorage.h\
DefaultPeerStorage.cc DefaultPeerStorage.h\
BtAnnounce.cc BtAnnounce.h\
@ -436,7 +433,8 @@ SRCS += PeerMessageUtil.cc PeerMessageUtil.h\
RangeBtMessageValidator.h\
IndexBtMessageValidator.h\
ExtensionMessageRegistry.h\
bencode.cc bencode.h
bencode.cc bencode.h\
bittorrent_helper.cc bittorrent_helper.h
endif # ENABLE_BITTORRENT
if ENABLE_METALINK

View File

@ -89,8 +89,6 @@ bin_PROGRAMS = aria2c$(EXEEXT)
@ENABLE_BITTORRENT_TRUE@ PeerListProcessor.h\
@ENABLE_BITTORRENT_TRUE@ AnnounceTier.h\
@ENABLE_BITTORRENT_TRUE@ AnnounceList.h AnnounceList.cc\
@ENABLE_BITTORRENT_TRUE@ BtContext.h\
@ENABLE_BITTORRENT_TRUE@ DefaultBtContext.cc DefaultBtContext.h\
@ENABLE_BITTORRENT_TRUE@ PeerStorage.h\
@ENABLE_BITTORRENT_TRUE@ DefaultPeerStorage.cc DefaultPeerStorage.h\
@ENABLE_BITTORRENT_TRUE@ BtAnnounce.cc BtAnnounce.h\
@ -231,7 +229,8 @@ bin_PROGRAMS = aria2c$(EXEEXT)
@ENABLE_BITTORRENT_TRUE@ RangeBtMessageValidator.h\
@ENABLE_BITTORRENT_TRUE@ IndexBtMessageValidator.h\
@ENABLE_BITTORRENT_TRUE@ ExtensionMessageRegistry.h\
@ENABLE_BITTORRENT_TRUE@ bencode.cc bencode.h
@ENABLE_BITTORRENT_TRUE@ bencode.cc bencode.h\
@ENABLE_BITTORRENT_TRUE@ bittorrent_helper.cc bittorrent_helper.h
@ENABLE_METALINK_TRUE@am__append_14 = Metalinker.cc Metalinker.h\
@ENABLE_METALINK_TRUE@ MetalinkEntry.cc MetalinkEntry.h\
@ -400,11 +399,10 @@ am__libaria2c_a_SOURCES_DIST = Socket.h SocketCore.cc SocketCore.h \
PeerConnection.cc PeerConnection.h ByteArrayDiskWriter.cc \
ByteArrayDiskWriter.h ByteArrayDiskWriterFactory.cc \
ByteArrayDiskWriterFactory.h ServerHost.cc ServerHost.h \
DownloadContext.cc DownloadContext.h \
SingleFileDownloadContext.cc SingleFileDownloadContext.h \
TimedHaltCommand.cc TimedHaltCommand.h CUIDCounter.h \
DNSCache.h DownloadResult.h Sequence.h IntSequence.h \
PostDownloadHandler.h PreDownloadHandler.h SingletonHolder.h \
DownloadContext.cc DownloadContext.h TimedHaltCommand.cc \
TimedHaltCommand.h CUIDCounter.h DNSCache.h DownloadResult.h \
Sequence.h IntSequence.h PostDownloadHandler.h \
PreDownloadHandler.h SingletonHolder.h \
TrueRequestGroupCriteria.h a2algo.h a2functional.h a2io.h \
a2netcompat.h a2time.h array_fun.h help_tags.h prefs.cc \
prefs.h usage_text.h ProtocolDetector.cc ProtocolDetector.h \
@ -466,8 +464,7 @@ am__libaria2c_a_SOURCES_DIST = Socket.h SocketCore.cc SocketCore.h \
TimeSeedCriteria.h ShareRatioSeedCriteria.h \
UnionSeedCriteria.h SeedCheckCommand.cc SeedCheckCommand.h \
PeerListProcessor.h AnnounceTier.h AnnounceList.h \
AnnounceList.cc BtContext.h DefaultBtContext.cc \
DefaultBtContext.h PeerStorage.h DefaultPeerStorage.cc \
AnnounceList.cc PeerStorage.h DefaultPeerStorage.cc \
DefaultPeerStorage.h BtAnnounce.cc BtAnnounce.h \
DefaultBtAnnounce.cc DefaultBtAnnounce.h BtRuntime.h \
BtMessage.h AbstractBtMessage.cc AbstractBtMessage.h \
@ -564,9 +561,9 @@ am__libaria2c_a_SOURCES_DIST = Socket.h SocketCore.cc SocketCore.h \
IndexBtMessage.cc IndexBtMessage.h ZeroBtMessage.cc \
ZeroBtMessage.h RangeBtMessageValidator.h \
IndexBtMessageValidator.h ExtensionMessageRegistry.h \
bencode.cc bencode.h Metalinker.cc Metalinker.h \
MetalinkEntry.cc MetalinkEntry.h MetalinkResource.cc \
MetalinkResource.h MetalinkProcessor.h \
bencode.cc bencode.h bittorrent_helper.cc bittorrent_helper.h \
Metalinker.cc Metalinker.h MetalinkEntry.cc MetalinkEntry.h \
MetalinkResource.cc MetalinkResource.h MetalinkProcessor.h \
MetalinkParserController.cc MetalinkParserController.h \
MetalinkParserStateMachine.cc MetalinkParserStateMachine.h \
MetalinkParserState.h InitialMetalinkParserState.cc \
@ -638,7 +635,6 @@ am__objects_6 =
@ENABLE_BITTORRENT_TRUE@ PeerChokeCommand.$(OBJEXT) \
@ENABLE_BITTORRENT_TRUE@ SeedCheckCommand.$(OBJEXT) \
@ENABLE_BITTORRENT_TRUE@ AnnounceList.$(OBJEXT) \
@ENABLE_BITTORRENT_TRUE@ DefaultBtContext.$(OBJEXT) \
@ENABLE_BITTORRENT_TRUE@ DefaultPeerStorage.$(OBJEXT) \
@ENABLE_BITTORRENT_TRUE@ BtAnnounce.$(OBJEXT) \
@ENABLE_BITTORRENT_TRUE@ DefaultBtAnnounce.$(OBJEXT) \
@ -734,7 +730,8 @@ am__objects_6 =
@ENABLE_BITTORRENT_TRUE@ RangeBtMessage.$(OBJEXT) \
@ENABLE_BITTORRENT_TRUE@ IndexBtMessage.$(OBJEXT) \
@ENABLE_BITTORRENT_TRUE@ ZeroBtMessage.$(OBJEXT) \
@ENABLE_BITTORRENT_TRUE@ bencode.$(OBJEXT)
@ENABLE_BITTORRENT_TRUE@ bencode.$(OBJEXT) \
@ENABLE_BITTORRENT_TRUE@ bittorrent_helper.$(OBJEXT)
@ENABLE_METALINK_TRUE@am__objects_14 = Metalinker.$(OBJEXT) \
@ENABLE_METALINK_TRUE@ MetalinkEntry.$(OBJEXT) \
@ENABLE_METALINK_TRUE@ MetalinkResource.$(OBJEXT) \
@ -833,10 +830,9 @@ am__objects_26 = SocketCore.$(OBJEXT) Command.$(OBJEXT) \
MultiFileAllocationIterator.$(OBJEXT) PeerConnection.$(OBJEXT) \
ByteArrayDiskWriter.$(OBJEXT) \
ByteArrayDiskWriterFactory.$(OBJEXT) ServerHost.$(OBJEXT) \
DownloadContext.$(OBJEXT) SingleFileDownloadContext.$(OBJEXT) \
TimedHaltCommand.$(OBJEXT) prefs.$(OBJEXT) \
ProtocolDetector.$(OBJEXT) StringFormat.$(OBJEXT) \
HttpSkipResponseCommand.$(OBJEXT) \
DownloadContext.$(OBJEXT) TimedHaltCommand.$(OBJEXT) \
prefs.$(OBJEXT) ProtocolDetector.$(OBJEXT) \
StringFormat.$(OBJEXT) HttpSkipResponseCommand.$(OBJEXT) \
InitiateConnectionCommand.$(OBJEXT) \
FtpFinishDownloadCommand.$(OBJEXT) A2STR.$(OBJEXT) \
RarestPieceSelector.$(OBJEXT) ChunkedDecoder.$(OBJEXT) \
@ -1157,11 +1153,10 @@ SRCS = Socket.h SocketCore.cc SocketCore.h BinaryStream.h Command.cc \
PeerConnection.cc PeerConnection.h ByteArrayDiskWriter.cc \
ByteArrayDiskWriter.h ByteArrayDiskWriterFactory.cc \
ByteArrayDiskWriterFactory.h ServerHost.cc ServerHost.h \
DownloadContext.cc DownloadContext.h \
SingleFileDownloadContext.cc SingleFileDownloadContext.h \
TimedHaltCommand.cc TimedHaltCommand.h CUIDCounter.h \
DNSCache.h DownloadResult.h Sequence.h IntSequence.h \
PostDownloadHandler.h PreDownloadHandler.h SingletonHolder.h \
DownloadContext.cc DownloadContext.h TimedHaltCommand.cc \
TimedHaltCommand.h CUIDCounter.h DNSCache.h DownloadResult.h \
Sequence.h IntSequence.h PostDownloadHandler.h \
PreDownloadHandler.h SingletonHolder.h \
TrueRequestGroupCriteria.h a2algo.h a2functional.h a2io.h \
a2netcompat.h a2time.h array_fun.h help_tags.h prefs.cc \
prefs.h usage_text.h ProtocolDetector.cc ProtocolDetector.h \
@ -1393,7 +1388,6 @@ distclean-compile:
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/DHTUtil.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/DefaultAuthResolver.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/DefaultBtAnnounce.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/DefaultBtContext.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/DefaultBtInteractive.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/DefaultBtMessageDispatcher.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/DefaultBtMessageFactory.Po@am__quote@
@ -1541,7 +1535,6 @@ distclean-compile:
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/SimpleLogger.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/SimpleRandomizer.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/SingleFileAllocationIterator.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/SingleFileDownloadContext.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/SizeMetalinkParserState.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/SkipTagMetalinkParserState.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/SleepCommand.Po@am__quote@
@ -1578,6 +1571,7 @@ distclean-compile:
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ZeroBtMessage.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/asctime_r.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/bencode.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/bittorrent_helper.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/download_helper.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/gai_strerror.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/getaddrinfo.Po@am__quote@

View File

@ -43,7 +43,7 @@
#include "prefs.h"
#include "Util.h"
#include "message.h"
#include "SingleFileDownloadContext.h"
#include "DownloadContext.h"
#include "MetalinkHelper.h"
#include "BinaryStream.h"
#include "MemoryBufferPreDownloadHandler.h"
@ -170,10 +170,11 @@ Metalink2RequestGroup::createRequestGroup
std::deque<std::string> uris;
uris.push_back((*itr)->url);
torrentRg.reset(new RequestGroup(option, uris));
SharedHandle<SingleFileDownloadContext> dctx
(new SingleFileDownloadContext(option->getAsInt(PREF_SEGMENT_SIZE),
0,
A2STR::NIL));
SharedHandle<DownloadContext> dctx
(new DownloadContext(option->getAsInt(PREF_SEGMENT_SIZE),
0,
A2STR::NIL));
// Since torrent is downloaded in memory, setting dir has no effect.
//dctx->setDir(_option->get(PREF_DIR));
torrentRg->setDownloadContext(dctx);
torrentRg->clearPreDowloadHandler();
@ -208,11 +209,10 @@ Metalink2RequestGroup::createRequestGroup
#else
pieceLength = option->getAsInt(PREF_SEGMENT_SIZE);
#endif // ENABLE_MESSAGE_DIGEST
SharedHandle<SingleFileDownloadContext> dctx
(new SingleFileDownloadContext
SharedHandle<DownloadContext> dctx
(new DownloadContext
(pieceLength,
entry->getLength(),
A2STR::NIL,
strconcat(option->get(PREF_DIR), "/", entry->file->getPath())));
dctx->setDir(option->get(PREF_DIR));
#ifdef ENABLE_MESSAGE_DIGEST
@ -222,7 +222,8 @@ Metalink2RequestGroup::createRequestGroup
dctx->setChecksumHashAlgo(entry->checksum->getAlgo());
}
} else {
dctx->setPieceHashes(entry->chunkChecksum->getChecksums());
dctx->setPieceHashes(entry->chunkChecksum->getChecksums().begin(),
entry->chunkChecksum->getChecksums().end());
dctx->setPieceHashAlgo(entry->chunkChecksum->getAlgo());
}
#endif // ENABLE_MESSAGE_DIGEST

View File

@ -62,7 +62,7 @@ void MetalinkPostDownloadHandler::getNextRequestGroups
RequestGroup* requestGroup)
{
_logger->debug("Generating RequestGroups for Metalink file %s",
requestGroup->getFilePath().c_str());
requestGroup->getFirstFilePath().c_str());
SharedHandle<DiskAdaptor> diskAdaptor = requestGroup->getPieceStorage()->getDiskAdaptor();
try {
diskAdaptor->openExistingFile();

View File

@ -166,7 +166,7 @@ void MultiDiskAdaptor::resetDiskWriterEntries()
return;
}
for(std::deque<SharedHandle<FileEntry> >::const_iterator i =
for(std::vector<SharedHandle<FileEntry> >::const_iterator i =
fileEntries.begin(); i != fileEntries.end(); ++i) {
diskWriterEntries.push_back
(createDiskWriterEntry(*i, (*i)->isRequested()));
@ -176,9 +176,9 @@ void MultiDiskAdaptor::resetDiskWriterEntries()
// TODO Currently, pieceLength == 0 is used for unit testing only.
if(pieceLength > 0) {
std::deque<SharedHandle<DiskWriterEntry> >::iterator done =
std::vector<SharedHandle<DiskWriterEntry> >::iterator done =
diskWriterEntries.begin();
for(std::deque<SharedHandle<DiskWriterEntry> >::iterator itr =
for(std::vector<SharedHandle<DiskWriterEntry> >::iterator itr =
diskWriterEntries.begin(); itr != diskWriterEntries.end();) {
const SharedHandle<FileEntry>& fileEntry = (*itr)->getFileEntry();
@ -189,7 +189,7 @@ void MultiDiskAdaptor::resetDiskWriterEntries()
off_t pieceStartOffset =
(fileEntry->getOffset()/pieceLength)*pieceLength;
if(itr != diskWriterEntries.begin()) {
for(std::deque<SharedHandle<DiskWriterEntry> >::iterator i =
for(std::vector<SharedHandle<DiskWriterEntry> >::iterator i =
itr-1; true; --i) {
const SharedHandle<FileEntry>& fileEntry = (*i)->getFileEntry();
if(pieceStartOffset <= fileEntry->getOffset() ||
@ -240,7 +240,7 @@ void MultiDiskAdaptor::resetDiskWriterEntries()
}
}
DefaultDiskWriterFactory dwFactory;
for(std::deque<SharedHandle<DiskWriterEntry> >::iterator i =
for(std::vector<SharedHandle<DiskWriterEntry> >::iterator i =
diskWriterEntries.begin(); i != diskWriterEntries.end(); ++i) {
if((*i)->needsFileAllocation() ||
dwreq.find((*i)->getFileEntry()->getPath()) != dwreq.end() ||
@ -260,7 +260,7 @@ void MultiDiskAdaptor::resetDiskWriterEntries()
void MultiDiskAdaptor::mkdir() const
{
for(std::deque<SharedHandle<DiskWriterEntry> >::const_iterator i =
for(std::vector<SharedHandle<DiskWriterEntry> >::const_iterator i =
diskWriterEntries.begin(); i != diskWriterEntries.end(); ++i) {
(*i)->getFileEntry()->setupDir();
}
@ -279,7 +279,7 @@ void MultiDiskAdaptor::openIfNot
// Cache is full.
// Choose one DiskWriterEntry randomly and close it.
size_t index = SimpleRandomizer::getInstance()->getRandomNumber(numOpened);
std::deque<SharedHandle<DiskWriterEntry> >::iterator i =
std::vector<SharedHandle<DiskWriterEntry> >::iterator i =
_openedDiskWriterEntries.begin();
std::advance(i, index);
(*i)->closeFile();
@ -443,7 +443,7 @@ ssize_t MultiDiskAdaptor::readData(unsigned char* data, size_t len, off_t offset
bool MultiDiskAdaptor::fileExists()
{
for(std::deque<SharedHandle<FileEntry> >::iterator i =
for(std::vector<SharedHandle<FileEntry> >::iterator i =
fileEntries.begin(); i != fileEntries.end(); ++i) {
if((*i)->exists()) {
return true;
@ -455,10 +455,9 @@ bool MultiDiskAdaptor::fileExists()
uint64_t MultiDiskAdaptor::size()
{
uint64_t size = 0;
for(DiskWriterEntries::const_iterator itr = diskWriterEntries.begin();
itr != diskWriterEntries.end(); ++itr) {
openIfNot(*itr, &DiskWriterEntry::openFile);
size += (*itr)->size();
for(std::vector<SharedHandle<FileEntry> >::iterator i =
fileEntries.begin(); i != fileEntries.end(); ++i) {
size += File((*i)->getPath()).size();
}
return size;
}
@ -496,7 +495,7 @@ void MultiDiskAdaptor::disableReadOnly()
void MultiDiskAdaptor::cutTrailingGarbage()
{
for(std::deque<SharedHandle<DiskWriterEntry> >::const_iterator i =
for(std::vector<SharedHandle<DiskWriterEntry> >::const_iterator i =
diskWriterEntries.begin(); i != diskWriterEntries.end(); ++i) {
uint64_t length = (*i)->getFileEntry()->getLength();
if(File((*i)->getFilePath()).size() > length) {
@ -515,7 +514,7 @@ void MultiDiskAdaptor::setMaxOpenFiles(size_t maxOpenFiles)
size_t MultiDiskAdaptor::utime(const Time& actime, const Time& modtime)
{
size_t numOK = 0;
for(std::deque<SharedHandle<FileEntry> >::const_iterator i =
for(std::vector<SharedHandle<FileEntry> >::const_iterator i =
fileEntries.begin(); i != fileEntries.end(); ++i) {
if((*i)->isRequested()) {
File f((*i)->getPath());

Some files were not shown because too many files have changed in this diff Show More