From 29374cd70c36a510aaf4b94654d1d7df17322234 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Tue, 21 Mar 2006 15:21:11 +0000 Subject: [PATCH] --- ChangeLog | 4 ++ README | 24 +++++++++++- src/MetaEntry.h | 1 - src/PeerAbstractCommand.cc | 4 +- src/PeerInteractionCommand.cc | 70 +---------------------------------- src/TorrentMan.cc | 8 ++-- src/TorrentMan.h | 2 +- test/TorrentManTest.cc | 26 ++++++------- 8 files changed, 50 insertions(+), 89 deletions(-) diff --git a/ChangeLog b/ChangeLog index 1c491187..52b196bd 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +2006-03-22 Tatsuhiro Tsujikawa + + * BitTorrent protocol support added. + 2006-03-17 Tatsuhiro Tsujikawa * SocketCore.cc: remove the assignment of addrinfo.ai_addr. diff --git a/README b/README index ba64745b..e42d531c 100644 --- a/README +++ b/README @@ -20,6 +20,7 @@ aria2 is in very early development stage. Currently it has following features: * Segmented download * Cookie support(currently aria2 ignores "expires") * It can run as a daemon process. +* BitTorrent protocol support 3. How to build --------------- @@ -30,4 +31,25 @@ The executable is aria2c in src directory. 4. SSL ------ -You need OpenSSL library(0.9.7b or higher) to enable HTTPS support. +You need OpenSSL library(0.9.7b or higher) to enable HTTPS and BitTorrent +support. + +5. BitTorrrent +-------------- +The filename of the downloaded file is determined as follows: + +single-file mode: + If "name" key is present in .torrent file, filename is the value of "name" + key. Otherwise, filename is the basename of .torrent file appended by + ".file". For example, .torrent file is "test.torrrent", then filename is + "test.torrent.file". + The directory to store the downloaded file can be specified by -d option. +multi-file mode: + The complete directory/file structure mentioned in .torrent file is + created. + The directory to store the top directory of downloaded files can be + specified by -d option. + +Note: -o option is used to change the filename of downloaded .torrent file. + +This version only supports compact peers list format. diff --git a/src/MetaEntry.h b/src/MetaEntry.h index 92176221..7ebe9020 100644 --- a/src/MetaEntry.h +++ b/src/MetaEntry.h @@ -22,7 +22,6 @@ #ifndef _D_META_ENTRY_H_ #define _D_META_ENTRY_H_ -//#include "MetaEntryVisitor.h" #include "common.h" class MetaEntryVisitor; diff --git a/src/PeerAbstractCommand.cc b/src/PeerAbstractCommand.cc index 6cbd615c..26d2d376 100644 --- a/src/PeerAbstractCommand.cc +++ b/src/PeerAbstractCommand.cc @@ -97,7 +97,8 @@ bool PeerAbstractCommand::execute() { onAbort(err); delete(err); return prepareForNextPeer(0); - } catch(DlRetryEx* err) { + } + /*catch(DlRetryEx* err) { e->logger->error(MSG_RESTARTING_DOWNLOAD, err, cuid); peer->tryCount++; bool isAbort = e->option->getAsInt(PREF_MAX_TRIES) != 0 && @@ -114,6 +115,7 @@ bool PeerAbstractCommand::execute() { return prepareForRetry(e->option->getAsInt(PREF_RETRY_WAIT)); } } + */ } // TODO this method removed when PeerBalancerCommand is implemented diff --git a/src/PeerInteractionCommand.cc b/src/PeerInteractionCommand.cc index 0d81fba7..1b5f60d9 100644 --- a/src/PeerInteractionCommand.cc +++ b/src/PeerInteractionCommand.cc @@ -102,7 +102,6 @@ bool PeerInteractionCommand::executeInternal() { } receiveMessage(); } - //detectTimeoutAndDuplicateBlock(); requestSlotMan->deleteTimedoutRequestSlot(piece); requestSlotMan->deleteCompletedRequestSlot(piece); sendInterest(); @@ -255,41 +254,6 @@ void PeerInteractionCommand::onGotWrongPiece() { e->torrentMan->updatePiece(piece); } -/* -const RequestSlot& PeerInteractionCommand::getRequestSlot(int index, int begin, int length) const { - for(RequestSlots::const_iterator itr = requestSlots.begin(); itr != requestSlots.end(); itr++) { - const RequestSlot& slot = *itr; - if(slot.index == index && slot.begin == begin && slot.length == length) { - return slot; - } - } - return RequestSlot::nullSlot; -} -*/ -/* -bool PeerInteractionCommand::deleteRequestSlot(const RequestSlot& slot) { - for(RequestSlots::iterator itr = requestSlots.begin(); itr != requestSlots.end(); itr++) { - if(slot.index == itr->index && slot.begin == itr->begin && slot.length == itr->length) { - requestSlots.erase(itr); - return true; - } - } - return false; -} -*/ -/* -void PeerInteractionCommand::deleteAllRequestSlot() { - if(!Piece::isNull(piece)) { - for(RequestSlots::const_iterator itr = requestSlots.begin(); itr != requestSlots.end(); itr++) { - if(itr->index == piece.getIndex()) { - piece.cancelBlock(itr->blockIndex); - } - } - e->torrentMan->updatePiece(piece); - } - requestSlots.clear(); -} -*/ // TODO this method removed when PeerBalancerCommand is implemented bool PeerInteractionCommand::prepareForNextPeer(int wait) { if(e->torrentMan->isPeerAvailable()) { @@ -306,38 +270,9 @@ bool PeerInteractionCommand::prepareForRetry(int wait) { e->commands.push(this); return false; } -/* -void PeerInteractionCommand::detectTimeoutAndDuplicateBlock() { - struct timeval now; - gettimeofday(&now, NULL); - for(RequestSlots::iterator itr = requestSlots.begin(); itr != requestSlots.end();) { - const RequestSlot& slot = *itr; - if(slot.isTimeout(120) || piece.hasBlock(slot.blockIndex)) { - e->logger->debug("CUID#%d - deleting requestslot blockIndex %d", cuid, slot.blockIndex); - if(slot.isTimeout(120)) { - e->logger->debug("CUID#%d - because of timeout", cuid); - } else { - e->logger->debug("CUID#%d - because of duplicate block", cuid); - } - piece.cancelBlock(slot.blockIndex); - e->torrentMan->updatePiece(piece); - // send cancel message - PendingMessage pendingMessage = - PendingMessage::createCancelMessage(piece.getIndex(), - slot.blockIndex*piece.getBlockLength(), - piece.getBlockLength(slot.blockIndex), - peerConnection); - pendingMessages.push_back(pendingMessage); - itr = requestSlots.erase(itr); - } else { - itr++; - } - } -} -*/ + Piece PeerInteractionCommand::getNewPieceAndSendInterest() { - Piece piece = e->torrentMan->getMissingPiece(peer->getBitfield(), - peer->getBitfieldLength()); + Piece piece = e->torrentMan->getMissingPiece(peer); if(Piece::isNull(piece)) { e->logger->debug("CUID#%d - try to send not-interested", cuid); PendingMessage pendingMessage(PeerMessage::NOT_INTERESTED, peerConnection); @@ -346,7 +281,6 @@ Piece PeerInteractionCommand::getNewPieceAndSendInterest() { e->logger->debug("CUID#%d - try to send interested", cuid); PendingMessage pendingMessage(PeerMessage::INTERESTED, peerConnection); pendingMessages.push_back(pendingMessage); - //piecefield = new BitfieldMan(16*1024, piece.length); } return piece; } diff --git a/src/TorrentMan.cc b/src/TorrentMan.cc index 2bd95b5f..688b9b7f 100644 --- a/src/TorrentMan.cc +++ b/src/TorrentMan.cc @@ -130,12 +130,12 @@ bool TorrentMan::isEndGame() const { return bitfield->countMissingBlock() <= END_GAME_PIECE_NUM; } -Piece TorrentMan::getMissingPiece(const unsigned char* peerBitfield, int length) { +Piece TorrentMan::getMissingPiece(const Peer* peer) { int index = -1; if(isEndGame()) { - index = bitfield->getMissingIndex(peerBitfield, length); + index = bitfield->getMissingIndex(peer->getBitfield(), peer->getBitfieldLength()); } else { - index = bitfield->getMissingUnusedIndex(peerBitfield, length); + index = bitfield->getMissingUnusedIndex(peer->getBitfield(), peer->getBitfieldLength()); } if(index == -1) { return Piece::nullPiece; @@ -308,7 +308,7 @@ void TorrentMan::setup(string metaInfoFile) { name = topName->toString(); } else { char* basec = strdup(metaInfoFile.c_str()); - name = string(basename(basec)); + name = string(basename(basec))+".file"; free(basec); } diff --git a/src/TorrentMan.h b/src/TorrentMan.h index 3041f3f8..5e9a38b9 100644 --- a/src/TorrentMan.h +++ b/src/TorrentMan.h @@ -119,7 +119,7 @@ public: bool isPeerAvailable() const; int deleteOldErrorPeers(int maxNum); - Piece getMissingPiece(const unsigned char* peerBitfield, int len); + Piece getMissingPiece(const Peer* peer); void completePiece(const Piece& piece); void cancelPiece(const Piece& piece); void updatePiece(const Piece& piece); diff --git a/test/TorrentManTest.cc b/test/TorrentManTest.cc index 41419bf0..b30bd746 100644 --- a/test/TorrentManTest.cc +++ b/test/TorrentManTest.cc @@ -33,11 +33,11 @@ CPPUNIT_TEST_SUITE_REGISTRATION( TorrentManTest ); Peers createPeers() { Peers peers; - Peer* peer1 = new Peer("192.168.0.1", 6881); + Peer* peer1 = new Peer("192.168.0.1", 6881, 512*1024, 5242870); peer1->entryId = 1; - Peer* peer2 = new Peer("192.168.0.2", 6881); + Peer* peer2 = new Peer("192.168.0.2", 6881, 512*1024, 5242870); peer2->entryId = 2; - Peer* peer3 = new Peer("192.168.0.3", 6881); + Peer* peer3 = new Peer("192.168.0.3", 6881, 512*1024, 5242870); peer3->entryId = 3; peers.push_back(peer1); peers.push_back(peer2); @@ -93,10 +93,10 @@ void TorrentManTest::testGetPeer() { TorrentMan tm; Peers peers = createPeers(); tm.updatePeers(peers); - CPPUNIT_ASSERT(tm.getPeer(1) != Peer::nullPeer); - CPPUNIT_ASSERT(tm.getPeer(2) != Peer::nullPeer); - CPPUNIT_ASSERT(tm.getPeer(3) != Peer::nullPeer); - CPPUNIT_ASSERT(tm.getPeer(4) == Peer::nullPeer); + CPPUNIT_ASSERT(tm.getPeer() != Peer::nullPeer); + CPPUNIT_ASSERT(tm.getPeer() != Peer::nullPeer); + CPPUNIT_ASSERT(tm.getPeer() != Peer::nullPeer); + CPPUNIT_ASSERT(tm.getPeer() == Peer::nullPeer); } void TorrentManTest::testGetMissingPiece() { @@ -108,12 +108,12 @@ void TorrentManTest::testGetMissingPiece() { unsigned char peerBitfield[2] = { 0xff, 0xff }; Piece piece1 = tm.getMissingPiece(peerBitfield, 2); - CPPUNIT_ASSERT_EQUAL(0, piece1.index); - CPPUNIT_ASSERT_EQUAL(512*1024, piece1.length); + CPPUNIT_ASSERT_EQUAL(0, piece1.getIndex()); + CPPUNIT_ASSERT_EQUAL(512*1024, piece1.getLength()); Piece piece2 = tm.getMissingPiece(peerBitfield, 2); - CPPUNIT_ASSERT_EQUAL(1, piece2.index); - CPPUNIT_ASSERT_EQUAL(512*1024, piece2.length); + CPPUNIT_ASSERT_EQUAL(1, piece2.getIndex()); + CPPUNIT_ASSERT_EQUAL(512*1024, piece2.getLength()); tm.completePiece(piece1); @@ -149,8 +149,8 @@ void TorrentManTest::testCancelPiece() { unsigned char peerBitfield[2] = { 0xff, 0xff }; Piece piece = tm.getMissingPiece(peerBitfield, 2); - CPPUNIT_ASSERT_EQUAL(0, piece.index); - CPPUNIT_ASSERT_EQUAL(512*1024, piece.length); + CPPUNIT_ASSERT_EQUAL(0, piece.getIndex()); + CPPUNIT_ASSERT_EQUAL(512*1024, piece.getLength()); tm.cancelPiece(piece); int len = tm.getBitfieldLength();