diff options
183 files changed, 2981 insertions, 3444 deletions
@@ -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. diff --git a/src/AbstractBtMessage.cc b/src/AbstractBtMessage.cc index f2aa432b..d68d49b1 100644 --- a/src/AbstractBtMessage.cc +++ b/src/AbstractBtMessage.cc @@ -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; diff --git a/src/AbstractBtMessage.h b/src/AbstractBtMessage.h index 16fa4013..588bc96e 100644 --- a/src/AbstractBtMessage.h +++ b/src/AbstractBtMessage.h @@ -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); diff --git a/src/AbstractCommand.cc b/src/AbstractCommand.cc index b1004a48..57f1a80a 100644 --- a/src/AbstractCommand.cc +++ b/src/AbstractCommand.cc @@ -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. diff --git a/src/AbstractCommand.h b/src/AbstractCommand.h index ba7a7aa4..d7ced6d2 100644 --- a/src/AbstractCommand.h +++ b/src/AbstractCommand.h @@ -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; diff --git a/src/AbstractProxyRequestCommand.cc b/src/AbstractProxyRequestCommand.cc index 4fc3fadc..698f5bb4 100644 --- a/src/AbstractProxyRequestCommand.cc +++ b/src/AbstractProxyRequestCommand.cc @@ -45,6 +45,7 @@ #include "CookieStorage.h" #include "AuthConfigFactory.h" #include "AuthConfig.h" +#include "DownloadContext.h" namespace aria2 { diff --git a/src/AbstractProxyResponseCommand.cc b/src/AbstractProxyResponseCommand.cc index 00520cc6..f6b1ac13 100644 --- a/src/AbstractProxyResponseCommand.cc +++ b/src/AbstractProxyResponseCommand.cc @@ -45,6 +45,7 @@ #include "DlRetryEx.h" #include "message.h" #include "HttpHeader.h" +#include "DownloadContext.h" namespace aria2 { diff --git a/src/ActivePeerConnectionCommand.cc b/src/ActivePeerConnectionCommand.cc index 7c76ab4a..7f5709d8 100644 --- a/src/ActivePeerConnectionCommand.cc +++ b/src/ActivePeerConnectionCommand.cc @@ -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); diff --git a/src/ActivePeerConnectionCommand.h b/src/ActivePeerConnectionCommand.h index d8c7c452..3f22b6a6 100644 --- a/src/ActivePeerConnectionCommand.h +++ b/src/ActivePeerConnectionCommand.h @@ -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(); diff --git a/src/AnnounceList.cc b/src/AnnounceList.cc index ffe97305..e63d0082 100644 --- a/src/AnnounceList.cc +++ b/src/AnnounceList.cc @@ -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()) { diff --git a/src/BtCheckIntegrityEntry.cc b/src/BtCheckIntegrityEntry.cc index 3197e6c2..8aa1277c 100644 --- a/src/BtCheckIntegrityEntry.cc +++ b/src/BtCheckIntegrityEntry.cc @@ -40,6 +40,7 @@ #include "DiskAdaptor.h" #include "prefs.h" #include "Option.h" +#include "DownloadContext.h" namespace aria2 { diff --git a/src/BtConstants.h b/src/BtConstants.h index 3d1b9cf9..28db953a 100644 --- a/src/BtConstants.h +++ b/src/BtConstants.h @@ -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 diff --git a/src/BtContext.h b/src/BtContext.h deleted file mode 100644 index 858b11cd..00000000 --- a/src/BtContext.h +++ /dev/null @@ -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_ diff --git a/src/BtDependency.cc b/src/BtDependency.cc index 0109f1de..d8f5c0d3 100644 --- a/src/BtDependency.cc +++ b/src/BtDependency.cc @@ -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. diff --git a/src/BtExtendedMessage.cc b/src/BtExtendedMessage.cc index afa19a35..39d61028 100644 --- a/src/BtExtendedMessage.cc +++ b/src/BtExtendedMessage.cc @@ -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" diff --git a/src/BtPieceMessage.cc b/src/BtPieceMessage.cc index 970a35dd..9381d6e4 100644 --- a/src/BtPieceMessage.cc +++ b/src/BtPieceMessage.cc @@ -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 diff --git a/src/BtPieceMessage.h b/src/BtPieceMessage.h index 9b543035..b87cc40b 100644 --- a/src/BtPieceMessage.h +++ b/src/BtPieceMessage.h @@ -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(); diff --git a/src/BtPostDownloadHandler.cc b/src/BtPostDownloadHandler.cc index 29685e6e..5e733d75 100644 --- a/src/BtPostDownloadHandler.cc +++ b/src/BtPostDownloadHandler.cc @@ -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); } diff --git a/src/BtRegistry.cc b/src/BtRegistry.cc index 0c90a20c..158da62b 100644 --- a/src/BtRegistry.cc +++ b/src/BtRegistry.cc @@ -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) diff --git a/src/BtRegistry.h b/src/BtRegistry.h index bc124b3f..3b3858f8 100644 --- a/src/BtRegistry.h +++ b/src/BtRegistry.h @@ -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(); diff --git a/src/BtSetup.cc b/src/BtSetup.cc index 26472d98..ea339780 100644 --- a/src/BtSetup.cc +++ b/src/BtSetup.cc @@ -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); diff --git a/src/CheckIntegrityCommand.cc b/src/CheckIntegrityCommand.cc index 4c2858e1..d7b92a2e 100644 --- a/src/CheckIntegrityCommand.cc +++ b/src/CheckIntegrityCommand.cc @@ -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; } diff --git a/src/CheckIntegrityEntry.cc b/src/CheckIntegrityEntry.cc index 85b493b9..2d2eb569 100644 --- a/src/CheckIntegrityEntry.cc +++ b/src/CheckIntegrityEntry.cc @@ -41,6 +41,7 @@ #include "FileAllocationEntry.h" #include "DownloadEngine.h" #include "Option.h" +#include "DownloadContext.h" namespace aria2 { diff --git a/src/ChecksumCheckIntegrityEntry.cc b/src/ChecksumCheckIntegrityEntry.cc index a7e62a1f..7a0897fa 100644 --- a/src/ChecksumCheckIntegrityEntry.cc +++ b/src/ChecksumCheckIntegrityEntry.cc @@ -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(); } diff --git a/src/ConsoleStatCalc.cc b/src/ConsoleStatCalc.cc index c6c94631..8add12b6 100644 --- a/src/ConsoleStatCalc.cc +++ b/src/ConsoleStatCalc.cc @@ -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: "; diff --git a/src/ContentTypeRequestGroupCriteria.cc b/src/ContentTypeRequestGroupCriteria.cc index cd06ab4b..d479bc88 100644 --- a/src/ContentTypeRequestGroupCriteria.cc +++ b/src/ContentTypeRequestGroupCriteria.cc @@ -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); } } diff --git a/src/DHTGetPeersCommand.cc b/src/DHTGetPeersCommand.cc index 594ff8ff..ee65dd84 100644 --- a/src/DHTGetPeersCommand.cc +++ b/src/DHTGetPeersCommand.cc @@ -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(); diff --git a/src/DHTGetPeersCommand.h b/src/DHTGetPeersCommand.h index f29bbc8e..dee0e21a 100644 --- a/src/DHTGetPeersCommand.h +++ b/src/DHTGetPeersCommand.h @@ -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(); diff --git a/src/DHTPeerLookupTask.cc b/src/DHTPeerLookupTask.cc index 6869498a..6d4a0a9c 100644 --- a/src/DHTPeerLookupTask.cc +++ b/src/DHTPeerLookupTask.cc @@ -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); } } diff --git a/src/DHTPeerLookupTask.h b/src/DHTPeerLookupTask.h index 261e61c3..d448f74a 100644 --- a/src/DHTPeerLookupTask.h +++ b/src/DHTPeerLookupTask.h @@ -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); diff --git a/src/DHTTaskFactory.h b/src/DHTTaskFactory.h index b5c8dcf9..91988e1a 100644 --- a/src/DHTTaskFactory.h +++ b/src/DHTTaskFactory.h @@ -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; diff --git a/src/DHTTaskFactoryImpl.cc b/src/DHTTaskFactoryImpl.cc index d4e93725..6d794b88 100644 --- a/src/DHTTaskFactoryImpl.cc +++ b/src/DHTTaskFactoryImpl.cc @@ -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) { diff --git a/src/DHTTaskFactoryImpl.h b/src/DHTTaskFactoryImpl.h index 9d8c1649..e6f2e5b2 100644 --- a/src/DHTTaskFactoryImpl.h +++ b/src/DHTTaskFactoryImpl.h @@ -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); diff --git a/src/DefaultBtAnnounce.cc b/src/DefaultBtAnnounce.cc index 377b0ae3..3cdeb360 100644 --- a/src/DefaultBtAnnounce.cc +++ b/src/DefaultBtAnnounce.cc @@ -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="; diff --git a/src/DefaultBtAnnounce.h b/src/DefaultBtAnnounce.h index a561bbec..812cb3e8 100644 --- a/src/DefaultBtAnnounce.h +++ b/src/DefaultBtAnnounce.h @@ -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(); diff --git a/src/DefaultBtContext.cc b/src/DefaultBtContext.cc deleted file mode 100644 index 29aaea47..00000000 --- a/src/DefaultBtContext.cc +++ /dev/null @@ -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 diff --git a/src/DefaultBtContext.h b/src/DefaultBtContext.h deleted file mode 100644 index a238c7a8..00000000 --- a/src/DefaultBtContext.h +++ /dev/null @@ -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_ diff --git a/src/DefaultBtInteractive.cc b/src/DefaultBtInteractive.cc index 5d712744..a69a9cdf 100644 --- a/src/DefaultBtInteractive.cc +++ b/src/DefaultBtInteractive.cc @@ -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(); diff --git a/src/DefaultBtInteractive.h b/src/DefaultBtInteractive.h index ee522aed..1129dbeb 100644 --- a/src/DefaultBtInteractive.h +++ b/src/DefaultBtInteractive.h @@ -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(); diff --git a/src/DefaultBtMessageDispatcher.cc b/src/DefaultBtMessageDispatcher.cc index 1adc3565..fa201198 100644 --- a/src/DefaultBtMessageDispatcher.cc +++ b/src/DefaultBtMessageDispatcher.cc @@ -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 diff --git a/src/DefaultBtMessageDispatcher.h b/src/DefaultBtMessageDispatcher.h index 2b3cc2a3..0b5caae6 100644 --- a/src/DefaultBtMessageDispatcher.h +++ b/src/DefaultBtMessageDispatcher.h @@ -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); diff --git a/src/DefaultBtMessageFactory.cc b/src/DefaultBtMessageFactory.cc index 0c6306a7..17bc280c 100644 --- a/src/DefaultBtMessageFactory.cc +++ b/src/DefaultBtMessageFactory.cc @@ -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 diff --git a/src/DefaultBtMessageFactory.h b/src/DefaultBtMessageFactory.h index c2ef06fc..7b289946 100644 --- a/src/DefaultBtMessageFactory.h +++ b/src/DefaultBtMessageFactory.h @@ -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); diff --git a/src/DefaultBtMessageReceiver.cc b/src/DefaultBtMessageReceiver.cc index d6afbb70..9e5ee0f4 100644 --- a/src/DefaultBtMessageReceiver.cc +++ b/src/DefaultBtMessageReceiver.cc @@ -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) diff --git a/src/DefaultBtMessageReceiver.h b/src/DefaultBtMessageReceiver.h index 8344eb10..a5bbbf27 100644 --- a/src/DefaultBtMessageReceiver.h +++ b/src/DefaultBtMessageReceiver.h @@ -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); diff --git a/src/DefaultBtProgressInfoFile.cc b/src/DefaultBtProgressInfoFile.cc index ef2d7474..9b573f95 100644 --- a/src/DefaultBtProgressInfoFile.cc +++ b/src/DefaultBtProgressInfoFile.cc @@ -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()); + } } } diff --git a/src/DefaultBtRequestFactory.cc b/src/DefaultBtRequestFactory.cc index f14462f1..059c411c 100644 --- a/src/DefaultBtRequestFactory.cc +++ b/src/DefaultBtRequestFactory.cc @@ -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) { diff --git a/src/DefaultBtRequestFactory.h b/src/DefaultBtRequestFactory.h index a4000368..bcf8c8cf 100644 --- a/src/DefaultBtRequestFactory.h +++ b/src/DefaultBtRequestFactory.h @@ -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); diff --git a/src/DefaultExtensionMessageFactory.cc b/src/DefaultExtensionMessageFactory.cc index 6210d108..f28680a4 100644 --- a/src/DefaultExtensionMessageFactory.cc +++ b/src/DefaultExtensionMessageFactory.cc @@ -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) { diff --git a/src/DefaultExtensionMessageFactory.h b/src/DefaultExtensionMessageFactory.h index a3ffd309..dfc1c0ec 100644 --- a/src/DefaultExtensionMessageFactory.h +++ b/src/DefaultExtensionMessageFactory.h @@ -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); diff --git a/src/DefaultPeerStorage.cc b/src/DefaultPeerStorage.cc index 18e36ceb..78116204 100644 --- a/src/DefaultPeerStorage.cc +++ b/src/DefaultPeerStorage.cc @@ -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), diff --git a/src/DefaultPeerStorage.h b/src/DefaultPeerStorage.h index 21f4b7ef..c52f0094 100644 --- a/src/DefaultPeerStorage.h +++ b/src/DefaultPeerStorage.h @@ -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(); diff --git a/src/DefaultPieceStorage.cc b/src/DefaultPieceStorage.cc index 3a707558..02547bf0 100644 --- a/src/DefaultPieceStorage.cc +++ b/src/DefaultPieceStorage.cc @@ -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(); } diff --git a/src/DefaultSegmentManFactory.cc b/src/DefaultSegmentManFactory.cc index 5ed7edef..8f020f98 100644 --- a/src/DefaultSegmentManFactory.cc +++ b/src/DefaultSegmentManFactory.cc @@ -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; diff --git a/src/DiskAdaptor.cc b/src/DiskAdaptor.cc index 38e0b3ea..5e17eb2b 100644 --- a/src/DiskAdaptor.cc +++ b/src/DiskAdaptor.cc @@ -47,8 +47,4 @@ DiskAdaptor::DiskAdaptor(): DiskAdaptor::~DiskAdaptor() {} -void DiskAdaptor::setFileEntries(const FileEntries& fileEntries) { - this->fileEntries = fileEntries; -} - } // namespace aria2 diff --git a/src/DiskAdaptor.h b/src/DiskAdaptor.h index 6e59826a..cb794391 100644 --- a/src/DiskAdaptor.h +++ b/src/DiskAdaptor.h @@ -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; } diff --git a/src/DownloadContext.cc b/src/DownloadContext.cc index a5dda4d2..f002eb3d 100644 --- a/src/DownloadContext.cc +++ b/src/DownloadContext.cc @@ -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) -{ - _dir = dir; -} - -SharedHandle<Signature> DownloadContext::getSignature() const -{ - return _signature; -} - -void DownloadContext::setSignature(const SharedHandle<Signature>& signature) +DownloadContext::DownloadContext(size_t pieceLength, + uint64_t totalLength, + const std::string& path): + _dir("."), + _pieceLength(pieceLength), + _knowsTotalLength(true), + _ownerRequestGroup(0), + _downloadStartTime(0), + _downloadStopTime(_downloadStartTime) { - _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 diff --git a/src/DownloadContext.h b/src/DownloadContext.h index f1aed08e..ef55b035 100644 --- a/src/DownloadContext.h +++ b/src/DownloadContext.h @@ -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(); - - enum FILE_MODE { - SINGLE, - MULTI - }; + // Convenient constructor that creates single file download. + DownloadContext(size_t pieceLength, + uint64_t totalLength, + const std::string& path = A2STR::NIL); - 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; + } + + template<typename InputIterator> + void setPieceHashes(InputIterator first, InputIterator last) + { + _pieceHashes.assign(first, last); + } - virtual uint64_t getTotalLength() const = 0; + uint64_t getTotalLength() const + { + if(_fileEntries.empty()) { + return 0; + } else { + return _fileEntries.back()->getLastOffset(); + } + } - virtual bool knowsTotalLength() const = 0; + bool knowsTotalLength() const { return _knowsTotalLength; } - virtual FILE_MODE getFileMode() const = 0; + void markTotalLengthIsUnknown() { _knowsTotalLength = false; } - const std::deque<SharedHandle<FileEntry> >& getFileEntries() const + const std::vector<SharedHandle<FileEntry> >& getFileEntries() const { return _fileEntries; } - virtual size_t getPieceLength() const = 0; + const SharedHandle<FileEntry>& getFirstFileEntry() const + { + assert(!_fileEntries.empty()); + return _fileEntries[0]; + } + + template<typename InputIterator> + void setFileEntries(InputIterator first, InputIterator last) + { + _fileEntries.assign(first, last); + } + + size_t getPieceLength() const { return _pieceLength; } + + 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; } - virtual size_t getNumPieces() const = 0; + 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; } - virtual const std::string& getPieceHashAlgo() const = 0; + void setDir(const std::string& dir) { _dir = dir; } - /** - * 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; + SharedHandle<Signature> getSignature() const { return _signature; } - const std::string& getDir() const + void setSignature(const SharedHandle<Signature>& signature) { - return _dir; + _signature = signature; } - void setDir(const std::string& dir); + RequestGroup* getOwnerRequestGroup() { return _ownerRequestGroup; } - SharedHandle<Signature> getSignature() const; + 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 setSignature(const SharedHandle<Signature>& signature); + 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_ diff --git a/src/DownloadEngine.cc b/src/DownloadEngine.cc index 5808e426..75c507f2 100644 --- a/src/DownloadEngine.cc +++ b/src/DownloadEngine.cc @@ -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 { diff --git a/src/DownloadEngineFactory.cc b/src/DownloadEngineFactory.cc index b55b158a..0f83a674 100644 --- a/src/DownloadEngineFactory.cc +++ b/src/DownloadEngineFactory.cc @@ -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 diff --git a/src/DownloadHandler.cc b/src/DownloadHandler.cc index 93b901d4..8840a630 100644 --- a/src/DownloadHandler.cc +++ b/src/DownloadHandler.cc @@ -37,6 +37,7 @@ #include "Logger.h" #include "RequestGroup.h" #include "RequestGroupCriteria.h" +#include "DownloadContext.h" namespace aria2 { diff --git a/src/DownloadResult.h b/src/DownloadResult.h index 973095ee..d52c79dc 100644 --- a/src/DownloadResult.h +++ b/src/DownloadResult.h @@ -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, diff --git a/src/FileAllocationCommand.cc b/src/FileAllocationCommand.cc index db4967a2..1f0eda1a 100644 --- a/src/FileAllocationCommand.cc +++ b/src/FileAllocationCommand.cc @@ -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; } diff --git a/src/FileAllocationDispatcherCommand.cc b/src/FileAllocationDispatcherCommand.cc index ba33aa0b..c86f26cb 100644 --- a/src/FileAllocationDispatcherCommand.cc +++ b/src/FileAllocationDispatcherCommand.cc @@ -37,6 +37,7 @@ #include "FileAllocationCommand.h" #include "message.h" #include "Logger.h" +#include "DownloadContext.h" namespace aria2 { diff --git a/src/FileAllocationEntry.cc b/src/FileAllocationEntry.cc index 6a6cd732..aba0b0c5 100644 --- a/src/FileAllocationEntry.cc +++ b/src/FileAllocationEntry.cc @@ -38,6 +38,7 @@ #include "RequestGroup.h" #include "PieceStorage.h" #include "DiskAdaptor.h" +#include "DownloadContext.h" namespace aria2 { diff --git a/src/FileEntry.h b/src/FileEntry.h index 05ba6067..a40be12e 100644 --- a/src/FileEntry.h +++ b/src/FileEntry.h @@ -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 diff --git a/src/FillRequestGroupCommand.cc b/src/FillRequestGroupCommand.cc index 869496b5..1a544b72 100644 --- a/src/FillRequestGroupCommand.cc +++ b/src/FillRequestGroupCommand.cc @@ -39,6 +39,7 @@ #include "RecoverableException.h" #include "message.h" #include "Logger.h" +#include "DownloadContext.h" namespace aria2 { diff --git a/src/FtpDownloadCommand.cc b/src/FtpDownloadCommand.cc index 49a62b5c..d66c0f20 100644 --- a/src/FtpDownloadCommand.cc +++ b/src/FtpDownloadCommand.cc @@ -43,6 +43,7 @@ #include "FtpFinishDownloadCommand.h" #include "FtpConnection.h" #include "Logger.h" +#include "DownloadContext.h" namespace aria2 { diff --git a/src/FtpFinishDownloadCommand.cc b/src/FtpFinishDownloadCommand.cc index bf8af2a2..dbf56edc 100644 --- a/src/FtpFinishDownloadCommand.cc +++ b/src/FtpFinishDownloadCommand.cc @@ -47,6 +47,7 @@ #include "SocketCore.h" #include "RequestGroup.h" #include "Logger.h" +#include "DownloadContext.h" namespace aria2 { diff --git a/src/FtpInitiateConnectionCommand.cc b/src/FtpInitiateConnectionCommand.cc index 7453337a..dcfcde6b 100644 --- a/src/FtpInitiateConnectionCommand.cc +++ b/src/FtpInitiateConnectionCommand.cc @@ -50,6 +50,7 @@ #include "prefs.h" #include "HttpConnection.h" #include "Socket.h" +#include "DownloadContext.h" namespace aria2 { diff --git a/src/FtpNegotiationCommand.cc b/src/FtpNegotiationCommand.cc index cbdea101..555ea605 100644 --- a/src/FtpNegotiationCommand.cc +++ b/src/FtpNegotiationCommand.cc @@ -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); } diff --git a/src/FtpTunnelRequestCommand.cc b/src/FtpTunnelRequestCommand.cc index 09a162c5..73e5ed9c 100644 --- a/src/FtpTunnelRequestCommand.cc +++ b/src/FtpTunnelRequestCommand.cc @@ -36,6 +36,7 @@ #include "FtpTunnelResponseCommand.h" #include "Request.h" #include "Socket.h" +#include "DownloadContext.h" namespace aria2 { diff --git a/src/FtpTunnelResponseCommand.cc b/src/FtpTunnelResponseCommand.cc index 293b88c8..0ecb3bd0 100644 --- a/src/FtpTunnelResponseCommand.cc +++ b/src/FtpTunnelResponseCommand.cc @@ -39,6 +39,7 @@ #include "HttpRequest.h" #include "Segment.h" #include "Socket.h" +#include "DownloadContext.h" namespace aria2 { diff --git a/src/HandshakeExtensionMessage.cc b/src/HandshakeExtensionMessage.cc index d276b8d9..370746f4 100644 --- a/src/HandshakeExtensionMessage.cc +++ b/src/HandshakeExtensionMessage.cc @@ -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); diff --git a/src/HandshakeExtensionMessage.h b/src/HandshakeExtensionMessage.h index dc054f52..3c9c7dd9 100644 --- a/src/HandshakeExtensionMessage.h +++ b/src/HandshakeExtensionMessage.h @@ -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); }; diff --git a/src/HaveEraseCommand.cc b/src/HaveEraseCommand.cc index 4ad39505..910ee761 100644 --- a/src/HaveEraseCommand.cc +++ b/src/HaveEraseCommand.cc @@ -37,6 +37,7 @@ #include "RequestGroupMan.h" #include "PieceStorage.h" #include "RequestGroup.h" +#include "DownloadContext.h" namespace aria2 { diff --git a/src/HttpDownloadCommand.cc b/src/HttpDownloadCommand.cc index 9819efea..98ada9eb 100644 --- a/src/HttpDownloadCommand.cc +++ b/src/HttpDownloadCommand.cc @@ -46,6 +46,7 @@ #include "HttpResponse.h" #include "HttpHeader.h" #include "Range.h" +#include "DownloadContext.h" namespace aria2 { diff --git a/src/HttpInitiateConnectionCommand.cc b/src/HttpInitiateConnectionCommand.cc index b9552db5..4245403a 100644 --- a/src/HttpInitiateConnectionCommand.cc +++ b/src/HttpInitiateConnectionCommand.cc @@ -47,6 +47,7 @@ #include "message.h" #include "prefs.h" #include "A2STR.h" +#include "DownloadContext.h" namespace aria2 { diff --git a/src/HttpProxyRequestCommand.cc b/src/HttpProxyRequestCommand.cc index 9d0a434b..04e534ee 100644 --- a/src/HttpProxyRequestCommand.cc +++ b/src/HttpProxyRequestCommand.cc @@ -36,6 +36,7 @@ #include "HttpProxyResponseCommand.h" #include "Request.h" #include "Socket.h" +#include "DownloadContext.h" namespace aria2 { diff --git a/src/HttpProxyResponseCommand.cc b/src/HttpProxyResponseCommand.cc index 0054d6d9..be6a58ff 100644 --- a/src/HttpProxyResponseCommand.cc +++ b/src/HttpProxyResponseCommand.cc @@ -39,6 +39,7 @@ #include "HttpRequest.h" #include "Segment.h" #include "Socket.h" +#include "DownloadContext.h" namespace aria2 { diff --git a/src/HttpRequest.cc b/src/HttpRequest.cc index d46f68ac..bac87114 100644 --- a/src/HttpRequest.cc +++ b/src/HttpRequest.cc @@ -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; diff --git a/src/HttpRequest.h b/src/HttpRequest.h index 9ec32171..def1ddd6 100644 --- a/src/HttpRequest.h +++ b/src/HttpRequest.h @@ -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 diff --git a/src/HttpRequestCommand.cc b/src/HttpRequestCommand.cc index d95e616b..961e8155 100644 --- a/src/HttpRequestCommand.cc +++ b/src/HttpRequestCommand.cc @@ -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); diff --git a/src/HttpResponseCommand.cc b/src/HttpResponseCommand.cc index 30de2641..ece2606a 100644 --- a/src/HttpResponseCommand.cc +++ b/src/HttpResponseCommand.cc @@ -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; diff --git a/src/HttpServerBodyCommand.cc b/src/HttpServerBodyCommand.cc index afbf601d..c40efde1 100644 --- a/src/HttpServerBodyCommand.cc +++ b/src/HttpServerBodyCommand.cc @@ -49,6 +49,7 @@ #include "XmlRpcMethod.h" #include "XmlRpcMethodFactory.h" #include "XmlRpcResponse.h" +#include "DownloadContext.h" namespace aria2 { diff --git a/src/HttpServerCommand.cc b/src/HttpServerCommand.cc index 55f32427..38b9bdee 100644 --- a/src/HttpServerCommand.cc +++ b/src/HttpServerCommand.cc @@ -46,6 +46,7 @@ #include "prefs.h" #include "Option.h" #include "Util.h" +#include "DownloadContext.h" namespace aria2 { diff --git a/src/HttpSkipResponseCommand.cc b/src/HttpSkipResponseCommand.cc index b9edb717..ff1bf174 100644 --- a/src/HttpSkipResponseCommand.cc +++ b/src/HttpSkipResponseCommand.cc @@ -53,6 +53,7 @@ #include "CookieStorage.h" #include "AuthConfigFactory.h" #include "AuthConfig.h" +#include "DownloadContext.h" namespace aria2 { diff --git a/src/InitiateConnectionCommandFactory.cc b/src/InitiateConnectionCommandFactory.cc index 2ad3dbda..450f6d46 100644 --- a/src/InitiateConnectionCommandFactory.cc +++ b/src/InitiateConnectionCommandFactory.cc @@ -43,6 +43,7 @@ #include "Option.h" #include "prefs.h" #include "SocketCore.h" +#include "DownloadContext.h" namespace aria2 { diff --git a/src/InitiatorMSEHandshakeCommand.cc b/src/InitiatorMSEHandshakeCommand.cc index b29fe38c..7afeab6e 100644 --- a/src/InitiatorMSEHandshakeCommand.cc +++ b/src/InitiatorMSEHandshakeCommand.cc @@ -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); diff --git a/src/InitiatorMSEHandshakeCommand.h b/src/InitiatorMSEHandshakeCommand.h index 46cbedf0..996a72e9 100644 --- a/src/InitiatorMSEHandshakeCommand.h +++ b/src/InitiatorMSEHandshakeCommand.h @@ -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(); diff --git a/src/IteratableChecksumValidator.cc b/src/IteratableChecksumValidator.cc index b250af4d..a3be0a21 100644 --- a/src/IteratableChecksumValidator.cc +++ b/src/IteratableChecksumValidator.cc @@ -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), diff --git a/src/IteratableChecksumValidator.h b/src/IteratableChecksumValidator.h index 2a8997a8..62112e95 100644 --- a/src/IteratableChecksumValidator.h +++ b/src/IteratableChecksumValidator.h @@ -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(); diff --git a/src/IteratableChunkChecksumValidator.cc b/src/IteratableChunkChecksumValidator.cc index bb3f2fcb..b58ab90d 100644 --- a/src/IteratableChunkChecksumValidator.cc +++ b/src/IteratableChunkChecksumValidator.cc @@ -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; diff --git a/src/MSEHandshake.cc b/src/MSEHandshake.cc index 70ce2ef7..760002bc 100644 --- a/src/MSEHandshake.cc +++ b/src/MSEHandshake.cc @@ -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); diff --git a/src/MSEHandshake.h b/src/MSEHandshake.h index f2ceb3a7..14ef0ad3 100644 --- a/src/MSEHandshake.h +++ b/src/MSEHandshake.h @@ -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(); diff --git a/src/Makefile.am b/src/Makefile.am index 5951269f..235befee 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -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 diff --git a/src/Makefile.in b/src/Makefile.in index f46e2465..09e34803 100644 --- a/src/Makefile.in +++ b/src/Makefile.in @@ -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@ diff --git a/src/Metalink2RequestGroup.cc b/src/Metalink2RequestGroup.cc index e8874c8f..1b9a36a4 100644 --- a/src/Metalink2RequestGroup.cc +++ b/src/Metalink2RequestGroup.cc @@ -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 diff --git a/src/MetalinkPostDownloadHandler.cc b/src/MetalinkPostDownloadHandler.cc index 7623f1e8..038a3687 100644 --- a/src/MetalinkPostDownloadHandler.cc +++ b/src/MetalinkPostDownloadHandler.cc @@ -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(); diff --git a/src/MultiDiskAdaptor.cc b/src/MultiDiskAdaptor.cc index 7083de7f..729eb676 100644 --- a/src/MultiDiskAdaptor.cc +++ b/src/MultiDiskAdaptor.cc @@ -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()); diff --git a/src/MultiDiskAdaptor.h b/src/MultiDiskAdaptor.h index 21018259..74713fee 100644 --- a/src/MultiDiskAdaptor.h +++ b/src/MultiDiskAdaptor.h @@ -37,6 +37,8 @@ #include "DiskAdaptor.h" +#include <deque> + namespace aria2 { class MultiFileAllocationIterator; @@ -112,7 +114,7 @@ public: typedef SharedHandle<DiskWriterEntry> DiskWriterEntryHandle; -typedef std::deque<DiskWriterEntryHandle> DiskWriterEntries; +typedef std::vector<DiskWriterEntryHandle> DiskWriterEntries; class MultiDiskAdaptor : public DiskAdaptor { friend class MultiFileAllocationIterator; @@ -120,7 +122,7 @@ private: size_t pieceLength; DiskWriterEntries diskWriterEntries; - std::deque<SharedHandle<DiskWriterEntry> > _openedDiskWriterEntries; + std::vector<SharedHandle<DiskWriterEntry> > _openedDiskWriterEntries; size_t _maxOpenFiles; @@ -192,7 +194,7 @@ public: virtual size_t utime(const Time& actime, const Time& modtime); - const std::deque<SharedHandle<DiskWriterEntry> >& + const std::vector<SharedHandle<DiskWriterEntry> >& getDiskWriterEntries() const { return diskWriterEntries; diff --git a/src/MultiFileAllocationIterator.cc b/src/MultiFileAllocationIterator.cc index ff131573..a1f772af 100644 --- a/src/MultiFileAllocationIterator.cc +++ b/src/MultiFileAllocationIterator.cc @@ -45,7 +45,8 @@ namespace aria2 { MultiFileAllocationIterator::MultiFileAllocationIterator(MultiDiskAdaptor* diskAdaptor): _diskAdaptor(diskAdaptor), - _entries(_diskAdaptor->diskWriterEntries), + _entries(_diskAdaptor->diskWriterEntries.begin(), + _diskAdaptor->diskWriterEntries.end()), _offset(0) {} @@ -111,7 +112,8 @@ uint64_t MultiFileAllocationIterator::getTotalLength() } } -const DiskWriterEntries& MultiFileAllocationIterator::getDiskWriterEntries() const +const std::deque<SharedHandle<DiskWriterEntry> >& +MultiFileAllocationIterator::getDiskWriterEntries() const { return _entries; } diff --git a/src/MultiUrlRequestInfo.cc b/src/MultiUrlRequestInfo.cc index c0fbb878..11351ad3 100644 --- a/src/MultiUrlRequestInfo.cc +++ b/src/MultiUrlRequestInfo.cc @@ -54,6 +54,7 @@ #include "File.h" #include "Netrc.h" #include "AuthConfigFactory.h" +#include "DownloadContext.h" #ifdef ENABLE_SSL # include "SocketCore.h" # include "TLSContext.h" diff --git a/src/PeerChokeCommand.cc b/src/PeerChokeCommand.cc index 8799605f..ca1bb1f0 100644 --- a/src/PeerChokeCommand.cc +++ b/src/PeerChokeCommand.cc @@ -34,18 +34,15 @@ /* copyright --> */ #include "PeerChokeCommand.h" #include "DownloadEngine.h" -#include "BtContext.h" #include "BtRuntime.h" #include "PeerStorage.h" namespace aria2 { PeerChokeCommand::PeerChokeCommand(int32_t cuid, - DownloadEngine* e, - const SharedHandle<BtContext>& btContext): + DownloadEngine* e): Command(cuid), - e(e), - _btContext(btContext) {} + e(e) {} PeerChokeCommand::~PeerChokeCommand() {} diff --git a/src/PeerChokeCommand.h b/src/PeerChokeCommand.h index b81781f3..7f8baf75 100644 --- a/src/PeerChokeCommand.h +++ b/src/PeerChokeCommand.h @@ -41,7 +41,6 @@ namespace aria2 { class DownloadEngine; -class BtContext; class BtRuntime; class PeerStorage; @@ -49,15 +48,12 @@ class PeerChokeCommand : public Command { private: DownloadEngine* e; - SharedHandle<BtContext> _btContext; - SharedHandle<PeerStorage> _peerStorage; SharedHandle<BtRuntime> _btRuntime; public: PeerChokeCommand(int32_t cuid, - DownloadEngine* e, - const SharedHandle<BtContext>& btContext); + DownloadEngine* e); virtual ~PeerChokeCommand(); diff --git a/src/PeerInitiateConnectionCommand.cc b/src/PeerInitiateConnectionCommand.cc index a815bced..bd16a354 100644 --- a/src/PeerInitiateConnectionCommand.cc +++ b/src/PeerInitiateConnectionCommand.cc @@ -42,12 +42,12 @@ #include "Socket.h" #include "Logger.h" #include "Peer.h" -#include "BtContext.h" #include "BtRuntime.h" #include "PeerStorage.h" #include "PieceStorage.h" #include "PeerConnection.h" #include "RequestGroup.h" +#include "DownloadContext.h" namespace aria2 { @@ -56,13 +56,11 @@ PeerInitiateConnectionCommand::PeerInitiateConnectionCommand RequestGroup* requestGroup, const PeerHandle& peer, DownloadEngine* e, - const SharedHandle<BtContext>& btContext, const SharedHandle<BtRuntime>& btRuntime, bool mseHandshakeEnabled) : PeerAbstractCommand(cuid, peer, e), _requestGroup(requestGroup), - _btContext(btContext), _btRuntime(btRuntime), _mseHandshakeEnabled(mseHandshakeEnabled) { @@ -83,7 +81,7 @@ bool PeerInitiateConnectionCommand::executeInternal() { socket->establishConnection(peer->ipaddr, peer->port); if(_mseHandshakeEnabled) { InitiatorMSEHandshakeCommand* c = - new InitiatorMSEHandshakeCommand(cuid, _requestGroup, peer, e, _btContext, + new InitiatorMSEHandshakeCommand(cuid, _requestGroup, peer, e, _btRuntime, socket); c->setPeerStorage(_peerStorage); c->setPieceStorage(_pieceStorage); @@ -91,7 +89,7 @@ bool PeerInitiateConnectionCommand::executeInternal() { } else { PeerInteractionCommand* command = new PeerInteractionCommand - (cuid, _requestGroup, peer, e, _btContext, _btRuntime, _pieceStorage, + (cuid, _requestGroup, peer, e, _btRuntime, _pieceStorage, socket, PeerInteractionCommand::INITIATOR_SEND_HANDSHAKE); command->setPeerStorage(_peerStorage); e->commands.push_back(command); @@ -106,7 +104,7 @@ bool PeerInitiateConnectionCommand::prepareForNextPeer(time_t wait) { 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); diff --git a/src/PeerInitiateConnectionCommand.h b/src/PeerInitiateConnectionCommand.h index 9ffd5afa..0723eade 100644 --- a/src/PeerInitiateConnectionCommand.h +++ b/src/PeerInitiateConnectionCommand.h @@ -40,7 +40,6 @@ namespace aria2 { class RequestGroup; -class BtContext; class BtRuntime; class PeerStorage; class PieceStorage; @@ -49,8 +48,6 @@ class PeerInitiateConnectionCommand : public PeerAbstractCommand { private: RequestGroup* _requestGroup; - SharedHandle<BtContext> _btContext; - SharedHandle<BtRuntime> _btRuntime; SharedHandle<PeerStorage> _peerStorage; @@ -69,7 +66,6 @@ public: RequestGroup* requestGroup, const SharedHandle<Peer>& peer, DownloadEngine* e, - const SharedHandle<BtContext>& btContext, const SharedHandle<BtRuntime>& btRuntime, bool mseHandshakeEnabled = true); diff --git a/src/PeerInteractionCommand.cc b/src/PeerInteractionCommand.cc index c3bbc1b9..38644aaf 100644 --- a/src/PeerInteractionCommand.cc +++ b/src/PeerInteractionCommand.cc @@ -44,7 +44,7 @@ #include "prefs.h" #include "Socket.h" #include "Option.h" -#include "BtContext.h" +#include "DownloadContext.h" #include "BtRegistry.h" #include "Peer.h" #include "BtMessage.h" @@ -70,6 +70,7 @@ #include "DefaultExtensionMessageFactory.h" #include "RequestGroupMan.h" #include "ExtensionMessageRegistry.h" +#include "bittorrent_helper.h" namespace aria2 { @@ -78,7 +79,6 @@ PeerInteractionCommand::PeerInteractionCommand RequestGroup* requestGroup, const PeerHandle& p, DownloadEngine* e, - const SharedHandle<BtContext>& btContext, const SharedHandle<BtRuntime>& btRuntime, const SharedHandle<PieceStorage>& pieceStorage, const SocketHandle& s, @@ -86,7 +86,6 @@ PeerInteractionCommand::PeerInteractionCommand const PeerConnectionHandle& passedPeerConnection) :PeerAbstractCommand(cuid, p, e, s), _requestGroup(requestGroup), - _btContext(btContext), _btRuntime(btRuntime), _pieceStorage(pieceStorage), sequence(sequence) @@ -98,20 +97,22 @@ PeerInteractionCommand::PeerInteractionCommand setTimeout(getOption()->getAsInt(PREF_PEER_CONNECTION_TIMEOUT)); } + const BDE& torrentAttrs = + _requestGroup->getDownloadContext()->getAttribute(bittorrent::BITTORRENT); SharedHandle<BtRegistry> btRegistry = e->getBtRegistry(); SharedHandle<PeerStorage> peerStorage = - btRegistry->get(_btContext->getInfoHashAsString())._peerStorage; + btRegistry->get(torrentAttrs[bittorrent::INFO_HASH].s())._peerStorage; SharedHandle<ExtensionMessageRegistry> exMsgRegistry (new ExtensionMessageRegistry()); SharedHandle<DefaultExtensionMessageFactory> extensionMessageFactory - (new DefaultExtensionMessageFactory(_btContext, peer, exMsgRegistry)); + (new DefaultExtensionMessageFactory(peer, exMsgRegistry)); extensionMessageFactory->setPeerStorage(peerStorage); SharedHandle<DefaultBtMessageFactory> factory(new DefaultBtMessageFactory()); factory->setCuid(cuid); - factory->setBtContext(_btContext); + factory->setDownloadContext(_requestGroup->getDownloadContext()); factory->setPieceStorage(pieceStorage); factory->setPeerStorage(peerStorage); factory->setExtensionMessageFactory(extensionMessageFactory); @@ -132,7 +133,7 @@ PeerInteractionCommand::PeerInteractionCommand (new DefaultBtMessageDispatcher()); dispatcher->setCuid(cuid); dispatcher->setPeer(peer); - dispatcher->setBtContext(_btContext); + dispatcher->setDownloadContext(_requestGroup->getDownloadContext()); dispatcher->setPieceStorage(pieceStorage); dispatcher->setPeerStorage(peerStorage); dispatcher->setRequestTimeout(getOption()->getAsInt(PREF_BT_REQUEST_TIMEOUT)); @@ -142,7 +143,7 @@ PeerInteractionCommand::PeerInteractionCommand DefaultBtMessageReceiverHandle receiver(new DefaultBtMessageReceiver()); receiver->setCuid(cuid); receiver->setPeer(peer); - receiver->setBtContext(_btContext); + receiver->setDownloadContext(_requestGroup->getDownloadContext()); receiver->setPeerConnection(peerConnection); receiver->setDispatcher(dispatcher); receiver->setBtMessageFactory(factory); @@ -151,13 +152,12 @@ PeerInteractionCommand::PeerInteractionCommand (new DefaultBtRequestFactory()); reqFactory->setCuid(cuid); reqFactory->setPeer(peer); - reqFactory->setBtContext(_btContext); reqFactory->setPieceStorage(pieceStorage); reqFactory->setBtMessageDispatcher(dispatcher); reqFactory->setBtMessageFactory(factory); DefaultBtInteractiveHandle btInteractive - (new DefaultBtInteractive(_btContext, peer)); + (new DefaultBtInteractive(_requestGroup->getDownloadContext(), peer)); btInteractive->setBtRuntime(_btRuntime); btInteractive->setPieceStorage(_pieceStorage); btInteractive->setPeerStorage(peerStorage); // Note: Not a member variable. @@ -172,7 +172,7 @@ PeerInteractionCommand::PeerInteractionCommand (getOption()->getAsInt(PREF_BT_KEEP_ALIVE_INTERVAL)); btInteractive->setRequestGroupMan(e->_requestGroupMan); btInteractive->setBtMessageFactory(factory); - if(!_btContext->isPrivate()) { + if(torrentAttrs[bittorrent::PRIVATE].i() == 0) { if(getOption()->getAsBool(PREF_ENABLE_PEER_EXCHANGE)) { btInteractive->setUTPexEnabled(true); } @@ -189,8 +189,9 @@ PeerInteractionCommand::PeerInteractionCommand factory->setBtRequestFactory(reqFactory); factory->setPeerConnection(peerConnection); - peer->allocateSessionResource(_btContext->getPieceLength(), - _btContext->getTotalLength()); + peer->allocateSessionResource + (_requestGroup->getDownloadContext()->getPieceLength(), + _requestGroup->getDownloadContext()->getTotalLength()); peer->setBtMessageDispatcher(dispatcher); _btRuntime->increaseConnections(); @@ -290,12 +291,8 @@ bool PeerInteractionCommand::prepareForNextPeer(time_t wait) { PeerHandle peer = _peerStorage->getUnusedPeer(); peer->usedBy(e->newCUID()); PeerInitiateConnectionCommand* command = - new PeerInitiateConnectionCommand(peer->usedBy(), - _requestGroup, - peer, - e, - _btContext, - _btRuntime); + new PeerInitiateConnectionCommand + (peer->usedBy(), _requestGroup, peer, e, _btRuntime); command->setPeerStorage(_peerStorage); command->setPieceStorage(_pieceStorage); e->commands.push_back(command); diff --git a/src/PeerInteractionCommand.h b/src/PeerInteractionCommand.h index a8ed8514..894cda7d 100644 --- a/src/PeerInteractionCommand.h +++ b/src/PeerInteractionCommand.h @@ -40,7 +40,6 @@ namespace aria2 { class RequestGroup; -class BtContext; class BtInteractive; class PeerConnection; class BtRuntime; @@ -58,8 +57,6 @@ public: private: RequestGroup* _requestGroup; - SharedHandle<BtContext> _btContext; - SharedHandle<BtRuntime> _btRuntime; SharedHandle<PieceStorage> _pieceStorage; @@ -81,7 +78,6 @@ public: RequestGroup* requestGroup, const SharedHandle<Peer>& peer, DownloadEngine* e, - const SharedHandle<BtContext>& btContext, const SharedHandle<BtRuntime>& btRuntime, const SharedHandle<PieceStorage>& pieceStorage, const SharedHandle<SocketCore>& s, diff --git a/src/PeerReceiveHandshakeCommand.cc b/src/PeerReceiveHandshakeCommand.cc index 20df4391..043ec7c2 100644 --- a/src/PeerReceiveHandshakeCommand.cc +++ b/src/PeerReceiveHandshakeCommand.cc @@ -37,7 +37,7 @@ #include "DownloadEngine.h" #include "BtHandshakeMessage.h" #include "Util.h" -#include "BtContext.h" +#include "DownloadContext.h" #include "DlAbortEx.h" #include "PeerInteractionCommand.h" #include "Peer.h" @@ -91,23 +91,25 @@ bool PeerReceiveHandshakeCommand::executeInternal() // To handle tracker's NAT-checking feature if(dataLength >= 48) { // check info_hash - std::string infoHash = Util::toHex(&data[28], INFO_HASH_LENGTH); + std::string infoHash = std::string(&data[28], &data[28+INFO_HASH_LENGTH]); BtObject btObject = e->getBtRegistry()->get(infoHash); - SharedHandle<BtContext> btContext = btObject._btContext; + SharedHandle<DownloadContext> downloadContext = btObject._downloadContext; SharedHandle<BtRuntime> btRuntime = btObject._btRuntime; SharedHandle<PieceStorage> pieceStorage = btObject._pieceStorage; SharedHandle<PeerStorage> peerStorage = btObject._peerStorage; - if(btContext.isNull() || !btRuntime->ready()) { + if(downloadContext.isNull() || !btRuntime->ready()) { throw DL_ABORT_EX - (StringFormat("Unknown info hash %s", infoHash.c_str()).str()); + (StringFormat("Unknown info hash %s", + Util::toHex(infoHash).c_str()).str()); } - TransferStat tstat = btContext->getOwnerRequestGroup()->calculateStat(); + TransferStat tstat = + downloadContext->getOwnerRequestGroup()->calculateStat(); const unsigned int maxDownloadLimit = - btContext->getOwnerRequestGroup()->getMaxDownloadSpeedLimit(); + downloadContext->getOwnerRequestGroup()->getMaxDownloadSpeedLimit(); unsigned int thresholdSpeed = - btContext->getOwnerRequestGroup()-> + downloadContext->getOwnerRequestGroup()-> getOption()->getAsInt(PREF_BT_REQUEST_PEER_SPEED_LIMIT); if(maxDownloadLimit > 0) { thresholdSpeed = std::min(maxDownloadLimit, thresholdSpeed); @@ -123,10 +125,9 @@ bool PeerReceiveHandshakeCommand::executeInternal() PeerInteractionCommand* command = new PeerInteractionCommand (cuid, - btContext->getOwnerRequestGroup(), + downloadContext->getOwnerRequestGroup(), peer, e, - btContext, btRuntime, pieceStorage, socket, diff --git a/src/PieceHashCheckIntegrityEntry.cc b/src/PieceHashCheckIntegrityEntry.cc index 188e8670..972a40e8 100644 --- a/src/PieceHashCheckIntegrityEntry.cc +++ b/src/PieceHashCheckIntegrityEntry.cc @@ -48,8 +48,10 @@ PieceHashCheckIntegrityEntry::~PieceHashCheckIntegrityEntry() {} bool PieceHashCheckIntegrityEntry::isValidationReady() { - DownloadContextHandle dctx = _requestGroup->getDownloadContext(); - return dctx->getPieceHashes().size() > 0 && + const SharedHandle<DownloadContext>& dctx = + _requestGroup->getDownloadContext(); + return !dctx->getPieceHashAlgo().empty() && + dctx->getPieceHashes().size() > 0 && dctx->getPieceHashes().size() == dctx->getNumPieces(); } diff --git a/src/RealtimeCommand.cc b/src/RealtimeCommand.cc index 0f444ee4..160f1f00 100644 --- a/src/RealtimeCommand.cc +++ b/src/RealtimeCommand.cc @@ -36,6 +36,7 @@ #include "DownloadEngine.h" #include "Exception.h" #include "RequestGroup.h" +#include "DownloadContext.h" namespace aria2 { diff --git a/src/ReceiverMSEHandshakeCommand.cc b/src/ReceiverMSEHandshakeCommand.cc index defe2a22..fa609b60 100644 --- a/src/ReceiverMSEHandshakeCommand.cc +++ b/src/ReceiverMSEHandshakeCommand.cc @@ -49,7 +49,7 @@ #include "ARC4Decryptor.h" #include "RequestGroupMan.h" #include "BtRegistry.h" -#include "BtContext.h" +#include "DownloadContext.h" #include "PeerStorage.h" #include "PieceStorage.h" #include "BtAnnounce.h" @@ -135,9 +135,10 @@ bool ReceiverMSEHandshakeCommand::executeInternal() break; } case RECEIVER_RECEIVE_PAD_C_LENGTH: { - std::deque<SharedHandle<BtContext> > btContexts; - e->getBtRegistry()->getAllBtContext(std::back_inserter(btContexts)); - if(_mseHandshake->receiveReceiverHashAndPadCLength(btContexts)) { + std::deque<SharedHandle<DownloadContext> > downloadContexts; + e->getBtRegistry()->getAllDownloadContext + (std::back_inserter(downloadContexts)); + if(_mseHandshake->receiveReceiverHashAndPadCLength(downloadContexts)) { _sequence = RECEIVER_RECEIVE_PAD_C; } break; diff --git a/src/RequestGroup.cc b/src/RequestGroup.cc index 0da471b9..2e7d9096 100644 --- a/src/RequestGroup.cc +++ b/src/RequestGroup.cc @@ -48,7 +48,6 @@ #include "File.h" #include "message.h" #include "Util.h" -#include "BtRegistry.h" #include "LogFactory.h" #include "Logger.h" #include "DiskAdaptor.h" @@ -57,8 +56,7 @@ #include "StreamCheckIntegrityEntry.h" #include "CheckIntegrityCommand.h" #include "UnknownLengthPieceStorage.h" -#include "BtContext.h" -#include "SingleFileDownloadContext.h" +#include "DownloadContext.h" #include "DlAbortEx.h" #include "DownloadFailureException.h" #include "RequestGroupMan.h" @@ -83,6 +81,8 @@ # include "CheckIntegrityCommand.h" #endif // ENABLE_MESSAGE_DIGEST #ifdef ENABLE_BITTORRENT +# include "bittorrent_helper.h" +# include "BtRegistry.h" # include "BtCheckIntegrityEntry.h" # include "DefaultPeerStorage.h" # include "DefaultBtAnnounce.h" @@ -205,39 +205,30 @@ void RequestGroup::createInitialCommand(std::deque<Command*>& commands, { #ifdef ENABLE_BITTORRENT { - BtContextHandle btContext = dynamic_pointer_cast<BtContext>(_downloadContext); - if(!btContext.isNull()) { + if(_downloadContext->hasAttribute(bittorrent::BITTORRENT)) { + const BDE& torrentAttrs = + _downloadContext->getAttribute(bittorrent::BITTORRENT); if(_option->getAsBool(PREF_DRY_RUN)) { throw DOWNLOAD_FAILURE_EXCEPTION ("Cancel BitTorrent download in dry-run context."); } SharedHandle<BtRegistry> btRegistry = e->getBtRegistry(); - if(!btRegistry->getBtContext(btContext->getInfoHashAsString()).isNull()) { + if(!btRegistry->getDownloadContext + (torrentAttrs[bittorrent::INFO_HASH].s()).isNull()) { throw DOWNLOAD_FAILURE_EXCEPTION - (StringFormat("InfoHash %s is already registered.", - btContext->getInfoHashAsString().c_str()).str()); + (StringFormat + ("InfoHash %s is already registered.", + Util::toHex(torrentAttrs[bittorrent::INFO_HASH].s()).c_str()).str()); } if(e->_requestGroupMan->isSameFileBeingDownloaded(this)) { throw DOWNLOAD_FAILURE_EXCEPTION (StringFormat(EX_DUPLICATE_FILE_DOWNLOAD, - getFilePath().c_str()).str()); + _downloadContext->getBasePath().c_str()).str()); } initPieceStorage(); - if(btContext->getFileEntries().size() > 1) { - // this is really multi file torrent. - // clear http/ftp uris because the current implementation does not - // allow integrating multi-file torrent and http/ftp. - _logger->debug("Clearing http/ftp URIs because the current implementation does not allow integrating multi-file torrent and http/ftp."); - _uris.clear(); - + if(_downloadContext->getFileEntries().size() > 1) { _pieceStorage->setupFileFilter(); - } else if(btContext->getFileEntries().size() == 1) { - // web-seeding is only enabled for single file torrent - SharedHandle<FileEntry> fileEntry = btContext->getFileEntries().front(); - _uris.insert(_uris.end(), - fileEntry->getAssociatedUris().begin(), - fileEntry->getAssociatedUris().end()); } SharedHandle<DefaultBtProgressInfoFile> @@ -252,14 +243,14 @@ void RequestGroup::createInitialCommand(std::deque<Command*>& commands, progressInfoFile->setBtRuntime(btRuntime); SharedHandle<DefaultPeerStorage> peerStorage - (new DefaultPeerStorage(btContext, _option.get())); + (new DefaultPeerStorage(_option.get())); peerStorage->setBtRuntime(btRuntime); peerStorage->setPieceStorage(_pieceStorage); _peerStorage = peerStorage; progressInfoFile->setPeerStorage(peerStorage); SharedHandle<DefaultBtAnnounce> btAnnounce - (new DefaultBtAnnounce(btContext, _option.get())); + (new DefaultBtAnnounce(_downloadContext, _option.get())); btAnnounce->setBtRuntime(btRuntime); btAnnounce->setPieceStorage(_pieceStorage); btAnnounce->setPeerStorage(peerStorage); @@ -267,8 +258,8 @@ void RequestGroup::createInitialCommand(std::deque<Command*>& commands, (_option->getAsInt(PREF_BT_TRACKER_INTERVAL)); btAnnounce->shuffleAnnounce(); - btRegistry->put(btContext->getInfoHashAsString(), - BtObject(btContext, + btRegistry->put(torrentAttrs[bittorrent::INFO_HASH].s(), + BtObject(_downloadContext, _pieceStorage, peerStorage, btAnnounce, @@ -280,20 +271,20 @@ void RequestGroup::createInitialCommand(std::deque<Command*>& commands, progressInfoFile->removeFile(); _logger->notice(MSG_REMOVED_DEFUNCT_CONTROL_FILE, progressInfoFile->getFilename().c_str(), - getFilePath().c_str()); + _downloadContext->getBasePath().c_str()); } { uint64_t actualFileSize = _pieceStorage->getDiskAdaptor()->size(); - if(actualFileSize == btContext->getTotalLength()) { + if(actualFileSize == _downloadContext->getTotalLength()) { // First, make DiskAdaptor read-only mode to allow the // program to seed file in read-only media. _pieceStorage->getDiskAdaptor()->enableReadOnly(); } else { // Open file in writable mode to allow the program - // truncate the file to btContext->getTotalLength() + // truncate the file to _downloadContext->getTotalLength() _logger->debug("File size not match. File is opened in writable mode." " Expected:%s Actual:%s", - Util::uitos(btContext->getTotalLength()).c_str(), + Util::uitos(_downloadContext->getTotalLength()).c_str(), Util::uitos(actualFileSize).c_str()); } } @@ -311,7 +302,7 @@ void RequestGroup::createInitialCommand(std::deque<Command*>& commands, throw DOWNLOAD_FAILURE_EXCEPTION (StringFormat (MSG_FILE_ALREADY_EXISTS, - getFilePath().c_str()).str()); + _downloadContext->getBasePath().c_str()).str()); } else { _pieceStorage->getDiskAdaptor()->openFile(); } @@ -324,14 +315,22 @@ void RequestGroup::createInitialCommand(std::deque<Command*>& commands, } _progressInfoFile = progressInfoFile; - if(!btContext->isPrivate() && _option->getAsBool(PREF_ENABLE_DHT)) { + if(torrentAttrs[bittorrent::PRIVATE].i() == 0 && + _option->getAsBool(PREF_ENABLE_DHT)) { std::deque<Command*> commands; DHTSetup().setup(commands, e, _option.get()); e->addCommand(commands); - if(!btContext->getNodes().empty() && DHTSetup::initialized()) { + if(!torrentAttrs[bittorrent::NODES].empty() && DHTSetup::initialized()) { + std::deque<std::pair<std::string, uint16_t> > entryPoints; + const BDE& nodes = torrentAttrs[bittorrent::NODES]; + for(BDE::List::const_iterator i = nodes.listBegin(); + i != nodes.listEnd(); ++i) { + std::pair<std::string, uint16_t> addr + ((*i)[bittorrent::HOSTNAME].s(), (*i)[bittorrent::PORT].i()); + entryPoints.push_back(addr); + } DHTEntryPointNameResolveCommand* command = - new DHTEntryPointNameResolveCommand(e->newCUID(), e, - btContext->getNodes()); + new DHTEntryPointNameResolveCommand(e->newCUID(), e, entryPoints); command->setTaskQueue(DHTRegistry::_taskQueue); command->setTaskFactory(DHTRegistry::_taskFactory); command->setRoutingTable(DHTRegistry::_routingTable); @@ -361,8 +360,10 @@ void RequestGroup::createInitialCommand(std::deque<Command*>& commands, if(e->_requestGroupMan->isSameFileBeingDownloaded(this)) { throw DOWNLOAD_FAILURE_EXCEPTION (StringFormat(EX_DUPLICATE_FILE_DOWNLOAD, - getFilePath().c_str()).str()); + _downloadContext->getBasePath().c_str()).str()); } + // TODO1.5 Renaming filename doesn't take into account of + // multi-file download. adjustFilename (SharedHandle<BtProgressInfoFile>(new DefaultBtProgressInfoFile (_downloadContext, @@ -375,7 +376,7 @@ void RequestGroup::createInitialCommand(std::deque<Command*>& commands, if(!infoFile->exists() && downloadFinishedByFileLength()) { _pieceStorage->markAllPiecesDone(); _logger->notice(MSG_DOWNLOAD_ALREADY_COMPLETED, - _gid, getFilePath().c_str()); + _gid, _downloadContext->getBasePath().c_str()); } else { loadAndOpenFile(infoFile); SharedHandle<CheckIntegrityEntry> checkIntegrityEntry @@ -412,7 +413,7 @@ void RequestGroup::initPieceStorage() // downloads. Currently multi-file integrated download is not supported. if(!_uris.empty() && _downloadContext->getFileEntries().size() == 1 && - !dynamic_pointer_cast<BtContext>(_downloadContext).isNull()) { + _downloadContext->hasAttribute(bittorrent::BITTORRENT)) { _logger->debug("Using LongestSequencePieceSelector"); ps->setPieceSelector (SharedHandle<PieceSelector>(new LongestSequencePieceSelector())); @@ -449,8 +450,7 @@ bool RequestGroup::downloadFinishedByFileLength() if(!_downloadContext->knowsTotalLength()) { return false; } - // TODO consider the case when the getFilePath() returns dir path. - File outfile(getFilePath()); + File outfile(getFirstFilePath()); if(outfile.exists() && _downloadContext->getTotalLength() == outfile.size()) { return true; } else { @@ -468,7 +468,7 @@ void RequestGroup::adjustFilename } else if(downloadFinishedByFileLength()) { // File was downloaded already, no need to change file name. } else { - File outfile(getFilePath()); + File outfile(getFirstFilePath()); if(outfile.exists() && _option->getAsBool(PREF_CONTINUE) && outfile.size() <= _downloadContext->getTotalLength()) { // File exists but user decided to resume it. @@ -498,14 +498,14 @@ void RequestGroup::loadAndOpenFile(const BtProgressInfoFileHandle& progressInfoF progressInfoFile->removeFile(); _logger->notice(MSG_REMOVED_DEFUNCT_CONTROL_FILE, progressInfoFile->getFilename().c_str(), - getFilePath().c_str()); + _downloadContext->getBasePath().c_str()); } if(progressInfoFile->exists()) { progressInfoFile->load(); _pieceStorage->getDiskAdaptor()->openExistingFile(); } else { - File outfile(getFilePath()); + File outfile(getFirstFilePath()); if(outfile.exists() && _option->getAsBool(PREF_CONTINUE) && outfile.size() <= getTotalLength()) { _pieceStorage->getDiskAdaptor()->openExistingFile(); @@ -535,43 +535,36 @@ void RequestGroup::shouldCancelDownloadForSafety() if(_option->getAsBool(PREF_ALLOW_OVERWRITE)) { return; } - File outfile(getFilePath()); + File outfile(getFirstFilePath()); if(outfile.exists()) { if(_option->getAsBool(PREF_AUTO_FILE_RENAMING)) { if(tryAutoFileRenaming()) { - _logger->notice(MSG_FILE_RENAMED, getFilePath().c_str()); + _logger->notice(MSG_FILE_RENAMED, getFirstFilePath().c_str()); } else { throw DOWNLOAD_FAILURE_EXCEPTION (StringFormat("File renaming failed: %s", - getFilePath().c_str()).str()); + getFirstFilePath().c_str()).str()); } } else { throw DOWNLOAD_FAILURE_EXCEPTION (StringFormat(MSG_FILE_ALREADY_EXISTS, - getFilePath().c_str()).str()); + getFirstFilePath().c_str()).str()); } } } bool RequestGroup::tryAutoFileRenaming() { - std::string filepath = getFilePath(); + std::string filepath = getFirstFilePath(); if(filepath.empty()) { return false; } - SingleFileDownloadContextHandle ctx = - dynamic_pointer_cast<SingleFileDownloadContext>(_downloadContext); - // Make a copy of ctx. - SingleFileDownloadContextHandle tempCtx(new SingleFileDownloadContext(*ctx.get())); - - DefaultBtProgressInfoFile tempInfoFile(tempCtx, SharedHandle<PieceStorage>(), 0); - for(unsigned int i = 1; i < 10000; ++i) { File newfile(strconcat(filepath, ".", Util::uitos(i))); - tempCtx->setUFilename(newfile.getPath()); - tempInfoFile.updateFilename(); - if(!newfile.exists() || (newfile.exists() && tempInfoFile.exists())) { - ctx->setUFilename(newfile.getPath()); + // TODO1.5 hard coded ".aria2" extension. + File ctrlfile(newfile.getPath()+".aria2"); + if(!newfile.exists() || (newfile.exists() && ctrlfile.exists())) { + _downloadContext->getFirstFileEntry()->setPath(newfile.getPath()); return true; } } @@ -639,8 +632,8 @@ void RequestGroup::createNextCommand(std::deque<Command*>& commands, static_cast<unsigned int>(_uris.size())); } } - std::deque<std::string> pendingURIs; + for(; numCommand--; ) { Command* command = new CreateRequestCommand(e->newCUID(), this, e); _logger->debug("filePath=%s", _downloadContext->getFileEntries().front()->getPath().c_str()); @@ -685,14 +678,14 @@ void RequestGroup::createNextCommand(std::deque<Command*>& commands, } } -std::string RequestGroup::getFilePath() const +std::string RequestGroup::getFirstFilePath() const { assert(!_downloadContext.isNull()); if(inMemoryDownload()) { static const std::string DIR_MEMORY("[MEMORY]"); - return DIR_MEMORY+File(_downloadContext->getActualBasePath()).getBasename(); + return DIR_MEMORY+File(_downloadContext->getFirstFileEntry()->getPath()).getBasename(); } else { - return _downloadContext->getActualBasePath(); + return _downloadContext->getFirstFileEntry()->getPath(); } } @@ -827,24 +820,25 @@ void RequestGroup::setForceHaltRequested(bool f, HaltReason haltReason) void RequestGroup::releaseRuntimeResource(DownloadEngine* e) { #ifdef ENABLE_BITTORRENT - BtContextHandle btContext = dynamic_pointer_cast<BtContext>(_downloadContext); - if(!btContext.isNull()) { + if(_downloadContext->hasAttribute(bittorrent::BITTORRENT)) { SharedHandle<BtRegistry> btRegistry = e->getBtRegistry(); - BtContextHandle btContextInReg = - btRegistry->getBtContext(btContext->getInfoHashAsString()); - // Make sure that the registered BtContext's GID is equal to - // this->getGID(). Even if createInitialCommand() throws - // exception without registering this BtContext, after that, this - // method is called. In this case, just finding BtContext using - // infoHash may detect another download's BtContext and deleting - // it from BtRegistry causes Segmentation Fault. - if(!btContextInReg.isNull() && - btContextInReg->getOwnerRequestGroup()->getGID() == - btContext->getOwnerRequestGroup()->getGID()) { - btRegistry->remove(btContext->getInfoHashAsString()); + const BDE& torrentAttrs = + _downloadContext->getAttribute(bittorrent::BITTORRENT); + const std::string& infoHash = torrentAttrs[bittorrent::INFO_HASH].s(); + SharedHandle<DownloadContext> contextInReg = + btRegistry->getDownloadContext(infoHash); + // Make sure that the registered DownloadContext's GID is equal to + // _gid. Even if createInitialCommand() throws exception without + // registering this DownloadContext, after that, this method is + // called. In this case, just finding DownloadContext using + // infoHash may detect another download's DownloadContext and + // deleting it from BtRegistry causes Segmentation Fault. + if(!contextInReg.isNull() && + contextInReg->getOwnerRequestGroup()->getGID() == _gid) { + btRegistry->remove(infoHash); if(!DHTRegistry::_peerAnnounceStorage.isNull()) { - DHTRegistry::_peerAnnounceStorage-> - removeLocalPeerAnnounce(btContext->getInfoHash()); + DHTRegistry::_peerAnnounceStorage->removeLocalPeerAnnounce + (torrentAttrs[bittorrent::INFO_HASH].uc()); } } } @@ -856,7 +850,8 @@ void RequestGroup::releaseRuntimeResource(DownloadEngine* e) void RequestGroup::preDownloadProcessing() { - _logger->debug("Finding PreDownloadHandler for path %s.", getFilePath().c_str()); + _logger->debug("Finding PreDownloadHandler for path %s.", + getFirstFilePath().c_str()); try { for(PreDownloadHandlers::const_iterator itr = _preDownloadHandlers.begin(); itr != _preDownloadHandlers.end(); ++itr) { @@ -876,7 +871,8 @@ void RequestGroup::preDownloadProcessing() void RequestGroup::postDownloadProcessing (std::deque<SharedHandle<RequestGroup> >& groups) { - _logger->debug("Finding PostDownloadHandler for path %s.", getFilePath().c_str()); + _logger->debug("Finding PostDownloadHandler for path %s.", + getFirstFilePath().c_str()); try { for(PostDownloadHandlers::const_iterator itr = _postDownloadHandlers.begin(); itr != _postDownloadHandlers.end(); ++itr) { @@ -970,11 +966,6 @@ void RequestGroup::clearPreDowloadHandler() _preDownloadHandlers.clear(); } -void RequestGroup::setDownloadContext(const DownloadContextHandle& downloadContext) -{ - _downloadContext = downloadContext; -} - void RequestGroup::setPieceStorage(const PieceStorageHandle& pieceStorage) { _pieceStorage = pieceStorage; @@ -1104,11 +1095,10 @@ void RequestGroup::removeIdenticalURI(const std::string& uri) void RequestGroup::reportDownloadFinished() { _logger->notice(MSG_FILE_DOWNLOAD_COMPLETED, - getFilePath().c_str()); + _downloadContext->getBasePath().c_str()); _uriSelector->resetCounters(); #ifdef ENABLE_BITTORRENT - SharedHandle<BtContext> ctx = dynamic_pointer_cast<BtContext>(_downloadContext); - if(!ctx.isNull()) { + if(_downloadContext->hasAttribute(bittorrent::BITTORRENT)) { TransferStat stat = calculateStat(); double shareRatio = ((stat.getAllTimeUploadLength()*10)/getCompletedLength())/10.0; _logger->notice(MSG_SHARE_RATIO_REPORT, diff --git a/src/RequestGroup.h b/src/RequestGroup.h index a8c4c388..3d579929 100644 --- a/src/RequestGroup.h +++ b/src/RequestGroup.h @@ -221,7 +221,7 @@ public: void closeFile(); - std::string getFilePath() const; + std::string getFirstFilePath() const; uint64_t getTotalLength() const; @@ -275,7 +275,10 @@ public: return _downloadContext; } - void setDownloadContext(const SharedHandle<DownloadContext>& downloadContext); + void setDownloadContext(const SharedHandle<DownloadContext>& downloadContext) + { + _downloadContext = downloadContext; + } const SharedHandle<PieceStorage>& getPieceStorage() const { diff --git a/src/RequestGroupEntry.cc b/src/RequestGroupEntry.cc index 01dd9b15..bba57f8b 100644 --- a/src/RequestGroupEntry.cc +++ b/src/RequestGroupEntry.cc @@ -35,6 +35,7 @@ #include "RequestGroupEntry.h" #include "RequestGroup.h" #include "Command.h" +#include "DownloadContext.h" namespace aria2 { diff --git a/src/RequestGroupMan.cc b/src/RequestGroupMan.cc index 41a0f515..9069a4dd 100644 --- a/src/RequestGroupMan.cc +++ b/src/RequestGroupMan.cc @@ -274,7 +274,7 @@ private: if(!sig.isNull() && !sig->getBody().empty()) { // filename of signature file is the path to download file followed by // ".sig". - std::string signatureFile = group->getFilePath()+".sig"; + std::string signatureFile = group->getFirstFilePath()+".sig"; if(sig->save(signatureFile)) { _logger->notice(MSG_SIGNATURE_SAVED, signatureFile.c_str()); } else { @@ -637,7 +637,7 @@ std::string RequestGroupMan::formatDownloadResult(const std::string& status, con o << "n/a"; } o << "|"; - const std::deque<SharedHandle<FileEntry> >& fileEntries = + const std::vector<SharedHandle<FileEntry> >& fileEntries = downloadResult->fileEntries; if(downloadResult->result == DownloadResult::FINISHED || downloadResult->numUri == 0) { @@ -677,7 +677,7 @@ bool RequestGroupMan::isSameFileBeingDownloaded(RequestGroup* requestGroup) cons for(RequestGroups::const_iterator itr = _requestGroups.begin(); itr != _requestGroups.end(); ++itr) { if((*itr).get() != requestGroup) { - const std::deque<SharedHandle<FileEntry> >& entries = + const std::vector<SharedHandle<FileEntry> >& entries = (*itr)->getDownloadContext()->getFileEntries(); std::transform(entries.begin(), entries.end(), std::back_inserter(files), @@ -685,7 +685,7 @@ bool RequestGroupMan::isSameFileBeingDownloaded(RequestGroup* requestGroup) cons } } std::sort(files.begin(), files.end()); - const std::deque<SharedHandle<FileEntry> >& entries = + const std::vector<SharedHandle<FileEntry> >& entries = requestGroup->getDownloadContext()->getFileEntries(); return sameFilePathExists(files.begin(), files.end(), entries.begin(), entries.end()); diff --git a/src/SeedCheckCommand.cc b/src/SeedCheckCommand.cc index d005f68e..680f1fdb 100644 --- a/src/SeedCheckCommand.cc +++ b/src/SeedCheckCommand.cc @@ -36,23 +36,21 @@ #include "DownloadEngine.h" #include "BtRuntime.h" #include "PieceStorage.h" -#include "BtContext.h" #include "Logger.h" #include "SeedCriteria.h" #include "message.h" #include "RequestGroup.h" +#include "DownloadContext.h" namespace aria2 { SeedCheckCommand::SeedCheckCommand(int cuid, RequestGroup* requestGroup, DownloadEngine* e, - const SharedHandle<BtContext>& btContext, const SeedCriteriaHandle& seedCriteria) :Command(cuid), _requestGroup(requestGroup), e(e), - _btContext(btContext), seedCriteria(seedCriteria), checkStarted(false) { diff --git a/src/SeedCheckCommand.h b/src/SeedCheckCommand.h index 19f6cf5d..7795e68f 100644 --- a/src/SeedCheckCommand.h +++ b/src/SeedCheckCommand.h @@ -43,7 +43,6 @@ namespace aria2 { class RequestGroup; class DownloadEngine; class SeedCriteria; -class BtContext; class BtRuntime; class PieceStorage; @@ -52,7 +51,6 @@ class SeedCheckCommand : public Command private: RequestGroup* _requestGroup; DownloadEngine* e; - SharedHandle<BtContext> _btContext; SharedHandle<PieceStorage> _pieceStorage; SharedHandle<BtRuntime> _btRuntime; SharedHandle<SeedCriteria> seedCriteria; @@ -61,7 +59,6 @@ public: SeedCheckCommand(int cuid, RequestGroup* requestGroup, DownloadEngine* e, - const SharedHandle<BtContext>& btContext, const SharedHandle<SeedCriteria>& seedCriteria); virtual ~SeedCheckCommand(); diff --git a/src/SegmentMan.cc b/src/SegmentMan.cc index 4598e3d3..a3c72a46 100644 --- a/src/SegmentMan.cc +++ b/src/SegmentMan.cc @@ -60,7 +60,7 @@ SegmentEntry::SegmentEntry(cuid_t cuid, const SegmentHandle& segment): SegmentEntry::~SegmentEntry() {} SegmentMan::SegmentMan(const Option* option, - const DownloadContextHandle& downloadContext, + const SharedHandle<DownloadContext>& downloadContext, const PieceStorageHandle& pieceStorage): _option(option), logger(LogFactory::getInstance()), @@ -104,7 +104,8 @@ void SegmentMan::setPieceStorage(const PieceStorageHandle& pieceStorage) _pieceStorage = pieceStorage; } -void SegmentMan::setDownloadContext(const DownloadContextHandle& downloadContext) +void SegmentMan::setDownloadContext +(const SharedHandle<DownloadContext>& downloadContext) { _downloadContext = downloadContext; } diff --git a/src/ShareRatioSeedCriteria.h b/src/ShareRatioSeedCriteria.h index d09d5219..e4eec635 100644 --- a/src/ShareRatioSeedCriteria.h +++ b/src/ShareRatioSeedCriteria.h @@ -36,7 +36,7 @@ #define _D_SHARE_RATIO_SEED_CRITERIA_H_ #include "SeedCriteria.h" -#include "BtContext.h" +#include "DownloadContext.h" #include "PeerStorage.h" #include "PieceStorage.h" @@ -45,20 +45,21 @@ namespace aria2 { class ShareRatioSeedCriteria : public SeedCriteria { private: double ratio; - SharedHandle<BtContext> btContext; + SharedHandle<DownloadContext> _downloadContext; SharedHandle<PeerStorage> _peerStorage; SharedHandle<PieceStorage> _pieceStorage; public: - ShareRatioSeedCriteria(double ratio, const SharedHandle<BtContext>& btContext) + ShareRatioSeedCriteria + (double ratio, const SharedHandle<DownloadContext>& downloadContext) :ratio(ratio), - btContext(btContext) {} + _downloadContext(downloadContext) {} virtual ~ShareRatioSeedCriteria() {} virtual void reset() {} virtual bool evaluate() { - if(btContext->getTotalLength() == 0) { + if(_downloadContext->getTotalLength() == 0) { return false; } TransferStat stat = _peerStorage->calculateStat(); diff --git a/src/SingleFileDownloadContext.cc b/src/SingleFileDownloadContext.cc deleted file mode 100644 index 1acbb2b1..00000000 --- a/src/SingleFileDownloadContext.cc +++ /dev/null @@ -1,108 +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 "SingleFileDownloadContext.h" -#include "FileEntry.h" - -namespace aria2 { - -const std::string SingleFileDownloadContext::DEFAULT_FILENAME("index.html"); - -SingleFileDownloadContext::SingleFileDownloadContext(size_t pieceLength, - uint64_t totalLength, - const std::string& filename, - const std::string& ufilename): - _pieceLength(pieceLength), - _filename(filename), - _ufilename(ufilename), - _knowsTotalLength(true) -{ - _fileEntries.push_back - (SharedHandle<FileEntry>(new FileEntry(filename, totalLength, 0))); - updateFileEntry(); -} - -void SingleFileDownloadContext::updateFileEntry() -{ - const SharedHandle<FileEntry>& fileEntry = _fileEntries.front(); - if(!_ufilename.empty()) { - fileEntry->setPath(_ufilename); - } else if(!_filename.empty()) { - fileEntry->setPath(_filename); - } else { - fileEntry->setPath(""); - } -} - -const std::deque<std::string>& -SingleFileDownloadContext::getPieceHashes() const -{ - return _pieceHashes; -} - -uint64_t SingleFileDownloadContext::getTotalLength() const -{ - return _fileEntries.front()->getLength(); -} - -bool SingleFileDownloadContext::knowsTotalLength() const -{ - return _knowsTotalLength; -} - -size_t SingleFileDownloadContext::getNumPieces() const -{ - if(_pieceLength == 0) { - return 0; - } else { - return (_fileEntries.front()->getLength()+_pieceLength-1)/_pieceLength; - } -} - -std::string SingleFileDownloadContext::getActualBasePath() const -{ - return _fileEntries.front()->getPath(); -} - -void SingleFileDownloadContext::setTotalLength(uint64_t totalLength) -{ - _fileEntries.front()->setLength(totalLength); -} - -void SingleFileDownloadContext::markTotalLengthIsUnknown() -{ - _knowsTotalLength = false; -} - -} // namespace aria2 diff --git a/src/SingleFileDownloadContext.h b/src/SingleFileDownloadContext.h deleted file mode 100644 index 7f07d9a9..00000000 --- a/src/SingleFileDownloadContext.h +++ /dev/null @@ -1,180 +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_SINGLE_FILE_DOWNLOAD_CONTEXT_H_ -#define _D_SINGLE_FILE_DOWNLOAD_CONTEXT_H_ - -#include "DownloadContext.h" - -#include <string> -#include <deque> - -#include "A2STR.h" - -namespace aria2 { - -class SingleFileDownloadContext:public DownloadContext -{ -private: - size_t _pieceLength; - /** - * _filename and _ufilename may contains directory path name. So - * /usr/local/aria2c is acceptable here. These should be complete - * file path. - */ - std::string _filename; - std::string _ufilename; - - std::string _contentType; - - std::deque<std::string> _pieceHashes; - std::string _pieceHashAlgo; - - std::string _checksum; - std::string _checksumHashAlgo; - - bool _knowsTotalLength; - - void updateFileEntry(); -public: - SingleFileDownloadContext(size_t pieceLength, - uint64_t totalLength, - const std::string& filename, - const std::string& ufilename = A2STR::NIL); - - virtual ~SingleFileDownloadContext() {} - - virtual 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; - - virtual uint64_t getTotalLength() const; - - virtual bool knowsTotalLength() const; - - - virtual FILE_MODE getFileMode() const - { - if(_fileEntries.size() == 1) { - return SINGLE; - } else { - return MULTI; - } - } - - virtual size_t getPieceLength() const - { - return _pieceLength; - } - - virtual size_t getNumPieces() const; - - virtual std::string getActualBasePath() const; - - virtual const std::string& getPieceHashAlgo() const - { - return _pieceHashAlgo; - } - - const std::string& getChecksumHashAlgo() const - { - return _checksumHashAlgo; - } - - const std::string& getChecksum() const - { - return _checksum; - } - - void setPieceHashes(const std::deque<std::string>& pieceHashes) - { - _pieceHashes = pieceHashes; - } - - void setChecksumHashAlgo(const std::string& algo) - { - _checksumHashAlgo = algo; - } - - void setChecksum(const std::string& checksum) - { - _checksum = checksum; - } - - void setFilename(const std::string& filename) - { - _filename = filename; - updateFileEntry(); - } - - void setUFilename(const std::string& ufilename) - { - _ufilename = ufilename; - updateFileEntry(); - } - - void setTotalLength(uint64_t totalLength); - - void markTotalLengthIsUnknown(); - - void setPieceHashAlgo(const std::string& algo) - { - _pieceHashAlgo = algo; - } - - void setContentType(const std::string& contentType) - { - _contentType = contentType; - } - - const std::string& getContentType() - { - return _contentType; - } - - static const std::string DEFAULT_FILENAME; -}; - -typedef SharedHandle<SingleFileDownloadContext> SingleFileDownloadContextHandle; - -} // namespace aria2 - -#endif // _D_SINGLE_FILE_DOWNLOAD_CONTEXT_H_ diff --git a/src/SleepCommand.cc b/src/SleepCommand.cc index 17912215..5fd18604 100644 --- a/src/SleepCommand.cc +++ b/src/SleepCommand.cc @@ -35,6 +35,7 @@ #include "SleepCommand.h" #include "RequestGroup.h" #include "DownloadEngine.h" +#include "DownloadContext.h" namespace aria2 { diff --git a/src/StreamCheckIntegrityEntry.cc b/src/StreamCheckIntegrityEntry.cc index 5a4014c3..39e25393 100644 --- a/src/StreamCheckIntegrityEntry.cc +++ b/src/StreamCheckIntegrityEntry.cc @@ -37,6 +37,7 @@ #include "DownloadEngine.h" #include "StreamFileAllocationEntry.h" #include "Request.h" +#include "DownloadContext.h" namespace aria2 { diff --git a/src/TrackerWatcherCommand.cc b/src/TrackerWatcherCommand.cc index 87a51c58..dddf163e 100644 --- a/src/TrackerWatcherCommand.cc +++ b/src/TrackerWatcherCommand.cc @@ -37,7 +37,6 @@ #include <sstream> #include "DownloadEngine.h" -#include "BtContext.h" #include "BtAnnounce.h" #include "BtRuntime.h" #include "PieceStorage.h" @@ -45,7 +44,6 @@ #include "Peer.h" #include "prefs.h" #include "message.h" -#include "SingleFileDownloadContext.h" #include "ByteArrayDiskWriterFactory.h" #include "RecoverableException.h" #include "PeerInitiateConnectionCommand.h" @@ -59,17 +57,16 @@ #include "SocketCore.h" #include "Request.h" #include "AnnounceTier.h" +#include "DownloadContext.h" +#include "bittorrent_helper.h" namespace aria2 { -TrackerWatcherCommand::TrackerWatcherCommand(int32_t cuid, - RequestGroup* requestGroup, - DownloadEngine* e, - const BtContextHandle& btContext): +TrackerWatcherCommand::TrackerWatcherCommand +(int32_t cuid, RequestGroup* requestGroup, DownloadEngine* e): Command(cuid), _requestGroup(requestGroup), - e(e), - _btContext(btContext) + e(e) { _requestGroup->increaseNumCommand(); } @@ -162,12 +159,8 @@ void TrackerWatcherCommand::processTrackerResponse } peer->usedBy(e->newCUID()); PeerInitiateConnectionCommand* command = - new PeerInitiateConnectionCommand(peer->usedBy(), - _requestGroup, - peer, - e, - _btContext, - _btRuntime); + new PeerInitiateConnectionCommand + (peer->usedBy(), _requestGroup, peer, e, _btRuntime); command->setPeerStorage(_peerStorage); command->setPieceStorage(_pieceStorage); e->commands.push_back(command); @@ -185,15 +178,17 @@ RequestGroupHandle TrackerWatcherCommand::createAnnounce() { } static bool backupTrackerIsAvailable -(const std::deque<SharedHandle<AnnounceTier> >& announceTiers) +(const SharedHandle<DownloadContext>& context) { - if(announceTiers.size() >= 2) { + const BDE& announceList = + context->getAttribute(bittorrent::BITTORRENT)[bittorrent::ANNOUNCE_LIST]; + if(announceList.size() >= 2) { return true; } - if(announceTiers.empty()) { + if(announceList.empty()) { return false; } - if(announceTiers[0]->urls.size() >= 2) { + if(announceList[0].size() >= 2) { return true; } else { return false; @@ -208,7 +203,7 @@ TrackerWatcherCommand::createRequestGroup(const std::string& uri) RequestGroupHandle rg(new RequestGroup(getOption(), uris)); // If backup tracker is available, only try 2 times for each tracker // and if they all fails, then try next one. - if(backupTrackerIsAvailable(_btContext->getAnnounceTiers())) { + if(backupTrackerIsAvailable(_requestGroup->getDownloadContext())) { logger->debug("This is multi-tracker announce."); rg->getOption()->put(PREF_MAX_TRIES, "2"); } else { @@ -217,11 +212,10 @@ TrackerWatcherCommand::createRequestGroup(const std::string& uri) } static const std::string TRACKER_ANNOUNCE_FILE("[tracker.announce]"); - SingleFileDownloadContextHandle dctx - (new SingleFileDownloadContext(getOption()->getAsInt(PREF_SEGMENT_SIZE), - 0, - A2STR::NIL, - TRACKER_ANNOUNCE_FILE)); + SharedHandle<DownloadContext> dctx + (new DownloadContext(getOption()->getAsInt(PREF_SEGMENT_SIZE), + 0, + TRACKER_ANNOUNCE_FILE)); dctx->setDir(A2STR::NIL); dctx->getFileEntries().front()->setUris(uris); rg->setDownloadContext(dctx); diff --git a/src/TrackerWatcherCommand.h b/src/TrackerWatcherCommand.h index 9c7c2417..7ec0c03f 100644 --- a/src/TrackerWatcherCommand.h +++ b/src/TrackerWatcherCommand.h @@ -42,7 +42,6 @@ namespace aria2 { class DownloadEngine; class RequestGroup; -class BtContext; class PeerStorage; class PieceStorage; class BtRuntime; @@ -56,8 +55,6 @@ private: DownloadEngine* e; - SharedHandle<BtContext> _btContext; - SharedHandle<PeerStorage> _peerStorage; SharedHandle<PieceStorage> _pieceStorage; @@ -81,8 +78,7 @@ private: public: TrackerWatcherCommand(int32_t cuid, RequestGroup* requestGroup, - DownloadEngine* e, - const SharedHandle<BtContext>& btContext); + DownloadEngine* e); virtual ~TrackerWatcherCommand(); diff --git a/src/UnknownLengthPieceStorage.cc b/src/UnknownLengthPieceStorage.cc index 369767c7..878fff93 100644 --- a/src/UnknownLengthPieceStorage.cc +++ b/src/UnknownLengthPieceStorage.cc @@ -46,8 +46,9 @@ namespace aria2 { -UnknownLengthPieceStorage::UnknownLengthPieceStorage(const DownloadContextHandle& downloadContext, - const Option* option): +UnknownLengthPieceStorage::UnknownLengthPieceStorage +(const SharedHandle<DownloadContext>& downloadContext, + const Option* option): _downloadContext(downloadContext), _option(option), _diskWriterFactory(new DefaultDiskWriterFactory()), @@ -60,7 +61,8 @@ void UnknownLengthPieceStorage::initStorage() { 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()); diff --git a/src/Util.cc b/src/Util.cc index c2cabda1..0ade0084 100644 --- a/src/Util.cc +++ b/src/Util.cc @@ -43,7 +43,6 @@ #include <cstring> #include <cstdio> #include <cstdlib> -#include <iomanip> #include <sstream> #include <ostream> #include <algorithm> @@ -709,20 +708,6 @@ time_t Util::httpGMT(const std::string& httpStdTime) return thetime; } -void Util::toStream(std::ostream& os, const FileEntries& fileEntries) -{ - os << _("Files:") << "\n"; - os << "idx|path/length" << "\n"; - os << "===+===========================================================================" << "\n"; - int32_t count = 1; - for(FileEntries::const_iterator itr = fileEntries.begin(); - itr != fileEntries.end(); ++count, ++itr) { - os << std::setw(3) << count << "|" << (*itr)->getPath() << "\n"; - os << " |" << Util::abbrevSize((*itr)->getLength()) << "B" << "\n"; - os << "---+---------------------------------------------------------------------------" << "\n"; - } -} - void Util::sleep(long seconds) { #ifdef HAVE_SLEEP ::sleep(seconds); @@ -43,8 +43,10 @@ #include <utility> #include <deque> #include <iosfwd> +#include <ostream> #include <numeric> #include <map> +#include <iomanip> #include "SharedHandle.h" #include "IntSequence.h" @@ -243,8 +245,20 @@ public: */ static time_t httpGMT(const std::string& httpTimeFormat); - static void toStream(std::ostream& os, - const std::deque<SharedHandle<FileEntry> >& entries); + template<typename InputIterator> + static void toStream + (InputIterator first, InputIterator last, std::ostream& os) + { + os << _("Files:") << "\n"; + os << "idx|path/length" << "\n"; + os << "===+===========================================================================" << "\n"; + int32_t count = 1; + for(; first != last; ++first, ++count) { + os << std::setw(3) << count << "|" << (*first)->getPath() << "\n"; + os << " |" << Util::abbrevSize((*first)->getLength()) << "B" << "\n"; + os << "---+---------------------------------------------------------------------------" << "\n"; + } + } static void sleep(long seconds); diff --git a/src/XmlRpcMethodImpl.cc b/src/XmlRpcMethodImpl.cc index 8bc629a2..954369c2 100644 --- a/src/XmlRpcMethodImpl.cc +++ b/src/XmlRpcMethodImpl.cc @@ -51,17 +51,20 @@ #include "StringFormat.h" #include "XmlRpcRequest.h" #include "PieceStorage.h" -#include "PeerStorage.h" -#include "BtContext.h" -#include "BtRegistry.h" -#include "Peer.h" +#include "DownloadContext.h" #include "DiskAdaptor.h" #include "FileEntry.h" #include "BtProgressInfoFile.h" -#include "BtRuntime.h" -#include "BtAnnounce.h" #include "prefs.h" #include "message.h" +#ifdef ENABLE_BITTORRENT +# include "bittorrent_helper.h" +# include "BtRegistry.h" +# include "PeerStorage.h" +# include "Peer.h" +# include "BtRuntime.h" +# include "BtAnnounce.h" +#endif // ENABLE_BITTORRENT namespace aria2 { @@ -286,13 +289,12 @@ static void gatherProgressCommon #ifdef ENABLE_BITTORRENT static void gatherProgressBitTorrent -(BDE& entryDict, const SharedHandle<BtContext>& btctx, - const SharedHandle<BtRegistry>& btreg) +(BDE& entryDict, const BDE& torrentAttrs, const SharedHandle<BtRegistry>& btreg) { - entryDict["infoHash"] = btctx->getInfoHashAsString(); + const std::string& infoHash = torrentAttrs[bittorrent::INFO_HASH].s(); + entryDict["infoHash"] = Util::toHex(infoHash); - SharedHandle<PeerStorage> peerStorage = - btreg->get(btctx->getInfoHashAsString())._peerStorage; + SharedHandle<PeerStorage> peerStorage = btreg->get(infoHash)._peerStorage; assert(!peerStorage.isNull()); std::deque<SharedHandle<Peer> > peers; @@ -329,11 +331,11 @@ static void gatherProgress { gatherProgressCommon(entryDict, group); #ifdef ENABLE_BITTORRENT - SharedHandle<BtContext> btctx = - dynamic_pointer_cast<BtContext>(group->getDownloadContext()); - if(!btctx.isNull()) { + if(group->getDownloadContext()->hasAttribute(bittorrent::BITTORRENT)) { + const BDE& torrentAttrs = + group->getDownloadContext()->getAttribute(bittorrent::BITTORRENT); SharedHandle<BtRegistry> btreg = e->getBtRegistry(); - gatherProgressBitTorrent(entryDict, btctx, btreg); + gatherProgressBitTorrent(entryDict, torrentAttrs, btreg); } #endif // ENABLE_BITTORRENT } @@ -400,9 +402,9 @@ BDE GetFilesXmlRpcMethod::process createFileEntry(files, dr->fileEntries.begin(), dr->fileEntries.end()); } } else { - const std::deque<SharedHandle<FileEntry> >& fileEntries = - group->getDownloadContext()->getFileEntries(); - createFileEntry(files, fileEntries.begin(), fileEntries.end()); + createFileEntry(files, + group->getDownloadContext()->getFileEntries().begin(), + group->getDownloadContext()->getFileEntries().end()); } return files; } @@ -455,12 +457,12 @@ BDE GetPeersXmlRpcMethod::process (StringFormat("No peer data is available for GID#%d", gid).str()); } BDE peers = BDE::list(); - SharedHandle<BtContext> btctx = - dynamic_pointer_cast<BtContext>(group->getDownloadContext()); - if(!btctx.isNull()) { + if(group->getDownloadContext()->hasAttribute(bittorrent::BITTORRENT)) { SharedHandle<BtRegistry> btreg = e->getBtRegistry(); + const BDE& torrentAttrs = + group->getDownloadContext()->getAttribute(bittorrent::BITTORRENT); SharedHandle<PeerStorage> peerStorage = - btreg->get(btctx->getInfoHashAsString())._peerStorage; + btreg->get(torrentAttrs[bittorrent::INFO_HASH].s())._peerStorage; assert(!peerStorage.isNull()); BDE entry = BDE::dict(); gatherPeer(peers, peerStorage); diff --git a/src/bittorrent_helper.cc b/src/bittorrent_helper.cc new file mode 100644 index 00000000..cb3be528 --- /dev/null +++ b/src/bittorrent_helper.cc @@ -0,0 +1,529 @@ +/* <!-- copyright */ +/* + * aria2 - The high speed download utility + * + * Copyright (C) 2009 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 "bittorrent_helper.h" + +#include <algorithm> +#include <deque> + +#include "DownloadContext.h" +#include "Randomizer.h" +#include "bencode.h" +#include "Util.h" +#include "DlAbortEx.h" +#include "message.h" +#include "StringFormat.h" +#include "BtConstants.h" +#include "messageDigest.h" +#include "MessageDigestHelper.h" +#include "PeerMessageUtil.h" +#include "SimpleRandomizer.h" + +namespace aria2 { + +namespace bittorrent { + +static const std::string C_NAME("name"); + +static const std::string C_NAME_UTF8("name.utf-8"); + +static const std::string C_FILES("files"); + +static const std::string C_LENGTH("length"); + +static const std::string C_PATH("path"); + +static const std::string C_PATH_UTF8("path.utf-8"); + +static const std::string C_INFO("info"); + +static const std::string C_PIECES("pieces"); + +static const std::string C_PIECE_LENGTH("piece length"); + +static const std::string C_PRIVATE("private"); + +static const std::string C_URL_LIST("url-list"); + +static const std::string C_ANNOUNCE("announce"); + +static const std::string C_ANNOUNCE_LIST("announce-list"); + +static const std::string C_NODES("nodes"); + +static const std::string DEFAULT_PEER_ID_PREFIX("-aria2-"); + +const std::string INFO_HASH("infoHash"); + +const std::string MODE("mode"); + +const std::string PRIVATE("private"); + +const std::string ANNOUNCE_LIST("announceList"); + +const std::string NODES("nodes"); + +const std::string HOSTNAME("hostname"); + +const std::string PORT("port"); + +const std::string NAME("name"); + +const std::string BITTORRENT("bittorrent"); + +const std::string MULTI("multi"); + +const std::string SINGLE("single"); + +static void extractPieceHash(const SharedHandle<DownloadContext>& ctx, + const std::string& hashData, + size_t hashLength, + size_t numPieces) +{ + std::vector<std::string> pieceHashes; + for(size_t i = 0; i < numPieces; ++i) { + pieceHashes.push_back(Util::toHex(hashData.data()+i*hashLength, + hashLength)); + } + ctx->setPieceHashes(pieceHashes.begin(), pieceHashes.end()); + ctx->setPieceHashAlgo(MessageDigestContext::SHA1); +} + +static void extractUrlList(std::vector<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()); + } +} + +static void extractFileEntries +(const SharedHandle<DownloadContext>& ctx, + BDE& torrent, + const BDE& infoDict, + const std::string& defaultName, + const std::string& overrideName, + const std::vector<std::string>& urlList) +{ + std::string name; + 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()) { + // Slice path by '/' just in case nasty ".." is included in name + std::deque<std::string> pathelems; + Util::slice(pathelems, nameData.s(), '/'); + name = Util::joinPath(pathelems.begin(), pathelems.end()); + torrent[NAME] = nameData.s(); + } else { + name = strconcat(File(defaultName).getBasename(), ".file"); + torrent[NAME] = name; + } + } else { + name = overrideName; + torrent[NAME] = name; + } + + const BDE& filesList = infoDict[C_FILES]; + std::vector<SharedHandle<FileEntry> > fileEntries; + if(filesList.isList()) { + uint64_t length = 0; + off_t offset = 0; + // multi-file mode + torrent[MODE] = 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)); + SharedHandle<FileEntry> fileEntry + (new FileEntry(strconcat(ctx->getDir(), "/", path), + fileLengthData.i(), + offset, uris)); + fileEntries.push_back(fileEntry); + offset += fileEntry->getLength(); + } + } else { + // single-file mode; + torrent[MODE] = SINGLE; + const BDE& lengthData = infoDict[C_LENGTH]; + if(!lengthData.isInteger()) { + throw DL_ABORT_EX(StringFormat(MSG_MISSING_BT_INFO, + C_LENGTH.c_str()).str()); + } + uint64_t totalLength = lengthData.i(); + + // 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::vector<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(ctx->getDir(), "/", name),totalLength, 0, + uris)); + fileEntries.push_back(fileEntry); + } + ctx->setFileEntries(fileEntries.begin(), fileEntries.end()); + if(torrent[MODE].s() == MULTI) { + ctx->setBasePath(strconcat(ctx->getDir(), "/", name)); + } +} + +static void extractAnnounce(BDE& torrent, const BDE& rootDict) +{ + const BDE& announceList = rootDict[C_ANNOUNCE_LIST]; + if(announceList.isList()) { + torrent[ANNOUNCE_LIST] = announceList; + } else { + const BDE& announce = rootDict[C_ANNOUNCE]; + BDE announceList = BDE::list(); + if(announce.isString()) { + announceList << BDE::list(); + announceList[0] << announce; + } + torrent[ANNOUNCE_LIST] = announceList; + } +} + +static void extractNodes(BDE& torrent, const BDE& nodesList) +{ + BDE nodes = BDE::list(); + if(nodesList.isList()) { + 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; + } + BDE node = BDE::dict(); + node[HOSTNAME] = hostname; + node[PORT] = port; + nodes << node; + } + } + torrent[NODES] = nodes; +} + +static void processRootDictionary +(const SharedHandle<DownloadContext>& ctx, + const BDE& rootDict, + const std::string& defaultName, + const std::string& overrideName) +{ + 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()); + } + BDE torrent = BDE::dict(); + + // retrieve infoHash + std::string encodedInfoDict = bencode::encode(infoDict); + unsigned char infoHash[INFO_HASH_LENGTH]; + MessageDigestHelper::digest(infoHash, INFO_HASH_LENGTH, + MessageDigestContext::SHA1, + encodedInfoDict.data(), + encodedInfoDict.size()); + torrent[INFO_HASH] = std::string(&infoHash[0], &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."); + } + size_t 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()); + } + size_t pieceLength = pieceLengthData.i(); + ctx->setPieceLength(pieceLength); + // retrieve piece hashes + extractPieceHash(ctx, piecesData.s(), PIECE_HASH_LENGTH, numPieces); + // private flag + const BDE& privateData = infoDict[C_PRIVATE]; + int privatefg = 0; + if(privateData.isInteger()) { + if(privateData.i() == 1) { + privatefg = 1; + } + } + torrent[PRIVATE] = BDE((int64_t)privatefg); + // retrieve uri-list. + // This implemantation obeys HTTP-Seeding specification: + // see http://www.getright.com/seedtorrent.html + std::vector<std::string> urlList; + extractUrlList(urlList, rootDict[C_URL_LIST]); + + // retrieve file entries + extractFileEntries(ctx, torrent, infoDict, defaultName, overrideName, urlList); + if((ctx->getTotalLength()+pieceLength-1)/pieceLength != numPieces) { + throw DL_ABORT_EX("Too few/many piece hash."); + } + // retrieve announce + extractAnnounce(torrent, rootDict); + // retrieve nodes + extractNodes(torrent, rootDict[C_NODES]); + + ctx->setAttribute(BITTORRENT, torrent); +} + +void load(const std::string& torrentFile, + const SharedHandle<DownloadContext>& ctx, + const std::string& overrideName) +{ + processRootDictionary(ctx, + bencode::decodeFromFile(torrentFile), + torrentFile, + overrideName); + +} + +void loadFromMemory(const unsigned char* content, + size_t length, + const SharedHandle<DownloadContext>& ctx, + const std::string& defaultName, + const std::string& overrideName) +{ + processRootDictionary(ctx, + bencode::decode(content, length), + defaultName, + overrideName); +} + +void loadFromMemory(const std::string& context, + const SharedHandle<DownloadContext>& ctx, + const std::string& defaultName, + const std::string& overrideName) +{ + processRootDictionary + (ctx, + bencode::decode(reinterpret_cast<const unsigned char*>(context.c_str()), + context.size()), + defaultName, overrideName); +} + +const unsigned char* +getInfoHash(const SharedHandle<DownloadContext>& downloadContext) +{ + return downloadContext->getAttribute(BITTORRENT)[INFO_HASH].uc(); +} + +std::string +getInfoHashString(const SharedHandle<DownloadContext>& downloadContext) +{ + return Util::toHex(downloadContext->getAttribute(BITTORRENT)[INFO_HASH].s()); +} + +void print(std::ostream& o, const SharedHandle<DownloadContext>& dctx) +{ + const BDE& torrentAttrs = dctx->getAttribute(BITTORRENT); + o << "*** BitTorrent File Information ***" << "\n"; + o << "Mode: " << torrentAttrs[MODE].s() << "\n"; + o << "Announce:" << "\n"; + const BDE& announceList = torrentAttrs[ANNOUNCE_LIST]; + for(BDE::List::const_iterator tieritr = announceList.listBegin(); + tieritr != announceList.listEnd(); ++tieritr) { + if(!(*tieritr).isList()) { + continue; + } + for(BDE::List::const_iterator i = (*tieritr).listBegin(); + i != (*tieritr).listEnd(); ++i) { + o << " " << (*i).s(); + } + o << "\n"; + } + o << "Info Hash: " << Util::toHex(torrentAttrs[INFO_HASH].s()) << "\n"; + o << "Piece Length: " << Util::abbrevSize(dctx->getPieceLength()) << "B\n"; + o << "The Number of Pieces: " << dctx->getNumPieces() << "\n"; + o << "Total Length: " << Util::abbrevSize(dctx->getTotalLength()) << "B\n"; + o << "Name: " << torrentAttrs[NAME].s() << "\n"; + Util::toStream(dctx->getFileEntries().begin(), dctx->getFileEntries().end(), o); +} + +void computeFastSet +(std::vector<size_t>& fastSet, const std::string& ipaddr, + size_t numPieces, const unsigned char* infoHash, 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::string generatePeerId +(const std::string& peerIdPrefix, const SharedHandle<Randomizer>& randomizer) +{ + std::string peerId = peerIdPrefix; + peerId += Util::randomAlpha(20-peerIdPrefix.size(), randomizer); + if(peerId.size() > 20) { + peerId.erase(20); + } + return peerId; +} + +static std::string peerId; + +const std::string& generateStaticPeerId +(const std::string& peerIdPrefix, const SharedHandle<Randomizer>& randomizer) +{ + if(peerId.empty()) { + peerId = generatePeerId(peerIdPrefix, randomizer); + } + return peerId; +} + +void setStaticPeerId(const std::string& newPeerId) +{ + peerId = newPeerId; +} + +// If PeerID is not generated, it is created with default peerIdPrefix +// (-aria2-) and SimpleRandomizer. +const unsigned char* getStaticPeerId() +{ + if(peerId.empty()) { + return + reinterpret_cast<const unsigned char*>(generateStaticPeerId(DEFAULT_PEER_ID_PREFIX, SimpleRandomizer::getInstance()).data()); + } else { + return reinterpret_cast<const unsigned char*>(peerId.data()); + } +} + +} // namespace bittorrent + +} // namespace aria2 diff --git a/src/bittorrent_helper.h b/src/bittorrent_helper.h new file mode 100644 index 00000000..99db7d9a --- /dev/null +++ b/src/bittorrent_helper.h @@ -0,0 +1,132 @@ +/* <!-- copyright */ +/* + * aria2 - The high speed download utility + * + * Copyright (C) 2009 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_BITTORRENT_HELPER_H_ +#define _D_BITTORRENT_HELPER_H_ + +#include "common.h" + +#include <string> +#include <vector> + +#include "SharedHandle.h" +#include "AnnounceTier.h" +#include "BDE.h" +#include "Util.h" + +namespace aria2 { + +class DownloadContext; +class Randomizer; + +namespace bittorrent { + +extern const std::string INFO_HASH; + +extern const std::string MODE; + +extern const std::string PRIVATE; + +extern const std::string ANNOUNCE_LIST; + +extern const std::string NODES; + +extern const std::string HOSTNAME; + +extern const std::string PORT; + +extern const std::string NAME; + +extern const std::string SINGLE; + +extern const std::string MULTI; + +extern const std::string BITTORRENT; + +void load(const std::string& torrentFile, + const SharedHandle<DownloadContext>& ctx, + const std::string& overrideName = ""); + +void loadFromMemory(const unsigned char* content, size_t length, + const SharedHandle<DownloadContext>& ctx, + const std::string& defaultName, + const std::string& overrideName = ""); + +void loadFromMemory(const std::string& context, + const SharedHandle<DownloadContext>& ctx, + const std::string& defaultName, + const std::string& overrideName = ""); + +// Generates Peer ID. BitTorrent specification says Peer ID is 20-byte +// length. This function uses peerIdPrefix as a Peer ID and it is +// less than 20bytes, random bytes are generated and appened to it. If +// peerIdPrefix is larger than 20bytes, first 20bytes are used. +std::string generatePeerId +(const std::string& peerIdPrefix, const SharedHandle<Randomizer>& randomizer); + +// Generates Peer ID and stores it in static variable. This function +// uses generatePeerId(peerIdPrefix, randomizer) to produce Peer ID. +const std::string& generateStaticPeerId +(const std::string& peerIdPrefix, const SharedHandle<Randomizer>& randomizer); + +// Returns Peer ID statically stored by generateStaticPeerId(). If +// Peer ID is not stored yet, this function calls +// generateStaticPeerId("-aria2-", randomizer) +const unsigned char* getStaticPeerId(); + +// Set newPeerId as a static Peer ID. newPeerId must be 20-byte +// length. +void setStaticPeerId(const std::string& newPeerId); + +// Computes fast set index and stores them in fastset. +void computeFastSet +(std::vector<size_t>& fastSet, const std::string& ipaddr, + size_t numPieces, const unsigned char* infoHash, size_t fastSetSize); + +// Writes the detailed information about torrent loaded in dctx. +void print(std::ostream& o, const SharedHandle<DownloadContext>& dctx); + +// Returns the value associated with INFO_HASH key in BITTORRENT +// attribute. +const unsigned char* +getInfoHash(const SharedHandle<DownloadContext>& downloadContext); + +std::string +getInfoHashString(const SharedHandle<DownloadContext>& downloadContext); + +} // namespace bittorrent + +} // namespace aria2 + +#endif // _D_BITTORRENT_HELPER_H_ diff --git a/src/download_helper.cc b/src/download_helper.cc index b232f33d..baabf94e 100644 --- a/src/download_helper.cc +++ b/src/download_helper.cc @@ -48,12 +48,11 @@ #include "ParameterizedStringParser.h" #include "PStringBuildVisitor.h" #include "UriListParser.h" -#include "SingleFileDownloadContext.h" +#include "DownloadContext.h" #include "RecoverableException.h" #include "DlAbortEx.h" #include "message.h" #include "StringFormat.h" -#include "DefaultBtContext.h" #include "FileEntry.h" #include "LogFactory.h" #include "File.h" @@ -62,6 +61,9 @@ #include "OptionHandler.h" #include "ByteArrayDiskWriter.h" #include "a2functional.h" +#ifdef ENABLE_BITTORRENT +# include "bittorrent_helper.h" +#endif // ENABLE_BITTORRENT namespace aria2 { @@ -178,16 +180,14 @@ static SharedHandle<RequestGroup> createRequestGroup bool useOutOption = false) { SharedHandle<RequestGroup> rg(new RequestGroup(option, uris)); - SharedHandle<SingleFileDownloadContext> dctx - (new SingleFileDownloadContext + SharedHandle<DownloadContext> dctx + (new DownloadContext (option->getAsInt(PREF_SEGMENT_SIZE), 0, - A2STR::NIL, useOutOption&&!option->blank(PREF_OUT)? strconcat(option->get(PREF_DIR), "/", option->get(PREF_OUT)):A2STR::NIL)); - dctx->setDir(option->get(PREF_DIR)); - dctx->getFileEntries().front()->setUris(uris); + dctx->getFirstFileEntry()->setUris(uris); rg->setDownloadContext(dctx); return rg; } @@ -202,28 +202,26 @@ createBtRequestGroup(const std::string& torrentFilePath, const std::string& torrentData = "") { SharedHandle<RequestGroup> rg(new RequestGroup(option, auxUris)); - SharedHandle<DefaultBtContext> btContext(new DefaultBtContext()); - btContext->setDir(option->get(PREF_DIR)); + SharedHandle<DownloadContext> dctx(new DownloadContext()); + dctx->setDir(option->get(PREF_DIR)); if(torrentData.empty()) { - btContext->load(torrentFilePath);// may throw exception + bittorrent::load(torrentFilePath, dctx);// may throw exception } else { - btContext->loadFromMemory(torrentData, "default"); // may throw exception - } - if(option->defined(PREF_PEER_ID_PREFIX)) { - btContext->setPeerIdPrefix(option->get(PREF_PEER_ID_PREFIX)); + bittorrent::loadFromMemory(torrentData, dctx, "default"); // may + // throw + // exception } - btContext->setFileFilter - (Util::parseIntRange(option->get(PREF_SELECT_FILE))); + dctx->setFileFilter(Util::parseIntRange(option->get(PREF_SELECT_FILE))); std::istringstream indexOutIn(option->get(PREF_INDEX_OUT)); std::map<size_t, std::string> indexPathMap = Util::createIndexPathMap(indexOutIn); for(std::map<size_t, std::string>::const_iterator i = indexPathMap.begin(); i != indexPathMap.end(); ++i) { - btContext->setFilePathWithIndex - ((*i).first, strconcat(btContext->getDir(), "/", (*i).second)); + dctx->setFilePathWithIndex + ((*i).first, strconcat(dctx->getDir(), "/", (*i).second)); } - rg->setDownloadContext(btContext); - btContext->setOwnerRequestGroup(rg.get()); + rg->setDownloadContext(dctx); + dctx->setOwnerRequestGroup(rg.get()); return rg; } diff --git a/src/main.cc b/src/main.cc index 1c4210bd..a3a13ffb 100644 --- a/src/main.cc +++ b/src/main.cc @@ -59,7 +59,6 @@ #include "a2io.h" #include "a2time.h" #include "Platform.h" -#include "DefaultBtContext.h" #include "FileEntry.h" #include "RequestGroup.h" #include "ConsoleStatCalc.h" @@ -69,6 +68,10 @@ #include "ProtocolDetector.h" #include "RecoverableException.h" #include "SocketCore.h" +#include "DownloadContext.h" +#ifdef ENABLE_BITTORRENT +# include "bittorrent_helper.h" +#endif // ENABLE_BITTORRENT #ifdef ENABLE_METALINK # include "MetalinkHelper.h" # include "MetalinkEntry.h" @@ -108,9 +111,9 @@ std::ostream& getSummaryOut(const SharedHandle<Option>& op) #ifdef ENABLE_BITTORRENT static void showTorrentFile(const std::string& uri) { - SharedHandle<DefaultBtContext> btContext(new DefaultBtContext()); - btContext->load(uri); - std::cout << btContext << std::endl; + SharedHandle<DownloadContext> dctx(new DownloadContext()); + bittorrent::load(uri, dctx); + bittorrent::print(std::cout, dctx); } #endif // ENABLE_BITTORRENT @@ -122,7 +125,7 @@ static void showMetalinkFile MetalinkHelper::parseAndQuery(metalinkEntries, uri, op.get()); std::deque<SharedHandle<FileEntry> > fileEntries; MetalinkEntry::toFileEntry(fileEntries, metalinkEntries); - Util::toStream(std::cout, fileEntries); + Util::toStream(fileEntries.begin(), fileEntries.end(), std::cout); std::cout << std::endl; } #endif // ENABLE_METALINK diff --git a/src/option_processing.cc b/src/option_processing.cc index 8b27f197..240f2bac 100644 --- a/src/option_processing.cc +++ b/src/option_processing.cc @@ -54,6 +54,8 @@ #include "StringFormat.h" #include "OptionHandlerException.h" #include "DownloadResult.h" +#include "SimpleRandomizer.h" +#include "bittorrent_helper.h" namespace aria2 { @@ -202,6 +204,10 @@ void option_processing(Option& op, std::deque<std::string>& uris, } } #endif // HAVE_DAEMON +#ifdef ENABLE_BITTORRENT + bittorrent::generateStaticPeerId(op.get(PREF_PEER_ID_PREFIX), + SimpleRandomizer::getInstance()); +#endif // ENABLE_BITTORRENT } } // namespace aria2 diff --git a/test/BitfieldManTest.cc b/test/BitfieldManTest.cc index 2b7f2f9d..f3900f5c 100644 --- a/test/BitfieldManTest.cc +++ b/test/BitfieldManTest.cc @@ -455,77 +455,105 @@ void BitfieldManTest::testGetMissingIndex() { } +// TODO1.5 add test using ignoreBitfield void BitfieldManTest::testGetSparceMissingUnusedIndex() { BitfieldMan bitfield(1024*1024, 10*1024*1024); - + const size_t length = 2; + unsigned char ignoreBitfield[length]; + memset(ignoreBitfield, 0, sizeof(ignoreBitfield)); size_t index; - CPPUNIT_ASSERT(bitfield.getSparseMissingUnusedIndex(index)); + CPPUNIT_ASSERT(bitfield.getSparseMissingUnusedIndex(index, + ignoreBitfield, length)); CPPUNIT_ASSERT_EQUAL((size_t)0, index); bitfield.setUseBit(0); - CPPUNIT_ASSERT(bitfield.getSparseMissingUnusedIndex(index)); + CPPUNIT_ASSERT(bitfield.getSparseMissingUnusedIndex(index, + ignoreBitfield, length)); CPPUNIT_ASSERT_EQUAL((size_t)5, index); bitfield.setUseBit(5); - CPPUNIT_ASSERT(bitfield.getSparseMissingUnusedIndex(index)); + CPPUNIT_ASSERT(bitfield.getSparseMissingUnusedIndex(index, + ignoreBitfield, length)); CPPUNIT_ASSERT_EQUAL((size_t)3, index); bitfield.setUseBit(3); - CPPUNIT_ASSERT(bitfield.getSparseMissingUnusedIndex(index)); + CPPUNIT_ASSERT(bitfield.getSparseMissingUnusedIndex(index, + ignoreBitfield, length)); CPPUNIT_ASSERT_EQUAL((size_t)8, index); bitfield.setUseBit(8); - CPPUNIT_ASSERT(bitfield.getSparseMissingUnusedIndex(index)); + CPPUNIT_ASSERT(bitfield.getSparseMissingUnusedIndex(index, + ignoreBitfield, length)); CPPUNIT_ASSERT_EQUAL((size_t)2, index); bitfield.setUseBit(2); - CPPUNIT_ASSERT(bitfield.getSparseMissingUnusedIndex(index)); + CPPUNIT_ASSERT(bitfield.getSparseMissingUnusedIndex(index, + ignoreBitfield, length)); CPPUNIT_ASSERT_EQUAL((size_t)7, index); bitfield.setUseBit(7); - CPPUNIT_ASSERT(bitfield.getSparseMissingUnusedIndex(index)); + CPPUNIT_ASSERT(bitfield.getSparseMissingUnusedIndex(index, + ignoreBitfield, length)); CPPUNIT_ASSERT_EQUAL((size_t)1, index); bitfield.setUseBit(1); - CPPUNIT_ASSERT(bitfield.getSparseMissingUnusedIndex(index)); + CPPUNIT_ASSERT(bitfield.getSparseMissingUnusedIndex(index, + ignoreBitfield, length)); CPPUNIT_ASSERT_EQUAL((size_t)4, index); bitfield.setUseBit(4); - CPPUNIT_ASSERT(bitfield.getSparseMissingUnusedIndex(index)); + CPPUNIT_ASSERT(bitfield.getSparseMissingUnusedIndex(index, + ignoreBitfield, length)); CPPUNIT_ASSERT_EQUAL((size_t)6, index); bitfield.setUseBit(6); - CPPUNIT_ASSERT(bitfield.getSparseMissingUnusedIndex(index)); + CPPUNIT_ASSERT(bitfield.getSparseMissingUnusedIndex(index, + ignoreBitfield, length)); CPPUNIT_ASSERT_EQUAL((size_t)9, index); bitfield.setUseBit(9); - CPPUNIT_ASSERT(!bitfield.getSparseMissingUnusedIndex(index)); + CPPUNIT_ASSERT(!bitfield.getSparseMissingUnusedIndex(index, + ignoreBitfield, length)); } void BitfieldManTest::testGetSparceMissingUnusedIndex_setBit() { BitfieldMan bitfield(1024*1024, 10*1024*1024); + const size_t length = 2; + unsigned char ignoreBitfield[length]; + memset(ignoreBitfield, 0, sizeof(ignoreBitfield)); size_t index; - CPPUNIT_ASSERT(bitfield.getSparseMissingUnusedIndex(index)); + CPPUNIT_ASSERT(bitfield.getSparseMissingUnusedIndex(index, + ignoreBitfield, length)); CPPUNIT_ASSERT_EQUAL((size_t)0, index); bitfield.setBit(0); - CPPUNIT_ASSERT(bitfield.getSparseMissingUnusedIndex(index)); + CPPUNIT_ASSERT(bitfield.getSparseMissingUnusedIndex(index, + ignoreBitfield, length)); CPPUNIT_ASSERT_EQUAL((size_t)1, index); bitfield.setBit(1); - CPPUNIT_ASSERT(bitfield.getSparseMissingUnusedIndex(index)); + CPPUNIT_ASSERT(bitfield.getSparseMissingUnusedIndex(index, + ignoreBitfield, length)); CPPUNIT_ASSERT_EQUAL((size_t)2, index); bitfield.setBit(2); - CPPUNIT_ASSERT(bitfield.getSparseMissingUnusedIndex(index)); + CPPUNIT_ASSERT(bitfield.getSparseMissingUnusedIndex(index, + ignoreBitfield, length)); CPPUNIT_ASSERT_EQUAL((size_t)3, index); bitfield.setBit(3); - CPPUNIT_ASSERT(bitfield.getSparseMissingUnusedIndex(index)); + CPPUNIT_ASSERT(bitfield.getSparseMissingUnusedIndex(index, + ignoreBitfield, length)); CPPUNIT_ASSERT_EQUAL((size_t)4, index); bitfield.setBit(4); - CPPUNIT_ASSERT(bitfield.getSparseMissingUnusedIndex(index)); + CPPUNIT_ASSERT(bitfield.getSparseMissingUnusedIndex(index, + ignoreBitfield, length)); CPPUNIT_ASSERT_EQUAL((size_t)5, index); bitfield.setBit(5); - CPPUNIT_ASSERT(bitfield.getSparseMissingUnusedIndex(index)); + CPPUNIT_ASSERT(bitfield.getSparseMissingUnusedIndex(index, + ignoreBitfield, length)); CPPUNIT_ASSERT_EQUAL((size_t)6, index); bitfield.setBit(6); - CPPUNIT_ASSERT(bitfield.getSparseMissingUnusedIndex(index)); + CPPUNIT_ASSERT(bitfield.getSparseMissingUnusedIndex(index, + ignoreBitfield, length)); CPPUNIT_ASSERT_EQUAL((size_t)7, index); bitfield.setBit(7); - CPPUNIT_ASSERT(bitfield.getSparseMissingUnusedIndex(index)); + CPPUNIT_ASSERT(bitfield.getSparseMissingUnusedIndex(index, + ignoreBitfield, length)); CPPUNIT_ASSERT_EQUAL((size_t)8, index); bitfield.setBit(8); - CPPUNIT_ASSERT(bitfield.getSparseMissingUnusedIndex(index)); + CPPUNIT_ASSERT(bitfield.getSparseMissingUnusedIndex(index, + ignoreBitfield, length)); CPPUNIT_ASSERT_EQUAL((size_t)9, index); bitfield.setBit(9); - CPPUNIT_ASSERT(!bitfield.getSparseMissingUnusedIndex(index)); + CPPUNIT_ASSERT(!bitfield.getSparseMissingUnusedIndex(index, + ignoreBitfield, length)); } void BitfieldManTest::testIsBitSetOffsetRange() diff --git a/test/BittorrentHelperTest.cc b/test/BittorrentHelperTest.cc new file mode 100644 index 00000000..321e371e --- /dev/null +++ b/test/BittorrentHelperTest.cc @@ -0,0 +1,632 @@ +#include "bittorrent_helper.h" + +#include <cstring> +#include <iostream> + +#include <cppunit/extensions/HelperMacros.h> + +#include "DownloadContext.h" +#include "Util.h" +#include "RecoverableException.h" +#include "AnnounceTier.h" +#include "FixedNumberRandomizer.h" +#include "FileEntry.h" +#include "array_fun.h" +#include "messageDigest.h" + +namespace aria2 { + +namespace bittorrent { + +class BittorrentHelperTest:public CppUnit::TestFixture { + + CPPUNIT_TEST_SUITE(BittorrentHelperTest); + CPPUNIT_TEST(testGetInfoHash); + CPPUNIT_TEST(testGetPieceHash); + CPPUNIT_TEST(testGetFileEntries); + CPPUNIT_TEST(testGetTotalLength); + CPPUNIT_TEST(testGetFileEntriesSingle); + CPPUNIT_TEST(testGetTotalLengthSingle); + CPPUNIT_TEST(testGetFileModeMulti); + CPPUNIT_TEST(testGetFileModeSingle); + CPPUNIT_TEST(testGetNameMulti); + CPPUNIT_TEST(testGetNameSingle); + CPPUNIT_TEST(testOverrideName); + CPPUNIT_TEST(testGetAnnounceTier); + CPPUNIT_TEST(testGetAnnounceTierAnnounceList); + CPPUNIT_TEST(testGetPieceLength); + CPPUNIT_TEST(testGetInfoHashAsString); + CPPUNIT_TEST(testGetPeerId); + CPPUNIT_TEST(testComputeFastSet); + CPPUNIT_TEST(testGetFileEntries_multiFileUrlList); + CPPUNIT_TEST(testGetFileEntries_singleFileUrlList); + CPPUNIT_TEST(testGetFileEntries_singleFileUrlListEndsWithSlash); + CPPUNIT_TEST(testLoadFromMemory); + CPPUNIT_TEST(testLoadFromMemory_somethingMissing); + CPPUNIT_TEST(testLoadFromMemory_overrideName); + CPPUNIT_TEST(testLoadFromMemory_joinPathMulti); + CPPUNIT_TEST(testLoadFromMemory_joinPathSingle); + CPPUNIT_TEST(testGetNodes); + CPPUNIT_TEST(testGetBasePath); + CPPUNIT_TEST(testSetFileFilter_single); + CPPUNIT_TEST(testSetFileFilter_multi); + CPPUNIT_TEST(testUTF8Torrent); + CPPUNIT_TEST_SUITE_END(); +public: + void setUp() { + } + + void testGetInfoHash(); + void testGetPieceHash(); + void testGetFileEntries(); + void testGetTotalLength(); + void testGetFileEntriesSingle(); + void testGetTotalLengthSingle(); + void testGetFileModeMulti(); + void testGetFileModeSingle(); + void testGetNameMulti(); + void testGetNameSingle(); + void testOverrideName(); + void testGetAnnounceTier(); + void testGetAnnounceTierAnnounceList(); + void testGetPieceLength(); + void testGetInfoHashAsString(); + void testGetPeerId(); + void testComputeFastSet(); + void testGetFileEntries_multiFileUrlList(); + void testGetFileEntries_singleFileUrlList(); + void testGetFileEntries_singleFileUrlListEndsWithSlash(); + void testLoadFromMemory(); + void testLoadFromMemory_somethingMissing(); + void testLoadFromMemory_overrideName(); + void testLoadFromMemory_joinPathMulti(); + void testLoadFromMemory_joinPathSingle(); + void testGetNodes(); + void testGetBasePath(); + void testSetFileFilter_single(); + void testSetFileFilter_multi(); + void testUTF8Torrent(); +}; + + +CPPUNIT_TEST_SUITE_REGISTRATION(BittorrentHelperTest); + +static const BDE& getAnnounceList(const SharedHandle<DownloadContext>& dctx) +{ + return dctx->getAttribute(BITTORRENT)[ANNOUNCE_LIST]; +} + +static const std::string& getMode(const SharedHandle<DownloadContext>& dctx) +{ + return dctx->getAttribute(BITTORRENT)[MODE].s(); +} + +static const std::string& getName(const SharedHandle<DownloadContext>& dctx) +{ + return dctx->getAttribute(BITTORRENT)[NAME].s(); +} + +static const BDE& getNodes(const SharedHandle<DownloadContext>& dctx) +{ + return dctx->getAttribute(BITTORRENT)[NODES]; +} + +void BittorrentHelperTest::testGetInfoHash() { + SharedHandle<DownloadContext> dctx(new DownloadContext()); + load("test.torrent", dctx); + + std::string correctHash = "248d0a1cd08284299de78d5c1ed359bb46717d8c"; + + CPPUNIT_ASSERT_EQUAL(correctHash, bittorrent::getInfoHashString(dctx)); +} + +void BittorrentHelperTest::testGetPieceHash() { + SharedHandle<DownloadContext> dctx(new DownloadContext()); + load("test.torrent", dctx); + + CPPUNIT_ASSERT_EQUAL(Util::toHex("AAAAAAAAAAAAAAAAAAAA", 20), + dctx->getPieceHash(0)); + CPPUNIT_ASSERT_EQUAL(Util::toHex("BBBBBBBBBBBBBBBBBBBB", 20), + dctx->getPieceHash(1)); + CPPUNIT_ASSERT_EQUAL(Util::toHex("CCCCCCCCCCCCCCCCCCCC", 20), + dctx->getPieceHash(2)); + CPPUNIT_ASSERT_EQUAL(std::string(""), + dctx->getPieceHash(3)); + + CPPUNIT_ASSERT_EQUAL(MessageDigestContext::SHA1, dctx->getPieceHashAlgo()); +} + +void BittorrentHelperTest::testGetFileEntries() { + SharedHandle<DownloadContext> dctx(new DownloadContext()); + load("test.torrent", dctx); + // This is multi-file torrent. + std::vector<SharedHandle<FileEntry> > fileEntries = dctx->getFileEntries(); + // There are 2 file entries. + CPPUNIT_ASSERT_EQUAL((size_t)2, fileEntries.size()); + std::vector<SharedHandle<FileEntry> >::iterator itr = fileEntries.begin(); + + SharedHandle<FileEntry> fileEntry1 = *itr; + CPPUNIT_ASSERT_EQUAL(std::string("./aria2-test/aria2/src/aria2c"), + fileEntry1->getPath()); + itr++; + SharedHandle<FileEntry> fileEntry2 = *itr; + CPPUNIT_ASSERT_EQUAL(std::string("./aria2-test/aria2-0.2.2.tar.bz2"), + fileEntry2->getPath()); +} + +void BittorrentHelperTest::testGetFileEntriesSingle() { + SharedHandle<DownloadContext> dctx(new DownloadContext()); + load("single.torrent", dctx); + // This is multi-file torrent. + std::vector<SharedHandle<FileEntry> > fileEntries = dctx->getFileEntries(); + // There is 1 file entry. + CPPUNIT_ASSERT_EQUAL((size_t)1, fileEntries.size()); + std::vector<SharedHandle<FileEntry> >::iterator itr = fileEntries.begin(); + + SharedHandle<FileEntry> fileEntry1 = *itr; + CPPUNIT_ASSERT_EQUAL(std::string("./aria2-0.8.2.tar.bz2"), + fileEntry1->getPath()); +} + +void BittorrentHelperTest::testGetTotalLength() { + SharedHandle<DownloadContext> dctx(new DownloadContext()); + load("test.torrent", dctx); + + CPPUNIT_ASSERT_EQUAL((uint64_t)384ULL, dctx->getTotalLength()); +} + +void BittorrentHelperTest::testGetTotalLengthSingle() { + SharedHandle<DownloadContext> dctx(new DownloadContext()); + load("single.torrent", dctx); + + CPPUNIT_ASSERT_EQUAL((uint64_t)384ULL, dctx->getTotalLength()); +} + +void BittorrentHelperTest::testGetFileModeMulti() { + SharedHandle<DownloadContext> dctx(new DownloadContext()); + load("test.torrent", dctx); + + CPPUNIT_ASSERT_EQUAL(MULTI, getMode(dctx)); +} + +void BittorrentHelperTest::testGetFileModeSingle() { + SharedHandle<DownloadContext> dctx(new DownloadContext()); + load("single.torrent", dctx); + + CPPUNIT_ASSERT_EQUAL(SINGLE, getMode(dctx)); +} + +void BittorrentHelperTest::testGetNameMulti() { + SharedHandle<DownloadContext> dctx(new DownloadContext()); + load("test.torrent", dctx); + + CPPUNIT_ASSERT_EQUAL(std::string("aria2-test"), getName(dctx)); +} + +void BittorrentHelperTest::testGetNameSingle() { + SharedHandle<DownloadContext> dctx(new DownloadContext()); + load("single.torrent", dctx); + + CPPUNIT_ASSERT_EQUAL(std::string("./aria2-0.8.2.tar.bz2"), + dctx->getBasePath()); + CPPUNIT_ASSERT_EQUAL(std::string("aria2-0.8.2.tar.bz2"), getName(dctx)); +} + +void BittorrentHelperTest::testOverrideName() +{ + SharedHandle<DownloadContext> dctx(new DownloadContext()); + load("test.torrent", dctx, "aria2-override.name"); + CPPUNIT_ASSERT_EQUAL(std::string("./aria2-override.name"), + dctx->getBasePath()); + CPPUNIT_ASSERT_EQUAL(std::string("aria2-override.name"), getName(dctx)); +} + + +void BittorrentHelperTest::testGetAnnounceTier() { + SharedHandle<DownloadContext> dctx(new DownloadContext()); + load("single.torrent", dctx); + + const BDE& announceList = getAnnounceList(dctx); + + // There is 1 tier. + CPPUNIT_ASSERT_EQUAL((size_t)1, announceList.size()); + + const BDE& tier = announceList[0]; + CPPUNIT_ASSERT_EQUAL((size_t)1, tier.size()); + CPPUNIT_ASSERT_EQUAL(std::string("http://aria.rednoah.com/announce.php"), + Util::trim(tier[0].s())); + +} + +void BittorrentHelperTest::testGetAnnounceTierAnnounceList() { + SharedHandle<DownloadContext> dctx(new DownloadContext()); + load("test.torrent", dctx); + + const BDE& announceList = getAnnounceList(dctx); + + // There are 3 tiers. + CPPUNIT_ASSERT_EQUAL((size_t)3, announceList.size()); + + const BDE& tier1 = announceList[0]; + CPPUNIT_ASSERT_EQUAL((size_t)1, tier1.size()); + CPPUNIT_ASSERT_EQUAL(std::string("http://tracker1"), + Util::trim(tier1[0].s())); + + const BDE& tier2 = announceList[1]; + CPPUNIT_ASSERT_EQUAL((size_t)1, tier2.size()); + CPPUNIT_ASSERT_EQUAL(std::string("http://tracker2"), tier2[0].s()); + + const BDE& tier3 = announceList[2]; + CPPUNIT_ASSERT_EQUAL((size_t)1, tier3.size()); + CPPUNIT_ASSERT_EQUAL(std::string("http://tracker3"), tier3[0].s()); +} + +void BittorrentHelperTest::testGetPieceLength() { + SharedHandle<DownloadContext> dctx(new DownloadContext()); + load("test.torrent", dctx); + + CPPUNIT_ASSERT_EQUAL((size_t)128, dctx->getPieceLength()); +} + +void BittorrentHelperTest::testGetInfoHashAsString() { + SharedHandle<DownloadContext> dctx(new DownloadContext()); + load("test.torrent", dctx); + + CPPUNIT_ASSERT_EQUAL(std::string("248d0a1cd08284299de78d5c1ed359bb46717d8c"), + getInfoHashString(dctx)); +} + +void BittorrentHelperTest::testGetPeerId() { + SharedHandle<DownloadContext> dctx(new DownloadContext()); + CPPUNIT_ASSERT_EQUAL + (std::string("-aria-AAAAAAAAAAAAAA"), + generatePeerId("-aria-", + SharedHandle<Randomizer>(new FixedNumberRandomizer()))); +} + +void BittorrentHelperTest::testComputeFastSet() +{ + std::string ipaddr = "192.168.0.1"; + unsigned char infoHash[20]; + memset(infoHash, 0, sizeof(infoHash)); + infoHash[0] = 0xff; + int fastSetSize = 10; + size_t numPieces = 1000; + { + std::vector<size_t> fastSet; + computeFastSet(fastSet, ipaddr, numPieces, infoHash, fastSetSize); + size_t ans[] = { 686, 459, 278, 200, 404, 834, 64, 203, 760, 950 }; + std::vector<size_t> ansSet(&ans[0], &ans[arrayLength(ans)]); + CPPUNIT_ASSERT(std::equal(fastSet.begin(), fastSet.end(), ansSet.begin())); + } + ipaddr = "10.0.0.1"; + { + std::vector<size_t> fastSet; + computeFastSet(fastSet, ipaddr, numPieces, infoHash, fastSetSize); + size_t ans[] = { 568, 188, 466, 452, 550, 662, 109, 226, 398, 11 }; + std::vector<size_t> ansSet(&ans[0], &ans[arrayLength(ans)]); + CPPUNIT_ASSERT(std::equal(fastSet.begin(), fastSet.end(), ansSet.begin())); + } + // See when pieces < fastSetSize + numPieces = 9; + { + std::vector<size_t> fastSet; + computeFastSet(fastSet, ipaddr, numPieces, infoHash, fastSetSize); + size_t ans[] = { 8, 6, 7, 5, 1, 4, 0, 2, 3 }; + std::vector<size_t> ansSet(&ans[0], &ans[arrayLength(ans)]); + CPPUNIT_ASSERT(std::equal(fastSet.begin(), fastSet.end(), ansSet.begin())); + } +} + +void BittorrentHelperTest::testGetFileEntries_multiFileUrlList() { + SharedHandle<DownloadContext> dctx(new DownloadContext()); + load("url-list-multiFile.torrent", dctx); + // This is multi-file torrent. + const std::vector<SharedHandle<FileEntry> >& fileEntries = + dctx->getFileEntries(); + // There are 2 file entries. + CPPUNIT_ASSERT_EQUAL((size_t)2, fileEntries.size()); + std::vector<SharedHandle<FileEntry> >::const_iterator itr = + fileEntries.begin(); + + const SharedHandle<FileEntry>& fileEntry1 = *itr; + CPPUNIT_ASSERT_EQUAL(std::string("./aria2-test/aria2/src/aria2c"), + fileEntry1->getPath()); + const std::deque<std::string>& uris1 = fileEntry1->getRemainingUris(); + CPPUNIT_ASSERT_EQUAL((size_t)2, uris1.size()); + CPPUNIT_ASSERT_EQUAL(std::string("http://localhost/dist/aria2-test/aria2/src/aria2c"), + uris1[0]); + CPPUNIT_ASSERT_EQUAL(std::string("http://mirror/dist/aria2-test/aria2/src/aria2c"), + uris1[1]); + + ++itr; + const SharedHandle<FileEntry>& fileEntry2 = *itr; + CPPUNIT_ASSERT_EQUAL(std::string("./aria2-test/aria2-0.2.2.tar.bz2"), + fileEntry2->getPath()); + const std::deque<std::string>& uris2 = fileEntry2->getRemainingUris(); + CPPUNIT_ASSERT_EQUAL((size_t)2, uris2.size()); + CPPUNIT_ASSERT_EQUAL(std::string("http://localhost/dist/aria2-test/aria2-0.2.2.tar.bz2"), + uris2[0]); + CPPUNIT_ASSERT_EQUAL(std::string("http://mirror/dist/aria2-test/aria2-0.2.2.tar.bz2"), + uris2[1]); +} + +void BittorrentHelperTest::testGetFileEntries_singleFileUrlList() { + SharedHandle<DownloadContext> dctx(new DownloadContext()); + load("url-list-singleFile.torrent", dctx); + // This is single-file torrent. + const std::vector<SharedHandle<FileEntry> >& fileEntries = + dctx->getFileEntries(); + // There are 1 file entries. + CPPUNIT_ASSERT_EQUAL((size_t)1, fileEntries.size()); + + const SharedHandle<FileEntry>& fileEntry1 = fileEntries.front(); + CPPUNIT_ASSERT_EQUAL(std::string("./aria2.tar.bz2"), + fileEntry1->getPath()); + const std::deque<std::string>& uris1 = fileEntry1->getRemainingUris(); + CPPUNIT_ASSERT_EQUAL((size_t)1, uris1.size()); + CPPUNIT_ASSERT_EQUAL(std::string("http://localhost/dist/aria2.tar.bz2"), + uris1[0]); +} + +void BittorrentHelperTest::testGetFileEntries_singleFileUrlListEndsWithSlash() { + SharedHandle<DownloadContext> dctx(new DownloadContext()); + load("url-list-singleFileEndsWithSlash.torrent", dctx); + // This is single-file torrent. + const std::vector<SharedHandle<FileEntry> >& fileEntries = + dctx->getFileEntries(); + // There are 1 file entries. + CPPUNIT_ASSERT_EQUAL((size_t)1, fileEntries.size()); + + const SharedHandle<FileEntry>& fileEntry1 = fileEntries.front(); + CPPUNIT_ASSERT_EQUAL(std::string("./aria2.tar.bz2"), + fileEntry1->getPath()); + const std::deque<std::string>& uris1 = fileEntry1->getRemainingUris(); + CPPUNIT_ASSERT_EQUAL((size_t)1, uris1.size()); + CPPUNIT_ASSERT_EQUAL(std::string("http://localhost/dist/aria2.tar.bz2"), + uris1[0]); +} + +void BittorrentHelperTest::testLoadFromMemory() +{ + std::string memory = "d8:announce36:http://aria.rednoah.com/announce.php13:announce-listll16:http://tracker1 el15:http://tracker2el15:http://tracker3ee7:comment17:REDNOAH.COM RULES13:creation datei1123456789e4:infod5:filesld6:lengthi284e4:pathl5:aria23:src6:aria2ceed6:lengthi100e4:pathl19:aria2-0.2.2.tar.bz2eee4:name10:aria2-test12:piece lengthi128e6:pieces60:AAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBBBBBCCCCCCCCCCCCCCCCCCCCee"; + + SharedHandle<DownloadContext> dctx(new DownloadContext()); + loadFromMemory(memory, dctx, "default"); + + std::string correctHash = "248d0a1cd08284299de78d5c1ed359bb46717d8c"; + + CPPUNIT_ASSERT_EQUAL(correctHash, getInfoHashString(dctx)); +} + +void BittorrentHelperTest::testLoadFromMemory_somethingMissing() +{ + // pieces missing + try { + std::string memory = "d8:announce36:http://aria.rednoah.com/announce.php4:infod4:name13:aria2.tar.bz26:lengthi262144eee"; + SharedHandle<DownloadContext> dctx(new DownloadContext()); + loadFromMemory(memory, dctx, "default"); + CPPUNIT_FAIL("exception must be thrown."); + } catch(Exception& e) { + // OK + } +} + +void BittorrentHelperTest::testLoadFromMemory_overrideName() +{ + std::string memory = "d8:announce36:http://aria.rednoah.com/announce.php13:announce-listll16:http://tracker1 el15:http://tracker2el15:http://tracker3ee7:comment17:REDNOAH.COM RULES13:creation datei1123456789e4:infod5:filesld6:lengthi284e4:pathl5:aria23:src6:aria2ceed6:lengthi100e4:pathl19:aria2-0.2.2.tar.bz2eee4:name10:aria2-test12:piece lengthi128e6:pieces60:AAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBBBBBCCCCCCCCCCCCCCCCCCCCee"; + + SharedHandle<DownloadContext> dctx(new DownloadContext()); + loadFromMemory(memory, dctx, "default", "aria2-override.name"); + + CPPUNIT_ASSERT_EQUAL(std::string("aria2-override.name"), getName(dctx)); +} + +void BittorrentHelperTest::testLoadFromMemory_joinPathMulti() +{ + std::string memory = + "d8:announce27:http://example.com/announce4:infod5:filesld6:lengthi262144e4:pathl7:../dir14:dir28:file.imgeee4:name14:../name1/name212:piece lengthi262144e6:pieces20:00000000000000000000ee"; + + SharedHandle<DownloadContext> dctx(new DownloadContext()); + dctx->setDir("/tmp"); + loadFromMemory(memory, dctx, "default"); + + // remove ".." element + CPPUNIT_ASSERT_EQUAL(std::string("../name1/name2"), getName(dctx)); + CPPUNIT_ASSERT_EQUAL(std::string("/tmp/name1/dir1/dir2/file.img"), + dctx->getFirstFileEntry()->getPath()); +} + +void BittorrentHelperTest::testLoadFromMemory_joinPathSingle() +{ + std::string memory = + "d8:announce27:http://example.com/announce4:infod4:name14:../name1/name26:lengthi262144e12:piece lengthi262144e6:pieces20:00000000000000000000ee"; + + SharedHandle<DownloadContext> dctx(new DownloadContext()); + dctx->setDir("/tmp"); + loadFromMemory(memory, dctx, "default"); + + CPPUNIT_ASSERT_EQUAL(std::string("../name1/name2"), getName(dctx)); + CPPUNIT_ASSERT_EQUAL(std::string("/tmp/name1/name2"), + dctx->getFirstFileEntry()->getPath()); +} + +void BittorrentHelperTest::testGetNodes() +{ + { + std::string memory = + "d5:nodesl" + "l11:192.168.0.1i6881ee" + "l11:192.168.0.2i6882ee" + "e4:infod4:name13:aria2.tar.bz26:lengthi262144e" + "12:piece lengthi262144e" + "6:pieces20:AAAAAAAAAAAAAAAAAAAA" + "ee"; + SharedHandle<DownloadContext> dctx(new DownloadContext()); + loadFromMemory(memory, dctx, "default"); + + const BDE& nodes = getNodes(dctx); + CPPUNIT_ASSERT_EQUAL((size_t)2, nodes.size()); + CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.1"), nodes[0][HOSTNAME].s()); + CPPUNIT_ASSERT_EQUAL((uint16_t)6881, (uint16_t)nodes[0][PORT].i()); + CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.2"), nodes[1][HOSTNAME].s()); + CPPUNIT_ASSERT_EQUAL((uint16_t)6882, (uint16_t)nodes[1][PORT].i()); + } + { + // empty hostname + std::string memory = + "d5:nodesl" + "l1: i6881ee" + "l11:192.168.0.2i6882ee" + "e4:infod4:name13:aria2.tar.bz26:lengthi262144e" + "12:piece lengthi262144e" + "6:pieces20:AAAAAAAAAAAAAAAAAAAA" + "ee"; + SharedHandle<DownloadContext> dctx(new DownloadContext()); + loadFromMemory(memory, dctx, "default"); + + const BDE& nodes = getNodes(dctx); + CPPUNIT_ASSERT_EQUAL((size_t)1, nodes.size()); + CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.2"), nodes[0][HOSTNAME].s()); + CPPUNIT_ASSERT_EQUAL((uint16_t)6882, (uint16_t)nodes[0][PORT].i()); + } + { + // bad port + std::string memory = + "d5:nodesl" + "l11:192.168.0.11:xe" + "l11:192.168.0.2i6882ee" + "e4:infod4:name13:aria2.tar.bz26:lengthi262144e" + "12:piece lengthi262144e" + "6:pieces20:AAAAAAAAAAAAAAAAAAAA" + "ee"; + SharedHandle<DownloadContext> dctx(new DownloadContext()); + loadFromMemory(memory, dctx, "default"); + + const BDE& nodes = getNodes(dctx); + CPPUNIT_ASSERT_EQUAL((size_t)1, nodes.size()); + CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.2"), nodes[0][HOSTNAME].s()); + CPPUNIT_ASSERT_EQUAL((uint16_t)6882, (uint16_t)nodes[0][PORT].i()); + } + { + // port missing + std::string memory = + "d5:nodesl" + "l11:192.168.0.1e" + "l11:192.168.0.2i6882ee" + "e4:infod4:name13:aria2.tar.bz26:lengthi262144e" + "12:piece lengthi262144e" + "6:pieces20:AAAAAAAAAAAAAAAAAAAA" + "ee"; + SharedHandle<DownloadContext> dctx(new DownloadContext()); + loadFromMemory(memory, dctx, "default"); + + const BDE& nodes = getNodes(dctx); + CPPUNIT_ASSERT_EQUAL((size_t)1, nodes.size()); + CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.2"), nodes[0][HOSTNAME].s()); + CPPUNIT_ASSERT_EQUAL((uint16_t)6882, (uint16_t)nodes[0][PORT].i()); + } + { + // nodes is not a list + std::string memory = + "d5:nodes" + "l11:192.168.0.1e" + "4:infod4:name13:aria2.tar.bz26:lengthi262144e" + "12:piece lengthi262144e" + "6:pieces20:AAAAAAAAAAAAAAAAAAAA" + "ee"; + SharedHandle<DownloadContext> dctx(new DownloadContext()); + loadFromMemory(memory, dctx, "default"); + + CPPUNIT_ASSERT_EQUAL((size_t)0, getNodes(dctx).size()); + } + { + // the element of node is not Data + std::string memory = + "d5:nodesl" + "ll11:192.168.0.1i6881eee" + "l11:192.168.0.2i6882ee" + "e4:infod4:name13:aria2.tar.bz26:lengthi262144e" + "12:piece lengthi262144e" + "6:pieces20:AAAAAAAAAAAAAAAAAAAA" + "ee"; + SharedHandle<DownloadContext> dctx(new DownloadContext()); + loadFromMemory(memory, dctx, "default"); + + const BDE& nodes = getNodes(dctx); + CPPUNIT_ASSERT_EQUAL((size_t)1, nodes.size()); + CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.2"), nodes[0][HOSTNAME].s()); + CPPUNIT_ASSERT_EQUAL((uint16_t)6882, (uint16_t)nodes[0][PORT].i()); + } +} + +void BittorrentHelperTest::testGetBasePath() +{ + SharedHandle<DownloadContext> singleCtx(new DownloadContext()); + load("single.torrent", singleCtx); + singleCtx->setFilePathWithIndex(1, "new-path"); + CPPUNIT_ASSERT_EQUAL(std::string("new-path"), singleCtx->getBasePath()); + + SharedHandle<DownloadContext> multiCtx(new DownloadContext()); + multiCtx->setDir("downloads"); + load("test.torrent", multiCtx); + CPPUNIT_ASSERT_EQUAL(std::string("downloads/aria2-test"), + multiCtx->getBasePath()); +} + +void BittorrentHelperTest::testSetFileFilter_single() +{ + SharedHandle<DownloadContext> dctx(new DownloadContext()); + load("single.torrent", dctx); + + CPPUNIT_ASSERT(dctx->getFirstFileEntry()->isRequested()); + + dctx->setFileFilter(Util::parseIntRange("")); + CPPUNIT_ASSERT(dctx->getFirstFileEntry()->isRequested()); + + dctx->setFileFilter(Util::parseIntRange("1")); + CPPUNIT_ASSERT(dctx->getFirstFileEntry()->isRequested()); + + // For single file torrent, file is always selected whatever range + // is passed. + dctx->setFileFilter(Util::parseIntRange("2")); + CPPUNIT_ASSERT(dctx->getFirstFileEntry()->isRequested()); +} + +void BittorrentHelperTest::testSetFileFilter_multi() +{ + SharedHandle<DownloadContext> dctx(new DownloadContext()); + load("test.torrent", dctx); + + CPPUNIT_ASSERT(dctx->getFileEntries()[0]->isRequested()); + CPPUNIT_ASSERT(dctx->getFileEntries()[1]->isRequested()); + + dctx->setFileFilter(Util::parseIntRange("")); + CPPUNIT_ASSERT(dctx->getFileEntries()[0]->isRequested()); + CPPUNIT_ASSERT(dctx->getFileEntries()[1]->isRequested()); + + dctx->setFileFilter(Util::parseIntRange("2")); + CPPUNIT_ASSERT(!dctx->getFileEntries()[0]->isRequested()); + CPPUNIT_ASSERT(dctx->getFileEntries()[1]->isRequested()); + + dctx->setFileFilter(Util::parseIntRange("3")); + CPPUNIT_ASSERT(!dctx->getFileEntries()[0]->isRequested()); + CPPUNIT_ASSERT(!dctx->getFileEntries()[1]->isRequested()); + + dctx->setFileFilter(Util::parseIntRange("1,2")); + CPPUNIT_ASSERT(dctx->getFileEntries()[0]->isRequested()); + CPPUNIT_ASSERT(dctx->getFileEntries()[1]->isRequested()); +} + +void BittorrentHelperTest::testUTF8Torrent() +{ + SharedHandle<DownloadContext> dctx(new DownloadContext()); + load("utf8.torrent", dctx); + CPPUNIT_ASSERT_EQUAL(std::string("name in utf-8"), getName(dctx)); + CPPUNIT_ASSERT_EQUAL(std::string("./name in utf-8/path in utf-8"), + dctx->getFirstFileEntry()->getPath()); +} + +} // namespace bittorrent + +} // namespace aria2 diff --git a/test/BtCancelMessageTest.cc b/test/BtCancelMessageTest.cc index efc8a09a..70654ea1 100644 --- a/test/BtCancelMessageTest.cc +++ b/test/BtCancelMessageTest.cc @@ -6,7 +6,6 @@ #include "PeerMessageUtil.h" #include "MockBtMessageDispatcher.h" -#include "MockBtContext.h" #include "Peer.h" #include "FileEntry.h" #include "Piece.h" @@ -22,12 +21,9 @@ class BtCancelMessageTest:public CppUnit::TestFixture { CPPUNIT_TEST_SUITE_END(); private: SharedHandle<Peer> peer; - SharedHandle<MockBtContext> btContext; public: void setUp() { peer.reset(new Peer("host", 6969)); - btContext.reset(new MockBtContext()); - btContext->setInfoHash((const unsigned char*)"12345678901234567890"); } void testCreate(); @@ -103,7 +99,6 @@ void BtCancelMessageTest::testDoReceivedAction() { msg.setIndex(1); msg.setBegin(2*16*1024); msg.setLength(16*1024); - msg.setBtContext(btContext); msg.setPeer(peer); SharedHandle<MockBtMessageDispatcher2> dispatcher (new MockBtMessageDispatcher2()); diff --git a/test/BtChokeMessageTest.cc b/test/BtChokeMessageTest.cc index b16e52a2..fae43412 100644 --- a/test/BtChokeMessageTest.cc +++ b/test/BtChokeMessageTest.cc @@ -7,7 +7,6 @@ #include "PeerMessageUtil.h" #include "MockBtMessageDispatcher.h" #include "MockBtRequestFactory.h" -#include "MockBtContext.h" #include "Peer.h" #include "FileEntry.h" @@ -25,16 +24,13 @@ class BtChokeMessageTest:public CppUnit::TestFixture { private: public: - BtChokeMessageTest():peer(0), btContext(0) {} + BtChokeMessageTest():peer(0) {} SharedHandle<Peer> peer; - SharedHandle<MockBtContext> btContext; void setUp() { peer.reset(new Peer("host", 6969)); peer->allocateSessionResource(1024, 1024*1024); - btContext.reset(new MockBtContext()); - btContext->setInfoHash((const unsigned char*)"12345678901234567890"); } void testCreate(); @@ -108,7 +104,6 @@ void BtChokeMessageTest::testGetMessage() { void BtChokeMessageTest::testDoReceivedAction() { BtChokeMessage msg; msg.setPeer(peer); - msg.setBtContext(btContext); SharedHandle<MockBtMessageDispatcher2> dispatcher(new MockBtMessageDispatcher2()); msg.setBtMessageDispatcher(dispatcher); @@ -124,7 +119,6 @@ void BtChokeMessageTest::testDoReceivedAction() { void BtChokeMessageTest::testOnSendComplete() { BtChokeMessage msg; msg.setPeer(peer); - msg.setBtContext(btContext); SharedHandle<MockBtMessageDispatcher2> dispatcher(new MockBtMessageDispatcher2()); msg.setBtMessageDispatcher(dispatcher); diff --git a/test/BtDependencyTest.cc b/test/BtDependencyTest.cc index 5d37f30f..104921b2 100644 --- a/test/BtDependencyTest.cc +++ b/test/BtDependencyTest.cc @@ -4,15 +4,15 @@ #include <cppunit/extensions/HelperMacros.h> -#include "SingleFileDownloadContext.h" #include "DefaultPieceStorage.h" -#include "BtContext.h" +#include "DownloadContext.h" #include "RequestGroup.h" #include "Option.h" #include "Exception.h" #include "SegmentMan.h" #include "FileEntry.h" #include "PieceSelector.h" +#include "bittorrent_helper.h" namespace aria2 { @@ -29,8 +29,8 @@ class BtDependencyTest:public CppUnit::TestFixture { { SharedHandle<RequestGroup> dependant (new RequestGroup(option, std::deque<std::string>())); - SharedHandle<SingleFileDownloadContext> dctx - (new SingleFileDownloadContext(0, 0, "/tmp/outfile.path")); + SharedHandle<DownloadContext> dctx + (new DownloadContext(0, 0, "/tmp/outfile.path")); dctx->setDir("/tmp"); dependant->setDownloadContext(dctx); return dependant; @@ -44,8 +44,8 @@ class BtDependencyTest:public CppUnit::TestFixture { { SharedHandle<RequestGroup> dependee (new RequestGroup(option, std::deque<std::string>())); - SharedHandle<SingleFileDownloadContext> dctx - (new SingleFileDownloadContext(1024*1024, length, torrentFile)); + SharedHandle<DownloadContext> dctx + (new DownloadContext(1024*1024, length, torrentFile)); dctx->setDir("."); dependee->setDownloadContext(dctx); DefaultPieceStorageHandle ps(new DefaultPieceStorage(dctx, option.get())); @@ -73,7 +73,7 @@ CPPUNIT_TEST_SUITE_REGISTRATION( BtDependencyTest ); void BtDependencyTest::testResolve() { - std::string filename = "test.torrent"; + std::string filename = "single.torrent"; SharedHandle<RequestGroup> dependant = createDependant(_option); SharedHandle<RequestGroup> dependee = createDependee(_option, filename, File(filename).size()); @@ -81,35 +81,28 @@ void BtDependencyTest::testResolve() BtDependency dep(dependant, dependee); CPPUNIT_ASSERT(dep.resolve()); - - SharedHandle<BtContext> btContext - (dynamic_pointer_cast<BtContext>(dependant->getDownloadContext())); - CPPUNIT_ASSERT(!btContext.isNull()); + + CPPUNIT_ASSERT_EQUAL + (std::string("cd41c7fdddfd034a15a04d7ff881216e01c4ceaf"), + bittorrent::getInfoHashString(dependant->getDownloadContext())); CPPUNIT_ASSERT_EQUAL(std::string("/tmp/outfile.path"), - btContext->getActualBasePath()); + dependant->getFirstFilePath()); } void BtDependencyTest::testResolve_loadError() { - try { - SharedHandle<RequestGroup> dependant = createDependant(_option); - SharedHandle<RequestGroup> dependee = - createDependee(_option, "notExist", 40); - dependee->getPieceStorage()->markAllPiecesDone(); + SharedHandle<RequestGroup> dependant = createDependant(_option); + SharedHandle<RequestGroup> dependee = + createDependee(_option, "notExist", 40); + dependee->getPieceStorage()->markAllPiecesDone(); - BtDependency dep(dependant, dependee); - CPPUNIT_ASSERT(dep.resolve()); + BtDependency dep(dependant, dependee); + CPPUNIT_ASSERT(dep.resolve()); - SharedHandle<SingleFileDownloadContext> dctx - (dynamic_pointer_cast<SingleFileDownloadContext> - (dependant->getDownloadContext())); - CPPUNIT_ASSERT(!dctx.isNull()); - CPPUNIT_ASSERT_EQUAL(std::string("/tmp/outfile.path"), - dctx->getActualBasePath()); - } catch(Exception& e) { - std::cerr << e.stackTrace() << std::endl; - CPPUNIT_FAIL("an exception was thrown."); - } + CPPUNIT_ASSERT + (!dependant->getDownloadContext()->hasAttribute(bittorrent::BITTORRENT)); + CPPUNIT_ASSERT_EQUAL(std::string("/tmp/outfile.path"), + dependant->getFirstFilePath()); } void BtDependencyTest::testResolve_dependeeFailure() @@ -120,17 +113,15 @@ void BtDependencyTest::testResolve_dependeeFailure() BtDependency dep(dependant, dependee); CPPUNIT_ASSERT(dep.resolve()); - SharedHandle<SingleFileDownloadContext> dctx - (dynamic_pointer_cast<SingleFileDownloadContext> - (dependant->getDownloadContext())); - CPPUNIT_ASSERT(!dctx.isNull()); + CPPUNIT_ASSERT + (!dependant->getDownloadContext()->hasAttribute(bittorrent::BITTORRENT)); CPPUNIT_ASSERT_EQUAL(std::string("/tmp/outfile.path"), - dctx->getActualBasePath()); + dependant->getFirstFilePath()); } void BtDependencyTest::testResolve_dependeeInProgress() { - std::string filename = "test.torrent"; + std::string filename = "single.torrent"; SharedHandle<RequestGroup> dependant = createDependant(_option); SharedHandle<RequestGroup> dependee = createDependee(_option, filename, File(filename).size()); @@ -138,6 +129,8 @@ void BtDependencyTest::testResolve_dependeeInProgress() BtDependency dep(dependant, dependee); CPPUNIT_ASSERT(!dep.resolve()); + CPPUNIT_ASSERT_EQUAL(std::string("/tmp/outfile.path"), + dependant->getFirstFilePath()); } } // namespace aria2 diff --git a/test/BtPieceMessageTest.cc b/test/BtPieceMessageTest.cc index 03525148..05703542 100644 --- a/test/BtPieceMessageTest.cc +++ b/test/BtPieceMessageTest.cc @@ -5,7 +5,6 @@ #include <cppunit/extensions/HelperMacros.h> #include "PeerMessageUtil.h" -#include "MockBtContext.h" #include "MockBtMessage.h" #include "MockBtMessageFactory.h" #include "MockBtMessageDispatcher.h" @@ -15,6 +14,7 @@ #include "Peer.h" #include "Piece.h" #include "BtHandshakeMessage.h" +#include "DownloadContext.h" namespace aria2 { @@ -72,21 +72,18 @@ public: } }; - SharedHandle<MockBtContext> _btContext; + SharedHandle<DownloadContext> _dctx; SharedHandle<MockBtMessageDispatcher> btMessageDispatcher; SharedHandle<MockBtMessageFactory> _btMessageFactory; SharedHandle<Peer> peer; SharedHandle<BtPieceMessage> msg; void setUp() { - _btContext.reset(new MockBtContext()); - _btContext->setInfoHash((const unsigned char*)"12345678901234567890"); - _btContext->setPieceLength(16*1024); - _btContext->setTotalLength(256*1024); + _dctx.reset(new DownloadContext(16*1024, 256*1024, "/path/to/file")); peer.reset(new Peer("host", 6969)); - peer->allocateSessionResource(_btContext->getPieceLength(), - _btContext->getTotalLength()); + peer->allocateSessionResource(_dctx->getPieceLength(), + _dctx->getTotalLength()); btMessageDispatcher.reset(new MockBtMessageDispatcher()); _btMessageFactory.reset(new MockBtMessageFactory2()); @@ -95,7 +92,7 @@ public: msg->setIndex(1); msg->setBegin(1024); msg->setBlockLength(16*1024); - msg->setBtContext(_btContext); + msg->setDownloadContext(_dctx); msg->setPeer(peer); msg->setBtMessageDispatcher(btMessageDispatcher); msg->setBtMessageFactory(_btMessageFactory); diff --git a/test/BtPostDownloadHandlerTest.cc b/test/BtPostDownloadHandlerTest.cc index 4d67d5ab..17d83c59 100644 --- a/test/BtPostDownloadHandlerTest.cc +++ b/test/BtPostDownloadHandlerTest.cc @@ -1,10 +1,12 @@ #include "BtPostDownloadHandler.h" -#include "BtContext.h" + +#include <cppunit/extensions/HelperMacros.h> + +#include "DownloadContext.h" #include "RequestGroup.h" #include "Option.h" -#include "SingleFileDownloadContext.h" #include "FileEntry.h" -#include <cppunit/extensions/HelperMacros.h> +#include "bittorrent_helper.h" namespace aria2 { @@ -33,8 +35,7 @@ CPPUNIT_TEST_SUITE_REGISTRATION( BtPostDownloadHandlerTest ); void BtPostDownloadHandlerTest::testCanHandle_extension() { - SharedHandle<SingleFileDownloadContext> dctx - (new SingleFileDownloadContext(0, 0, "test.torrent")); + SharedHandle<DownloadContext> dctx(new DownloadContext(0, 0, "test.torrent")); RequestGroup rg(_option, std::deque<std::string>()); rg.setDownloadContext(dctx); @@ -42,15 +43,14 @@ void BtPostDownloadHandlerTest::testCanHandle_extension() CPPUNIT_ASSERT(handler.canHandle(&rg)); - dctx->setFilename("test.torrent2"); + dctx->getFirstFileEntry()->setPath("test.torrent2"); CPPUNIT_ASSERT(!handler.canHandle(&rg)); } void BtPostDownloadHandlerTest::testCanHandle_contentType() { - SharedHandle<SingleFileDownloadContext> dctx - (new SingleFileDownloadContext(0, 0, "test")); - dctx->setContentType("application/x-bittorrent"); + SharedHandle<DownloadContext> dctx(new DownloadContext(0, 0, "test")); + dctx->getFirstFileEntry()->setContentType("application/x-bittorrent"); RequestGroup rg(_option, std::deque<std::string>()); rg.setDownloadContext(dctx); @@ -58,14 +58,14 @@ void BtPostDownloadHandlerTest::testCanHandle_contentType() CPPUNIT_ASSERT(handler.canHandle(&rg)); - dctx->setContentType("application/octet-stream"); + dctx->getFirstFileEntry()->setContentType("application/octet-stream"); CPPUNIT_ASSERT(!handler.canHandle(&rg)); } void BtPostDownloadHandlerTest::testGetNextRequestGroups() { - SharedHandle<SingleFileDownloadContext> dctx - (new SingleFileDownloadContext(1024, 0, "test.torrent")); + SharedHandle<DownloadContext> dctx + (new DownloadContext(1024, 0, "test.torrent")); RequestGroup rg(_option, std::deque<std::string>()); rg.setDownloadContext(dctx); rg.initPieceStorage(); @@ -74,10 +74,9 @@ void BtPostDownloadHandlerTest::testGetNextRequestGroups() std::deque<SharedHandle<RequestGroup> > groups; handler.getNextRequestGroups(groups, &rg); CPPUNIT_ASSERT_EQUAL((size_t)1, groups.size()); - SharedHandle<BtContext> btctx - (dynamic_pointer_cast<BtContext>(groups.front()->getDownloadContext())); - CPPUNIT_ASSERT(!btctx.isNull()); - CPPUNIT_ASSERT_EQUAL(std::string("aria2-test"), btctx->getName()); + CPPUNIT_ASSERT_EQUAL + (std::string("248d0a1cd08284299de78d5c1ed359bb46717d8c"), + bittorrent::getInfoHashString(groups.front()->getDownloadContext())); } } // namespace aria2 diff --git a/test/BtRegistryTest.cc b/test/BtRegistryTest.cc index 61b63d40..cbb19f68 100644 --- a/test/BtRegistryTest.cc +++ b/test/BtRegistryTest.cc @@ -3,7 +3,7 @@ #include <cppunit/extensions/HelperMacros.h> #include "Exception.h" -#include "MockBtContext.h" +#include "DownloadContext.h" #include "MockPeerStorage.h" #include "MockPieceStorage.h" #include "MockBtAnnounce.h" @@ -16,16 +16,16 @@ namespace aria2 { class BtRegistryTest:public CppUnit::TestFixture { CPPUNIT_TEST_SUITE(BtRegistryTest); - CPPUNIT_TEST(testGetBtContext); - CPPUNIT_TEST(testGetAllBtContext); + CPPUNIT_TEST(testGetDownloadContext); + CPPUNIT_TEST(testGetAllDownloadContext); CPPUNIT_TEST(testRemove); CPPUNIT_TEST(testRemoveAll); CPPUNIT_TEST_SUITE_END(); private: public: - void testGetBtContext(); - void testGetAllBtContext(); + void testGetDownloadContext(); + void testGetAllDownloadContext(); void testRemove(); void testRemoveAll(); }; @@ -33,44 +33,43 @@ public: CPPUNIT_TEST_SUITE_REGISTRATION( BtRegistryTest ); -void BtRegistryTest::testGetBtContext() +void BtRegistryTest::testGetDownloadContext() { BtRegistry btRegistry; - CPPUNIT_ASSERT(btRegistry.getBtContext("test").isNull()); - SharedHandle<BtContext> btContext(new MockBtContext()); + CPPUNIT_ASSERT(btRegistry.getDownloadContext("test").isNull()); + SharedHandle<DownloadContext> dctx(new DownloadContext()); BtObject btObject; - btObject._btContext = btContext; + btObject._downloadContext = dctx; btRegistry.put("test", btObject); - CPPUNIT_ASSERT_EQUAL(btContext.get(), - btRegistry.getBtContext("test").get()); + CPPUNIT_ASSERT_EQUAL(dctx.get(), btRegistry.getDownloadContext("test").get()); } -static void addTwoBtContext(BtRegistry& btRegistry) +static void addTwoDownloadContext(BtRegistry& btRegistry) { - SharedHandle<BtContext> btContext1(new MockBtContext()); - SharedHandle<BtContext> btContext2(new MockBtContext()); + SharedHandle<DownloadContext> dctx1(new DownloadContext()); + SharedHandle<DownloadContext> dctx2(new DownloadContext()); BtObject btObject1; - btObject1._btContext = btContext1; + btObject1._downloadContext = dctx1; BtObject btObject2; - btObject2._btContext = btContext2; + btObject2._downloadContext = dctx2; btRegistry.put("ctx1", btObject1); btRegistry.put("ctx2", btObject2); } -void BtRegistryTest::testGetAllBtContext() +void BtRegistryTest::testGetAllDownloadContext() { BtRegistry btRegistry; - addTwoBtContext(btRegistry); + addTwoDownloadContext(btRegistry); - std::vector<SharedHandle<BtContext> > result; - btRegistry.getAllBtContext(std::back_inserter(result)); + std::vector<SharedHandle<DownloadContext> > result; + btRegistry.getAllDownloadContext(std::back_inserter(result)); CPPUNIT_ASSERT_EQUAL((size_t)2, result.size()); } void BtRegistryTest::testRemove() { BtRegistry btRegistry; - addTwoBtContext(btRegistry); + addTwoDownloadContext(btRegistry); CPPUNIT_ASSERT(btRegistry.remove("ctx1")); CPPUNIT_ASSERT(btRegistry.get("ctx1").isNull()); CPPUNIT_ASSERT(!btRegistry.get("ctx2").isNull()); @@ -79,7 +78,7 @@ void BtRegistryTest::testRemove() void BtRegistryTest::testRemoveAll() { BtRegistry btRegistry; - addTwoBtContext(btRegistry); + addTwoDownloadContext(btRegistry); btRegistry.removeAll(); CPPUNIT_ASSERT(btRegistry.get("ctx1").isNull()); CPPUNIT_ASSERT(btRegistry.get("ctx2").isNull()); diff --git a/test/BtRejectMessageTest.cc b/test/BtRejectMessageTest.cc index b88ab62c..5264d39e 100644 --- a/test/BtRejectMessageTest.cc +++ b/test/BtRejectMessageTest.cc @@ -8,7 +8,6 @@ #include "Peer.h" #include "FileEntry.h" #include "MockBtMessageDispatcher.h" -#include "MockBtContext.h" namespace aria2 { @@ -63,7 +62,6 @@ public: typedef SharedHandle<MockBtMessageDispatcher2> MockBtMessageDispatcher2Handle; - SharedHandle<MockBtContext> _btContext; SharedHandle<Peer> peer; SharedHandle<MockBtMessageDispatcher2> dispatcher; SharedHandle<BtRejectMessage> msg; @@ -72,14 +70,10 @@ public: peer.reset(new Peer("host", 6969)); peer->allocateSessionResource(1024, 1024*1024); - _btContext.reset(new MockBtContext()); - _btContext->setInfoHash((const unsigned char*)"12345678901234567890"); - dispatcher.reset(new MockBtMessageDispatcher2()); msg.reset(new BtRejectMessage()); msg->setPeer(peer); - msg->setBtContext(_btContext); msg->setIndex(1); msg->setBegin(16); msg->setLength(32); diff --git a/test/BtRequestMessageTest.cc b/test/BtRequestMessageTest.cc index 28c9a628..e7a86fcc 100644 --- a/test/BtRequestMessageTest.cc +++ b/test/BtRequestMessageTest.cc @@ -5,7 +5,6 @@ #include <cppunit/extensions/HelperMacros.h> #include "PeerMessageUtil.h" -#include "MockBtContext.h" #include "MockBtMessage.h" #include "MockPieceStorage.h" #include "MockBtMessageFactory.h" @@ -15,6 +14,7 @@ #include "FileEntry.h" #include "BtHandshakeMessage.h" #include "RangeBtMessageValidator.h" +#include "DlAbortEx.h" namespace aria2 { @@ -95,7 +95,6 @@ public: typedef SharedHandle<MockBtMessageFactory2> MockBtMessageFactory2Handle; - SharedHandle<MockBtContext> _btContext; SharedHandle<MockPieceStorage> _pieceStorage; SharedHandle<Peer> _peer; SharedHandle<MockBtMessageDispatcher> _dispatcher; @@ -103,23 +102,16 @@ public: SharedHandle<BtRequestMessage> msg; void setUp() { - _btContext.reset(new MockBtContext()); - _btContext->setInfoHash((const unsigned char*)"12345678901234567890"); - _btContext->setPieceLength(16*1024); - _btContext->setTotalLength(256*1024); - _pieceStorage.reset(new MockPieceStorage2()); _peer.reset(new Peer("host", 6969)); - _peer->allocateSessionResource(_btContext->getPieceLength(), - _btContext->getTotalLength()); + _peer->allocateSessionResource(16*1024, 256*1024); _dispatcher.reset(new MockBtMessageDispatcher()); _messageFactory.reset(new MockBtMessageFactory2()); msg.reset(new BtRequestMessage()); - msg->setBtContext(_btContext); msg->setPeer(_peer); msg->setIndex(1); msg->setBegin(16); diff --git a/test/DHTPeerAnnounceStorageTest.cc b/test/DHTPeerAnnounceStorageTest.cc index 8951befb..698fc72a 100644 --- a/test/DHTPeerAnnounceStorageTest.cc +++ b/test/DHTPeerAnnounceStorageTest.cc @@ -1,14 +1,17 @@ #include "DHTPeerAnnounceStorage.h" +#include <cstring> + #include <cppunit/extensions/HelperMacros.h> #include "Exception.h" #include "Util.h" -#include "MockBtContext.h" +#include "DownloadContext.h" #include "MockPeerStorage.h" #include "DHTConstants.h" #include "Peer.h" #include "FileEntry.h" +#include "bittorrent_helper.h" namespace aria2 { @@ -47,33 +50,40 @@ void DHTPeerAnnounceStorageTest::testAddAnnounce() CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.4"), peers[1]->ipaddr); } +static SharedHandle<DownloadContext> createDownloadContext +(const unsigned char* infoHash) +{ + SharedHandle<DownloadContext> dctx(new DownloadContext()); + BDE torrentAttrs = BDE::dict(); + torrentAttrs[bittorrent::INFO_HASH] = + std::string(&infoHash[0], &infoHash[DHT_ID_LENGTH]); + dctx->setAttribute(bittorrent::BITTORRENT, torrentAttrs); + return dctx; +} + void DHTPeerAnnounceStorageTest::testRemovePeerAnnounce() { - unsigned char infohash1[DHT_ID_LENGTH]; - memset(infohash1, 0xff, DHT_ID_LENGTH); - unsigned char infohash2[DHT_ID_LENGTH]; - memset(infohash2, 0xf0, DHT_ID_LENGTH); + unsigned char infoHash1[DHT_ID_LENGTH]; + memset(infoHash1, 0xff, DHT_ID_LENGTH); + unsigned char infoHash2[DHT_ID_LENGTH]; + memset(infoHash2, 0xf0, DHT_ID_LENGTH); DHTPeerAnnounceStorage storage; - SharedHandle<MockBtContext> ctx1(new MockBtContext()); - ctx1->setInfoHash(infohash1); + SharedHandle<DownloadContext> ctx1 = createDownloadContext(infoHash1); + SharedHandle<DownloadContext> ctx2 = createDownloadContext(infoHash2); SharedHandle<MockPeerStorage> peerStorage1(new MockPeerStorage()); - - SharedHandle<MockBtContext> ctx2(new MockBtContext()); - ctx2->setInfoHash(infohash2); - SharedHandle<MockPeerStorage> peerStorage2(new MockPeerStorage()); - storage.addPeerAnnounce(infohash1, "192.168.0.1", 6881); - storage.addPeerAnnounce(ctx1->getInfoHash(), peerStorage1); - storage.addPeerAnnounce(ctx2->getInfoHash(), peerStorage2); + storage.addPeerAnnounce(infoHash1, "192.168.0.1", 6881); + storage.addPeerAnnounce(infoHash1, peerStorage1); + storage.addPeerAnnounce(infoHash2, peerStorage2); - storage.removeLocalPeerAnnounce(ctx2->getInfoHash()); - CPPUNIT_ASSERT(!storage.contains(infohash2)); + storage.removeLocalPeerAnnounce(bittorrent::getInfoHash(ctx2)); + CPPUNIT_ASSERT(!storage.contains(infoHash2)); - storage.removeLocalPeerAnnounce(ctx1->getInfoHash()); - CPPUNIT_ASSERT(storage.contains(infohash1)); + storage.removeLocalPeerAnnounce(bittorrent::getInfoHash(ctx1)); + CPPUNIT_ASSERT(storage.contains(infoHash1)); } } // namespace aria2 diff --git a/test/DefaultBtAnnounceTest.cc b/test/DefaultBtAnnounceTest.cc index 46788b2b..8205c3f9 100644 --- a/test/DefaultBtAnnounceTest.cc +++ b/test/DefaultBtAnnounceTest.cc @@ -4,11 +4,9 @@ #include <cppunit/extensions/HelperMacros.h> -#include "DefaultBtContext.h" #include "Option.h" #include "Util.h" #include "Exception.h" -#include "MockBtContext.h" #include "MockPieceStorage.h" #include "MockPeerStorage.h" #include "BtRuntime.h" @@ -16,6 +14,9 @@ #include "FixedNumberRandomizer.h" #include "FileEntry.h" #include "prefs.h" +#include "DownloadContext.h" +#include "bittorrent_helper.h" +#include "array_fun.h" namespace aria2 { @@ -34,7 +35,7 @@ class DefaultBtAnnounceTest:public CppUnit::TestFixture { CPPUNIT_TEST(testProcessAnnounceResponse); CPPUNIT_TEST_SUITE_END(); private: - SharedHandle<MockBtContext> _btContext; + SharedHandle<DownloadContext> _dctx; SharedHandle<MockPieceStorage> _pieceStorage; SharedHandle<MockPeerStorage> _peerStorage; SharedHandle<BtRuntime> _btRuntime; @@ -54,11 +55,12 @@ public: std::string peerId = "-aria2-ultrafastdltl"; - _btContext.reset(new MockBtContext()); - _btContext->setInfoHash(infoHash); - _btContext->setTotalLength(totalLength); - _btContext->setPieceLength(pieceLength); - _btContext->setPeerId(reinterpret_cast<const unsigned char*>(peerId.c_str())); + _dctx.reset(new DownloadContext(pieceLength, totalLength)); + BDE torrentAttrs = BDE::dict(); + torrentAttrs[bittorrent::INFO_HASH] = + std::string(&infoHash[0], &infoHash[arrayLength(infoHash)]); + _dctx->setAttribute(bittorrent::BITTORRENT, torrentAttrs); + bittorrent::setStaticPeerId(peerId); _pieceStorage.reset(new MockPieceStorage()); _pieceStorage->setTotalLength(totalLength); @@ -94,23 +96,39 @@ public: CPPUNIT_TEST_SUITE_REGISTRATION(DefaultBtAnnounceTest); -void DefaultBtAnnounceTest::testNoMoreAnnounce() +template<typename InputIterator> +static BDE createAnnounceTier(InputIterator first, InputIterator last) { - std::string trackerURI1 = "http://localhost/announce"; - std::deque<std::string> uris1; - uris1.push_back(trackerURI1); - SharedHandle<AnnounceTier> announceTier1(new AnnounceTier(uris1)); + BDE announceTier = BDE::list(); + for(; first != last; ++first) { + announceTier << BDE(*first); + } + return announceTier; +} - std::string trackerURI2 = "http://backup/announce"; - std::deque<std::string> uris2; - uris2.push_back(trackerURI2); - SharedHandle<AnnounceTier> announceTier2(new AnnounceTier(uris2)); +static BDE createAnnounceTier(const std::string& uri) +{ + BDE announceTier = BDE::list(); + announceTier << uri; + return announceTier; +} +static void setAnnounceList(const SharedHandle<DownloadContext>& dctx, + const BDE& announceList) +{ + dctx->getAttribute(bittorrent::BITTORRENT)[bittorrent::ANNOUNCE_LIST] = + announceList; +} - _btContext->addAnnounceTier(announceTier1); - _btContext->addAnnounceTier(announceTier2); +void DefaultBtAnnounceTest::testNoMoreAnnounce() +{ + BDE announceList = BDE::list(); + announceList << createAnnounceTier("http://localhost/announce"); + announceList << createAnnounceTier("http://backup/announce"); + + setAnnounceList(_dctx, announceList); - DefaultBtAnnounce btAnnounce(_btContext, _option); + DefaultBtAnnounce btAnnounce(_dctx, _option); btAnnounce.setPieceStorage(_pieceStorage); btAnnounce.setPeerStorage(_peerStorage); btAnnounce.setBtRuntime(_btRuntime); @@ -154,14 +172,12 @@ void DefaultBtAnnounceTest::testNoMoreAnnounce() void DefaultBtAnnounceTest::testGetAnnounceUrl() { - std::string trackerURI = "http://localhost/announce"; - std::deque<std::string> uris; - uris.push_back(trackerURI); - SharedHandle<AnnounceTier> announceTier(new AnnounceTier(uris)); - _btContext->addAnnounceTier(announceTier); + BDE announceList = BDE::list(); + announceList << createAnnounceTier("http://localhost/announce"); + setAnnounceList(_dctx, announceList); - DefaultBtAnnounce btAnnounce(_btContext, _option); + DefaultBtAnnounce btAnnounce(_dctx, _option); btAnnounce.setPieceStorage(_pieceStorage); btAnnounce.setPeerStorage(_peerStorage); btAnnounce.setBtRuntime(_btRuntime); @@ -189,14 +205,11 @@ void DefaultBtAnnounceTest::testGetAnnounceUrl() void DefaultBtAnnounceTest::testGetAnnounceUrl_withQuery() { - std::string trackerURI = "http://localhost/announce?k=v"; - std::deque<std::string> uris; - uris.push_back(trackerURI); - SharedHandle<AnnounceTier> announceTier(new AnnounceTier(uris)); + BDE announceList = BDE::list(); + announceList << createAnnounceTier("http://localhost/announce?k=v"); + setAnnounceList(_dctx, announceList); - _btContext->addAnnounceTier(announceTier); - - DefaultBtAnnounce btAnnounce(_btContext, _option); + DefaultBtAnnounce btAnnounce(_dctx, _option); btAnnounce.setPieceStorage(_pieceStorage); btAnnounce.setPeerStorage(_peerStorage); btAnnounce.setBtRuntime(_btRuntime); @@ -215,15 +228,12 @@ void DefaultBtAnnounceTest::testGetAnnounceUrl_withQuery() void DefaultBtAnnounceTest::testGetAnnounceUrl_externalIP() { - std::string trackerURI = "http://localhost/announce"; - std::deque<std::string> uris; - uris.push_back(trackerURI); - SharedHandle<AnnounceTier> announceTier(new AnnounceTier(uris)); - - _btContext->addAnnounceTier(announceTier); + BDE announceList = BDE::list(); + announceList << createAnnounceTier("http://localhost/announce"); + setAnnounceList(_dctx, announceList); _option->put(PREF_BT_EXTERNAL_IP, "192.168.1.1"); - DefaultBtAnnounce btAnnounce(_btContext, _option); + DefaultBtAnnounce btAnnounce(_dctx, _option); btAnnounce.setPieceStorage(_pieceStorage); btAnnounce.setPeerStorage(_peerStorage); btAnnounce.setBtRuntime(_btRuntime); @@ -242,21 +252,12 @@ void DefaultBtAnnounceTest::testGetAnnounceUrl_externalIP() void DefaultBtAnnounceTest::testIsAllAnnounceFailed() { - std::string trackerURI1 = "http://localhost/announce"; - std::deque<std::string> uris1; - uris1.push_back(trackerURI1); - SharedHandle<AnnounceTier> announceTier1(new AnnounceTier(uris1)); - - std::string trackerURI2 = "http://backup/announce"; - std::deque<std::string> uris2; - uris2.push_back(trackerURI2); - SharedHandle<AnnounceTier> announceTier2(new AnnounceTier(uris2)); - - - _btContext->addAnnounceTier(announceTier1); - _btContext->addAnnounceTier(announceTier2); + BDE announceList = BDE::list(); + announceList << createAnnounceTier("http://localhost/announce"); + announceList << createAnnounceTier("http://backup/announce"); + setAnnounceList(_dctx, announceList); - DefaultBtAnnounce btAnnounce(_btContext, _option); + DefaultBtAnnounce btAnnounce(_dctx, _option); btAnnounce.setPieceStorage(_pieceStorage); btAnnounce.setPeerStorage(_peerStorage); btAnnounce.setBtRuntime(_btRuntime); @@ -284,11 +285,12 @@ void DefaultBtAnnounceTest::testURLOrderInStoppedEvent() { const char* urls[] = { "http://localhost1/announce", "http://localhost2/announce" }; - SharedHandle<AnnounceTier> announceTier(new AnnounceTier(std::deque<std::string>(&urls[0], &urls[2]))); - _btContext->addAnnounceTier(announceTier); + BDE announceList = BDE::list(); + announceList << createAnnounceTier(&urls[0], &urls[arrayLength(urls)]); + setAnnounceList(_dctx, announceList); - DefaultBtAnnounce btAnnounce(_btContext, _option); + DefaultBtAnnounce btAnnounce(_dctx, _option); btAnnounce.setPieceStorage(_pieceStorage); btAnnounce.setPeerStorage(_peerStorage); btAnnounce.setBtRuntime(_btRuntime); @@ -314,11 +316,12 @@ void DefaultBtAnnounceTest::testURLOrderInCompletedEvent() { const char* urls[] = { "http://localhost1/announce", "http://localhost2/announce" }; - SharedHandle<AnnounceTier> announceTier(new AnnounceTier(std::deque<std::string>(&urls[0], &urls[2]))); - _btContext->addAnnounceTier(announceTier); + BDE announceList = BDE::list(); + announceList << createAnnounceTier(&urls[0], &urls[arrayLength(urls)]); + setAnnounceList(_dctx, announceList); - DefaultBtAnnounce btAnnounce(_btContext, _option); + DefaultBtAnnounce btAnnounce(_dctx, _option); btAnnounce.setPieceStorage(_pieceStorage); btAnnounce.setPeerStorage(_peerStorage); btAnnounce.setBtRuntime(_btRuntime); @@ -344,8 +347,7 @@ void DefaultBtAnnounceTest::testProcessAnnounceResponse_malformed() { try { std::string res = "i123e"; - SharedHandle<MockBtContext> ctx(new MockBtContext()); - DefaultBtAnnounce(ctx, _option).processAnnounceResponse(reinterpret_cast<const unsigned char*>(res.c_str()), res.size()); + DefaultBtAnnounce(_dctx, _option).processAnnounceResponse(reinterpret_cast<const unsigned char*>(res.c_str()), res.size()); CPPUNIT_FAIL("exception must be thrown."); } catch(Exception& e) { std::cerr << e.stackTrace() << std::endl; @@ -356,8 +358,7 @@ void DefaultBtAnnounceTest::testProcessAnnounceResponse_failureReason() { try { std::string res = "d14:failure reason11:hello worlde"; - SharedHandle<MockBtContext> ctx(new MockBtContext()); - DefaultBtAnnounce(ctx, _option).processAnnounceResponse(reinterpret_cast<const unsigned char*>(res.c_str()), res.size()); + DefaultBtAnnounce(_dctx, _option).processAnnounceResponse(reinterpret_cast<const unsigned char*>(res.c_str()), res.size()); CPPUNIT_FAIL("exception must be thrown."); } catch(Exception& e) { std::cerr << e.stackTrace() << std::endl; @@ -375,8 +376,7 @@ void DefaultBtAnnounceTest::testProcessAnnounceResponse() "10:incompletei200e" "e"; - SharedHandle<MockBtContext> ctx(new MockBtContext()); - DefaultBtAnnounce an(ctx, _option); + DefaultBtAnnounce an(_dctx, _option); an.processAnnounceResponse(reinterpret_cast<const unsigned char*>(res.c_str()), res.size()); CPPUNIT_ASSERT_EQUAL(std::string("foo"), an.getTrackerID()); CPPUNIT_ASSERT_EQUAL((time_t)3000, an.getInterval()); diff --git a/test/DefaultBtContextTest.cc b/test/DefaultBtContextTest.cc deleted file mode 100644 index 10375d13..00000000 --- a/test/DefaultBtContextTest.cc +++ /dev/null @@ -1,610 +0,0 @@ -#include "DefaultBtContext.h" - -#include <cstring> -#include <iostream> - -#include <cppunit/extensions/HelperMacros.h> - -#include "Util.h" -#include "RecoverableException.h" -#include "AnnounceTier.h" -#include "FixedNumberRandomizer.h" -#include "FileEntry.h" -#include "array_fun.h" - -namespace aria2 { - -class DefaultBtContextTest:public CppUnit::TestFixture { - - CPPUNIT_TEST_SUITE(DefaultBtContextTest); - CPPUNIT_TEST(testGetInfoHash); - CPPUNIT_TEST(testGetPieceHash); - CPPUNIT_TEST(testGetFileEntries); - CPPUNIT_TEST(testGetTotalLength); - CPPUNIT_TEST(testGetFileEntriesSingle); - CPPUNIT_TEST(testGetTotalLengthSingle); - CPPUNIT_TEST(testGetFileModeMulti); - CPPUNIT_TEST(testGetFileModeSingle); - CPPUNIT_TEST(testGetNameMulti); - CPPUNIT_TEST(testGetNameSingle); - CPPUNIT_TEST(testOverrideName); - CPPUNIT_TEST(testGetAnnounceTier); - CPPUNIT_TEST(testGetAnnounceTierAnnounceList); - CPPUNIT_TEST(testGetPieceLength); - CPPUNIT_TEST(testGetInfoHashAsString); - CPPUNIT_TEST(testGetPeerId); - CPPUNIT_TEST(testComputeFastSet); - CPPUNIT_TEST(testGetFileEntries_multiFileUrlList); - CPPUNIT_TEST(testGetFileEntries_singleFileUrlList); - CPPUNIT_TEST(testGetFileEntries_singleFileUrlListEndsWithSlash); - CPPUNIT_TEST(testLoadFromMemory); - CPPUNIT_TEST(testLoadFromMemory_somethingMissing); - CPPUNIT_TEST(testLoadFromMemory_overrideName); - CPPUNIT_TEST(testLoadFromMemory_joinPathMulti); - CPPUNIT_TEST(testLoadFromMemory_joinPathSingle); - CPPUNIT_TEST(testGetNodes); - CPPUNIT_TEST(testGetActualBasePath); - CPPUNIT_TEST(testSetFileFilter_single); - CPPUNIT_TEST(testSetFileFilter_multi); - CPPUNIT_TEST(testUTF8Torrent); - CPPUNIT_TEST_SUITE_END(); -public: - void setUp() { - } - - void testGetInfoHash(); - void testGetPieceHash(); - void testGetFileEntries(); - void testGetTotalLength(); - void testGetFileEntriesSingle(); - void testGetTotalLengthSingle(); - void testGetFileModeMulti(); - void testGetFileModeSingle(); - void testGetNameMulti(); - void testGetNameSingle(); - void testOverrideName(); - void testGetAnnounceTier(); - void testGetAnnounceTierAnnounceList(); - void testGetPieceLength(); - void testGetInfoHashAsString(); - void testGetPeerId(); - void testComputeFastSet(); - void testGetFileEntries_multiFileUrlList(); - void testGetFileEntries_singleFileUrlList(); - void testGetFileEntries_singleFileUrlListEndsWithSlash(); - void testLoadFromMemory(); - void testLoadFromMemory_somethingMissing(); - void testLoadFromMemory_overrideName(); - void testLoadFromMemory_joinPathMulti(); - void testLoadFromMemory_joinPathSingle(); - void testGetNodes(); - void testGetActualBasePath(); - void testSetFileFilter_single(); - void testSetFileFilter_multi(); - void testUTF8Torrent(); -}; - - -CPPUNIT_TEST_SUITE_REGISTRATION(DefaultBtContextTest); - -void DefaultBtContextTest::testGetInfoHash() { - DefaultBtContext btContext; - btContext.load("test.torrent"); - - std::string correctHash = "248d0a1cd08284299de78d5c1ed359bb46717d8c"; - - CPPUNIT_ASSERT_EQUAL((size_t)20, btContext.getInfoHashLength()); - CPPUNIT_ASSERT_EQUAL(correctHash, Util::toHex(btContext.getInfoHash(), - btContext.getInfoHashLength())); -} - -void DefaultBtContextTest::testGetPieceHash() { - DefaultBtContext btContext; - btContext.load("test.torrent"); - - CPPUNIT_ASSERT_EQUAL(Util::toHex((const unsigned char*)"AAAAAAAAAAAAAAAAAAAA", 20), - btContext.getPieceHash(0)); - CPPUNIT_ASSERT_EQUAL(Util::toHex((const unsigned char*)"BBBBBBBBBBBBBBBBBBBB", 20), - btContext.getPieceHash(1)); - CPPUNIT_ASSERT_EQUAL(Util::toHex((const unsigned char*)"CCCCCCCCCCCCCCCCCCCC", 20), - btContext.getPieceHash(2)); - CPPUNIT_ASSERT_EQUAL(std::string(""), - btContext.getPieceHash(3)); -} - -void DefaultBtContextTest::testGetFileEntries() { - DefaultBtContext btContext; - btContext.load("test.torrent"); - // This is multi-file torrent. - std::deque<SharedHandle<FileEntry> > fileEntries = btContext.getFileEntries(); - // There are 2 file entries. - CPPUNIT_ASSERT_EQUAL((size_t)2, fileEntries.size()); - std::deque<SharedHandle<FileEntry> >::iterator itr = fileEntries.begin(); - - SharedHandle<FileEntry> fileEntry1 = *itr; - CPPUNIT_ASSERT_EQUAL(std::string("./aria2-test/aria2/src/aria2c"), - fileEntry1->getPath()); - itr++; - SharedHandle<FileEntry> fileEntry2 = *itr; - CPPUNIT_ASSERT_EQUAL(std::string("./aria2-test/aria2-0.2.2.tar.bz2"), - fileEntry2->getPath()); -} - -void DefaultBtContextTest::testGetFileEntriesSingle() { - DefaultBtContext btContext; - btContext.load("single.torrent"); - // This is multi-file torrent. - std::deque<SharedHandle<FileEntry> > fileEntries = btContext.getFileEntries(); - // There is 1 file entry. - CPPUNIT_ASSERT_EQUAL((size_t)1, fileEntries.size()); - std::deque<SharedHandle<FileEntry> >::iterator itr = fileEntries.begin(); - - SharedHandle<FileEntry> fileEntry1 = *itr; - CPPUNIT_ASSERT_EQUAL(std::string("./aria2-0.8.2.tar.bz2"), - fileEntry1->getPath()); -} - -void DefaultBtContextTest::testGetTotalLength() { - DefaultBtContext btContext; - btContext.load("test.torrent"); - - CPPUNIT_ASSERT_EQUAL((uint64_t)384ULL, btContext.getTotalLength()); -} - -void DefaultBtContextTest::testGetTotalLengthSingle() { - DefaultBtContext btContext; - btContext.load("single.torrent"); - - CPPUNIT_ASSERT_EQUAL((uint64_t)384ULL, btContext.getTotalLength()); -} - -void DefaultBtContextTest::testGetFileModeMulti() { - DefaultBtContext btContext; - btContext.load("test.torrent"); - - CPPUNIT_ASSERT_EQUAL(BtContext::MULTI, btContext.getFileMode()); -} - -void DefaultBtContextTest::testGetFileModeSingle() { - DefaultBtContext btContext; - btContext.load("single.torrent"); - - CPPUNIT_ASSERT_EQUAL(BtContext::SINGLE, btContext.getFileMode()); -} - -void DefaultBtContextTest::testGetNameMulti() { - DefaultBtContext btContext; - btContext.load("test.torrent"); - - CPPUNIT_ASSERT_EQUAL(std::string("aria2-test"), btContext.getName()); -} - -void DefaultBtContextTest::testGetNameSingle() { - DefaultBtContext btContext; - btContext.load("single.torrent"); - - CPPUNIT_ASSERT_EQUAL(std::string("aria2-0.8.2.tar.bz2"), btContext.getName()); -} - -void DefaultBtContextTest::testOverrideName() -{ - DefaultBtContext btContext; - btContext.load("test.torrent", "aria2-override.name"); - CPPUNIT_ASSERT_EQUAL(std::string("aria2-override.name"), btContext.getName()); -} - -void DefaultBtContextTest::testGetAnnounceTier() { - DefaultBtContext btContext; - btContext.load("single.torrent"); - - std::deque<SharedHandle<AnnounceTier> > tiers = btContext.getAnnounceTiers(); - - // There is 1 tier. - CPPUNIT_ASSERT_EQUAL((size_t)1, tiers.size()); - - std::deque<SharedHandle<AnnounceTier> >::iterator itr = tiers.begin(); - SharedHandle<AnnounceTier> tier1 = *itr; - CPPUNIT_ASSERT_EQUAL((size_t)1, tier1->urls.size()); - CPPUNIT_ASSERT_EQUAL(std::string("http://aria.rednoah.com/announce.php"), - tier1->urls.at(0)); - -} - -void DefaultBtContextTest::testGetAnnounceTierAnnounceList() { - DefaultBtContext btContext; - btContext.load("test.torrent"); - - std::deque<SharedHandle<AnnounceTier> > tiers = btContext.getAnnounceTiers(); - - // There are 3 tiers. - CPPUNIT_ASSERT_EQUAL((size_t)3, tiers.size()); - - SharedHandle<AnnounceTier> tier1 = tiers.at(0); - CPPUNIT_ASSERT_EQUAL((size_t)1, tier1->urls.size()); - CPPUNIT_ASSERT_EQUAL(std::string("http://tracker1"), - tier1->urls.at(0)); - - SharedHandle<AnnounceTier> tier2 = tiers.at(1); - CPPUNIT_ASSERT_EQUAL((size_t)1, tier2->urls.size()); - CPPUNIT_ASSERT_EQUAL(std::string("http://tracker2"), tier2->urls.at(0)); - - SharedHandle<AnnounceTier> tier3 = tiers.at(2); - CPPUNIT_ASSERT_EQUAL((size_t)1, tier3->urls.size()); - CPPUNIT_ASSERT_EQUAL(std::string("http://tracker3"), tier3->urls.at(0)); -} - -void DefaultBtContextTest::testGetPieceLength() { - DefaultBtContext btContext; - btContext.load("test.torrent"); - - CPPUNIT_ASSERT_EQUAL((size_t)128, btContext.getPieceLength()); -} - -void DefaultBtContextTest::testGetInfoHashAsString() { - DefaultBtContext btContext; - btContext.load("test.torrent"); - - CPPUNIT_ASSERT_EQUAL(std::string("248d0a1cd08284299de78d5c1ed359bb46717d8c"), - btContext.getInfoHashAsString()); -} - -void DefaultBtContextTest::testGetPeerId() { - DefaultBtContext btContext; - btContext.setRandomizer(SharedHandle<Randomizer>(new FixedNumberRandomizer())); - CPPUNIT_ASSERT_EQUAL(std::string("%2Daria2%2DAAAAAAAAAAAAA"), Util::torrentUrlencode(btContext.getPeerId(), 20)); -} - -void DefaultBtContextTest::testComputeFastSet() -{ - std::string ipaddr = "192.168.0.1"; - unsigned char infoHash[20]; - memset(infoHash, 0, sizeof(infoHash)); - infoHash[0] = 0xff; - - int fastSetSize = 10; - - DefaultBtContext btContext; - btContext.setInfoHash(infoHash); - btContext.setNumPieces(1000); - - { - std::deque<size_t> fastSet; - btContext.computeFastSet(fastSet, ipaddr, fastSetSize); - size_t ans[] = { 686, 459, 278, 200, 404, 834, 64, 203, 760, 950 }; - std::deque<size_t> ansSet(&ans[0], &ans[arrayLength(ans)]); - CPPUNIT_ASSERT(std::equal(fastSet.begin(), fastSet.end(), ansSet.begin())); - } - ipaddr = "10.0.0.1"; - { - std::deque<size_t> fastSet; - btContext.computeFastSet(fastSet, ipaddr, fastSetSize); - size_t ans[] = { 568, 188, 466, 452, 550, 662, 109, 226, 398, 11 }; - std::deque<size_t> ansSet(&ans[0], &ans[arrayLength(ans)]); - CPPUNIT_ASSERT(std::equal(fastSet.begin(), fastSet.end(), ansSet.begin())); - } - // See when pieces < fastSetSize - btContext.setNumPieces(9); - { - std::deque<size_t> fastSet; - btContext.computeFastSet(fastSet, ipaddr, fastSetSize); - size_t ans[] = { 8, 6, 7, 5, 1, 4, 0, 2, 3 }; - std::deque<size_t> ansSet(&ans[0], &ans[arrayLength(ans)]); - CPPUNIT_ASSERT(std::equal(fastSet.begin(), fastSet.end(), ansSet.begin())); - } -} - -void DefaultBtContextTest::testGetFileEntries_multiFileUrlList() { - DefaultBtContext btContext; - btContext.load("url-list-multiFile.torrent"); - // This is multi-file torrent. - std::deque<SharedHandle<FileEntry> > fileEntries = btContext.getFileEntries(); - // There are 2 file entries. - CPPUNIT_ASSERT_EQUAL((size_t)2, fileEntries.size()); - std::deque<SharedHandle<FileEntry> >::iterator itr = fileEntries.begin(); - - SharedHandle<FileEntry> fileEntry1 = *itr; - CPPUNIT_ASSERT_EQUAL(std::string("./aria2-test/aria2/src/aria2c"), - fileEntry1->getPath()); - std::deque<std::string> uris1 = fileEntry1->getAssociatedUris(); - CPPUNIT_ASSERT_EQUAL((size_t)2, uris1.size()); - CPPUNIT_ASSERT_EQUAL(std::string("http://localhost/dist/aria2-test/aria2/src/aria2c"), - uris1[0]); - CPPUNIT_ASSERT_EQUAL(std::string("http://mirror/dist/aria2-test/aria2/src/aria2c"), - uris1[1]); - - itr++; - SharedHandle<FileEntry> fileEntry2 = *itr; - CPPUNIT_ASSERT_EQUAL(std::string("./aria2-test/aria2-0.2.2.tar.bz2"), - fileEntry2->getPath()); - std::deque<std::string> uris2 = fileEntry2->getAssociatedUris(); - CPPUNIT_ASSERT_EQUAL((size_t)2, uris2.size()); - CPPUNIT_ASSERT_EQUAL(std::string("http://localhost/dist/aria2-test/aria2-0.2.2.tar.bz2"), - uris2[0]); - CPPUNIT_ASSERT_EQUAL(std::string("http://mirror/dist/aria2-test/aria2-0.2.2.tar.bz2"), - uris2[1]); -} - -void DefaultBtContextTest::testGetFileEntries_singleFileUrlList() { - DefaultBtContext btContext; - btContext.load("url-list-singleFile.torrent"); - // This is single-file torrent. - std::deque<SharedHandle<FileEntry> > fileEntries = btContext.getFileEntries(); - // There are 1 file entries. - CPPUNIT_ASSERT_EQUAL((size_t)1, fileEntries.size()); - - SharedHandle<FileEntry> fileEntry1 = fileEntries.front(); - CPPUNIT_ASSERT_EQUAL(std::string("./aria2.tar.bz2"), - fileEntry1->getPath()); - std::deque<std::string> uris1 = fileEntry1->getAssociatedUris(); - CPPUNIT_ASSERT_EQUAL((size_t)1, uris1.size()); - CPPUNIT_ASSERT_EQUAL(std::string("http://localhost/dist/aria2.tar.bz2"), - uris1[0]); -} - -void DefaultBtContextTest::testGetFileEntries_singleFileUrlListEndsWithSlash() { - DefaultBtContext btContext; - btContext.load("url-list-singleFileEndsWithSlash.torrent"); - // This is single-file torrent. - std::deque<SharedHandle<FileEntry> > fileEntries = btContext.getFileEntries(); - // There are 1 file entries. - CPPUNIT_ASSERT_EQUAL((size_t)1, fileEntries.size()); - - SharedHandle<FileEntry> fileEntry1 = fileEntries.front(); - CPPUNIT_ASSERT_EQUAL(std::string("./aria2.tar.bz2"), - fileEntry1->getPath()); - std::deque<std::string> uris1 = fileEntry1->getAssociatedUris(); - CPPUNIT_ASSERT_EQUAL((size_t)1, uris1.size()); - CPPUNIT_ASSERT_EQUAL(std::string("http://localhost/dist/aria2.tar.bz2"), - uris1[0]); -} - -void DefaultBtContextTest::testLoadFromMemory() -{ - std::string memory = "d8:announce36:http://aria.rednoah.com/announce.php13:announce-listll16:http://tracker1 el15:http://tracker2el15:http://tracker3ee7:comment17:REDNOAH.COM RULES13:creation datei1123456789e4:infod5:filesld6:lengthi284e4:pathl5:aria23:src6:aria2ceed6:lengthi100e4:pathl19:aria2-0.2.2.tar.bz2eee4:name10:aria2-test12:piece lengthi128e6:pieces60:AAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBBBBBCCCCCCCCCCCCCCCCCCCCee"; - - DefaultBtContext btContext; - btContext.loadFromMemory(memory, "default"); - - std::string correctHash = "248d0a1cd08284299de78d5c1ed359bb46717d8c"; - - CPPUNIT_ASSERT_EQUAL((size_t)20, btContext.getInfoHashLength()); - CPPUNIT_ASSERT_EQUAL(correctHash, Util::toHex(btContext.getInfoHash(), - btContext.getInfoHashLength())); -} - -void DefaultBtContextTest::testLoadFromMemory_somethingMissing() -{ - // pieces missing - try { - std::string memory = "d8:announce36:http://aria.rednoah.com/announce.php4:infod4:name13:aria2.tar.bz26:lengthi262144eee"; - DefaultBtContext btContext; - btContext.loadFromMemory(memory, "default"); - CPPUNIT_FAIL("exception must be thrown."); - } catch(Exception& e) { - std::cerr << e.stackTrace() << std::endl; - } -} - -void DefaultBtContextTest::testLoadFromMemory_overrideName() -{ - std::string memory = "d8:announce36:http://aria.rednoah.com/announce.php13:announce-listll16:http://tracker1 el15:http://tracker2el15:http://tracker3ee7:comment17:REDNOAH.COM RULES13:creation datei1123456789e4:infod5:filesld6:lengthi284e4:pathl5:aria23:src6:aria2ceed6:lengthi100e4:pathl19:aria2-0.2.2.tar.bz2eee4:name10:aria2-test12:piece lengthi128e6:pieces60:AAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBBBBBCCCCCCCCCCCCCCCCCCCCee"; - - DefaultBtContext btContext; - btContext.loadFromMemory(memory, "default", "aria2-override.name"); - - CPPUNIT_ASSERT_EQUAL(std::string("aria2-override.name"), btContext.getName()); -} - -void DefaultBtContextTest::testLoadFromMemory_joinPathMulti() -{ - std::string memory = - "d8:announce27:http://example.com/announce4:infod5:filesld6:lengthi262144e4:pathl7:../dir14:dir28:file.imgeee4:name14:../name1/name212:piece lengthi262144e6:pieces20:00000000000000000000ee"; - - DefaultBtContext btContext; - btContext.setDir("/tmp"); - btContext.loadFromMemory(memory, "default"); - - CPPUNIT_ASSERT_EQUAL(std::string("../name1/name2"), btContext.getName()); - CPPUNIT_ASSERT_EQUAL(std::string("/tmp/name1/dir1/dir2/file.img"), - btContext.getFileEntries().front()->getPath()); -} - -void DefaultBtContextTest::testLoadFromMemory_joinPathSingle() -{ - std::string memory = - "d8:announce27:http://example.com/announce4:infod4:name14:../name1/name26:lengthi262144e12:piece lengthi262144e6:pieces20:00000000000000000000ee"; - - DefaultBtContext btContext; - btContext.setDir("/tmp"); - btContext.loadFromMemory(memory, "default"); - - CPPUNIT_ASSERT_EQUAL(std::string("../name1/name2"), btContext.getName()); - CPPUNIT_ASSERT_EQUAL(std::string("/tmp/name1/name2"), - btContext.getFileEntries().front()->getPath()); -} - -void DefaultBtContextTest::testGetNodes() -{ - { - std::string memory = - "d5:nodesl" - "l11:192.168.0.1i6881ee" - "l11:192.168.0.2i6882ee" - "e4:infod4:name13:aria2.tar.bz26:lengthi262144e" - "12:piece lengthi262144e" - "6:pieces20:AAAAAAAAAAAAAAAAAAAA" - "ee"; - DefaultBtContext btContext; - btContext.loadFromMemory(memory, "default"); - - const std::deque<std::pair<std::string, uint16_t> >& nodes = - btContext.getNodes(); - CPPUNIT_ASSERT_EQUAL((size_t)2, nodes.size()); - CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.1"), nodes[0].first); - CPPUNIT_ASSERT_EQUAL((uint16_t)6881, nodes[0].second); - CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.2"), nodes[1].first); - CPPUNIT_ASSERT_EQUAL((uint16_t)6882, nodes[1].second); - } - { - // empty hostname - std::string memory = - "d5:nodesl" - "l1: i6881ee" - "l11:192.168.0.2i6882ee" - "e4:infod4:name13:aria2.tar.bz26:lengthi262144e" - "12:piece lengthi262144e" - "6:pieces20:AAAAAAAAAAAAAAAAAAAA" - "ee"; - DefaultBtContext btContext; - btContext.loadFromMemory(memory, "default"); - - const std::deque<std::pair<std::string, uint16_t> >& nodes = - btContext.getNodes(); - CPPUNIT_ASSERT_EQUAL((size_t)1, nodes.size()); - CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.2"), nodes[0].first); - CPPUNIT_ASSERT_EQUAL((uint16_t)6882, nodes[0].second); - } - { - // bad port - std::string memory = - "d5:nodesl" - "l11:192.168.0.11:xe" - "l11:192.168.0.2i6882ee" - "e4:infod4:name13:aria2.tar.bz26:lengthi262144e" - "12:piece lengthi262144e" - "6:pieces20:AAAAAAAAAAAAAAAAAAAA" - "ee"; - DefaultBtContext btContext; - btContext.loadFromMemory(memory, "default"); - - const std::deque<std::pair<std::string, uint16_t> >& nodes = - btContext.getNodes(); - CPPUNIT_ASSERT_EQUAL((size_t)1, nodes.size()); - CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.2"), nodes[0].first); - CPPUNIT_ASSERT_EQUAL((uint16_t)6882, nodes[0].second); - } - { - // port missing - std::string memory = - "d5:nodesl" - "l11:192.168.0.1e" - "l11:192.168.0.2i6882ee" - "e4:infod4:name13:aria2.tar.bz26:lengthi262144e" - "12:piece lengthi262144e" - "6:pieces20:AAAAAAAAAAAAAAAAAAAA" - "ee"; - DefaultBtContext btContext; - btContext.loadFromMemory(memory, "default"); - - const std::deque<std::pair<std::string, uint16_t> >& nodes = - btContext.getNodes(); - CPPUNIT_ASSERT_EQUAL((size_t)1, nodes.size()); - CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.2"), nodes[0].first); - CPPUNIT_ASSERT_EQUAL((uint16_t)6882, nodes[0].second); - } - { - // nodes is not a list - std::string memory = - "d5:nodes" - "l11:192.168.0.1e" - "4:infod4:name13:aria2.tar.bz26:lengthi262144e" - "12:piece lengthi262144e" - "6:pieces20:AAAAAAAAAAAAAAAAAAAA" - "ee"; - DefaultBtContext btContext; - btContext.loadFromMemory(memory, "default"); - - const std::deque<std::pair<std::string, uint16_t> >& nodes = - btContext.getNodes(); - CPPUNIT_ASSERT_EQUAL((size_t)0, nodes.size()); - } - { - // the element of node is not Data - std::string memory = - "d5:nodesl" - "ll11:192.168.0.1i6881eee" - "l11:192.168.0.2i6882ee" - "e4:infod4:name13:aria2.tar.bz26:lengthi262144e" - "12:piece lengthi262144e" - "6:pieces20:AAAAAAAAAAAAAAAAAAAA" - "ee"; - DefaultBtContext btContext; - btContext.loadFromMemory(memory, "default"); - - const std::deque<std::pair<std::string, uint16_t> >& nodes = - btContext.getNodes(); - CPPUNIT_ASSERT_EQUAL((size_t)1, nodes.size()); - CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.2"), nodes[0].first); - CPPUNIT_ASSERT_EQUAL((uint16_t)6882, nodes[0].second); - } - -} - -void DefaultBtContextTest::testGetActualBasePath() -{ - DefaultBtContext singleCtx; - singleCtx.load("single.torrent"); - singleCtx.setFilePathWithIndex(1, "new-path"); - CPPUNIT_ASSERT_EQUAL(std::string("new-path"), singleCtx.getActualBasePath()); - - DefaultBtContext multiCtx; - multiCtx.setDir("downloads"); - multiCtx.load("test.torrent"); - CPPUNIT_ASSERT_EQUAL(std::string("downloads/aria2-test"), - multiCtx.getActualBasePath()); -} - -void DefaultBtContextTest::testSetFileFilter_single() -{ - DefaultBtContext ctx; - ctx.load("single.torrent"); - - CPPUNIT_ASSERT(ctx.getFileEntries()[0]->isRequested()); - - ctx.setFileFilter(Util::parseIntRange("")); - CPPUNIT_ASSERT(ctx.getFileEntries()[0]->isRequested()); - - ctx.setFileFilter(Util::parseIntRange("1")); - CPPUNIT_ASSERT(ctx.getFileEntries()[0]->isRequested()); - - // For single file torrent, file is always selected whatever range - // is passed. - ctx.setFileFilter(Util::parseIntRange("2")); - CPPUNIT_ASSERT(ctx.getFileEntries()[0]->isRequested()); -} - -void DefaultBtContextTest::testSetFileFilter_multi() -{ - DefaultBtContext ctx; - ctx.load("test.torrent"); - - CPPUNIT_ASSERT(ctx.getFileEntries()[0]->isRequested()); - CPPUNIT_ASSERT(ctx.getFileEntries()[1]->isRequested()); - - ctx.setFileFilter(Util::parseIntRange("")); - CPPUNIT_ASSERT(ctx.getFileEntries()[0]->isRequested()); - CPPUNIT_ASSERT(ctx.getFileEntries()[1]->isRequested()); - - ctx.setFileFilter(Util::parseIntRange("2")); - CPPUNIT_ASSERT(!ctx.getFileEntries()[0]->isRequested()); - CPPUNIT_ASSERT(ctx.getFileEntries()[1]->isRequested()); - - ctx.setFileFilter(Util::parseIntRange("3")); - CPPUNIT_ASSERT(!ctx.getFileEntries()[0]->isRequested()); - CPPUNIT_ASSERT(!ctx.getFileEntries()[1]->isRequested()); - - ctx.setFileFilter(Util::parseIntRange("1,2")); - CPPUNIT_ASSERT(ctx.getFileEntries()[0]->isRequested()); - CPPUNIT_ASSERT(ctx.getFileEntries()[1]->isRequested()); -} - -void DefaultBtContextTest::testUTF8Torrent() -{ - DefaultBtContext ctx; - ctx.load("utf8.torrent"); - CPPUNIT_ASSERT_EQUAL(std::string("name in utf-8"), ctx.getName()); - CPPUNIT_ASSERT_EQUAL(std::string("./name in utf-8/path in utf-8"), - ctx.getFileEntries()[0]->getPath()); -} - -} // namespace aria2 diff --git a/test/DefaultBtMessageDispatcherTest.cc b/test/DefaultBtMessageDispatcherTest.cc index 073ae542..a8de2984 100644 --- a/test/DefaultBtMessageDispatcherTest.cc +++ b/test/DefaultBtMessageDispatcherTest.cc @@ -8,7 +8,6 @@ #include "Exception.h" #include "MockPieceStorage.h" #include "MockPeerStorage.h" -#include "DefaultBtContext.h" #include "MockBtMessage.h" #include "MockBtMessageFactory.h" #include "prefs.h" @@ -18,6 +17,8 @@ #include "RequestGroupMan.h" #include "ServerStatMan.h" #include "RequestGroup.h" +#include "DownloadContext.h" +#include "bittorrent_helper.h" namespace aria2 { @@ -41,7 +42,7 @@ class DefaultBtMessageDispatcherTest:public CppUnit::TestFixture { CPPUNIT_TEST(testRemoveOutstandingRequest); CPPUNIT_TEST_SUITE_END(); private: - SharedHandle<DefaultBtContext> btContext; + SharedHandle<DownloadContext> _dctx; SharedHandle<Peer> peer; SharedHandle<DefaultBtMessageDispatcher> btMessageDispatcher; SharedHandle<MockPeerStorage> peerStorage; @@ -138,13 +139,13 @@ public: _rg.reset(new RequestGroup(_option, std::deque<std::string>())); - btContext.reset(new DefaultBtContext()); - btContext->load("test.torrent"); - btContext->setOwnerRequestGroup(_rg.get()); + _dctx.reset(new DownloadContext()); + bittorrent::load("test.torrent", _dctx); + _dctx->setOwnerRequestGroup(_rg.get()); peer.reset(new Peer("192.168.0.1", 6969)); - peer->allocateSessionResource(btContext->getPieceLength(), - btContext->getTotalLength()); + peer->allocateSessionResource + (_dctx->getPieceLength(), _dctx->getTotalLength()); peerStorage.reset(new MockPeerStorage()); pieceStorage.reset(new MockPieceStorage()); @@ -155,7 +156,7 @@ public: btMessageDispatcher.reset(new DefaultBtMessageDispatcher()); btMessageDispatcher->setPeer(peer); - btMessageDispatcher->setBtContext(btContext); + btMessageDispatcher->setDownloadContext(_dctx); btMessageDispatcher->setPieceStorage(pieceStorage); btMessageDispatcher->setPeerStorage(peerStorage); btMessageDispatcher->setBtMessageFactory(_messageFactory); diff --git a/test/DefaultBtMessageFactoryTest.cc b/test/DefaultBtMessageFactoryTest.cc index abf8a420..304573a7 100644 --- a/test/DefaultBtMessageFactoryTest.cc +++ b/test/DefaultBtMessageFactoryTest.cc @@ -8,7 +8,7 @@ #include "Peer.h" #include "PeerMessageUtil.h" -#include "MockBtContext.h" +#include "DownloadContext.h" #include "MockExtensionMessageFactory.h" #include "BtExtendedMessage.h" #include "BtPortMessage.h" @@ -24,17 +24,14 @@ class DefaultBtMessageFactoryTest:public CppUnit::TestFixture { CPPUNIT_TEST(testCreatePortMessage); CPPUNIT_TEST_SUITE_END(); private: - SharedHandle<MockBtContext> _btContext; + SharedHandle<DownloadContext> _dctx; SharedHandle<Peer> _peer; SharedHandle<MockExtensionMessageFactory> _exmsgFactory; SharedHandle<DefaultBtMessageFactory> _factory; public: void setUp() { - _btContext.reset(new MockBtContext()); - unsigned char infohash[20]; - memset(infohash, 0, sizeof(infohash)); - _btContext->setInfoHash(infohash); + _dctx.reset(new DownloadContext()); _peer.reset(new Peer("192.168.0.1", 6969)); _peer->allocateSessionResource(1024, 1024*1024); @@ -43,7 +40,7 @@ public: _exmsgFactory.reset(new MockExtensionMessageFactory()); _factory.reset(new DefaultBtMessageFactory()); - _factory->setBtContext(_btContext); + _factory->setDownloadContext(_dctx); _factory->setPeer(_peer); _factory->setExtensionMessageFactory(_exmsgFactory); } diff --git a/test/DefaultBtProgressInfoFileTest.cc b/test/DefaultBtProgressInfoFileTest.cc index 2c8d45e4..9f95d80d 100644 --- a/test/DefaultBtProgressInfoFileTest.cc +++ b/test/DefaultBtProgressInfoFileTest.cc @@ -7,16 +7,17 @@ #include "Option.h" #include "Util.h" #include "Exception.h" -#ifdef ENABLE_BITTORRENT -#include "MockBtContext.h" -#include "MockPeerStorage.h" -#include "BtRuntime.h" -#endif // ENABLE_BITTORRENT #include "MockPieceStorage.h" #include "prefs.h" -#include "SingleFileDownloadContext.h" +#include "DownloadContext.h" #include "Piece.h" #include "FileEntry.h" +#include "array_fun.h" +#ifdef ENABLE_BITTORRENT +# include "MockPeerStorage.h" +# include "BtRuntime.h" +# include "bittorrent_helper.h" +#endif // ENABLE_BITTORRENT namespace aria2 { @@ -41,7 +42,7 @@ class DefaultBtProgressInfoFileTest:public CppUnit::TestFixture { private: #ifdef ENABLE_BITTORRENT - SharedHandle<MockBtContext> _btContext; + SharedHandle<DownloadContext> _dctx; SharedHandle<MockPeerStorage> _peerStorage; @@ -68,9 +69,18 @@ public: 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x00, 0xff, 0xff, 0xff, 0xff, }; - _btContext.reset(new MockBtContext()); - _btContext->setInfoHash(infoHash); - _btContext->setDir(_option->get(PREF_DIR)); + _dctx.reset(new DownloadContext()); + BDE torrentAttrs = BDE::dict(); + torrentAttrs[bittorrent::INFO_HASH] = + std::string(&infoHash[0], &infoHash[arrayLength(infoHash)]); + _dctx->setAttribute(bittorrent::BITTORRENT, torrentAttrs); + _dctx->setDir(_option->get(PREF_DIR)); + const SharedHandle<FileEntry> fileEntries[] = { + SharedHandle<FileEntry>(new FileEntry("/path/to/file",totalLength,0)) + }; + _dctx->setFileEntries(&fileEntries[0], + &fileEntries[arrayLength(fileEntries)]); + _dctx->setPieceLength(pieceLength); _peerStorage.reset(new MockPeerStorage()); _btRuntime.reset(new BtRuntime()); #endif // ENABLE_BITTORRENT @@ -105,24 +115,20 @@ CPPUNIT_TEST_SUITE_REGISTRATION(DefaultBtProgressInfoFileTest); void DefaultBtProgressInfoFileTest::testLoad_compat() { initializeMembers(1024, 81920); + _dctx->setBasePath("load"); - _btContext->setName("load"); - _btContext->setPieceLength(1024); - _btContext->setTotalLength(81920); - _btContext->setNumPieces(80); - - DefaultBtProgressInfoFile infoFile(_btContext, _pieceStorage, _option.get()); + DefaultBtProgressInfoFile infoFile(_dctx, _pieceStorage, _option.get()); infoFile.setBtRuntime(_btRuntime); infoFile.setPeerStorage(_peerStorage); - CPPUNIT_ASSERT_EQUAL(std::string("./load.aria2"), infoFile.getFilename()); + CPPUNIT_ASSERT_EQUAL(std::string("load.aria2"), infoFile.getFilename()); infoFile.load(); // check the contents of objects // total length - CPPUNIT_ASSERT_EQUAL((uint64_t)81920, _btContext->getTotalLength()); + CPPUNIT_ASSERT_EQUAL((uint64_t)81920, _dctx->getTotalLength()); // upload length CPPUNIT_ASSERT_EQUAL((uint64_t)1024, _btRuntime->getUploadLengthAtStartup()); @@ -158,13 +164,10 @@ void DefaultBtProgressInfoFileTest::testLoad() { initializeMembers(1024, 81920); - _btContext->setName("load-v0001"); - _btContext->setPieceLength(1024); - _btContext->setTotalLength(81920); - _btContext->setNumPieces(80); + _dctx->setBasePath("load-v0001"); - DefaultBtProgressInfoFile infoFile(_btContext, _pieceStorage, _option.get()); - CPPUNIT_ASSERT_EQUAL(std::string("./load-v0001.aria2"), + DefaultBtProgressInfoFile infoFile(_dctx, _pieceStorage, _option.get()); + CPPUNIT_ASSERT_EQUAL(std::string("load-v0001.aria2"), infoFile.getFilename()); infoFile.setBtRuntime(_btRuntime); infoFile.setPeerStorage(_peerStorage); @@ -174,7 +177,7 @@ void DefaultBtProgressInfoFileTest::testLoad() // check the contents of objects // total length - CPPUNIT_ASSERT_EQUAL((uint64_t)81920, _btContext->getTotalLength()); + CPPUNIT_ASSERT_EQUAL((uint64_t)81920, _dctx->getTotalLength()); // upload length CPPUNIT_ASSERT_EQUAL((uint64_t)1024, _btRuntime->getUploadLengthAtStartup()); @@ -209,9 +212,7 @@ void DefaultBtProgressInfoFileTest::testSave() { initializeMembers(1024, 81920); - _btContext->setName("save-temp"); - _btContext->setPieceLength(1024); - _btContext->setTotalLength(81920); + _dctx->setBasePath("save-temp"); _bitfield->setAllBit(); _bitfield->unsetBit(79); _pieceStorage->setCompletedLength(80896); @@ -226,11 +227,11 @@ void DefaultBtProgressInfoFileTest::testSave() inFlightPieces.push_back(p2); _pieceStorage->addInFlightPiece(inFlightPieces); - DefaultBtProgressInfoFile infoFile(_btContext, _pieceStorage, _option.get()); + DefaultBtProgressInfoFile infoFile(_dctx, _pieceStorage, _option.get()); infoFile.setBtRuntime(_btRuntime); infoFile.setPeerStorage(_peerStorage); - CPPUNIT_ASSERT_EQUAL(std::string("./save-temp.aria2"), + CPPUNIT_ASSERT_EQUAL(std::string("save-temp.aria2"), infoFile.getFilename()); infoFile.save(); @@ -332,12 +333,12 @@ void DefaultBtProgressInfoFileTest::testLoad_nonBt_compat() { initializeMembers(1024, 81920); - SharedHandle<SingleFileDownloadContext> dctx - (new SingleFileDownloadContext(1024, 81920, "./load-nonBt")); + SharedHandle<DownloadContext> dctx + (new DownloadContext(1024, 81920, "load-nonBt")); DefaultBtProgressInfoFile infoFile(dctx, _pieceStorage, _option.get()); - CPPUNIT_ASSERT_EQUAL(std::string("./load-nonBt.aria2"), + CPPUNIT_ASSERT_EQUAL(std::string("load-nonBt.aria2"), infoFile.getFilename()); infoFile.load(); @@ -377,12 +378,12 @@ void DefaultBtProgressInfoFileTest::testLoad_nonBt() { initializeMembers(1024, 81920); - SharedHandle<SingleFileDownloadContext> dctx - (new SingleFileDownloadContext(1024, 81920, "./load-nonBt-v0001")); + SharedHandle<DownloadContext> dctx + (new DownloadContext(1024, 81920, "load-nonBt-v0001")); DefaultBtProgressInfoFile infoFile(dctx, _pieceStorage, _option.get()); - CPPUNIT_ASSERT_EQUAL(std::string("./load-nonBt-v0001.aria2"), + CPPUNIT_ASSERT_EQUAL(std::string("load-nonBt-v0001.aria2"), infoFile.getFilename()); infoFile.load(); @@ -422,12 +423,12 @@ void DefaultBtProgressInfoFileTest::testLoad_nonBt_pieceLengthShorter() initializeMembers(512, 81920); _option->put(PREF_ALLOW_PIECE_LENGTH_CHANGE, V_TRUE); - SharedHandle<SingleFileDownloadContext> dctx - (new SingleFileDownloadContext(512, 81920, "./load-nonBt-v0001")); + SharedHandle<DownloadContext> dctx + (new DownloadContext(512, 81920, "load-nonBt-v0001")); DefaultBtProgressInfoFile infoFile(dctx, _pieceStorage, _option.get()); - CPPUNIT_ASSERT_EQUAL(std::string("./load-nonBt-v0001.aria2"), + CPPUNIT_ASSERT_EQUAL(std::string("load-nonBt-v0001.aria2"), infoFile.getFilename()); infoFile.load(); @@ -447,8 +448,8 @@ void DefaultBtProgressInfoFileTest::testSave_nonBt() { initializeMembers(1024, 81920); - SharedHandle<SingleFileDownloadContext> dctx - (new SingleFileDownloadContext(1024, 81920, "./save-temp")); + SharedHandle<DownloadContext> dctx + (new DownloadContext(1024, 81920, "save-temp")); _bitfield->setAllBit(); _bitfield->unsetBit(79); @@ -463,7 +464,7 @@ void DefaultBtProgressInfoFileTest::testSave_nonBt() DefaultBtProgressInfoFile infoFile(dctx, _pieceStorage, _option.get()); - CPPUNIT_ASSERT_EQUAL(std::string("./save-temp.aria2"), + CPPUNIT_ASSERT_EQUAL(std::string("save-temp.aria2"), infoFile.getFilename()); infoFile.save(); @@ -554,8 +555,8 @@ void DefaultBtProgressInfoFileTest::testSave_nonBt() void DefaultBtProgressInfoFileTest::testUpdateFilename() { - SharedHandle<SingleFileDownloadContext> dctx - (new SingleFileDownloadContext(1024, 81920, "./file1")); + SharedHandle<DownloadContext> dctx + (new DownloadContext(1024, 81920, "./file1")); DefaultBtProgressInfoFile infoFile(dctx, SharedHandle<MockPieceStorage>(), 0); #ifdef ENABLE_BITTORRENT @@ -565,7 +566,7 @@ void DefaultBtProgressInfoFileTest::testUpdateFilename() CPPUNIT_ASSERT_EQUAL(std::string("./file1.aria2"), infoFile.getFilename()); - dctx->setUFilename("./file1.1"); + dctx->getFirstFileEntry()->setPath("./file1.1"); CPPUNIT_ASSERT_EQUAL(std::string("./file1.aria2"), infoFile.getFilename()); diff --git a/test/DefaultBtRequestFactoryTest.cc b/test/DefaultBtRequestFactoryTest.cc index 64a8202c..9a5faf85 100644 --- a/test/DefaultBtRequestFactoryTest.cc +++ b/test/DefaultBtRequestFactoryTest.cc @@ -7,11 +7,12 @@ #include "MockBtMessage.h" #include "MockBtMessageFactory.h" #include "MockBtMessageDispatcher.h" -#include "MockBtContext.h" #include "MockPieceStorage.h" #include "Peer.h" #include "FileEntry.h" #include "BtHandshakeMessage.h" +#include "DownloadContext.h" +#include "bittorrent_helper.h" namespace aria2 { @@ -28,7 +29,7 @@ class DefaultBtRequestFactoryTest:public CppUnit::TestFixture { private: SharedHandle<Peer> _peer; SharedHandle<DefaultBtRequestFactory> _requestFactory; - SharedHandle<MockBtContext> _btContext; + SharedHandle<DownloadContext> _dctx; SharedHandle<MockPieceStorage> _pieceStorage; SharedHandle<MockBtMessageFactory> _messageFactory; SharedHandle<MockBtMessageDispatcher> _dispatcher; @@ -87,11 +88,6 @@ public: void setUp() { - _btContext.reset(new MockBtContext()); - _btContext->setInfoHash((const unsigned char*)"12345678901234567890"); - _btContext->setPieceLength(16*1024); - _btContext->setTotalLength(256*1024); - _pieceStorage.reset(new MockPieceStorage()); _peer.reset(new Peer("host", 6969)); @@ -101,7 +97,6 @@ public: _dispatcher.reset(new MockBtMessageDispatcher()); _requestFactory.reset(new DefaultBtRequestFactory()); - _requestFactory->setBtContext(_btContext); _requestFactory->setPieceStorage(_pieceStorage); _requestFactory->setPeer(_peer); _requestFactory->setBtMessageDispatcher(_dispatcher); @@ -229,9 +224,9 @@ void DefaultBtRequestFactoryTest::testRemoveTargetPiece() { void DefaultBtRequestFactoryTest::testGetTargetPieceIndexes() { - SharedHandle<Piece> piece1(new Piece(1, _btContext->getPieceLength())); - SharedHandle<Piece> piece3(new Piece(3, _btContext->getPieceLength())); - SharedHandle<Piece> piece5(new Piece(5, _btContext->getPieceLength())); + SharedHandle<Piece> piece1(new Piece(1, 16*1024)); + SharedHandle<Piece> piece3(new Piece(3, 16*1024)); + SharedHandle<Piece> piece5(new Piece(5, 16*1024)); _requestFactory->addTargetPiece(piece3); _requestFactory->addTargetPiece(piece1); diff --git a/test/DefaultExtensionMessageFactoryTest.cc b/test/DefaultExtensionMessageFactoryTest.cc index 5c56ecf8..740004de 100644 --- a/test/DefaultExtensionMessageFactoryTest.cc +++ b/test/DefaultExtensionMessageFactoryTest.cc @@ -5,7 +5,6 @@ #include <cppunit/extensions/HelperMacros.h> #include "Peer.h" -#include "MockBtContext.h" #include "MockPeerStorage.h" #include "PeerMessageUtil.h" #include "HandshakeExtensionMessage.h" @@ -24,7 +23,6 @@ class DefaultExtensionMessageFactoryTest:public CppUnit::TestFixture { CPPUNIT_TEST(testCreateMessage_UTPex); CPPUNIT_TEST_SUITE_END(); private: - SharedHandle<MockBtContext> _btContext; SharedHandle<MockPeerStorage> _peerStorage; SharedHandle<Peer> _peer; SharedHandle<DefaultExtensionMessageFactory> _factory; @@ -32,11 +30,6 @@ private: public: void setUp() { - _btContext.reset(new MockBtContext()); - unsigned char infohash[20]; - memset(infohash, 0, sizeof(infohash)); - _btContext->setInfoHash(infohash); - _peerStorage.reset(new MockPeerStorage()); _peer.reset(new Peer("192.168.0.1", 6969)); @@ -46,7 +39,6 @@ public: _registry.reset(new ExtensionMessageRegistry()); _factory.reset(new DefaultExtensionMessageFactory()); - _factory->setBtContext(_btContext); _factory->setPeerStorage(_peerStorage); _factory->setPeer(_peer); _factory->setExtensionMessageRegistry(_registry); diff --git a/test/DefaultPeerStorageTest.cc b/test/DefaultPeerStorageTest.cc index d2a0d8a2..6779cacd 100644 --- a/test/DefaultPeerStorageTest.cc +++ b/test/DefaultPeerStorageTest.cc @@ -1,12 +1,14 @@ #include "DefaultPeerStorage.h" -#include "DefaultBtContext.h" + +#include <algorithm> + +#include <cppunit/extensions/HelperMacros.h> + #include "Util.h" #include "Exception.h" #include "Peer.h" #include "Option.h" #include "BtRuntime.h" -#include <algorithm> -#include <cppunit/extensions/HelperMacros.h> namespace aria2 { @@ -24,13 +26,10 @@ class DefaultPeerStorageTest:public CppUnit::TestFixture { CPPUNIT_TEST(testOnErasingPeer); CPPUNIT_TEST_SUITE_END(); private: - SharedHandle<BtContext> btContext; SharedHandle<BtRuntime> btRuntime; Option* option; public: void setUp() { - btContext.reset(new DefaultBtContext()); - btContext->load("test.torrent"); option = new Option(); btRuntime.reset(new BtRuntime()); } @@ -54,7 +53,7 @@ public: CPPUNIT_TEST_SUITE_REGISTRATION(DefaultPeerStorageTest); void DefaultPeerStorageTest::testCountPeer() { - DefaultPeerStorage ps(btContext, option); + DefaultPeerStorage ps(option); CPPUNIT_ASSERT_EQUAL((size_t)0, ps.countPeer()); @@ -65,7 +64,7 @@ void DefaultPeerStorageTest::testCountPeer() { } void DefaultPeerStorageTest::testDeleteUnusedPeer() { - DefaultPeerStorage ps(btContext, option); + DefaultPeerStorage ps(option); SharedHandle<Peer> peer1(new Peer("192.168.0.1", 6889)); SharedHandle<Peer> peer2(new Peer("192.168.0.2", 6889)); @@ -96,7 +95,7 @@ void DefaultPeerStorageTest::testDeleteUnusedPeer() { } void DefaultPeerStorageTest::testAddPeer() { - DefaultPeerStorage ps(btContext, option); + DefaultPeerStorage ps(option); SharedHandle<BtRuntime> btRuntime(new BtRuntime()); btRuntime->setMaxPeers(3); ps.setBtRuntime(btRuntime); @@ -133,7 +132,7 @@ void DefaultPeerStorageTest::testAddPeer() { } void DefaultPeerStorageTest::testGetUnusedPeer() { - DefaultPeerStorage ps(btContext, option); + DefaultPeerStorage ps(option); ps.setBtRuntime(btRuntime); SharedHandle<Peer> peer1(new Peer("192.168.0.1", 6889)); @@ -154,7 +153,7 @@ void DefaultPeerStorageTest::testGetUnusedPeer() { } void DefaultPeerStorageTest::testIsPeerAvailable() { - DefaultPeerStorage ps(btContext, option); + DefaultPeerStorage ps(option); ps.setBtRuntime(btRuntime); CPPUNIT_ASSERT_EQUAL(false, ps.isPeerAvailable()); @@ -177,7 +176,7 @@ void DefaultPeerStorageTest::testIsPeerAvailable() { } void DefaultPeerStorageTest::testActivatePeer() { - DefaultPeerStorage ps(btContext, option); + DefaultPeerStorage ps(option); { std::deque<SharedHandle<Peer> > peers; @@ -196,8 +195,7 @@ void DefaultPeerStorageTest::testActivatePeer() { CPPUNIT_ASSERT_EQUAL((size_t)0, activePeers.size()); } { - peer1->allocateSessionResource(btContext->getPieceLength(), - btContext->getTotalLength()); + peer1->allocateSessionResource(1024*1024, 1024*1024*10); std::deque<SharedHandle<Peer> > activePeers; ps.getActivePeers(activePeers); @@ -211,14 +209,12 @@ void DefaultPeerStorageTest::testCalculateStat() { void DefaultPeerStorageTest::testReturnPeer() { - DefaultPeerStorage ps(btContext, option); + DefaultPeerStorage ps(option); SharedHandle<Peer> peer1(new Peer("192.168.0.1", 0)); - peer1->allocateSessionResource(btContext->getPieceLength(), - btContext->getTotalLength()); + peer1->allocateSessionResource(1024*1024, 1024*1024*10); SharedHandle<Peer> peer2(new Peer("192.168.0.2", 6889)); - peer2->allocateSessionResource(btContext->getPieceLength(), - btContext->getTotalLength()); + peer2->allocateSessionResource(1024*1024, 1024*1024*10); SharedHandle<Peer> peer3(new Peer("192.168.0.1", 6889)); ps.addPeer(peer1); ps.addPeer(peer2); diff --git a/test/DefaultPieceStorageTest.cc b/test/DefaultPieceStorageTest.cc index 7d3e490f..ef787c19 100644 --- a/test/DefaultPieceStorageTest.cc +++ b/test/DefaultPieceStorageTest.cc @@ -2,7 +2,6 @@ #include <cppunit/extensions/HelperMacros.h> -#include "DefaultBtContext.h" #include "Util.h" #include "Exception.h" #include "FixedNumberRandomizer.h" @@ -11,9 +10,10 @@ #include "Peer.h" #include "Option.h" #include "FileEntry.h" -#include "MockBtContext.h" #include "RarestPieceSelector.h" #include "InOrderPieceSelector.h" +#include "DownloadContext.h" +#include "bittorrent_helper.h" namespace aria2 { @@ -35,7 +35,7 @@ class DefaultPieceStorageTest:public CppUnit::TestFixture { CPPUNIT_TEST(testGetCompletedLength); CPPUNIT_TEST_SUITE_END(); private: - SharedHandle<BtContext> btContext; + SharedHandle<DownloadContext> _dctx; SharedHandle<Peer> peer; Option* option; SharedHandle<PieceSelector> _pieceSelector; @@ -48,11 +48,11 @@ public: } void setUp() { - btContext.reset(new DefaultBtContext()); - btContext->load("test.torrent"); + _dctx.reset(new DownloadContext()); + bittorrent::load("test.torrent", _dctx); peer.reset(new Peer("192.168.0.1", 6889)); - peer->allocateSessionResource(btContext->getPieceLength(), - btContext->getTotalLength()); + peer->allocateSessionResource(_dctx->getPieceLength(), + _dctx->getTotalLength()); option = new Option(); _pieceSelector.reset(new InOrderPieceSelector()); } @@ -82,13 +82,13 @@ public: CPPUNIT_TEST_SUITE_REGISTRATION(DefaultPieceStorageTest); void DefaultPieceStorageTest::testGetTotalLength() { - DefaultPieceStorage pss(btContext, option); + DefaultPieceStorage pss(_dctx, option); CPPUNIT_ASSERT_EQUAL((uint64_t)384ULL, pss.getTotalLength()); } void DefaultPieceStorageTest::testGetMissingPiece() { - DefaultPieceStorage pss(btContext, option); + DefaultPieceStorage pss(_dctx, option); pss.setPieceSelector(_pieceSelector); pss.setEndGamePieceNum(0); @@ -109,7 +109,7 @@ void DefaultPieceStorageTest::testGetMissingPiece() { void DefaultPieceStorageTest::testGetMissingPiece_excludedIndexes() { - DefaultPieceStorage pss(btContext, option); + DefaultPieceStorage pss(_dctx, option); pss.setPieceSelector(_pieceSelector); pss.setEndGamePieceNum(0); @@ -131,7 +131,7 @@ void DefaultPieceStorageTest::testGetMissingPiece_excludedIndexes() } void DefaultPieceStorageTest::testGetMissingFastPiece() { - DefaultPieceStorage pss(btContext, option); + DefaultPieceStorage pss(_dctx, option); pss.setPieceSelector(_pieceSelector); pss.setEndGamePieceNum(0); @@ -148,7 +148,7 @@ void DefaultPieceStorageTest::testGetMissingFastPiece() { void DefaultPieceStorageTest::testGetMissingFastPiece_excludedIndexes() { - DefaultPieceStorage pss(btContext, option); + DefaultPieceStorage pss(_dctx, option); pss.setPieceSelector(_pieceSelector); pss.setEndGamePieceNum(0); @@ -168,7 +168,7 @@ void DefaultPieceStorageTest::testGetMissingFastPiece_excludedIndexes() } void DefaultPieceStorageTest::testHasMissingPiece() { - DefaultPieceStorage pss(btContext, option); + DefaultPieceStorage pss(_dctx, option); CPPUNIT_ASSERT(!pss.hasMissingPiece(peer)); @@ -178,7 +178,7 @@ void DefaultPieceStorageTest::testHasMissingPiece() { } void DefaultPieceStorageTest::testCompletePiece() { - DefaultPieceStorage pss(btContext, option); + DefaultPieceStorage pss(_dctx, option); pss.setPieceSelector(_pieceSelector); pss.setEndGamePieceNum(0); @@ -200,7 +200,7 @@ void DefaultPieceStorageTest::testCompletePiece() { } void DefaultPieceStorageTest::testGetPiece() { - DefaultPieceStorage pss(btContext, option); + DefaultPieceStorage pss(_dctx, option); SharedHandle<Piece> pieceGot = pss.getPiece(0); CPPUNIT_ASSERT_EQUAL((size_t)0, pieceGot->getIndex()); @@ -209,7 +209,7 @@ void DefaultPieceStorageTest::testGetPiece() { } void DefaultPieceStorageTest::testGetPieceInUsedPieces() { - DefaultPieceStorage pss(btContext, option); + DefaultPieceStorage pss(_dctx, option); SharedHandle<Piece> piece = SharedHandle<Piece>(new Piece(0, 128)); piece->completeBlock(0); pss.addUsedPiece(piece); @@ -220,7 +220,7 @@ void DefaultPieceStorageTest::testGetPieceInUsedPieces() { } void DefaultPieceStorageTest::testGetPieceCompletedPiece() { - DefaultPieceStorage pss(btContext, option); + DefaultPieceStorage pss(_dctx, option); SharedHandle<Piece> piece = SharedHandle<Piece>(new Piece(0, 128)); pss.completePiece(piece); SharedHandle<Piece> pieceGot = pss.getPiece(0); @@ -237,19 +237,17 @@ void DefaultPieceStorageTest::testCancelPiece() uris1.push_back("http://localhost/src/file1.txt"); SharedHandle<FileEntry> file1(new FileEntry("src/file1.txt", totalLength, 0 /*, uris1*/)); - SharedHandle<MockBtContext> dctx(new MockBtContext()); - dctx->setPieceLength(pieceLength); - dctx->setTotalLength(totalLength); - dctx->addFileEntry(file1); + SharedHandle<DownloadContext> dctx + (new DownloadContext(pieceLength, totalLength, "src/file1.txt")); SharedHandle<DefaultPieceStorage> ps(new DefaultPieceStorage(dctx, option)); - SharedHandle<Piece> p = ps->getMissingPiece(); + SharedHandle<Piece> p = ps->getMissingPiece(0); p->completeBlock(0); ps->cancelPiece(p); - SharedHandle<Piece> p2 = ps->getMissingPiece(); + SharedHandle<Piece> p2 = ps->getMissingPiece(0); CPPUNIT_ASSERT(p2->hasBlock(0)); } @@ -258,9 +256,8 @@ void DefaultPieceStorageTest::testMarkPiecesDone() { size_t pieceLength = 256*1024; uint64_t totalLength = 4*1024*1024; - SharedHandle<MockBtContext> dctx(new MockBtContext()); - dctx->setPieceLength(pieceLength); - dctx->setTotalLength(totalLength); + SharedHandle<DownloadContext> dctx + (new DownloadContext(pieceLength, totalLength)); DefaultPieceStorage ps(dctx, option); @@ -283,9 +280,8 @@ void DefaultPieceStorageTest::testMarkPiecesDone() void DefaultPieceStorageTest::testGetCompletedLength() { - SharedHandle<MockBtContext> dctx(new MockBtContext()); - dctx->setPieceLength(1024*1024); - dctx->setTotalLength(256*1024*1024); + SharedHandle<DownloadContext> dctx + (new DownloadContext(1024*1024, 256*1024*1024)); DefaultPieceStorage ps(dctx, option); diff --git a/test/DirectDiskAdaptorTest.cc b/test/DirectDiskAdaptorTest.cc index 983d5785..b393ffb2 100644 --- a/test/DirectDiskAdaptorTest.cc +++ b/test/DirectDiskAdaptorTest.cc @@ -1,11 +1,12 @@ #include "DirectDiskAdaptor.h" + +#include <cppunit/extensions/HelperMacros.h> + #include "FileEntry.h" #include "DefaultDiskWriter.h" #include "Exception.h" #include "Util.h" #include "TestUtil.h" -#include <iostream> -#include <cppunit/extensions/HelperMacros.h> namespace aria2 { @@ -33,14 +34,14 @@ void DirectDiskAdaptorTest::testCutTrailingGarbage() 256, 0)); createFile(entry->getPath(), entry->getLength()+100); - std::deque<SharedHandle<FileEntry> > fileEntries; + std::vector<SharedHandle<FileEntry> > fileEntries; fileEntries.push_back(entry); DirectDiskAdaptor adaptor; adaptor.setDiskWriter (SharedHandle<DiskWriter>(new DefaultDiskWriter(entry->getPath()))); adaptor.setTotalLength(entry->getLength()); - adaptor.setFileEntries(fileEntries); + adaptor.setFileEntries(fileEntries.begin(), fileEntries.end()); adaptor.openFile(); adaptor.cutTrailingGarbage(); diff --git a/test/DownloadContextTest.cc b/test/DownloadContextTest.cc index bfe88400..42c03182 100644 --- a/test/DownloadContextTest.cc +++ b/test/DownloadContextTest.cc @@ -3,7 +3,7 @@ #include <cppunit/extensions/HelperMacros.h> #include "FileEntry.h" -#include "MockDownloadContext.h" +#include "array_fun.h" namespace aria2 { @@ -11,9 +11,15 @@ class DownloadContextTest:public CppUnit::TestFixture { CPPUNIT_TEST_SUITE(DownloadContextTest); CPPUNIT_TEST(testFindFileEntryByOffset); + CPPUNIT_TEST(testGetPieceHash); + CPPUNIT_TEST(testGetNumPieces); + CPPUNIT_TEST(testGetBasePath); CPPUNIT_TEST_SUITE_END(); public: void testFindFileEntryByOffset(); + void testGetPieceHash(); + void testGetNumPieces(); + void testGetBasePath(); }; @@ -21,16 +27,20 @@ CPPUNIT_TEST_SUITE_REGISTRATION(DownloadContextTest); void DownloadContextTest::testFindFileEntryByOffset() { - MockDownloadContext ctx; + DownloadContext ctx; CPPUNIT_ASSERT(ctx.findFileEntryByOffset(0).isNull()); - ctx.addFileEntry(SharedHandle<FileEntry>(new FileEntry("file1",1000,0))); - ctx.addFileEntry(SharedHandle<FileEntry>(new FileEntry("file2",0,1000))); - ctx.addFileEntry(SharedHandle<FileEntry>(new FileEntry("file3",0,1000))); - ctx.addFileEntry(SharedHandle<FileEntry>(new FileEntry("file4",2000,1000))); - ctx.addFileEntry(SharedHandle<FileEntry>(new FileEntry("file5",3000,3000))); - ctx.addFileEntry(SharedHandle<FileEntry>(new FileEntry("file6",0,6000))); + const SharedHandle<FileEntry> fileEntries[] = + { SharedHandle<FileEntry>(new FileEntry("file1",1000,0)), + SharedHandle<FileEntry>(new FileEntry("file2",0,1000)), + SharedHandle<FileEntry>(new FileEntry("file3",0,1000)), + SharedHandle<FileEntry>(new FileEntry("file4",2000,1000)), + SharedHandle<FileEntry>(new FileEntry("file5",3000,3000)), + SharedHandle<FileEntry>(new FileEntry("file6",0,6000)) + }; + ctx.setFileEntries(&fileEntries[0], + &fileEntries[arrayLength(fileEntries)]); CPPUNIT_ASSERT_EQUAL(std::string("file1"), ctx.findFileEntryByOffset(0)->getPath()); @@ -41,4 +51,30 @@ void DownloadContextTest::testFindFileEntryByOffset() CPPUNIT_ASSERT(ctx.findFileEntryByOffset(6000).isNull()); } +void DownloadContextTest::testGetPieceHash() +{ + DownloadContext ctx; + const std::string pieceHashes[] = { "hash1","hash2","shash3" }; + ctx.setPieceHashes(&pieceHashes[0], &pieceHashes[3]); + CPPUNIT_ASSERT_EQUAL(std::string("hash1"), ctx.getPieceHash(0)); + CPPUNIT_ASSERT_EQUAL(std::string(""), ctx.getPieceHash(3)); +} + +void DownloadContextTest::testGetNumPieces() +{ + DownloadContext ctx(345, 9889, ""); + CPPUNIT_ASSERT_EQUAL((size_t)29, ctx.getNumPieces()); +} + +void DownloadContextTest::testGetBasePath() +{ + DownloadContext ctx(0, 0, ""); + CPPUNIT_ASSERT_EQUAL(std::string(""), ctx.getBasePath()); + ctx.getFirstFileEntry()->setPath("aria2.tar.bz2"); + CPPUNIT_ASSERT_EQUAL(std::string("aria2.tar.bz2"), ctx.getBasePath()); + ctx.setDir("/tmp"); + // See dir doesn't effect getBasePath(). + CPPUNIT_ASSERT_EQUAL(std::string("aria2.tar.bz2"), ctx.getBasePath()); +} + } // namespace aria2 diff --git a/test/DownloadHandlerFactoryTest.cc b/test/DownloadHandlerFactoryTest.cc index 67cc6581..4bf4eea3 100644 --- a/test/DownloadHandlerFactoryTest.cc +++ b/test/DownloadHandlerFactoryTest.cc @@ -4,7 +4,7 @@ #include "RequestGroup.h" #include "Option.h" -#include "SingleFileDownloadContext.h" +#include "DownloadContext.h" #include "MemoryBufferPreDownloadHandler.h" #include "FileEntry.h" @@ -50,8 +50,7 @@ CPPUNIT_TEST_SUITE_REGISTRATION( DownloadHandlerFactoryTest ); void DownloadHandlerFactoryTest::testGetMetalinkPreDownloadHandler_extension() { - SharedHandle<SingleFileDownloadContext> dctx - (new SingleFileDownloadContext(0, 0, "test.metalink")); + SharedHandle<DownloadContext> dctx(new DownloadContext(0, 0, "test.metalink")); RequestGroup rg(_option, std::deque<std::string>()); rg.setDownloadContext(dctx); @@ -59,15 +58,14 @@ void DownloadHandlerFactoryTest::testGetMetalinkPreDownloadHandler_extension() CPPUNIT_ASSERT(handler->canHandle(&rg)); - dctx->setFilename("test.metalink2"); + dctx->getFirstFileEntry()->setPath("test.metalink2"); CPPUNIT_ASSERT(!handler->canHandle(&rg)); } void DownloadHandlerFactoryTest::testGetMetalinkPreDownloadHandler_contentType() { - SharedHandle<SingleFileDownloadContext> dctx - (new SingleFileDownloadContext(0, 0, "test")); - dctx->setContentType("application/metalink+xml"); + SharedHandle<DownloadContext> dctx(new DownloadContext(0, 0, "test")); + dctx->getFirstFileEntry()->setContentType("application/metalink+xml"); RequestGroup rg(_option, std::deque<std::string>()); rg.setDownloadContext(dctx); @@ -75,7 +73,7 @@ void DownloadHandlerFactoryTest::testGetMetalinkPreDownloadHandler_contentType() CPPUNIT_ASSERT(handler->canHandle(&rg)); - dctx->setContentType("application/octet-stream"); + dctx->getFirstFileEntry()->setContentType("application/octet-stream"); CPPUNIT_ASSERT(!handler->canHandle(&rg)); } @@ -85,8 +83,7 @@ void DownloadHandlerFactoryTest::testGetMetalinkPreDownloadHandler_contentType() void DownloadHandlerFactoryTest::testGetBtPreDownloadHandler_extension() { - SharedHandle<SingleFileDownloadContext> dctx - (new SingleFileDownloadContext(0, 0, "test.torrent")); + SharedHandle<DownloadContext> dctx(new DownloadContext(0, 0, "test.torrent")); RequestGroup rg(_option, std::deque<std::string>()); rg.setDownloadContext(dctx); @@ -94,15 +91,14 @@ void DownloadHandlerFactoryTest::testGetBtPreDownloadHandler_extension() CPPUNIT_ASSERT(handler->canHandle(&rg)); - dctx->setFilename("test.torrent2"); + dctx->getFirstFileEntry()->setPath("test.torrent2"); CPPUNIT_ASSERT(!handler->canHandle(&rg)); } void DownloadHandlerFactoryTest::testGetBtPreDownloadHandler_contentType() { - SharedHandle<SingleFileDownloadContext> dctx - (new SingleFileDownloadContext(0, 0, "test")); - dctx->setContentType("application/x-bittorrent"); + SharedHandle<DownloadContext> dctx(new DownloadContext(0, 0, "test")); + dctx->getFirstFileEntry()->setContentType("application/x-bittorrent"); RequestGroup rg(_option, std::deque<std::string>()); rg.setDownloadContext(dctx); @@ -110,7 +106,7 @@ void DownloadHandlerFactoryTest::testGetBtPreDownloadHandler_contentType() CPPUNIT_ASSERT(handler->canHandle(&rg)); - dctx->setContentType("application/octet-stream"); + dctx->getFirstFileEntry()->setContentType("application/octet-stream"); CPPUNIT_ASSERT(!handler->canHandle(&rg)); } diff --git a/test/DownloadHelperTest.cc b/test/DownloadHelperTest.cc index 99969634..b0d0999c 100644 --- a/test/DownloadHelperTest.cc +++ b/test/DownloadHelperTest.cc @@ -90,8 +90,7 @@ void DownloadHelperTest::testCreateRequestGroupForUri() CPPUNIT_ASSERT_EQUAL((unsigned int)3, group->getNumConcurrentCommand()); SharedHandle<DownloadContext> ctx = group->getDownloadContext(); CPPUNIT_ASSERT_EQUAL(std::string("/tmp"), ctx->getDir()); - CPPUNIT_ASSERT_EQUAL(std::string("/tmp/file.out"), - ctx->getActualBasePath()); + CPPUNIT_ASSERT_EQUAL(std::string("/tmp/file.out"), ctx->getBasePath()); } _option->put(PREF_SPLIT, "5"); { @@ -149,7 +148,7 @@ void DownloadHelperTest::testCreateRequestGroupForUri() SharedHandle<DownloadContext> alphaCtx = alphaGroup->getDownloadContext(); CPPUNIT_ASSERT_EQUAL(std::string("/tmp"), alphaCtx->getDir()); // See filename is not assigned yet - CPPUNIT_ASSERT_EQUAL(std::string(""), alphaCtx->getActualBasePath()); + CPPUNIT_ASSERT_EQUAL(std::string(""), alphaCtx->getBasePath()); } } @@ -182,8 +181,7 @@ void DownloadHelperTest::testCreateRequestGroupForUri_parameterized() CPPUNIT_ASSERT_EQUAL((unsigned int)3, group->getNumConcurrentCommand()); SharedHandle<DownloadContext> ctx = group->getDownloadContext(); CPPUNIT_ASSERT_EQUAL(std::string("/tmp"), ctx->getDir()); - CPPUNIT_ASSERT_EQUAL(std::string("/tmp/file.out"), - ctx->getActualBasePath()); + CPPUNIT_ASSERT_EQUAL(std::string("/tmp/file.out"), ctx->getBasePath()); } } @@ -219,7 +217,7 @@ void DownloadHelperTest::testCreateRequestGroupForUri_BitTorrent() SharedHandle<DownloadContext> ctx = group->getDownloadContext(); CPPUNIT_ASSERT_EQUAL(std::string("/tmp"), ctx->getDir()); CPPUNIT_ASSERT_EQUAL(std::string("/tmp/file.out"), - ctx->getActualBasePath()); + ctx->getBasePath()); SharedHandle<RequestGroup> torrentGroup = result[1]; std::deque<std::string> auxURIs; @@ -230,7 +228,7 @@ void DownloadHelperTest::testCreateRequestGroupForUri_BitTorrent() SharedHandle<DownloadContext> btctx = torrentGroup->getDownloadContext(); CPPUNIT_ASSERT_EQUAL(std::string("/tmp"), btctx->getDir()); CPPUNIT_ASSERT_EQUAL(std::string("/tmp/aria2-test"), - btctx->getActualBasePath()); + btctx->getBasePath()); } } #endif // ENABLE_BITTORRENT @@ -273,7 +271,7 @@ void DownloadHelperTest::testCreateRequestGroupForUri_Metalink() SharedHandle<DownloadContext> ctx = group->getDownloadContext(); CPPUNIT_ASSERT_EQUAL(std::string("/tmp"), ctx->getDir()); CPPUNIT_ASSERT_EQUAL(std::string("/tmp/file.out"), - ctx->getActualBasePath()); + ctx->getBasePath()); SharedHandle<RequestGroup> aria2052Group = result[1]; CPPUNIT_ASSERT_EQUAL((unsigned int)1, // because of maxconnections attribute @@ -282,7 +280,7 @@ void DownloadHelperTest::testCreateRequestGroupForUri_Metalink() aria2052Group->getDownloadContext(); CPPUNIT_ASSERT_EQUAL(std::string("/tmp"), aria2052Ctx->getDir()); CPPUNIT_ASSERT_EQUAL(std::string("/tmp/aria2-0.5.2.tar.bz2"), - aria2052Ctx->getActualBasePath()); + aria2052Ctx->getBasePath()); SharedHandle<RequestGroup> aria2051Group = result[2]; CPPUNIT_ASSERT_EQUAL((unsigned int)2, @@ -314,13 +312,13 @@ void DownloadHelperTest::testCreateRequestGroupForUriList() SharedHandle<DownloadContext> fileCtx = fileGroup->getDownloadContext(); CPPUNIT_ASSERT_EQUAL(std::string("/mydownloads"), fileCtx->getDir()); CPPUNIT_ASSERT_EQUAL(std::string("/mydownloads/myfile.out"), - fileCtx->getActualBasePath()); + fileCtx->getBasePath()); SharedHandle<RequestGroup> fileISOGroup = result[1]; SharedHandle<DownloadContext> fileISOCtx = fileISOGroup->getDownloadContext(); CPPUNIT_ASSERT_EQUAL(std::string("/tmp"), fileISOCtx->getDir()); CPPUNIT_ASSERT_EQUAL(std::string("/tmp/file.out"), - fileISOCtx->getActualBasePath()); + fileISOCtx->getBasePath()); } #ifdef ENABLE_BITTORRENT diff --git a/test/HandshakeExtensionMessageTest.cc b/test/HandshakeExtensionMessageTest.cc index eb97efeb..b05154d9 100644 --- a/test/HandshakeExtensionMessageTest.cc +++ b/test/HandshakeExtensionMessageTest.cc @@ -5,7 +5,6 @@ #include <cppunit/extensions/HelperMacros.h> #include "Peer.h" -#include "MockBtContext.h" #include "Exception.h" #include "FileEntry.h" @@ -22,8 +21,6 @@ class HandshakeExtensionMessageTest:public CppUnit::TestFixture { CPPUNIT_TEST(testCreate); CPPUNIT_TEST(testCreate_stringnum); CPPUNIT_TEST_SUITE_END(); -private: - SharedHandle<BtContext> _btContext; public: void setUp() {} @@ -87,7 +84,6 @@ void HandshakeExtensionMessageTest::testDoReceivedAction() msg.setExtension("ut_pex", 1); msg.setExtension("a2_dht", 2); msg.setPeer(peer); - msg.setBtContext(_btContext); msg.doReceivedAction(); diff --git a/test/HttpRequestTest.cc b/test/HttpRequestTest.cc index ba24a04c..0f370114 100644 --- a/test/HttpRequestTest.cc +++ b/test/HttpRequestTest.cc @@ -69,13 +69,14 @@ void HttpRequestTest::testGetStartByte() HttpRequest httpRequest; SharedHandle<Piece> p(new Piece(1, 1024)); SharedHandle<Segment> segment(new PiecedSegment(1024, p)); + SharedHandle<FileEntry> fileEntry(new FileEntry("file", 1024*10, 0)); CPPUNIT_ASSERT_EQUAL((off_t)0LL, httpRequest.getStartByte()); httpRequest.setSegment(segment); + httpRequest.setFileEntry(fileEntry); CPPUNIT_ASSERT_EQUAL((off_t)1024LL, httpRequest.getStartByte()); - } void HttpRequestTest::testGetEndByte() @@ -87,6 +88,7 @@ void HttpRequestTest::testGetEndByte() HttpRequest httpRequest; SharedHandle<Piece> piece(new Piece(index, length)); SharedHandle<Segment> segment(new PiecedSegment(segmentLength, piece)); + SharedHandle<FileEntry> fileEntry(new FileEntry("file", segmentLength*10, 0)); CPPUNIT_ASSERT_EQUAL((off_t)0LL, httpRequest.getEndByte()); @@ -99,10 +101,16 @@ void HttpRequestTest::testGetEndByte() request->setPipeliningHint(true); httpRequest.setRequest(request); + httpRequest.setFileEntry(fileEntry); CPPUNIT_ASSERT_EQUAL((off_t)(segmentLength*index+length-1), httpRequest.getEndByte()); + // The end byte of FileEntry are placed inside segment + fileEntry->setLength(segmentLength+100); + + CPPUNIT_ASSERT_EQUAL((off_t)(segmentLength*index+100-1), + httpRequest.getEndByte()); request->setPipeliningHint(false); @@ -120,11 +128,13 @@ void HttpRequestTest::testCreateRequest() p.reset(new Piece(0, 1024)); SharedHandle<Segment> segment(new PiecedSegment(1024, p)); + SharedHandle<FileEntry> fileEntry(new FileEntry("file", 1024*1024*10, 0)); HttpRequest httpRequest; httpRequest.disableContentEncoding(); httpRequest.setRequest(request); httpRequest.setSegment(segment); + httpRequest.setFileEntry(fileEntry); httpRequest.setAuthConfigFactory(_authConfigFactory); // remove "Connection: close" and add end byte range @@ -311,9 +321,12 @@ void HttpRequestTest::testCreateRequest_ftp() SharedHandle<Piece> p(new Piece(0, 1024*1024)); SharedHandle<Segment> segment (new PiecedSegment(1024*1024, p)); + SharedHandle<FileEntry> fileEntry(new FileEntry("file", 1024*1024*10, 0)); + httpRequest.disableContentEncoding(); httpRequest.setRequest(request); httpRequest.setSegment(segment); + httpRequest.setFileEntry(fileEntry); httpRequest.setAuthConfigFactory(_authConfigFactory); httpRequest.setProxyRequest(proxyRequest); @@ -360,6 +373,7 @@ void HttpRequestTest::testCreateRequest_with_cookie() SharedHandle<Piece> p(new Piece(0, 1024*1024)); SharedHandle<Segment> segment (new PiecedSegment(1024*1024, p)); + SharedHandle<FileEntry> fileEntry(new FileEntry("file", 1024*1024*10, 0)); Cookie cookie1("name1", "value1", "/archives", "localhost", false); Cookie cookie2("name2", "value2", "/archives/download", "localhost", false); @@ -377,6 +391,7 @@ void HttpRequestTest::testCreateRequest_with_cookie() httpRequest.disableContentEncoding(); httpRequest.setRequest(request); httpRequest.setSegment(segment); + httpRequest.setFileEntry(fileEntry); httpRequest.setCookieStorage(st); httpRequest.setAuthConfigFactory(_authConfigFactory); @@ -547,11 +562,13 @@ void HttpRequestTest::testIsRangeSatisfied() request->setPipeliningHint(false); // default: false SharedHandle<Piece> p(new Piece(0, 1024*1024)); SharedHandle<Segment> segment(new PiecedSegment(1024*1024, p)); + SharedHandle<FileEntry> fileEntry(new FileEntry("file", 0, 0)); HttpRequest httpRequest; httpRequest.setRequest(request); httpRequest.setSegment(segment); + httpRequest.setFileEntry(fileEntry); SharedHandle<Range> range(new Range()); @@ -569,11 +586,11 @@ void HttpRequestTest::testIsRangeSatisfied() CPPUNIT_ASSERT(httpRequest.isRangeSatisfied(range)); - httpRequest.setEntityLength(entityLength-1); + fileEntry->setLength(entityLength-1); CPPUNIT_ASSERT(!httpRequest.isRangeSatisfied(range)); - httpRequest.setEntityLength(entityLength); + fileEntry->setLength(entityLength); CPPUNIT_ASSERT(httpRequest.isRangeSatisfied(range)); @@ -598,13 +615,13 @@ void HttpRequestTest::testUserAgent() SharedHandle<Request> request(new Request()); request->setUrl("http://localhost:8080/archives/aria2-1.0.0.tar.bz2"); - SharedHandle<Piece> p(new Piece(0, 1024)); - SharedHandle<Segment> segment(new PiecedSegment(1024, p)); + //SharedHandle<Piece> p(new Piece(0, 1024)); + //SharedHandle<Segment> segment(new PiecedSegment(1024, p)); HttpRequest httpRequest; httpRequest.disableContentEncoding(); httpRequest.setRequest(request); - httpRequest.setSegment(segment); + //httpRequest.setSegment(segment); httpRequest.setUserAgent("aria2 (Linux)"); httpRequest.setAuthConfigFactory(_authConfigFactory); diff --git a/test/HttpResponseTest.cc b/test/HttpResponseTest.cc index 2c4fc348..59924c75 100644 --- a/test/HttpResponseTest.cc +++ b/test/HttpResponseTest.cc @@ -358,12 +358,14 @@ void HttpResponseTest::testValidateResponse_good_range() SharedHandle<Piece> p(new Piece(1, 1024*1024)); SharedHandle<Segment> segment(new PiecedSegment(1024*1024, p)); httpRequest->setSegment(segment); + SharedHandle<FileEntry> fileEntry(new FileEntry("file", 1024*1024*10, 0)); + httpRequest->setFileEntry(fileEntry); SharedHandle<Request> request(new Request()); request->setUrl("http://localhost/archives/aria2-1.0.0.tar.bz2"); httpRequest->setRequest(request); httpResponse.setHttpRequest(httpRequest); httpHeader->setResponseStatus("206"); - httpHeader->put("Content-Range", "bytes 1048576-10485760/10485761"); + httpHeader->put("Content-Range", "bytes 1048576-10485760/10485760"); try { httpResponse.validateResponse(); @@ -383,6 +385,8 @@ void HttpResponseTest::testValidateResponse_bad_range() SharedHandle<Piece> p(new Piece(1, 1024*1024)); SharedHandle<Segment> segment(new PiecedSegment(1024*1024, p)); httpRequest->setSegment(segment); + SharedHandle<FileEntry> fileEntry(new FileEntry("file", 1024*1024*10, 0)); + httpRequest->setFileEntry(fileEntry); SharedHandle<Request> request(new Request()); request->setUrl("http://localhost/archives/aria2-1.0.0.tar.bz2"); httpRequest->setRequest(request); @@ -407,6 +411,8 @@ void HttpResponseTest::testValidateResponse_chunked() SharedHandle<Piece> p(new Piece(1, 1024*1024)); SharedHandle<Segment> segment(new PiecedSegment(1024*1024, p)); httpRequest->setSegment(segment); + SharedHandle<FileEntry> fileEntry(new FileEntry("file", 1024*1024*10, 0)); + httpRequest->setFileEntry(fileEntry); SharedHandle<Request> request(new Request()); request->setUrl("http://localhost/archives/aria2-1.0.0.tar.bz2"); httpRequest->setRequest(request); diff --git a/test/IteratableChecksumValidatorTest.cc b/test/IteratableChecksumValidatorTest.cc index 287fd548..a4ce5528 100644 --- a/test/IteratableChecksumValidatorTest.cc +++ b/test/IteratableChecksumValidatorTest.cc @@ -2,7 +2,7 @@ #include <cppunit/extensions/HelperMacros.h> -#include "SingleFileDownloadContext.h" +#include "DownloadContext.h" #include "DefaultPieceStorage.h" #include "Option.h" #include "DiskAdaptor.h" @@ -32,8 +32,8 @@ CPPUNIT_TEST_SUITE_REGISTRATION( IteratableChecksumValidatorTest ); void IteratableChecksumValidatorTest::testValidate() { Option option; - SharedHandle<SingleFileDownloadContext> dctx - (new SingleFileDownloadContext(100, 250, "chunkChecksumTestFile250.txt")); + SharedHandle<DownloadContext> dctx + (new DownloadContext(100, 250, "chunkChecksumTestFile250.txt")); dctx->setChecksum("898a81b8e0181280ae2ee1b81e269196d91e869a"); dctx->setChecksumHashAlgo("sha1"); SharedHandle<DefaultPieceStorage> ps(new DefaultPieceStorage(dctx, &option)); @@ -51,8 +51,8 @@ void IteratableChecksumValidatorTest::testValidate() { void IteratableChecksumValidatorTest::testValidate_fail() { Option option; - SharedHandle<SingleFileDownloadContext> dctx - (new SingleFileDownloadContext(100, 250, "chunkChecksumTestFile250.txt")); + SharedHandle<DownloadContext> dctx + (new DownloadContext(100, 250, "chunkChecksumTestFile250.txt")); dctx->setChecksum(std::string(40, '0')); // set wrong checksum dctx->setChecksumHashAlgo("sha1"); SharedHandle<DefaultPieceStorage> ps(new DefaultPieceStorage(dctx, &option)); diff --git a/test/IteratableChunkChecksumValidatorTest.cc b/test/IteratableChunkChecksumValidatorTest.cc index bc89a19f..823b6e8c 100644 --- a/test/IteratableChunkChecksumValidatorTest.cc +++ b/test/IteratableChunkChecksumValidatorTest.cc @@ -2,7 +2,7 @@ #include <cppunit/extensions/HelperMacros.h> -#include "SingleFileDownloadContext.h" +#include "DownloadContext.h" #include "DefaultPieceStorage.h" #include "Option.h" #include "DiskAdaptor.h" @@ -37,9 +37,9 @@ const char* IteratableChunkChecksumValidatorTest::csArray[] = { "29b0e7878271645 void IteratableChunkChecksumValidatorTest::testValidate() { Option option; - SharedHandle<SingleFileDownloadContext> dctx - (new SingleFileDownloadContext(100, 250, "chunkChecksumTestFile250.txt")); - dctx->setPieceHashes(std::deque<std::string>(&csArray[0], &csArray[3])); + SharedHandle<DownloadContext> dctx + (new DownloadContext(100, 250, "chunkChecksumTestFile250.txt")); + dctx->setPieceHashes(&csArray[0], &csArray[3]); dctx->setPieceHashAlgo("sha1"); SharedHandle<DefaultPieceStorage> ps (new DefaultPieceStorage(dctx, &option)); @@ -60,7 +60,7 @@ void IteratableChunkChecksumValidatorTest::testValidate() { // make the test fail std::deque<std::string> badHashes(&csArray[0], &csArray[3]); badHashes[1] = "ffffffffffffffffffffffffffffffffffffffff"; - dctx->setPieceHashes(badHashes); + dctx->setPieceHashes(badHashes.begin(), badHashes.end()); validator.init(); @@ -74,12 +74,12 @@ void IteratableChunkChecksumValidatorTest::testValidate() { void IteratableChunkChecksumValidatorTest::testValidate_readError() { Option option; - SharedHandle<SingleFileDownloadContext> dctx - (new SingleFileDownloadContext(100, 500, "chunkChecksumTestFile250.txt")); + SharedHandle<DownloadContext> dctx + (new DownloadContext(100, 500, "chunkChecksumTestFile250.txt")); std::deque<std::string> hashes(&csArray[0], &csArray[3]); hashes.push_back("ffffffffffffffffffffffffffffffffffffffff"); hashes.push_back("ffffffffffffffffffffffffffffffffffffffff"); - dctx->setPieceHashes(hashes); + dctx->setPieceHashes(hashes.begin(), hashes.end()); dctx->setPieceHashAlgo("sha1"); SharedHandle<DefaultPieceStorage> ps(new DefaultPieceStorage(dctx, &option)); ps->initStorage(); diff --git a/test/MSEHandshakeTest.cc b/test/MSEHandshakeTest.cc index 2a5f52ce..3eb78b5c 100644 --- a/test/MSEHandshakeTest.cc +++ b/test/MSEHandshakeTest.cc @@ -1,5 +1,7 @@ #include "MSEHandshake.h" +#include <cstring> + #include <cppunit/extensions/HelperMacros.h> #include "Exception.h" @@ -7,8 +9,10 @@ #include "prefs.h" #include "Socket.h" #include "Option.h" -#include "MockBtContext.h" +#include "DownloadContext.h" #include "FileEntry.h" +#include "array_fun.h" +#include "bittorrent_helper.h" namespace aria2 { @@ -18,7 +22,7 @@ class MSEHandshakeTest:public CppUnit::TestFixture { CPPUNIT_TEST(testHandshake); CPPUNIT_TEST_SUITE_END(); private: - SharedHandle<MockBtContext> _btctx; + SharedHandle<DownloadContext> _dctx; void doHandshake(const SharedHandle<MSEHandshake>& initiator, const SharedHandle<MSEHandshake>& receiver); @@ -26,10 +30,13 @@ private: public: void setUp() { - _btctx.reset(new MockBtContext()); + _dctx.reset(new DownloadContext()); unsigned char infoHash[20]; memset(infoHash, 0, sizeof(infoHash)); - _btctx->setInfoHash(infoHash); + BDE torrentAttrs = BDE::dict(); + torrentAttrs[bittorrent::INFO_HASH] = + std::string(&infoHash[0], &infoHash[arrayLength(infoHash)]); + _dctx->setAttribute(bittorrent::BITTORRENT, torrentAttrs); } void testHandshake(); @@ -67,13 +74,13 @@ void MSEHandshakeTest::doHandshake(const SharedHandle<MSEHandshake>& initiator, receiver->sendPublicKey(); while(!initiator->receivePublicKey()); - initiator->initCipher(_btctx->getInfoHash()); + initiator->initCipher(bittorrent::getInfoHash(_dctx)); initiator->sendInitiatorStep2(); while(!receiver->findReceiverHashMarker()); - std::deque<SharedHandle<BtContext> > btContexts; - btContexts.push_back(_btctx); - while(!receiver->receiveReceiverHashAndPadCLength(btContexts)); + std::deque<SharedHandle<DownloadContext> > contexts; + contexts.push_back(_dctx); + while(!receiver->receiveReceiverHashAndPadCLength(contexts)); while(!receiver->receivePad()); while(!receiver->receiveReceiverIALength()); while(!receiver->receiveReceiverIA()); diff --git a/test/Makefile.am b/test/Makefile.am index 18e03245..95143fdb 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -13,7 +13,6 @@ aria2c_SOURCES = AllTest.cc\ GrowSegmentTest.cc\ SingleFileAllocationIteratorTest.cc\ DefaultBtProgressInfoFileTest.cc\ - SingleFileDownloadContextTest.cc\ RequestGroupTest.cc\ PStringBuildVisitorTest.cc\ ParameterizedStringParserTest.cc\ @@ -71,8 +70,7 @@ aria2c_SOURCES = AllTest.cc\ a2algoTest.cc\ bitfieldTest.cc\ BDETest.cc\ - DownloadContextTest.cc\ - MockDownloadContext.h + DownloadContextTest.cc if ENABLE_XML_RPC aria2c_SOURCES += XmlRpcRequestParserControllerTest.cc\ @@ -118,7 +116,6 @@ aria2c_SOURCES += BtAllowedFastMessageTest.cc\ BtUnchokeMessageTest.cc\ DefaultPieceStorageTest.cc\ DefaultBtAnnounceTest.cc\ - DefaultBtContextTest.cc\ DefaultBtMessageDispatcherTest.cc\ DefaultBtRequestFactoryTest.cc\ MockBtMessage.h\ @@ -184,7 +181,8 @@ aria2c_SOURCES += BtAllowedFastMessageTest.cc\ MockExtensionMessage.h\ MockExtensionMessageFactory.h\ MockPieceStorage.h\ - BencodeTest.cc + BencodeTest.cc\ + BittorrentHelperTest.cc endif # ENABLE_BITTORRENT if ENABLE_METALINK diff --git a/test/Makefile.in b/test/Makefile.in index a11a66bb..44249337 100644 --- a/test/Makefile.in +++ b/test/Makefile.in @@ -64,7 +64,6 @@ check_PROGRAMS = $(am__EXEEXT_1) @ENABLE_BITTORRENT_TRUE@ BtUnchokeMessageTest.cc\ @ENABLE_BITTORRENT_TRUE@ DefaultPieceStorageTest.cc\ @ENABLE_BITTORRENT_TRUE@ DefaultBtAnnounceTest.cc\ -@ENABLE_BITTORRENT_TRUE@ DefaultBtContextTest.cc\ @ENABLE_BITTORRENT_TRUE@ DefaultBtMessageDispatcherTest.cc\ @ENABLE_BITTORRENT_TRUE@ DefaultBtRequestFactoryTest.cc\ @ENABLE_BITTORRENT_TRUE@ MockBtMessage.h\ @@ -130,7 +129,8 @@ check_PROGRAMS = $(am__EXEEXT_1) @ENABLE_BITTORRENT_TRUE@ MockExtensionMessage.h\ @ENABLE_BITTORRENT_TRUE@ MockExtensionMessageFactory.h\ @ENABLE_BITTORRENT_TRUE@ MockPieceStorage.h\ -@ENABLE_BITTORRENT_TRUE@ BencodeTest.cc +@ENABLE_BITTORRENT_TRUE@ BencodeTest.cc\ +@ENABLE_BITTORRENT_TRUE@ BittorrentHelperTest.cc @ENABLE_METALINK_TRUE@am__append_7 = MetalinkerTest.cc\ @ENABLE_METALINK_TRUE@ MetalinkEntryTest.cc\ @@ -175,8 +175,7 @@ am__aria2c_SOURCES_DIST = AllTest.cc TestUtil.cc TestUtil.h \ SequenceTest.cc a2functionalTest.cc FileEntryTest.cc \ PieceTest.cc SegmentTest.cc GrowSegmentTest.cc \ SingleFileAllocationIteratorTest.cc \ - DefaultBtProgressInfoFileTest.cc \ - SingleFileDownloadContextTest.cc RequestGroupTest.cc \ + DefaultBtProgressInfoFileTest.cc RequestGroupTest.cc \ PStringBuildVisitorTest.cc ParameterizedStringParserTest.cc \ UtilTest.cc AlphaNumberDecoratorTest.cc UriListParserTest.cc \ HttpHeaderProcessorTest.cc RequestTest.cc CookieParserTest.cc \ @@ -201,7 +200,7 @@ am__aria2c_SOURCES_DIST = AllTest.cc TestUtil.cc TestUtil.h \ PieceStatManTest.cc InOrderPieceSelector.h \ LongestSequencePieceSelectorTest.cc a2algoTest.cc \ bitfieldTest.cc BDETest.cc DownloadContextTest.cc \ - MockDownloadContext.h XmlRpcRequestParserControllerTest.cc \ + XmlRpcRequestParserControllerTest.cc \ XmlRpcRequestProcessorTest.cc XmlRpcMethodTest.cc \ FallocFileAllocationIteratorTest.cc GZipDecoderTest.cc \ Sqlite3MozCookieParserTest.cc MessageDigestHelperTest.cc \ @@ -216,7 +215,7 @@ am__aria2c_SOURCES_DIST = AllTest.cc TestUtil.cc TestUtil.h \ BtRejectMessageTest.cc BtRequestMessageTest.cc \ BtSuggestPieceMessageTest.cc BtUnchokeMessageTest.cc \ DefaultPieceStorageTest.cc DefaultBtAnnounceTest.cc \ - DefaultBtContextTest.cc DefaultBtMessageDispatcherTest.cc \ + DefaultBtMessageDispatcherTest.cc \ DefaultBtRequestFactoryTest.cc MockBtMessage.h \ MockBtMessageDispatcher.h MockBtMessageFactory.h \ PeerListProcessorTest.cc AnnounceListTest.cc \ @@ -248,8 +247,8 @@ am__aria2c_SOURCES_DIST = AllTest.cc TestUtil.cc TestUtil.h \ MockDHTMessageFactory.h MockDHTTask.h MockDHTTaskFactory.h \ MockDHTTaskQueue.h MockExtensionMessage.h \ MockExtensionMessageFactory.h MockPieceStorage.h \ - BencodeTest.cc MetalinkerTest.cc MetalinkEntryTest.cc \ - Metalink2RequestGroupTest.cc \ + BencodeTest.cc BittorrentHelperTest.cc MetalinkerTest.cc \ + MetalinkEntryTest.cc Metalink2RequestGroupTest.cc \ MetalinkPostDownloadHandlerTest.cc MetalinkHelperTest.cc \ MetalinkParserControllerTest.cc MetalinkProcessorTest.cc @ENABLE_XML_RPC_TRUE@am__objects_1 = XmlRpcRequestParserControllerTest.$(OBJEXT) \ @@ -283,7 +282,6 @@ am__aria2c_SOURCES_DIST = AllTest.cc TestUtil.cc TestUtil.h \ @ENABLE_BITTORRENT_TRUE@ BtUnchokeMessageTest.$(OBJEXT) \ @ENABLE_BITTORRENT_TRUE@ DefaultPieceStorageTest.$(OBJEXT) \ @ENABLE_BITTORRENT_TRUE@ DefaultBtAnnounceTest.$(OBJEXT) \ -@ENABLE_BITTORRENT_TRUE@ DefaultBtContextTest.$(OBJEXT) \ @ENABLE_BITTORRENT_TRUE@ DefaultBtMessageDispatcherTest.$(OBJEXT) \ @ENABLE_BITTORRENT_TRUE@ DefaultBtRequestFactoryTest.$(OBJEXT) \ @ENABLE_BITTORRENT_TRUE@ PeerListProcessorTest.$(OBJEXT) \ @@ -331,7 +329,8 @@ am__aria2c_SOURCES_DIST = AllTest.cc TestUtil.cc TestUtil.h \ @ENABLE_BITTORRENT_TRUE@ ARC4Test.$(OBJEXT) \ @ENABLE_BITTORRENT_TRUE@ MSEHandshakeTest.$(OBJEXT) \ @ENABLE_BITTORRENT_TRUE@ DHTUtilTest.$(OBJEXT) \ -@ENABLE_BITTORRENT_TRUE@ BencodeTest.$(OBJEXT) +@ENABLE_BITTORRENT_TRUE@ BencodeTest.$(OBJEXT) \ +@ENABLE_BITTORRENT_TRUE@ BittorrentHelperTest.$(OBJEXT) @ENABLE_METALINK_TRUE@am__objects_7 = MetalinkerTest.$(OBJEXT) \ @ENABLE_METALINK_TRUE@ MetalinkEntryTest.$(OBJEXT) \ @ENABLE_METALINK_TRUE@ Metalink2RequestGroupTest.$(OBJEXT) \ @@ -347,7 +346,6 @@ am_aria2c_OBJECTS = AllTest.$(OBJEXT) TestUtil.$(OBJEXT) \ GrowSegmentTest.$(OBJEXT) \ SingleFileAllocationIteratorTest.$(OBJEXT) \ DefaultBtProgressInfoFileTest.$(OBJEXT) \ - SingleFileDownloadContextTest.$(OBJEXT) \ RequestGroupTest.$(OBJEXT) PStringBuildVisitorTest.$(OBJEXT) \ ParameterizedStringParserTest.$(OBJEXT) UtilTest.$(OBJEXT) \ AlphaNumberDecoratorTest.$(OBJEXT) UriListParserTest.$(OBJEXT) \ @@ -582,8 +580,7 @@ aria2c_SOURCES = AllTest.cc TestUtil.cc TestUtil.h SocketCoreTest.cc \ a2functionalTest.cc FileEntryTest.cc PieceTest.cc \ SegmentTest.cc GrowSegmentTest.cc \ SingleFileAllocationIteratorTest.cc \ - DefaultBtProgressInfoFileTest.cc \ - SingleFileDownloadContextTest.cc RequestGroupTest.cc \ + DefaultBtProgressInfoFileTest.cc RequestGroupTest.cc \ PStringBuildVisitorTest.cc ParameterizedStringParserTest.cc \ UtilTest.cc AlphaNumberDecoratorTest.cc UriListParserTest.cc \ HttpHeaderProcessorTest.cc RequestTest.cc CookieParserTest.cc \ @@ -608,9 +605,9 @@ aria2c_SOURCES = AllTest.cc TestUtil.cc TestUtil.h SocketCoreTest.cc \ PieceStatManTest.cc InOrderPieceSelector.h \ LongestSequencePieceSelectorTest.cc a2algoTest.cc \ bitfieldTest.cc BDETest.cc DownloadContextTest.cc \ - MockDownloadContext.h $(am__append_1) $(am__append_2) \ - $(am__append_3) $(am__append_4) $(am__append_5) \ - $(am__append_6) $(am__append_7) + $(am__append_1) $(am__append_2) $(am__append_3) \ + $(am__append_4) $(am__append_5) $(am__append_6) \ + $(am__append_7) #aria2c_CXXFLAGS = ${CPPUNIT_CFLAGS} -I../src -I../lib -Wall -D_FILE_OFFSET_BITS=64 #aria2c_LDFLAGS = ${CPPUNIT_LIBS} @@ -712,6 +709,7 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/Base64Test.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/BencodeTest.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/BitfieldManTest.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/BittorrentHelperTest.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/BtAllowedFastMessageTest.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/BtBitfieldMessageTest.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/BtCancelMessageTest.Po@am__quote@ @@ -764,7 +762,6 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/DHTUtilTest.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/DefaultAuthResolverTest.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/DefaultBtAnnounceTest.Po@am__quote@ -@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/DefaultBtContextTest.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/DefaultBtMessageDispatcherTest.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/DefaultBtMessageFactoryTest.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/DefaultBtProgressInfoFileTest.Po@am__quote@ @@ -836,7 +833,6 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/SignatureTest.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/SimpleDNSCacheTest.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/SingleFileAllocationIteratorTest.Po@am__quote@ -@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/SingleFileDownloadContextTest.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/SingletonHolderTest.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/SocketCoreTest.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/SpeedCalcTest.Po@am__quote@ diff --git a/test/Metalink2RequestGroupTest.cc b/test/Metalink2RequestGroupTest.cc index 1aab6af8..49ad2932 100644 --- a/test/Metalink2RequestGroupTest.cc +++ b/test/Metalink2RequestGroupTest.cc @@ -1,11 +1,14 @@ #include "Metalink2RequestGroup.h" -#include "SingleFileDownloadContext.h" + +#include <algorithm> + +#include <cppunit/extensions/HelperMacros.h> + +#include "DownloadContext.h" #include "prefs.h" #include "Option.h" #include "RequestGroup.h" #include "FileEntry.h" -#include <algorithm> -#include <cppunit/extensions/HelperMacros.h> namespace aria2 { @@ -47,9 +50,7 @@ void Metalink2RequestGroupTest::testGenerate() CPPUNIT_ASSERT_EQUAL (std::string("http://httphost/aria2-0.5.2.tar.bz2"), uris[1]); - SharedHandle<SingleFileDownloadContext> dctx - (dynamic_pointer_cast<SingleFileDownloadContext> - (rg->getDownloadContext())); + const SharedHandle<DownloadContext>& dctx = rg->getDownloadContext(); CPPUNIT_ASSERT(!dctx.isNull()); CPPUNIT_ASSERT_EQUAL((uint64_t)0ULL, dctx->getTotalLength()); @@ -70,9 +71,7 @@ void Metalink2RequestGroupTest::testGenerate() rg->getURIs(uris); CPPUNIT_ASSERT_EQUAL((size_t)2, uris.size()); - SharedHandle<SingleFileDownloadContext> dctx - (dynamic_pointer_cast<SingleFileDownloadContext> - (rg->getDownloadContext())); + const SharedHandle<DownloadContext>& dctx = rg->getDownloadContext(); CPPUNIT_ASSERT(!dctx.isNull()); CPPUNIT_ASSERT_EQUAL(std::string("/tmp"), dctx->getDir()); @@ -95,9 +94,7 @@ void Metalink2RequestGroupTest::testGenerate() CPPUNIT_ASSERT_EQUAL((size_t)1, uris.size()); CPPUNIT_ASSERT_EQUAL (std::string("http://host/torrent-http.integrated.torrent"), uris[0]); - SharedHandle<SingleFileDownloadContext> dctx - (dynamic_pointer_cast<SingleFileDownloadContext> - (rg->getDownloadContext())); + const SharedHandle<DownloadContext>& dctx = rg->getDownloadContext(); CPPUNIT_ASSERT(!dctx.isNull()); // PREF_DIR has no effect for internal torrent file download @@ -118,9 +115,7 @@ void Metalink2RequestGroupTest::testGenerate() CPPUNIT_ASSERT_EQUAL (std::string("http://host/torrent-http.integrated"), uris[0]); - SharedHandle<SingleFileDownloadContext> dctx - (dynamic_pointer_cast<SingleFileDownloadContext> - (rg->getDownloadContext())); + const SharedHandle<DownloadContext>& dctx = rg->getDownloadContext(); CPPUNIT_ASSERT(!dctx.isNull()); CPPUNIT_ASSERT_EQUAL(std::string("/tmp"), dctx->getDir()); diff --git a/test/MetalinkPostDownloadHandlerTest.cc b/test/MetalinkPostDownloadHandlerTest.cc index 15ec8f71..8e9abbdc 100644 --- a/test/MetalinkPostDownloadHandlerTest.cc +++ b/test/MetalinkPostDownloadHandlerTest.cc @@ -1,9 +1,11 @@ #include "MetalinkPostDownloadHandler.h" + +#include <cppunit/extensions/HelperMacros.h> + #include "RequestGroup.h" #include "Option.h" -#include "SingleFileDownloadContext.h" +#include "DownloadContext.h" #include "FileEntry.h" -#include <cppunit/extensions/HelperMacros.h> namespace aria2 { @@ -32,8 +34,8 @@ CPPUNIT_TEST_SUITE_REGISTRATION( MetalinkPostDownloadHandlerTest ); void MetalinkPostDownloadHandlerTest::testCanHandle_extension() { - SharedHandle<SingleFileDownloadContext> dctx - (new SingleFileDownloadContext(0, 0, "test.metalink")); + SharedHandle<DownloadContext> dctx + (new DownloadContext(0, 0, "test.metalink")); RequestGroup rg(_option, std::deque<std::string>()); rg.setDownloadContext(dctx); @@ -41,15 +43,14 @@ void MetalinkPostDownloadHandlerTest::testCanHandle_extension() CPPUNIT_ASSERT(handler.canHandle(&rg)); - dctx->setFilename("test.metalink2"); + dctx->getFirstFileEntry()->setPath("test.metalink2"); CPPUNIT_ASSERT(!handler.canHandle(&rg)); } void MetalinkPostDownloadHandlerTest::testCanHandle_contentType() { - SharedHandle<SingleFileDownloadContext> dctx - (new SingleFileDownloadContext(0, 0, "test")); - dctx->setContentType("application/metalink+xml"); + SharedHandle<DownloadContext> dctx(new DownloadContext(0, 0, "test")); + dctx->getFirstFileEntry()->setContentType("application/metalink+xml"); RequestGroup rg(_option, std::deque<std::string>()); rg.setDownloadContext(dctx); @@ -57,14 +58,14 @@ void MetalinkPostDownloadHandlerTest::testCanHandle_contentType() CPPUNIT_ASSERT(handler.canHandle(&rg)); - dctx->setContentType("application/octet-stream"); + dctx->getFirstFileEntry()->setContentType("application/octet-stream"); CPPUNIT_ASSERT(!handler.canHandle(&rg)); } void MetalinkPostDownloadHandlerTest::testGetNextRequestGroups() { - SharedHandle<SingleFileDownloadContext> dctx - (new SingleFileDownloadContext(1024, 0, "test.xml")); + SharedHandle<DownloadContext> dctx + (new DownloadContext(1024, 0, "test.xml")); RequestGroup rg(_option, std::deque<std::string>()); rg.setDownloadContext(dctx); rg.initPieceStorage(); diff --git a/test/MockBtContext.h b/test/MockBtContext.h deleted file mode 100644 index 3a5ed7ea..00000000 --- a/test/MockBtContext.h +++ /dev/null @@ -1,178 +0,0 @@ -#ifndef _D_MOCK_BT_CONTEXT_H_ -#define _D_MOCK_BT_CONTEXT_H_ - -#include "BtContext.h" -#include "Util.h" -#include "AnnounceTier.h" -#include "messageDigest.h" -#include <cstring> - -namespace aria2 { - -class MockBtContext : public BtContext { -private: - unsigned char infoHash[20]; - std::string _infoHashString; - std::deque<std::string> pieceHashes; - uint64_t totalLength; - FILE_MODE fileMode; - std::string name; - size_t pieceLength; - size_t numPieces; - unsigned char peerId[20]; - std::deque<SharedHandle<FileEntry> > fileEntries; - std::deque<SharedHandle<AnnounceTier> > announceTiers; - std::deque<std::pair<std::string, uint16_t> > _nodes; - std::deque<size_t> fastSet; -public: - MockBtContext():totalLength(0), - pieceLength(0), - numPieces(0) {} - - virtual ~MockBtContext() {} - - virtual const unsigned char* getInfoHash() const { - return infoHash; - } - - void setInfoHash(const unsigned char* infoHash) { - memcpy(this->infoHash, infoHash, sizeof(this->infoHash)); - _infoHashString = Util::toHex(this->infoHash, sizeof(this->infoHash)); - } - - virtual size_t getInfoHashLength() const { - return sizeof(infoHash); - } - - virtual const std::string& getInfoHashAsString() const { - return _infoHashString; - } - - virtual const std::string& getPieceHash(size_t index) const { - return pieceHashes.at(index); - } - - virtual const std::deque<std::string>& getPieceHashes() const { - return pieceHashes; - } - - void addPieceHash(const std::string& pieceHash) { - pieceHashes.push_back(pieceHash); - } - - virtual uint64_t getTotalLength() const { - return totalLength; - } - - virtual bool knowsTotalLength() const - { - return true; - } - - void setTotalLength(uint64_t length) { - this->totalLength = length; - } - - virtual FILE_MODE getFileMode() const { - return fileMode; - } - - void setFileMode(FILE_MODE fileMode) { - this->fileMode = fileMode; - } - - virtual std::deque<SharedHandle<FileEntry> > getFileEntries() const { - return fileEntries; - } - - void addFileEntry(const SharedHandle<FileEntry>& fileEntry) { - fileEntries.push_back(fileEntry); - } - - virtual const std::deque<SharedHandle<AnnounceTier> >& - getAnnounceTiers() const - { - return announceTiers; - } - - void addAnnounceTier(const SharedHandle<AnnounceTier>& announceTier) { - announceTiers.push_back(announceTier); - } - - virtual void load(const std::string& torrentFile, - const std::string& overrideName = "") {} - - virtual const std::string& getName() const { - return name; - } - - void setName(const std::string& name) { - this->name = name; - } - - virtual size_t getPieceLength() const { - return pieceLength; - } - - void setPieceLength(size_t pieceLength) { - this->pieceLength = pieceLength; - } - - virtual size_t getNumPieces() const { - return numPieces; - } - - void setNumPieces(size_t numPieces) { - this->numPieces = numPieces; - } - - virtual const unsigned char* getPeerId() { - return peerId; - } - - void setPeerId(const unsigned char* peerId) { - memcpy(this->peerId, peerId, sizeof(this->peerId)); - } - - virtual void computeFastSet - (std::deque<size_t>& fastSet, const std::string& ipaddr, size_t fastSetSize) - { - fastSet.insert(fastSet.end(), this->fastSet.begin(), this->fastSet.end()); - } - - void setFastSet(const std::deque<size_t>& fastSet) - { - this->fastSet = fastSet; - } - - virtual const std::string& getPieceHashAlgo() const - { - return MessageDigestContext::SHA1; - } - - virtual std::string getActualBasePath() const - { - return _dir+"/"+name; - } - - virtual RequestGroup* getOwnerRequestGroup() - { - return 0; - } - - virtual std::deque<std::pair<std::string, uint16_t> >& getNodes() - { - return _nodes; - } - - void setNodes(const std::deque<std::pair<std::string, uint16_t> >& nodes) - { - _nodes = nodes; - } - - virtual void setFileFilter(IntSequence seq) {} -}; - -} // namespace aria2 - -#endif // _D_MOCK_BT_CONTEXT_H_ diff --git a/test/MockDHTTaskFactory.h b/test/MockDHTTaskFactory.h index 960ccc48..31228d13 100644 --- a/test/MockDHTTaskFactory.h +++ b/test/MockDHTTaskFactory.h @@ -35,7 +35,7 @@ public: } virtual SharedHandle<DHTTask> - createPeerLookupTask(const SharedHandle<BtContext>& ctx, + createPeerLookupTask(const SharedHandle<DownloadContext>& ctx, const SharedHandle<BtRuntime>& btRuntime, const SharedHandle<PeerStorage>& peerStorage) { diff --git a/test/MockDownloadContext.h b/test/MockDownloadContext.h deleted file mode 100644 index b01fc8c0..00000000 --- a/test/MockDownloadContext.h +++ /dev/null @@ -1,67 +0,0 @@ -#ifndef _D_MOCK_DOWNLOAD_CONTEXT_H_ -#define _D_MOCK_DOWNLOAD_CONTEXT_H_ - -#include "DownloadContext.h" -#include "A2STR.h" - -namespace aria2 { - -class MockDownloadContext:public DownloadContext -{ -private: - std::deque<std::string> _pieceHashes; -public: - virtual const std::string& getPieceHash(size_t index) const - { - return A2STR::NIL; - } - - virtual const std::deque<std::string>& getPieceHashes() const - { - return _pieceHashes; - } - - virtual uint64_t getTotalLength() const - { - return 0; - } - - virtual bool knowsTotalLength() const - { - return false; - } - - virtual FILE_MODE getFileMode() const - { - return MULTI; - } - - virtual size_t getPieceLength() const - { - return 0; - } - - virtual size_t getNumPieces() const - { - return 0; - } - - virtual const std::string& getPieceHashAlgo() const - { - return A2STR::NIL; - } - - virtual std::string getActualBasePath() const - { - return A2STR::NIL; - } - - void addFileEntry(const SharedHandle<FileEntry>& fileEntry) - { - _fileEntries.push_back(fileEntry); - } -}; - -} // namespace aria2 - -#endif // _D_MOCK_DOWNLOAD_CONTEXT_H_ diff --git a/test/MockPieceStorage.h b/test/MockPieceStorage.h index 185498eb..f64e93f7 100644 --- a/test/MockPieceStorage.h +++ b/test/MockPieceStorage.h @@ -64,7 +64,8 @@ public: #endif // ENABLE_BITTORRENT - virtual SharedHandle<Piece> getMissingPiece() + virtual SharedHandle<Piece> getSparseMissingUnusedPiece + (const unsigned char* ignoreBitfield, size_t length) { return SharedHandle<Piece>(new Piece()); } diff --git a/test/MultiDiskAdaptorTest.cc b/test/MultiDiskAdaptorTest.cc index e491e274..e1b6f18c 100644 --- a/test/MultiDiskAdaptorTest.cc +++ b/test/MultiDiskAdaptorTest.cc @@ -44,7 +44,7 @@ public: CPPUNIT_TEST_SUITE_REGISTRATION( MultiDiskAdaptorTest ); -std::deque<SharedHandle<FileEntry> > createEntries() { +std::vector<SharedHandle<FileEntry> > createEntries() { SharedHandle<FileEntry> array[] = { SharedHandle<FileEntry>(new FileEntry("file0.txt", 0, 0)), SharedHandle<FileEntry>(new FileEntry("file1.txt", 15, 0)), @@ -68,9 +68,9 @@ std::deque<SharedHandle<FileEntry> > createEntries() { // *** file6 // |file7 // ** file8 - std::deque<SharedHandle<FileEntry> > entries(&array[0], - &array[arrayLength(array)]); - for(std::deque<SharedHandle<FileEntry> >::const_iterator i = entries.begin(); + std::vector<SharedHandle<FileEntry> > entries(&array[0], + &array[arrayLength(array)]); + for(std::vector<SharedHandle<FileEntry> >::const_iterator i = entries.begin(); i != entries.end(); ++i) { File((*i)->getPath()).remove(); } @@ -80,12 +80,12 @@ std::deque<SharedHandle<FileEntry> > createEntries() { void MultiDiskAdaptorTest::testResetDiskWriterEntries() { { - std::deque<SharedHandle<FileEntry> > fileEntries = createEntries(); - adaptor->setFileEntries(fileEntries); + std::vector<SharedHandle<FileEntry> > fileEntries = createEntries(); + adaptor->setFileEntries(fileEntries.begin(), fileEntries.end()); // In openFile(), resetDiskWriterEntries() are called. adaptor->openFile(); - std::deque<SharedHandle<DiskWriterEntry> > entries = + std::vector<SharedHandle<DiskWriterEntry> > entries = adaptor->getDiskWriterEntries(); CPPUNIT_ASSERT(!entries[0]->getDiskWriter().isNull()); CPPUNIT_ASSERT(!entries[1]->getDiskWriter().isNull()); @@ -97,13 +97,13 @@ void MultiDiskAdaptorTest::testResetDiskWriterEntries() adaptor->closeFile(); } { - std::deque<SharedHandle<FileEntry> > fileEntries = createEntries(); + std::vector<SharedHandle<FileEntry> > fileEntries = createEntries(); fileEntries[0]->setRequested(false); - adaptor->setFileEntries(fileEntries); + adaptor->setFileEntries(fileEntries.begin(), fileEntries.end()); // In openFile(), resetDiskWriterEntries() are called. adaptor->openFile(); - std::deque<SharedHandle<DiskWriterEntry> > entries = + std::vector<SharedHandle<DiskWriterEntry> > entries = adaptor->getDiskWriterEntries(); // Because entries[1] spans entries[0] CPPUNIT_ASSERT(!entries[0]->getDiskWriter().isNull()); @@ -116,14 +116,14 @@ void MultiDiskAdaptorTest::testResetDiskWriterEntries() adaptor->closeFile(); } { - std::deque<SharedHandle<FileEntry> > fileEntries = createEntries(); + std::vector<SharedHandle<FileEntry> > fileEntries = createEntries(); fileEntries[0]->setRequested(false); fileEntries[1]->setRequested(false); - adaptor->setFileEntries(fileEntries); + adaptor->setFileEntries(fileEntries.begin(), fileEntries.end()); // In openFile(), resetDiskWriterEntries() are called. adaptor->openFile(); - std::deque<SharedHandle<DiskWriterEntry> > entries = + std::vector<SharedHandle<DiskWriterEntry> > entries = adaptor->getDiskWriterEntries(); CPPUNIT_ASSERT(entries[0]->getDiskWriter().isNull()); // Because entries[2] spans entries[1] @@ -137,13 +137,13 @@ void MultiDiskAdaptorTest::testResetDiskWriterEntries() adaptor->closeFile(); } { - std::deque<SharedHandle<FileEntry> > fileEntries = createEntries(); + std::vector<SharedHandle<FileEntry> > fileEntries = createEntries(); fileEntries[3]->setRequested(false); - adaptor->setFileEntries(fileEntries); + adaptor->setFileEntries(fileEntries.begin(), fileEntries.end()); // In openFile(), resetDiskWriterEntries() are called. adaptor->openFile(); - std::deque<SharedHandle<DiskWriterEntry> > entries = + std::vector<SharedHandle<DiskWriterEntry> > entries = adaptor->getDiskWriterEntries(); CPPUNIT_ASSERT(!entries[0]->getDiskWriter().isNull()); CPPUNIT_ASSERT(!entries[1]->getDiskWriter().isNull()); @@ -157,13 +157,13 @@ void MultiDiskAdaptorTest::testResetDiskWriterEntries() adaptor->closeFile(); } { - std::deque<SharedHandle<FileEntry> > fileEntries = createEntries(); + std::vector<SharedHandle<FileEntry> > fileEntries = createEntries(); fileEntries[4]->setRequested(false); - adaptor->setFileEntries(fileEntries); + adaptor->setFileEntries(fileEntries.begin(), fileEntries.end()); // In openFile(), resetDiskWriterEntries() are called. adaptor->openFile(); - std::deque<SharedHandle<DiskWriterEntry> > entries = + std::vector<SharedHandle<DiskWriterEntry> > entries = adaptor->getDiskWriterEntries(); CPPUNIT_ASSERT(!entries[0]->getDiskWriter().isNull()); CPPUNIT_ASSERT(!entries[1]->getDiskWriter().isNull()); @@ -176,14 +176,14 @@ void MultiDiskAdaptorTest::testResetDiskWriterEntries() adaptor->closeFile(); } { - std::deque<SharedHandle<FileEntry> > fileEntries = createEntries(); + std::vector<SharedHandle<FileEntry> > fileEntries = createEntries(); fileEntries[3]->setRequested(false); fileEntries[4]->setRequested(false); - adaptor->setFileEntries(fileEntries); + adaptor->setFileEntries(fileEntries.begin(), fileEntries.end()); // In openFile(), resetDiskWriterEntries() are called. adaptor->openFile(); - std::deque<SharedHandle<DiskWriterEntry> > entries = + std::vector<SharedHandle<DiskWriterEntry> > entries = adaptor->getDiskWriterEntries(); CPPUNIT_ASSERT(!entries[0]->getDiskWriter().isNull()); CPPUNIT_ASSERT(!entries[1]->getDiskWriter().isNull()); @@ -195,15 +195,15 @@ void MultiDiskAdaptorTest::testResetDiskWriterEntries() adaptor->closeFile(); } { - std::deque<SharedHandle<FileEntry> > fileEntries = createEntries(); + std::vector<SharedHandle<FileEntry> > fileEntries = createEntries(); for(size_t i = 5; i < 9; ++i) { fileEntries[i]->setRequested(false); } - adaptor->setFileEntries(fileEntries); + adaptor->setFileEntries(fileEntries.begin(), fileEntries.end()); // In openFile(), resetDiskWriterEntries() are called. adaptor->openFile(); - std::deque<SharedHandle<DiskWriterEntry> > entries = + std::vector<SharedHandle<DiskWriterEntry> > entries = adaptor->getDiskWriterEntries(); CPPUNIT_ASSERT(!entries[0]->getDiskWriter().isNull()); CPPUNIT_ASSERT(!entries[1]->getDiskWriter().isNull()); @@ -215,13 +215,13 @@ void MultiDiskAdaptorTest::testResetDiskWriterEntries() adaptor->closeFile(); } { - std::deque<SharedHandle<FileEntry> > fileEntries = createEntries(); + std::vector<SharedHandle<FileEntry> > fileEntries = createEntries(); for(size_t i = 1; i < 9; ++i) { fileEntries[i]->setRequested(false); } - adaptor->setFileEntries(fileEntries); + adaptor->setFileEntries(fileEntries.begin(), fileEntries.end()); adaptor->openFile(); - std::deque<SharedHandle<DiskWriterEntry> > entries = + std::vector<SharedHandle<DiskWriterEntry> > entries = adaptor->getDiskWriterEntries(); CPPUNIT_ASSERT(!entries[0]->getDiskWriter().isNull()); CPPUNIT_ASSERT(entries[1]->getDiskWriter().isNull()); @@ -233,13 +233,13 @@ void MultiDiskAdaptorTest::testResetDiskWriterEntries() adaptor->closeFile(); } { - std::deque<SharedHandle<FileEntry> > fileEntries = createEntries(); + std::vector<SharedHandle<FileEntry> > fileEntries = createEntries(); for(size_t i = 2; i < 9; ++i) { fileEntries[i]->setRequested(false); } - adaptor->setFileEntries(fileEntries); + adaptor->setFileEntries(fileEntries.begin(), fileEntries.end()); adaptor->openFile(); - std::deque<SharedHandle<DiskWriterEntry> > entries = + std::vector<SharedHandle<DiskWriterEntry> > entries = adaptor->getDiskWriterEntries(); CPPUNIT_ASSERT(!entries[0]->getDiskWriter().isNull()); CPPUNIT_ASSERT(!entries[1]->getDiskWriter().isNull()); @@ -253,14 +253,14 @@ void MultiDiskAdaptorTest::testResetDiskWriterEntries() adaptor->closeFile(); } { - std::deque<SharedHandle<FileEntry> > fileEntries = createEntries(); + std::vector<SharedHandle<FileEntry> > fileEntries = createEntries(); for(size_t i = 0; i < 6; ++i) { fileEntries[i]->setRequested(false); } fileEntries[8]->setRequested(false); - adaptor->setFileEntries(fileEntries); + adaptor->setFileEntries(fileEntries.begin(), fileEntries.end()); adaptor->openFile(); - std::deque<SharedHandle<DiskWriterEntry> > entries = + std::vector<SharedHandle<DiskWriterEntry> > entries = adaptor->getDiskWriterEntries(); CPPUNIT_ASSERT(entries[0]->getDiskWriter().isNull()); CPPUNIT_ASSERT(entries[1]->getDiskWriter().isNull()); @@ -290,8 +290,8 @@ void readFile(const std::string& filename, char* buf, int bufLength) { } void MultiDiskAdaptorTest::testWriteData() { - try { - adaptor->setFileEntries(createEntries()); + std::vector<SharedHandle<FileEntry> > fileEntries(createEntries()); + adaptor->setFileEntries(fileEntries.begin(), fileEntries.end()); adaptor->openFile(); std::string msg = "12345"; @@ -335,22 +335,18 @@ void MultiDiskAdaptorTest::testWriteData() { CPPUNIT_ASSERT_EQUAL(std::string("12"), std::string(buf)); CPPUNIT_ASSERT(File("file5.txt").isFile()); - - } catch(Exception& e) { - CPPUNIT_FAIL(e.stackTrace()); - } } void MultiDiskAdaptorTest::testReadData() { SharedHandle<FileEntry> entry1(new FileEntry("file1r.txt", 15, 0)); SharedHandle<FileEntry> entry2(new FileEntry("file2r.txt", 7, 15)); SharedHandle<FileEntry> entry3(new FileEntry("file3r.txt", 3, 22)); - std::deque<SharedHandle<FileEntry> > entries; + std::vector<SharedHandle<FileEntry> > entries; entries.push_back(entry1); entries.push_back(entry2); entries.push_back(entry3); - adaptor->setFileEntries(entries); + adaptor->setFileEntries(entries.begin(), entries.end()); adaptor->openFile(); unsigned char buf[128]; @@ -379,11 +375,11 @@ void MultiDiskAdaptorTest::testCutTrailingGarbage() for(size_t i = 0; i < arrayLength(entries); ++i) { createFile(entries[i]->getPath(), entries[i]->getLength()+100); } - std::deque<SharedHandle<FileEntry> > fileEntries + std::vector<SharedHandle<FileEntry> > fileEntries (&entries[0], &entries[arrayLength(entries)]); MultiDiskAdaptor adaptor; - adaptor.setFileEntries(fileEntries); + adaptor.setFileEntries(fileEntries.begin(), fileEntries.end()); adaptor.setMaxOpenFiles(1); adaptor.setPieceLength(1); @@ -408,11 +404,11 @@ void MultiDiskAdaptorTest::testSize() for(size_t i = 0; i < arrayLength(entries); ++i) { createFile(entries[i]->getPath(), entries[i]->getLength()); } - std::deque<SharedHandle<FileEntry> > fileEntries + std::vector<SharedHandle<FileEntry> > fileEntries (&entries[0], &entries[arrayLength(entries)]); MultiDiskAdaptor adaptor; - adaptor.setFileEntries(fileEntries); + adaptor.setFileEntries(fileEntries.begin(), fileEntries.end()); adaptor.setMaxOpenFiles(1); adaptor.setPieceLength(1); @@ -438,10 +434,10 @@ void MultiDiskAdaptorTest::testUtime() entries[2]->setRequested(false); - std::deque<SharedHandle<FileEntry> > fileEntries + std::vector<SharedHandle<FileEntry> > fileEntries (&entries[0], &entries[arrayLength(entries)]); MultiDiskAdaptor adaptor; - adaptor.setFileEntries(fileEntries); + adaptor.setFileEntries(fileEntries.begin(), fileEntries.end()); time_t atime = (time_t) 100000; time_t mtime = (time_t) 200000; diff --git a/test/MultiFileAllocationIteratorTest.cc b/test/MultiFileAllocationIteratorTest.cc index 0c8e75e6..22f26517 100644 --- a/test/MultiFileAllocationIteratorTest.cc +++ b/test/MultiFileAllocationIteratorTest.cc @@ -64,8 +64,7 @@ void MultiFileAllocationIteratorTest::testMakeDiskWriterEntries() createFile(storeDir+std::string("/file4"), 0); SharedHandle<MultiDiskAdaptor> diskAdaptor(new MultiDiskAdaptor()); - diskAdaptor->setFileEntries - (std::deque<SharedHandle<FileEntry> >(&fs[0], &fs[arrayLength(fs)])); + diskAdaptor->setFileEntries(&fs[0], &fs[arrayLength(fs)]); diskAdaptor->setPieceLength(1024); diskAdaptor->openFile(); @@ -73,7 +72,8 @@ void MultiFileAllocationIteratorTest::testMakeDiskWriterEntries() (dynamic_pointer_cast<MultiFileAllocationIterator> (diskAdaptor->fileAllocationIterator())); - DiskWriterEntries entries = itr->getDiskWriterEntries(); + const std::deque<SharedHandle<DiskWriterEntry> >& entries = + itr->getDiskWriterEntries(); CPPUNIT_ASSERT_EQUAL((size_t)11, entries.size()); @@ -187,16 +187,16 @@ void MultiFileAllocationIteratorTest::testAllocate() offset)); fileEntry6->setRequested(false); - std::deque<SharedHandle<FileEntry> > fs; + std::vector<SharedHandle<FileEntry> > fs; fs.push_back(fileEntry1); fs.push_back(fileEntry2); fs.push_back(fileEntry3); fs.push_back(fileEntry4); fs.push_back(fileEntry5); fs.push_back(fileEntry6); - diskAdaptor->setFileEntries(fs); + diskAdaptor->setFileEntries(fs.begin(), fs.end()); - for(std::deque<SharedHandle<FileEntry> >::const_iterator i = fs.begin(); + for(std::vector<SharedHandle<FileEntry> >::const_iterator i = fs.begin(); i != fs.end(); ++i) { File((*i)->getPath()).remove(); } diff --git a/test/RequestGroupManTest.cc b/test/RequestGroupManTest.cc index d55c0364..ba83984d 100644 --- a/test/RequestGroupManTest.cc +++ b/test/RequestGroupManTest.cc @@ -5,7 +5,7 @@ #include <cppunit/extensions/HelperMacros.h> #include "prefs.h" -#include "SingleFileDownloadContext.h" +#include "DownloadContext.h" #include "RequestGroup.h" #include "Option.h" #include "DownloadResult.h" @@ -48,10 +48,10 @@ void RequestGroupManTest::testIsSameFileBeingDownloaded() SharedHandle<RequestGroup> rg1(new RequestGroup(_option, uris)); SharedHandle<RequestGroup> rg2(new RequestGroup(_option, uris)); - SharedHandle<SingleFileDownloadContext> dctx1 - (new SingleFileDownloadContext(0, 0, "aria2.tar.bz2")); - SharedHandle<SingleFileDownloadContext> dctx2 - (new SingleFileDownloadContext(0, 0, "aria2.tar.bz2")); + SharedHandle<DownloadContext> dctx1 + (new DownloadContext(0, 0, "aria2.tar.bz2")); + SharedHandle<DownloadContext> dctx2 + (new DownloadContext(0, 0, "aria2.tar.bz2")); rg1->setDownloadContext(dctx1); rg2->setDownloadContext(dctx2); @@ -64,7 +64,7 @@ void RequestGroupManTest::testIsSameFileBeingDownloaded() CPPUNIT_ASSERT(gm.isSameFileBeingDownloaded(rg1.get())); - dctx2->setFilename("aria2.tar.gz"); + dctx2->getFirstFileEntry()->setPath("aria2.tar.gz"); CPPUNIT_ASSERT(!gm.isSameFileBeingDownloaded(rg1.get())); diff --git a/test/RequestGroupTest.cc b/test/RequestGroupTest.cc index b19489e7..17ab9c02 100644 --- a/test/RequestGroupTest.cc +++ b/test/RequestGroupTest.cc @@ -4,7 +4,7 @@ #include "ServerHost.h" #include "Option.h" -#include "SingleFileDownloadContext.h" +#include "DownloadContext.h" #include "FileEntry.h" #include "PieceStorage.h" @@ -15,7 +15,7 @@ class RequestGroupTest : public CppUnit::TestFixture { CPPUNIT_TEST_SUITE(RequestGroupTest); CPPUNIT_TEST(testRegisterSearchRemove); CPPUNIT_TEST(testRemoveURIWhoseHostnameIs); - CPPUNIT_TEST(testGetFilePath); + CPPUNIT_TEST(testGetFirstFilePath); CPPUNIT_TEST(testCreateDownloadResult); CPPUNIT_TEST(testExtractURIResult); CPPUNIT_TEST_SUITE_END(); @@ -29,7 +29,7 @@ public: void testRegisterSearchRemove(); void testRemoveURIWhoseHostnameIs(); - void testGetFilePath(); + void testGetFirstFilePath(); void testCreateDownloadResult(); void testExtractURIResult(); }; @@ -81,26 +81,26 @@ void RequestGroupTest::testRemoveURIWhoseHostnameIs() rg.getRemainingUris()[0]); } -void RequestGroupTest::testGetFilePath() +void RequestGroupTest::testGetFirstFilePath() { - SharedHandle<SingleFileDownloadContext> ctx - (new SingleFileDownloadContext(1024, 1024, "/tmp/myfile")); + SharedHandle<DownloadContext> ctx + (new DownloadContext(1024, 1024, "/tmp/myfile")); std::deque<std::string> uris; RequestGroup group(_option, uris); group.setDownloadContext(ctx); - CPPUNIT_ASSERT_EQUAL(std::string("/tmp/myfile"), group.getFilePath()); + CPPUNIT_ASSERT_EQUAL(std::string("/tmp/myfile"), group.getFirstFilePath()); group.markInMemoryDownload(); - CPPUNIT_ASSERT_EQUAL(std::string("[MEMORY]myfile"), group.getFilePath()); + CPPUNIT_ASSERT_EQUAL(std::string("[MEMORY]myfile"), group.getFirstFilePath()); } void RequestGroupTest::testCreateDownloadResult() { - SharedHandle<SingleFileDownloadContext> ctx - (new SingleFileDownloadContext(1024, 1024*1024, "/tmp/myfile")); + SharedHandle<DownloadContext> ctx + (new DownloadContext(1024, 1024*1024, "/tmp/myfile")); //ctx->setDir("/tmp"); std::deque<std::string> uris; uris.push_back("http://first/file"); diff --git a/test/SegmentManTest.cc b/test/SegmentManTest.cc index 8f7eefd8..f8b5d3ff 100644 --- a/test/SegmentManTest.cc +++ b/test/SegmentManTest.cc @@ -5,7 +5,7 @@ #include "File.h" #include "prefs.h" #include "Util.h" -#include "SingleFileDownloadContext.h" +#include "DownloadContext.h" #include "UnknownLengthPieceStorage.h" #include "DefaultPieceStorage.h" #include "Segment.h" @@ -43,8 +43,8 @@ CPPUNIT_TEST_SUITE_REGISTRATION( SegmentManTest ); void SegmentManTest::testNullBitfield() { Option op; - SharedHandle<SingleFileDownloadContext> dctx - (new SingleFileDownloadContext(0, 0, "aria2.tar.bz2")); + SharedHandle<DownloadContext> dctx + (new DownloadContext(0, 0, "aria2.tar.bz2")); SharedHandle<UnknownLengthPieceStorage> ps (new UnknownLengthPieceStorage(dctx, &op)); SegmentMan segmentMan(&op, dctx, ps); @@ -68,8 +68,8 @@ void SegmentManTest::testCompleteSegment() Option op; size_t pieceLength = 1024*1024; uint64_t totalLength = 64*1024*1024; - SharedHandle<SingleFileDownloadContext> dctx - (new SingleFileDownloadContext(pieceLength, totalLength, "aria2.tar.bz2")); + SharedHandle<DownloadContext> dctx + (new DownloadContext(pieceLength, totalLength, "aria2.tar.bz2")); SharedHandle<DefaultPieceStorage> ps(new DefaultPieceStorage(dctx, &op)); SegmentMan segmentMan(&op, dctx, ps); @@ -94,8 +94,8 @@ void SegmentManTest::testGetPeerStat() Option op; size_t pieceLength = 1; uint64_t totalLength = 1; - SharedHandle<SingleFileDownloadContext> dctx - (new SingleFileDownloadContext(pieceLength, totalLength, "aria2.tar.bz2")); + SharedHandle<DownloadContext> dctx + (new DownloadContext(pieceLength, totalLength, "aria2.tar.bz2")); SharedHandle<PieceStorage> ps(new MockPieceStorage()); SegmentMan segmentMan(&op, dctx, ps); @@ -128,8 +128,8 @@ void SegmentManTest::testGetSegment_segmentForward() Option op; size_t pieceLength = 1; uint64_t totalLength = 1; - SharedHandle<SingleFileDownloadContext> dctx - (new SingleFileDownloadContext(pieceLength, totalLength, "aria2.tar.bz2")); + SharedHandle<DownloadContext> dctx + (new DownloadContext(pieceLength, totalLength, "aria2.tar.bz2")); SharedHandle<PieceStorage> ps(new DefaultPieceStorage(dctx, &op)); SegmentMan segmentMan(&op, dctx, ps); diff --git a/test/ShareRatioSeedCriteriaTest.cc b/test/ShareRatioSeedCriteriaTest.cc index 8b5d8a15..3197ed14 100644 --- a/test/ShareRatioSeedCriteriaTest.cc +++ b/test/ShareRatioSeedCriteriaTest.cc @@ -2,7 +2,7 @@ #include <cppunit/extensions/HelperMacros.h> -#include "MockBtContext.h" +#include "DownloadContext.h" #include "MockPeerStorage.h" #include "MockPieceStorage.h" #include "FileEntry.h" @@ -23,13 +23,7 @@ public: CPPUNIT_TEST_SUITE_REGISTRATION(ShareRatioSeedCriteriaTest); void ShareRatioSeedCriteriaTest::testEvaluate() { - std::string infoHash = "01234567890123456789"; - std::string infoHashString = - Util::toHex((const unsigned char*)infoHash.c_str(), infoHash.size()); - SharedHandle<MockBtContext> btContext(new MockBtContext()); - btContext->setTotalLength(1000000); - btContext->setInfoHash((const unsigned char*)infoHash.c_str()); - + SharedHandle<DownloadContext> dctx(new DownloadContext(1024*1024, 1000000)); SharedHandle<MockPeerStorage> peerStorage(new MockPeerStorage()); TransferStat stat; stat.setAllTimeUploadLength(1000000); @@ -38,7 +32,7 @@ void ShareRatioSeedCriteriaTest::testEvaluate() { SharedHandle<MockPieceStorage> pieceStorage(new MockPieceStorage()); pieceStorage->setCompletedLength(1000000); - ShareRatioSeedCriteria cri(1.0, btContext); + ShareRatioSeedCriteria cri(1.0, dctx); cri.setPeerStorage(peerStorage); cri.setPieceStorage(pieceStorage); @@ -47,7 +41,7 @@ void ShareRatioSeedCriteriaTest::testEvaluate() { cri.setRatio(2.0); CPPUNIT_ASSERT(!cri.evaluate()); // check div by zero - btContext->setTotalLength(0); + dctx->getFirstFileEntry()->setLength(0); CPPUNIT_ASSERT(!cri.evaluate()); } diff --git a/test/SingleFileDownloadContextTest.cc b/test/SingleFileDownloadContextTest.cc deleted file mode 100644 index a69836af..00000000 --- a/test/SingleFileDownloadContextTest.cc +++ /dev/null @@ -1,58 +0,0 @@ -#include "SingleFileDownloadContext.h" -#include "FileEntry.h" -#include <cppunit/extensions/HelperMacros.h> - -namespace aria2 { - -class SingleFileDownloadContextTest:public CppUnit::TestFixture { - - CPPUNIT_TEST_SUITE(SingleFileDownloadContextTest); - CPPUNIT_TEST(testGetPieceHash); - CPPUNIT_TEST(testGetNumPieces); - CPPUNIT_TEST(testGetActualBasePath); - CPPUNIT_TEST_SUITE_END(); -public: - SingleFileDownloadContextTest() {} - - void setUp() {} - - void testGetPieceHash(); - void testGetNumPieces(); - void testGetActualBasePath(); -}; - - -CPPUNIT_TEST_SUITE_REGISTRATION( SingleFileDownloadContextTest ); - -void SingleFileDownloadContextTest::testGetPieceHash() -{ - SingleFileDownloadContext ctx(0, 0, ""); - std::deque<std::string> pieceHashes; - pieceHashes.push_back("0000"); - pieceHashes.push_back("0001"); - pieceHashes.push_back("0002"); - ctx.setPieceHashes(pieceHashes); - CPPUNIT_ASSERT_EQUAL(std::string("0000"), ctx.getPieceHash(0)); - CPPUNIT_ASSERT_EQUAL(std::string(""), ctx.getPieceHash(3)); -} - -void SingleFileDownloadContextTest::testGetNumPieces() -{ - SingleFileDownloadContext ctx(345, 9889, ""); - CPPUNIT_ASSERT_EQUAL((size_t)29, ctx.getNumPieces()); -} - -void SingleFileDownloadContextTest::testGetActualBasePath() -{ - SingleFileDownloadContext ctx(0, 0, ""); - CPPUNIT_ASSERT_EQUAL(std::string(""), ctx.getActualBasePath()); - ctx.setFilename("aria2.tar.bz2"); - CPPUNIT_ASSERT_EQUAL(std::string("aria2.tar.bz2"), ctx.getActualBasePath()); - ctx.setUFilename("/t/aria.tar.bz2"); - CPPUNIT_ASSERT_EQUAL(std::string("/t/aria.tar.bz2"), ctx.getActualBasePath()); - ctx.setDir("/tmp"); - // See dir doesn't effect getActualBasePath(). - CPPUNIT_ASSERT_EQUAL(std::string("/t/aria.tar.bz2"), ctx.getActualBasePath()); -} - -} // namespace aria2 diff --git a/test/UtilTest.cc b/test/UtilTest.cc index 6f6f4cdf..ed6524e3 100644 --- a/test/UtilTest.cc +++ b/test/UtilTest.cc @@ -391,7 +391,7 @@ void UtilTest::testToStream() std::deque<SharedHandle<FileEntry> > entries; entries.push_back(f1); entries.push_back(f2); - Util::toStream(os, entries); + Util::toStream(entries.begin(), entries.end(), os); CPPUNIT_ASSERT_EQUAL( std::string("Files:\n" "idx|path/length\n" diff --git a/test/XmlRpcMethodTest.cc b/test/XmlRpcMethodTest.cc index cfc763b2..e8589d92 100644 --- a/test/XmlRpcMethodTest.cc +++ b/test/XmlRpcMethodTest.cc @@ -214,7 +214,7 @@ void XmlRpcMethodTest::testAddTorrent() SharedHandle<RequestGroup> group = _e->_requestGroupMan->findReservedGroup(1); CPPUNIT_ASSERT(!group.isNull()); CPPUNIT_ASSERT_EQUAL(std::string("/tmp/aria2-0.8.2.tar.bz2"), - group->getFilePath()); + group->getFirstFilePath()); CPPUNIT_ASSERT_EQUAL((size_t)1, group->getRemainingUris().size()); CPPUNIT_ASSERT_EQUAL(std::string("http://localhost/aria2-0.8.2.tar.bz2"), group->getRemainingUris()[0]); @@ -227,7 +227,7 @@ void XmlRpcMethodTest::testAddTorrent() XmlRpcResponse res = m.execute(req, _e.get()); CPPUNIT_ASSERT_EQUAL(0, res._code); CPPUNIT_ASSERT_EQUAL(std::string("/sink/aria2-0.8.2.tar.bz2"), - _e->_requestGroupMan->findReservedGroup(2)->getFilePath()); + _e->_requestGroupMan->findReservedGroup(2)->getFirstFilePath()); } } @@ -288,11 +288,11 @@ void XmlRpcMethodTest::testAddMetalink() SharedHandle<RequestGroup> tar = _e->_requestGroupMan->findReservedGroup(1); CPPUNIT_ASSERT(!tar.isNull()); CPPUNIT_ASSERT_EQUAL(std::string("/tmp/aria2-5.0.0.tar.bz2"), - tar->getFilePath()); + tar->getFirstFilePath()); SharedHandle<RequestGroup> deb = _e->_requestGroupMan->findReservedGroup(2); CPPUNIT_ASSERT(!deb.isNull()); CPPUNIT_ASSERT_EQUAL(std::string("/tmp/aria2-5.0.0.deb"), - deb->getFilePath()); + deb->getFirstFilePath()); } // with options BDE opt = BDE::dict(); @@ -302,7 +302,7 @@ void XmlRpcMethodTest::testAddMetalink() XmlRpcResponse res = m.execute(req, _e.get()); CPPUNIT_ASSERT_EQUAL(0, res._code); CPPUNIT_ASSERT_EQUAL(std::string("/sink/aria2-5.0.0.tar.bz2"), - _e->_requestGroupMan->findReservedGroup(3)->getFilePath()); + _e->_requestGroupMan->findReservedGroup(3)->getFirstFilePath()); } } @@ -342,7 +342,7 @@ void XmlRpcMethodTest::testAddMetalink_withPosition() CPPUNIT_ASSERT_EQUAL(std::string("/tmp/aria2-5.0.0.tar.bz2"), _e->_requestGroupMan->getReservedGroups()[0]-> - getFilePath()); + getFirstFilePath()); } #endif // ENABLE_METALINK |
