From 07e91896e1558f6beb10bcdc3e5c3ff38904317f Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Sun, 1 Oct 2006 11:29:14 +0000 Subject: [PATCH] 2006-10-01 Tatsuhiro Tsujikawa To add timeout to async name resolution: * src/AbstractCommand.h (nameResolveFinished): New function. This is not elegant way. It needs to be more refined. (nameResolverCheck): New variable. * src/AbstractCommand.cc (AbstractCommand): Added nameResolverCheck. (execute): Added the check whether the name resolution has finished. (setNameResolverCheck): Set nameResolverCheck to true. (disableNameResolverCheck): Set nameResolverCheck to false. (nameResolverFinished): New function. * src/FtpInitiateConnectionCommand.h (nameResolverFinished): New function. * src/HttpInitiateConnectionCommand.h (nameResolverFinished): New function. To add the support for a non-compact response from a tracker: * src/PeerListProcessor.h: New class. * src/DefaultPeerListProcessor.h: New class. * src/DefaultPeerListProcessor.cc: New class. * src/CompactPeerListProcessor.h: New class. * src/CompactPeerListProcessor.cc: New class. * src/DelegatingPeerListProcessor.h: New class. * src/DelegatingPeerListProcessor.cc: New class. * src/TorrentMan.cc (addPeer): New function(overload). (addPeer): Delete unused peers only when new peer is added. * src/TorrentMan.h (PeerListProcessor.h): Included. (Peers): Removed. (addPeer): New function(overload). * src/TrackerUpdateCommand.cc (netinet/in.h): Removed. (DelegatingPeerListProcessor.h): Included. (execute): Updated to use DelegatingPeerListProcessor. To fix the memory leak in TorrentMan::peers: * src/PeerAbstractCommand.cc (onAbort): Added peer->resetStatus(). * src/Peer.h (resetStatus): Made public. To improve the precision of the speed calculation: * src/SpeedCalc.h (nextInterval): New variable. * src/SpeedCalc.cc (reset): Added nextInterval. (isIntervalOver): Use nextInterval instead of CHANGE_INTERVAL_SEC. (changeSw): Set nextInterval to 15 seconds relative to the current instant time. * src/main.cc (showVersion): Updated. --- ChangeLog | 60 ++++++++++++++++++++++++ src/AbstractCommand.cc | 14 +++++- src/AbstractCommand.h | 2 + src/CompactPeerListProcessor.cc | 64 +++++++++++++++++++++++++ src/CompactPeerListProcessor.h | 55 ++++++++++++++++++++++ src/DefaultPeerListProcessor.cc | 70 ++++++++++++++++++++++++++++ src/DefaultPeerListProcessor.h | 56 ++++++++++++++++++++++ src/DelegatingPeerListProcessor.cc | 57 ++++++++++++++++++++++ src/DelegatingPeerListProcessor.h | 63 +++++++++++++++++++++++++ src/FtpInitiateConnectionCommand.h | 6 +++ src/HttpInitiateConnectionCommand.h | 6 +++ src/Makefile.am | 6 ++- src/Makefile.in | 22 +++++++-- src/Peer.h | 4 +- src/PeerAbstractCommand.cc | 1 + src/PeerListProcessor.h | 56 ++++++++++++++++++++++ src/SpeedCalc.cc | 4 +- src/SpeedCalc.h | 1 + src/TorrentMan.cc | 17 +++++-- src/TorrentMan.h | 3 +- src/TrackerUpdateCommand.cc | 37 +++++---------- src/main.cc | 4 +- test/DefaultPeerListProcessorTest.cc | 57 ++++++++++++++++++++++ test/Makefile.am | 3 +- test/Makefile.in | 6 ++- test/SegmentManTest.cc | 60 ++++++++++++++---------- 26 files changed, 666 insertions(+), 68 deletions(-) create mode 100644 src/CompactPeerListProcessor.cc create mode 100644 src/CompactPeerListProcessor.h create mode 100644 src/DefaultPeerListProcessor.cc create mode 100644 src/DefaultPeerListProcessor.h create mode 100644 src/DelegatingPeerListProcessor.cc create mode 100644 src/DelegatingPeerListProcessor.h create mode 100644 src/PeerListProcessor.h create mode 100644 test/DefaultPeerListProcessorTest.cc diff --git a/ChangeLog b/ChangeLog index eb85a87d..519612da 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,63 @@ +2006-10-01 Tatsuhiro Tsujikawa + + To add timeout to async name resolution: + + * src/AbstractCommand.h + (nameResolveFinished): New function. This is not elegant way. It needs + to be more refined. + (nameResolverCheck): New variable. + * src/AbstractCommand.cc + (AbstractCommand): Added nameResolverCheck. + (execute): Added the check whether the name resolution has finished. + (setNameResolverCheck): Set nameResolverCheck to true. + (disableNameResolverCheck): Set nameResolverCheck to false. + (nameResolverFinished): New function. + * src/FtpInitiateConnectionCommand.h + (nameResolverFinished): New function. + * src/HttpInitiateConnectionCommand.h + (nameResolverFinished): New function. + + To add the support for a non-compact response from a tracker: + + * src/PeerListProcessor.h: New class. + * src/DefaultPeerListProcessor.h: New class. + * src/DefaultPeerListProcessor.cc: New class. + * src/CompactPeerListProcessor.h: New class. + * src/CompactPeerListProcessor.cc: New class. + * src/DelegatingPeerListProcessor.h: New class. + * src/DelegatingPeerListProcessor.cc: New class. + * src/TorrentMan.cc + (addPeer): New function(overload). + (addPeer): Delete unused peers only when new peer is added. + * src/TorrentMan.h + (PeerListProcessor.h): Included. + (Peers): Removed. + (addPeer): New function(overload). + * src/TrackerUpdateCommand.cc + (netinet/in.h): Removed. + (DelegatingPeerListProcessor.h): Included. + (execute): Updated to use DelegatingPeerListProcessor. + + To fix the memory leak in TorrentMan::peers: + + * src/PeerAbstractCommand.cc + (onAbort): Added peer->resetStatus(). + * src/Peer.h + (resetStatus): Made public. + + To improve the precision of the speed calculation: + + * src/SpeedCalc.h + (nextInterval): New variable. + * src/SpeedCalc.cc + (reset): Added nextInterval. + (isIntervalOver): Use nextInterval instead of CHANGE_INTERVAL_SEC. + (changeSw): Set nextInterval to 15 seconds relative to the current + instant time. + + * src/main.cc + (showVersion): Updated. + 2006-09-23 Tatsuhiro Tsujikawa * src/main.cc diff --git a/src/AbstractCommand.cc b/src/AbstractCommand.cc index d772312a..9ffa549c 100644 --- a/src/AbstractCommand.cc +++ b/src/AbstractCommand.cc @@ -44,7 +44,8 @@ AbstractCommand::AbstractCommand(int cuid, Request* req, DownloadEngine* e, const SocketHandle& s): Command(cuid), req(req), e(e), socket(s), - checkSocketIsReadable(false), checkSocketIsWritable(false) { + checkSocketIsReadable(false), checkSocketIsWritable(false), + nameResolverCheck(false) { setReadCheckSocket(socket); timeout = this->e->option->getAsInt(PREF_TIMEOUT); @@ -73,7 +74,10 @@ bool AbstractCommand::execute() { } if(checkSocketIsReadable && readCheckTarget->isReadable(0) || checkSocketIsWritable && writeCheckTarget->isWritable(0) || - !checkSocketIsReadable && !checkSocketIsWritable) { +#ifdef ENABLE_ASYNC_DNS + nameResolverCheck && nameResolveFinished() || +#endif // ENABLE_ASYNC_DNS + !checkSocketIsReadable && !checkSocketIsWritable && !nameResolverCheck) { checkPoint.reset(); Segment segment; if(e->segmentMan->downloadStarted) { @@ -200,10 +204,12 @@ void AbstractCommand::setWriteCheckSocket(const SocketHandle& socket) { #ifdef ENABLE_ASYNC_DNS void AbstractCommand::setNameResolverCheck(const NameResolverHandle& resolver) { + nameResolverCheck = true; e->addNameResolverCheck(resolver, this); } void AbstractCommand::disableNameResolverCheck(const NameResolverHandle& resolver) { + nameResolverCheck = false; e->deleteNameResolverCheck(resolver, this); } @@ -228,4 +234,8 @@ bool AbstractCommand::resolveHostname(const string& hostname, return false; } } + +bool AbstractCommand::nameResolveFinished() const { + return false; +} #endif // ENABLE_ASYNC_DNS diff --git a/src/AbstractCommand.h b/src/AbstractCommand.h index c92cf4a1..fba4997f 100644 --- a/src/AbstractCommand.h +++ b/src/AbstractCommand.h @@ -63,6 +63,7 @@ protected: void setNameResolverCheck(const NameResolverHandle& resolver); void disableNameResolverCheck(const NameResolverHandle& resolver); bool resolveHostname(const string& hostname, const NameResolverHandle& nameResolver); + virtual bool nameResolveFinished() const; #endif // ENABLE_ASYNC_DNS void setTimeout(int timeout) { this->timeout = timeout; } private: @@ -70,6 +71,7 @@ private: bool checkSocketIsWritable; SocketHandle readCheckTarget; SocketHandle writeCheckTarget; + bool nameResolverCheck; public: AbstractCommand(int cuid, Request* req, DownloadEngine* e, const SocketHandle& s = SocketHandle()); virtual ~AbstractCommand(); diff --git a/src/CompactPeerListProcessor.cc b/src/CompactPeerListProcessor.cc new file mode 100644 index 00000000..bae88c2b --- /dev/null +++ b/src/CompactPeerListProcessor.cc @@ -0,0 +1,64 @@ +/* + * 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 "CompactPeerListProcessor.h" +#include "Data.h" +#include + +bool CompactPeerListProcessor::canHandle(const MetaEntry* peersEntry) const { + return dynamic_cast(peersEntry) != 0; +} + +Peers CompactPeerListProcessor::extractPeer(const MetaEntry* peersEntry) { + Peers peers; + + const Data* peersData = (const Data*)peersEntry; + if(peersData->getLen() > 0) { + for(int i = 0; i < peersData->getLen(); i += 6) { + unsigned int ipaddr1 = (unsigned char)*(peersData->getData()+i); + unsigned int ipaddr2 = (unsigned char)*(peersData->getData()+i+1); + unsigned int ipaddr3 = (unsigned char)*(peersData->getData()+i+2); + unsigned int ipaddr4 = (unsigned char)*(peersData->getData()+i+3); + unsigned int port = ntohs(*(unsigned short int*)(peersData->getData()+i+4)); + char ipaddr[16]; + + snprintf(ipaddr, sizeof(ipaddr), "%d.%d.%d.%d", + ipaddr1, ipaddr2, ipaddr3, ipaddr4); + PeerHandle peer = + PeerHandle(new Peer(ipaddr, port, pieceLength, totalLength)); + + peers.push_back(peer); + } + } + return peers; +} diff --git a/src/CompactPeerListProcessor.h b/src/CompactPeerListProcessor.h new file mode 100644 index 00000000..31f64f86 --- /dev/null +++ b/src/CompactPeerListProcessor.h @@ -0,0 +1,55 @@ +/* + * 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_COMPACT_PEER_LIST_PROCESSOR_H_ +#define _D_COMPACT_PEER_LIST_PROCESSOR_H_ + +#include "PeerListProcessor.h" + +class CompactPeerListProcessor : public PeerListProcessor { +private: + int pieceLength; + long long int totalLength; +public: + CompactPeerListProcessor(int pieceLength, long long int totalLength) + :pieceLength(pieceLength), + totalLength(totalLength) {} + + virtual ~CompactPeerListProcessor() {} + + virtual Peers extractPeer(const MetaEntry* peersEntry); + + virtual bool canHandle(const MetaEntry* peersEntry) const; +}; + +#endif // _D_COMPACT_PEER_LIST_PROCESSOR_H_ diff --git a/src/DefaultPeerListProcessor.cc b/src/DefaultPeerListProcessor.cc new file mode 100644 index 00000000..9f79f3fd --- /dev/null +++ b/src/DefaultPeerListProcessor.cc @@ -0,0 +1,70 @@ +/* */ +#include "DefaultPeerListProcessor.h" +#include "List.h" +#include "Dictionary.h" +#include "Data.h" + +bool DefaultPeerListProcessor::canHandle(const MetaEntry* peersEntry) const { + const List* peersList = dynamic_cast(peersEntry); + return peersList != 0; +} + +Peers DefaultPeerListProcessor::extractPeer(const MetaEntry* peersEntry) { + Peers peers; + const List* peersList = dynamic_cast(peersEntry); + if(!peersList) { + return peers; + } + const MetaList& metaList = peersList->getList(); + for(MetaList::const_iterator itr = metaList.begin(); + itr != metaList.end(); itr++) { + const Dictionary* peerDic = dynamic_cast(*itr); + if(!peerDic) { + break; + } + const Data* ip = dynamic_cast(peerDic->get("ip")); + const Data* port = dynamic_cast(peerDic->get("port")); + if(!ip || !port || !port->isNumber()) { + continue; + } + PeerHandle peer = PeerHandle(new Peer(ip->toString(), + port->toInt(), + pieceLength, + totalLength)); + peers.push_back(peer); + } + return peers; +} diff --git a/src/DefaultPeerListProcessor.h b/src/DefaultPeerListProcessor.h new file mode 100644 index 00000000..659e168c --- /dev/null +++ b/src/DefaultPeerListProcessor.h @@ -0,0 +1,56 @@ +/* */ +#ifndef _D_DEFAULT_PEER_LIST_PROCESSOR_H_ +#define _D_DEFAULT_PEER_LIST_PROCESSOR_H_ + +#include "PeerListProcessor.h" + +class DefaultPeerListProcessor : public PeerListProcessor { +private: + int pieceLength; + long long int totalLength; +public: + DefaultPeerListProcessor(int pieceLength, long long int totalLength) + :pieceLength(pieceLength), + totalLength(totalLength) {} + + virtual ~DefaultPeerListProcessor() {} + + virtual Peers extractPeer(const MetaEntry* peersEntry); + + virtual bool canHandle(const MetaEntry* peersEntry) const; +}; + +#endif // _D_DEFAULT_PEER_LIST_PROCESSOR_H_ diff --git a/src/DelegatingPeerListProcessor.cc b/src/DelegatingPeerListProcessor.cc new file mode 100644 index 00000000..b91554bb --- /dev/null +++ b/src/DelegatingPeerListProcessor.cc @@ -0,0 +1,57 @@ +/* + * 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 "DelegatingPeerListProcessor.h" + +Peers DelegatingPeerListProcessor::extractPeer(const MetaEntry* peersEntry) { + Peers peers; + for(PeerListProcessors::iterator itr = processors.begin(); + itr != processors.end(); itr++) { + PeerListProcessorHandle processor = *itr; + if(processor->canHandle(peersEntry)) { + Peers tempPeers = processor->extractPeer(peersEntry); + copy(tempPeers.begin(), tempPeers.end(), back_inserter(peers)); + break; + } + } + return peers; +} + +bool DelegatingPeerListProcessor::canHandle(const MetaEntry* peersEntry) const { + for(PeerListProcessors::const_iterator itr = processors.begin(); + itr != processors.end(); itr++) { + if((*itr)->canHandle(peersEntry)) { + return true; + } + } +} diff --git a/src/DelegatingPeerListProcessor.h b/src/DelegatingPeerListProcessor.h new file mode 100644 index 00000000..19ff37ea --- /dev/null +++ b/src/DelegatingPeerListProcessor.h @@ -0,0 +1,63 @@ +/* + * 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_DELEGATING_PEER_LIST_PROCESSOR_H_ +#define _D_DELEGATING_PEER_LIST_PROCESSOR_H_ + +#include "PeerListProcessor.h" +#include "DefaultPeerListProcessor.h" +#include "CompactPeerListProcessor.h" + +typedef deque PeerListProcessors; + +class DelegatingPeerListProcessor : public PeerListProcessor { +private: + int pieceLength; + long long int totalLength; + PeerListProcessors processors; +public: + DelegatingPeerListProcessor(int pieceLength, long long int totalLength) + :pieceLength(pieceLength), + totalLength(totalLength) { + processors.push_back(new DefaultPeerListProcessor(pieceLength, totalLength)); + processors.push_back(new CompactPeerListProcessor(pieceLength, totalLength)); + } + + virtual ~DelegatingPeerListProcessor() {} + + virtual Peers extractPeer(const MetaEntry* peersEntry); + + virtual bool canHandle(const MetaEntry* peersEntry) const; +}; + +#endif // _D_DELEGATING_PEER_LIST_PROCESSOR_H_ diff --git a/src/FtpInitiateConnectionCommand.h b/src/FtpInitiateConnectionCommand.h index 73757906..48f0adc2 100644 --- a/src/FtpInitiateConnectionCommand.h +++ b/src/FtpInitiateConnectionCommand.h @@ -45,6 +45,12 @@ private: bool useHttpProxy() const; bool useHttpProxyGet() const; bool useHttpProxyConnect() const; +#ifdef ENABLE_ASYNC_DNS + virtual bool nameResolveFinished() const { + return nameResolver->getStatus() == NameResolver::STATUS_SUCCESS || + nameResolver->getStatus() == NameResolver::STATUS_ERROR; + } +#endif // ENABLE_ASYNC_DNS protected: bool executeInternal(Segment& segment); public: diff --git a/src/HttpInitiateConnectionCommand.h b/src/HttpInitiateConnectionCommand.h index f7e83829..8eecbbcc 100644 --- a/src/HttpInitiateConnectionCommand.h +++ b/src/HttpInitiateConnectionCommand.h @@ -55,6 +55,12 @@ protected: * evaluated by RequestCommand. */ bool executeInternal(Segment& segment); +#ifdef ENABLE_ASYNC_DNS + virtual bool nameResolveFinished() const { + return nameResolver->getStatus() == NameResolver::STATUS_SUCCESS || + nameResolver->getStatus() == NameResolver::STATUS_ERROR; + } +#endif // ENABLE_ASYNC_DNS public: HttpInitiateConnectionCommand(int cuid, Request* req, DownloadEngine* e); ~HttpInitiateConnectionCommand(); diff --git a/src/Makefile.am b/src/Makefile.am index 0e0b96b0..cc0f408d 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -120,7 +120,11 @@ SRCS += MetaEntry.h\ TimeSeedCriteria.h\ ShareRatioSeedCriteria.h\ UnionSeedCriteria.h\ - SeedCheckCommand.cc SeedCheckCommand.h + SeedCheckCommand.cc SeedCheckCommand.h\ + PeerListProcessor.h\ + DefaultPeerListProcessor.cc DefaultPeerListProcessor.h\ + CompactPeerListProcessor.cc CompactPeerListProcessor.h\ + DelegatingPeerListProcessor.cc DelegatingPeerListProcessor.h endif # ENABLE_BITTORRENT if ENABLE_METALINK diff --git a/src/Makefile.in b/src/Makefile.in index 92410315..5670f7e6 100644 --- a/src/Makefile.in +++ b/src/Makefile.in @@ -97,7 +97,11 @@ bin_PROGRAMS = aria2c$(EXEEXT) @ENABLE_BITTORRENT_TRUE@ TimeSeedCriteria.h\ @ENABLE_BITTORRENT_TRUE@ ShareRatioSeedCriteria.h\ @ENABLE_BITTORRENT_TRUE@ UnionSeedCriteria.h\ -@ENABLE_BITTORRENT_TRUE@ SeedCheckCommand.cc SeedCheckCommand.h +@ENABLE_BITTORRENT_TRUE@ SeedCheckCommand.cc SeedCheckCommand.h\ +@ENABLE_BITTORRENT_TRUE@ PeerListProcessor.h\ +@ENABLE_BITTORRENT_TRUE@ DefaultPeerListProcessor.cc DefaultPeerListProcessor.h\ +@ENABLE_BITTORRENT_TRUE@ CompactPeerListProcessor.cc CompactPeerListProcessor.h\ +@ENABLE_BITTORRENT_TRUE@ DelegatingPeerListProcessor.cc DelegatingPeerListProcessor.h @ENABLE_METALINK_TRUE@am__append_3 = Metalinker.cc Metalinker.h\ @ENABLE_METALINK_TRUE@ MetalinkEntry.cc MetalinkEntry.h\ @@ -203,8 +207,12 @@ am__libaria2c_a_SOURCES_DIST = Socket.h SocketCore.cc SocketCore.h \ TorrentRequestInfo.cc TorrentRequestInfo.h SeedCriteria.h \ TimeSeedCriteria.h ShareRatioSeedCriteria.h \ UnionSeedCriteria.h SeedCheckCommand.cc SeedCheckCommand.h \ - Metalinker.cc Metalinker.h MetalinkEntry.cc MetalinkEntry.h \ - MetalinkResource.cc MetalinkResource.h MetalinkProcessor.h \ + PeerListProcessor.h DefaultPeerListProcessor.cc \ + DefaultPeerListProcessor.h CompactPeerListProcessor.cc \ + CompactPeerListProcessor.h DelegatingPeerListProcessor.cc \ + DelegatingPeerListProcessor.h Metalinker.cc Metalinker.h \ + MetalinkEntry.cc MetalinkEntry.h MetalinkResource.cc \ + MetalinkResource.h MetalinkProcessor.h \ Xml2MetalinkProcessor.cc Xml2MetalinkProcessor.h \ MetalinkRequestInfo.cc MetalinkRequestInfo.h @ENABLE_ASYNC_DNS_TRUE@am__objects_1 = NameResolver.$(OBJEXT) @@ -257,7 +265,10 @@ am__libaria2c_a_SOURCES_DIST = Socket.h SocketCore.cc SocketCore.h \ @ENABLE_BITTORRENT_TRUE@ PeerMessageFactory.$(OBJEXT) \ @ENABLE_BITTORRENT_TRUE@ HaveEraseCommand.$(OBJEXT) \ @ENABLE_BITTORRENT_TRUE@ TorrentRequestInfo.$(OBJEXT) \ -@ENABLE_BITTORRENT_TRUE@ SeedCheckCommand.$(OBJEXT) +@ENABLE_BITTORRENT_TRUE@ SeedCheckCommand.$(OBJEXT) \ +@ENABLE_BITTORRENT_TRUE@ DefaultPeerListProcessor.$(OBJEXT) \ +@ENABLE_BITTORRENT_TRUE@ CompactPeerListProcessor.$(OBJEXT) \ +@ENABLE_BITTORRENT_TRUE@ DelegatingPeerListProcessor.$(OBJEXT) @ENABLE_METALINK_TRUE@am__objects_3 = Metalinker.$(OBJEXT) \ @ENABLE_METALINK_TRUE@ MetalinkEntry.$(OBJEXT) \ @ENABLE_METALINK_TRUE@ MetalinkResource.$(OBJEXT) \ @@ -583,11 +594,14 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ChokeMessage.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ChunkedEncoding.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/Command.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/CompactPeerListProcessor.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ConsoleDownloadEngine.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/CookieBox.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/CopyDiskAdaptor.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/Data.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/DefaultDiskWriter.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/DefaultPeerListProcessor.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/DelegatingPeerListProcessor.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/Dictionary.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/DirectDiskAdaptor.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/Directory.Po@am__quote@ diff --git a/src/Peer.h b/src/Peer.h index 9dc9963b..cc9fe26e 100644 --- a/src/Peer.h +++ b/src/Peer.h @@ -75,8 +75,6 @@ private: long long int sessionDownloadLength; int pieceLength; int latency; - - void resetStatus(); public: Peer(string ipaddr, int port, int pieceLength, long long int totalLength) :entryId(0), ipaddr(ipaddr), port(port), error(0), @@ -108,6 +106,8 @@ public: return !(*this == p); } + void resetStatus(); + void updateUploadLength(int bytes) { peerStat.updateUploadLength(bytes); sessionUploadLength += bytes; diff --git a/src/PeerAbstractCommand.cc b/src/PeerAbstractCommand.cc index 50e5d5d6..6127450d 100644 --- a/src/PeerAbstractCommand.cc +++ b/src/PeerAbstractCommand.cc @@ -99,6 +99,7 @@ void PeerAbstractCommand::onAbort(Exception* ex) { } else { peer->error += MAX_PEER_ERROR; } + peer->resetStatus(); } void PeerAbstractCommand::disableReadCheckSocket() { diff --git a/src/PeerListProcessor.h b/src/PeerListProcessor.h new file mode 100644 index 00000000..617042af --- /dev/null +++ b/src/PeerListProcessor.h @@ -0,0 +1,56 @@ +/* */ +#ifndef _D_PEER_LIST_PROCESSOR_H_ +#define _D_PEER_LIST_PROCESSOR_H_ + +#include "common.h" +#include "MetaEntry.h" +#include "Peer.h" +#include "SharedHandle.h" + +typedef deque Peers; + +class PeerListProcessor { +public: + virtual ~PeerListProcessor() {} + + virtual Peers extractPeer(const MetaEntry* peersEntry) = 0; + + virtual bool canHandle(const MetaEntry* peersEntry) const = 0; +}; + +typedef SharedHandle PeerListProcessorHandle; + +#endif // _D_PEER_LIST_PROCESSOR_H_ diff --git a/src/SpeedCalc.cc b/src/SpeedCalc.cc index a15adc1e..af3d2d7f 100644 --- a/src/SpeedCalc.cc +++ b/src/SpeedCalc.cc @@ -54,6 +54,7 @@ void SpeedCalc::reset() { prevSpeed = 0; start.reset(); accumulatedLength = 0; + nextInterval = CHANGE_INTERVAL_SEC; } int SpeedCalc::calculateSpeed() { @@ -88,13 +89,14 @@ void SpeedCalc::update(int bytes) { } bool SpeedCalc::isIntervalOver() const { - return CHANGE_INTERVAL_SEC <= cpArray[sw].difference(); + return nextInterval <= cpArray[sw].difference(); } void SpeedCalc::changeSw() { lengthArray[sw] = 0; cpArray[sw].reset(); sw ^= 0x01; + nextInterval = cpArray[sw].difference()+CHANGE_INTERVAL_SEC; } int SpeedCalc::getAvgSpeed() const { diff --git a/src/SpeedCalc.h b/src/SpeedCalc.h index c5c891be..b8d64d14 100644 --- a/src/SpeedCalc.h +++ b/src/SpeedCalc.h @@ -47,6 +47,7 @@ private: int prevSpeed; Time start; long long int accumulatedLength; + int nextInterval; bool isIntervalOver() const; void changeSw(); diff --git a/src/TorrentMan.cc b/src/TorrentMan.cc index 074b61cf..82bc254f 100644 --- a/src/TorrentMan.cc +++ b/src/TorrentMan.cc @@ -89,12 +89,23 @@ void TorrentMan::updatePeers(const Peers& peers) { this->peers = peers; } -bool TorrentMan::addPeer(const PeerHandle& peer) { - if(peers.size() >= MAX_PEER_LIST_SIZE) { - deleteUnusedPeer(peers.size()-MAX_PEER_LIST_SIZE+15); +void TorrentMan::addPeer(const Peers& peers) { + for(Peers::const_iterator itr = peers.begin(); + itr != peers.end(); itr++) { + const PeerHandle& peer = *itr; + if(addPeer(peer)) { + logger->debug("Adding peer %s:%d", + peer->ipaddr.c_str(), peer->port); + } } +} + +bool TorrentMan::addPeer(const PeerHandle& peer) { Peers::iterator itr = find(peers.begin(), peers.end(), peer); if(itr == peers.end()) { + if(peers.size() > MAX_PEER_LIST_SIZE) { + deleteUnusedPeer(peers.size()-MAX_PEER_LIST_SIZE+1); + } ++peerEntryIdCounter; peer->entryId = peerEntryIdCounter; peers.push_back(peer); diff --git a/src/TorrentMan.h b/src/TorrentMan.h index 1b734ddd..54473c12 100644 --- a/src/TorrentMan.h +++ b/src/TorrentMan.h @@ -47,6 +47,7 @@ #include "DiskAdaptor.h" #include "Request.h" #include "TimeA2.h" +#include "PeerListProcessor.h" #include #include #include @@ -85,7 +86,6 @@ public: index(index) {} }; -typedef deque Peers; typedef deque Haves; typedef deque PieceIndexes; typedef deque Pieces; @@ -156,6 +156,7 @@ public: // TODO do not use this method void updatePeers(const Peers& peers); bool addPeer(const PeerHandle& peer); + void addPeer(const Peers& peers); //void updatePeer(const Peer* peer); const Peers& getPeers() const { return peers; } PeerHandle getPeer() const; diff --git a/src/TrackerUpdateCommand.cc b/src/TrackerUpdateCommand.cc index f13b5d4c..bb39dc86 100644 --- a/src/TrackerUpdateCommand.cc +++ b/src/TrackerUpdateCommand.cc @@ -40,7 +40,7 @@ #include "PeerInitiateConnectionCommand.h" #include "SleepCommand.h" #include "Util.h" -#include +#include "DelegatingPeerListProcessor.h" TrackerUpdateCommand::TrackerUpdateCommand(int cuid, TorrentDownloadEngine*e):Command(cuid), e(e) { logger = LogFactory::getInstance(); @@ -132,32 +132,15 @@ bool TrackerUpdateCommand::execute() { logger->debug("CUID#%d - Incomplete:%d", cuid, e->torrentMan->incomplete); } + const MetaEntry* peersEntry = response->get("peers"); if(!e->torrentMan->isHalt() && e->torrentMan->connections < MIN_PEERS && - dynamic_cast(response->get("peers"))) { - Data* peers = (Data*)response->get("peers"); - if(peers != NULL && peers->getLen() > 0) { - for(int i = 0; i < peers->getLen(); i += 6) { - unsigned int ipaddr1 = (unsigned char)*(peers->getData()+i); - unsigned int ipaddr2 = (unsigned char)*(peers->getData()+i+1); - unsigned int ipaddr3 = (unsigned char)*(peers->getData()+i+2); - unsigned int ipaddr4 = (unsigned char)*(peers->getData()+i+3); - unsigned int port = ntohs(*(unsigned short int*)(peers->getData()+i+4)); - char ipaddr[16]; - - snprintf(ipaddr, sizeof(ipaddr), "%d.%d.%d.%d", - ipaddr1, ipaddr2, ipaddr3, ipaddr4); - PeerHandle peer = - PeerHandle(new Peer(ipaddr, port, e->torrentMan->pieceLength, - e->torrentMan->getTotalLength())); - if(e->torrentMan->addPeer(peer)) { - logger->debug("CUID#%d - Adding peer %s:%d", - cuid, peer->ipaddr.c_str(), peer->port); - } - } - } else { - logger->info("CUID#%d - No peer list received.", cuid); - } + peersEntry) { + DelegatingPeerListProcessor proc(e->torrentMan->pieceLength, + e->torrentMan->getTotalLength()); + Peers peers = proc.extractPeer(peersEntry); + e->torrentMan->addPeer(peers); + while(e->torrentMan->isPeerAvailable() && e->torrentMan->connections < MIN_PEERS) { PeerHandle peer = e->torrentMan->getPeer(); @@ -169,6 +152,10 @@ bool TrackerUpdateCommand::execute() { logger->debug("CUID#%d - Adding new command CUID#%d", cuid, newCuid); } } + if(!peersEntry) { + logger->info("CUID#%d - No peer list received.", cuid); + } + if(e->torrentMan->req->getTrackerEvent() == Request::STARTED) { e->torrentMan->req->setTrackerEvent(Request::AUTO); } diff --git a/src/main.cc b/src/main.cc index 71c1dc63..c3c629a4 100644 --- a/src/main.cc +++ b/src/main.cc @@ -85,11 +85,11 @@ void setSignalHander(int signal, void (*handler)(int), int flags) { void showVersion() { cout << PACKAGE << _(" version ") << PACKAGE_VERSION << endl; - cout << "Copyright (C) 2006 Tatsuhiro Tsujikawa" << endl; - cout << endl; cout << "**Configuration**" << endl; cout << FeatureConfig::getInstance()->getConfigurationSummary(); cout << endl; + cout << "Copyright (C) 2006 Tatsuhiro Tsujikawa" << endl; + cout << endl; cout << _("This program is free software; you can redistribute it and/or modify\n" "it under the terms of the GNU General Public License as published by\n" diff --git a/test/DefaultPeerListProcessorTest.cc b/test/DefaultPeerListProcessorTest.cc new file mode 100644 index 00000000..0bcc7065 --- /dev/null +++ b/test/DefaultPeerListProcessorTest.cc @@ -0,0 +1,57 @@ +#include "DefaultPeerListProcessor.h" +#include "MetaFileUtil.h" +#include "Exception.h" +#include + +using namespace std; + +class DefaultPeerListProcessorTest:public CppUnit::TestFixture { + + CPPUNIT_TEST_SUITE(DefaultPeerListProcessorTest); + CPPUNIT_TEST(testExtractPeer); + CPPUNIT_TEST(testExtract2Peers); + CPPUNIT_TEST_SUITE_END(); +private: + +public: + void setUp() { + } + + void testExtractPeer(); + void testExtract2Peers(); +}; + + +CPPUNIT_TEST_SUITE_REGISTRATION( DefaultPeerListProcessorTest ); + +void DefaultPeerListProcessorTest::testExtractPeer() { + DefaultPeerListProcessor proc(1024*1024, 10*1024*1024); + string peersString = "d5:peersld2:ip11:192.168.0.17:peer id20:aria2-000000000000004:porti2006eeee"; + + Dictionary* dic = (Dictionary*)MetaFileUtil::bdecoding(peersString.c_str(), peersString.size()); + + CPPUNIT_ASSERT(proc.canHandle(dic->get("peers")); + + Peers peers = proc.extractPeer(dic->get("peers")); + CPPUNIT_ASSERT_EQUAL((size_t)1, peers.size()); + PeerHandle peer = *peers.begin(); + CPPUNIT_ASSERT_EQUAL(string("192.168.0.1"), peer->ipaddr); + CPPUNIT_ASSERT_EQUAL(2006, peer->port); +} + +void DefaultPeerListProcessorTest::testExtract2Peers() { + DefaultPeerListProcessor proc(1024*1024, 10*1024*1024); + string peersString = "d5:peersld2:ip11:192.168.0.17:peer id20:aria2-000000000000004:porti2006eed2:ip11:192.168.0.27:peer id20:aria2-000000000000004:porti2007eeee"; + + Dictionary* dic = (Dictionary*)MetaFileUtil::bdecoding(peersString.c_str(), peersString.size()); + + Peers peers = proc.extractPeer(dic->get("peers")); + CPPUNIT_ASSERT_EQUAL((size_t)2, peers.size()); + PeerHandle peer = *peers.begin(); + CPPUNIT_ASSERT_EQUAL(string("192.168.0.1"), peer->ipaddr); + CPPUNIT_ASSERT_EQUAL(2006, peer->port); + + peer = *(peers.begin()+1); + CPPUNIT_ASSERT_EQUAL(string("192.168.0.2"), peer->ipaddr); + CPPUNIT_ASSERT_EQUAL(2007, peer->port); +} diff --git a/test/Makefile.am b/test/Makefile.am index aae91f06..56a0f04a 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -39,7 +39,8 @@ aria2c_SOURCES = AllTest.cc\ ShareRatioSeedCriteriaTest.cc\ TimeSeedCriteriaTest.cc\ SegmentManTest.cc\ - SpeedCalcTest.cc + SpeedCalcTest.cc\ + DefaultPeerListProcessorTest.cc #aria2c_CXXFLAGS = ${CPPUNIT_CFLAGS} -I../src -I../lib -Wall -D_FILE_OFFSET_BITS=64 #aria2c_LDFLAGS = ${CPPUNIT_LIBS} diff --git a/test/Makefile.in b/test/Makefile.in index bd5ef14b..5096be88 100644 --- a/test/Makefile.in +++ b/test/Makefile.in @@ -77,7 +77,7 @@ am_aria2c_OBJECTS = AllTest.$(OBJEXT) RequestTest.$(OBJEXT) \ MetalinkEntryTest.$(OBJEXT) FeatureConfigTest.$(OBJEXT) \ ShareRatioSeedCriteriaTest.$(OBJEXT) \ TimeSeedCriteriaTest.$(OBJEXT) SegmentManTest.$(OBJEXT) \ - SpeedCalcTest.$(OBJEXT) + SpeedCalcTest.$(OBJEXT) DefaultPeerListProcessorTest.$(OBJEXT) aria2c_OBJECTS = $(am_aria2c_OBJECTS) am__DEPENDENCIES_1 = aria2c_DEPENDENCIES = ../src/libaria2c.a $(am__DEPENDENCIES_1) @@ -270,7 +270,8 @@ aria2c_SOURCES = AllTest.cc\ ShareRatioSeedCriteriaTest.cc\ TimeSeedCriteriaTest.cc\ SegmentManTest.cc\ - SpeedCalcTest.cc + SpeedCalcTest.cc\ + DefaultPeerListProcessorTest.cc #aria2c_CXXFLAGS = ${CPPUNIT_CFLAGS} -I../src -I../lib -Wall -D_FILE_OFFSET_BITS=64 #aria2c_LDFLAGS = ${CPPUNIT_LIBS} @@ -342,6 +343,7 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/CookieBoxTest.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/DataTest.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/DefaultDiskWriterTest.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/DefaultPeerListProcessorTest.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/DictionaryTest.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/FeatureConfigTest.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/FileTest.Po@am__quote@ diff --git a/test/SegmentManTest.cc b/test/SegmentManTest.cc index 9c311818..bd4949e2 100644 --- a/test/SegmentManTest.cc +++ b/test/SegmentManTest.cc @@ -12,6 +12,7 @@ class SegmentManTest:public CppUnit::TestFixture { CPPUNIT_TEST(testSaveAndLoad); CPPUNIT_TEST(testNullBitfield); CPPUNIT_TEST(testCancelSegmentOnNullBitfield); + CPPUNIT_TEST(testBug); CPPUNIT_TEST_SUITE_END(); private: @@ -22,6 +23,7 @@ public: void testSaveAndLoad(); void testNullBitfield(); void testCancelSegmentOnNullBitfield(); + void testBug(); }; @@ -41,20 +43,20 @@ void SegmentManTest::testSaveAndLoad() { segmentMan.ufilename = filename; segmentMan.initBitfield(1024*1024, segmentMan.totalSize); - Segment2 seg1; - segmentMan.getSegment2(seg1, 1); + Segment seg1; + segmentMan.getSegment(seg1, 1); seg1.writtenLength = seg1.length; - segmentMan.completeSegment2(1, seg1); + segmentMan.completeSegment(1, seg1); - Segment2 seg2; - segmentMan.getSegment2(seg2, 2); + Segment seg2; + segmentMan.getSegment(seg2, 2); seg2.writtenLength = 512*1024; - segmentMan.updateSegment2(2, seg2); + segmentMan.updateSegment(2, seg2); - Segment2 seg3; - segmentMan.getSegment2(seg3, 3); + Segment seg3; + segmentMan.getSegment(seg3, 3); seg2.writtenLength = 512*1024; - segmentMan.updateSegment2(2, seg2); + segmentMan.updateSegment(2, seg2); segmentMan.save(); @@ -66,12 +68,12 @@ void SegmentManTest::testSaveAndLoad() { CPPUNIT_ASSERT_EQUAL(segmentMan.totalSize, segmentManLoad.totalSize); - Segment2 seg2Load; - segmentManLoad.getSegment2(seg2Load, 2, seg2.index); + Segment seg2Load; + segmentManLoad.getSegment(seg2Load, 2, seg2.index); CPPUNIT_ASSERT_EQUAL(seg2, seg2Load); - Segment2 seg3Load; - segmentManLoad.getSegment2(seg3Load, 3, seg3.index); + Segment seg3Load; + segmentManLoad.getSegment(seg3Load, 3, seg3.index); CPPUNIT_ASSERT_EQUAL(seg3, seg3Load); CPPUNIT_ASSERT_EQUAL(segmentMan.getDownloadLength(), segmentManLoad.getDownloadLength()); @@ -88,26 +90,36 @@ void SegmentManTest::testNullBitfield() { op.put(PREF_SEGMENT_SIZE, Util::itos(1024*1024)); segmentMan.option = &op; - Segment2 segment; - CPPUNIT_ASSERT(segmentMan.getSegment2(segment, 1)); - CPPUNIT_ASSERT_EQUAL(Segment2(0, 0, 0), segment); + Segment segment; + CPPUNIT_ASSERT(segmentMan.getSegment(segment, 1)); + CPPUNIT_ASSERT_EQUAL(Segment(0, 0, 0), segment); - Segment2 segment2; - CPPUNIT_ASSERT(!segmentMan.getSegment2(segment2, 2)); + Segment segment2; + CPPUNIT_ASSERT(!segmentMan.getSegment(segment2, 2)); long long int totalLength = 1024*1024; segment.writtenLength = totalLength; - CPPUNIT_ASSERT(segmentMan.updateSegment2(1, segment)); + CPPUNIT_ASSERT(segmentMan.updateSegment(1, segment)); CPPUNIT_ASSERT_EQUAL(totalLength, segmentMan.getDownloadLength()); - CPPUNIT_ASSERT(segmentMan.completeSegment2(1, segment)); + CPPUNIT_ASSERT(segmentMan.completeSegment(1, segment)); CPPUNIT_ASSERT_EQUAL(totalLength, segmentMan.getDownloadLength()); } void SegmentManTest::testCancelSegmentOnNullBitfield() { SegmentMan segmentMan; - Segment2 segment; - CPPUNIT_ASSERT(segmentMan.getSegment2(segment, 1)); - CPPUNIT_ASSERT(segmentMan.cancelSegment2(1, segment)); - CPPUNIT_ASSERT(segmentMan.getSegment2(segment, 1)); + Segment segment; + CPPUNIT_ASSERT(segmentMan.getSegment(segment, 1)); + segmentMan.cancelSegment(1); + CPPUNIT_ASSERT(segmentMan.getSegment(segment, 1)); +} + +void SegmentManTest::testBug() { + SegmentMan segmentMan; + + segmentMan.ufilename = "bug"; + + cerr << "########################################" << endl; + segmentMan.load(); + }