2006-11-05 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
To divide TorrentMan into 6 classes: BtContext, BtRuntime,
PeerStorage, PieceStorage, BtAnnounce and BtProgressInfoFile
* src/TrackerWatcherComand.h: Made subclass of
BtContextAwareCommand.
* src/SeedCheckCommand.cc: Use pieceStorage, btRuntime
* src/PeerAbstractCommand.h: Made subclass of
BtContextAwareCommand.
* src/PeerAbstractCommand.cc: Use btRuntime.
* src/BtContextAwareCommand.h: New class.
* src/FileEntry.h: Added accessor methods for following
variables.
(path): Made private.
(length): Made private.
(offset): Made private.
(extracted): Made private.
(requested): Made private.
(FileEntries): New definition.
(FileEntryHandle): New definition.
* src/FileEntry.cc: New file.
* src/HaveEraseCommand.h: Made subclass of
BtContextAwareCommand.
* src/HaveEraseCommand.cc: Use btRuntime, pieceStorage.
* src/PeerChokeCommand.h: Made subclass of
BtContextAwareCommand.
* src/PeerChokeCommand.cc: Use btRuntime, peerStorage,
pieceStorage.
* src/PieceStorage.h: New file.
* src/PeerInteractionCommand.h: Use btContext.
* src/PeerInteractionCommand.cc: Use pieceStorage, peerStorage,
btRuntime.
* src/DefaultBtProgressInfoFile.h: New file.
* src/DefaultBtProgressInfoFile.cc: New file.
* src/File.cc
(Util.h): New include.
(mkdirs): New function.
* src/MultiDiskAdaptor.h
(mkdir): New function.
* src/PeerListProcessor.h
(Peers): Removed.
* src/PeerInteraction.h
(torrentMan): Removed.
(btContext): New variable.
(peerStorage): New variable.
(pieceStorage): New variable.
(btAnnounce): New variable.
(getTorrentMan): Removed.
(getBtContext): New function.
* src/PeerInteraction.cc: Use btContext, peerStorage,
pieceStorage,
btAnnounce.
* src/HandshakeMessage.h
(TorrentMan.h): Removed.
* src/HandshakeMessage.cc: Use btContext.
* src/DefaultBtAnnounce.cc: New file.
* src/MultiDiskWriter.cc: Use the accessor methods of FileEntry.
* src/DefaultPieceStorage.cc: New file.
* src/DefaultBtContext.h: New file.
* src/TorrentRequestInfo.cc: Use btContext, pieceStorage.
Use the accessor methods of FileEntry.
* src/CookieBox.cc: Updated to use Util::slice().
* src/PieceMessage.cc: Use btContext, pieceStorage.
* src/common.h (SharedHandle.h): New include.
* src/PeerMessage.cc (PeerMessage): Added btContext,
peerStorage,
pieceStorage.
* src/TorrentAutoSaveCommand.h: Made subclass of
BtContextAwareCommand.
* src/DiskAdaptor.h
(topDir): Removed.
(getFileEntryFromPath): Changed the return type to
FileEntryHandle.
(setTopDir): Removed.
(getTopDir): Removed.
* src/BtContext.h: New file.
* src/DefaultPeerStorage.h: New file.
* src/PieceMessage.h (TorrentMan.h): Removed.
* src/RequestMessage.h (TorrentMan.h): Removed.
* src/TorrentDownloadEngine.h
(uploadLength): New variable.
(btContext): New variable.
(btRuntime): New variable.
(pieceStorage): New variable.
(peerStorage): New variable.
(btAnnounce): New variable.
(btProgressInfoFile): New variable.
(torrentMan): Removed.
(setBtContext): New function.
* src/TorrentDownloadEngine.cc: Use BtContext, BtRuntime,
pieceStorage,
peerStorage, btAnnounce, btProgressInfoFile.
* src/Piece.h
(toString): New function.
(Pieces): New type definition.
* src/Peer.h
(active): New variable.
(Peer): Added active.
(activate): Set active to true.
(deactivate): Set active to false.
(isActive): New function.
(Peers): New type definition.
* src/DirectDiskAdaptor.cc
(getFilePath): Use the accessor methods of FileEntry.
* src/TorrentConsoleDownloadEngine.h
(afterEachIteration): New function.
* src/TorrentConsoleDownloadEngine.cc
(haltRequested): New variable.
(sendStatistics): Use pieceStorage, btRuntime.
(afterEachIteration): New function.
* src/AnnounceList: AnnounceTier->AnnounceTierHandle.
* src/Directry.h
(Directory): New function.
(DirectoryHandle): New type definition.
* src/BtProgressInfoFile.h: New file.
* src/RequestMessage.cc: Use pieceStorage.
* src/BtRuntime.h: New file.
* src/DefaultBtContext.cc: New file.
* src/BitfieldMan.h
(getCompletedLength): New function(private).
(getCompletedLength): New function.
(getFilteredCompletedLength): New function.
* src/BitfieldMan.h
(getCompletedLength): New function(private).
(getCompletedLength): New function.
(getFilteredCompletedLength): New function.
* src/MultiDiskAdaptor.cc
(mkdir): New function.
(openFile): Call mkdir().
(initAndOpenFile): Call mkdir().
* src/CancelMessage.h
(TorrentMan.h): Removed.
* src/RejectMessage.h
(TorrentMan.h): Removed.
* src/DownloadEngineFactory.cc
(DefaultPieceStorage.h): New include.
(DefaultPeerStorage.h): New include.
(DefaultBtAnnounce.h): New include.
(DefaultBtProgressInfoFile.h): New include.
(newTorrentConsoleEngine): Rewritten.
* src/ShareRatioSeedCriteria.h
(torrentMan): Removed.
(btContext): New variable.
(peerStorage): New variable.
(btRuntime): New variable.
(evaluate): Use btContext, btRuntime, peerStorage.
* src/AnnounceTier.h: New file.
* src/BtAnnounce.h: New file.
* src/BtRegistry.h: New file.
* src/PeerInitiateConnectionCommand.h: Added btContext.
* src/PeerConnection.h (TorrentMan.h): Removed.
* src/PeerMessageFactory.cc: Use btContext, pieceStorage.
* src/Util.h
(slice): Added an argument.
* src/Util.cc
(slice): Added an argument to control whether trim is made or
not.
* src/PeerStorage.h: New file.
* src/BtRegistry.cc: New file.
* src/TrackerUpdateCommand.h: Made subclass of
BtContextAwareCommand.
* src/CopyDiskAdaptor.cc: Use the accessor methods of FileEntry.
* src/MultiDiskWriter.h: FileEntry -> FileEntryHandle
* src/PeerListenCommand.cc: Use btRuntime, peerStorage,
btContext.
* src/TorrentRequestInfo.h
(e): Removed.
(showFileEntry): Added an argument.
(getDownloadEngine): Return 0.
* src/DefaultBtAnnounce.h: New file.
* src/TorrentAutoSaveCommand.cc: Use btRuntime,
btProgressInfoFile.
* src/TrackerWatcherComand.cc: Use btRuntime, btAnnounce,
* src/PeerMessageFactory.h
(btContext): New variable.
(pieceStorage): New variable.
* src/TrackerUpdateCommand.cc: Use btRuntime, peerStorage,
btContext,
btAnnounce.
* src/DiskAdaptor.cc
(DiskAdaptor): Removed topDir.
(~DiskAdaptor): Removed topDir.
* src/PeerListenCommand.h: Made subclass of
BtContextAwareCommand.
* src/SeedCheckCommand.h: Made subclass of
BtContextAwareCommand.
* src/File.h (mkdirs): New function.
* src/DefaultPeerStorage): New file.
* src/DownloadEngineFactory.h
(newTorrentConsoleEngine): Use btContext.
* src/BtContextAwareCommand.cc: New file.
* src/PeerInitiateConnectionCommand.cc: Use btRuntime,
peerStorage.
* src/PeerMessage.h
(btContext): New variable.
(peerStorage): New variable.
(pieceStorage): New variable.
(setBtContext): New function.
* src/Directry.cc
(Directory): New function.
(createDir): Do nothing if name.size() == 0.
* src/AnnounceList.h
(AnnounceTier): Removed.
(AnnounceTiers): Removed.
* src/DefaultPieceStorage.h: New file.
* src/Piece.cc (toString): New function.
To fix typo:
* src/main.cc (showVersion): Fixed typo.
To fix compile warning:
* src/DelegatingPeerListProcessor.cc
(canHandle): Added "return false".
2006-11-05 15:04:17 +00:00
|
|
|
/* <!-- copyright */
|
|
|
|
/*
|
|
|
|
* aria2 - The high speed download utility
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006 Tatsuhiro Tsujikawa
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
2010-01-05 16:01:46 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2006-11-05 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
To divide TorrentMan into 6 classes: BtContext, BtRuntime,
PeerStorage, PieceStorage, BtAnnounce and BtProgressInfoFile
* src/TrackerWatcherComand.h: Made subclass of
BtContextAwareCommand.
* src/SeedCheckCommand.cc: Use pieceStorage, btRuntime
* src/PeerAbstractCommand.h: Made subclass of
BtContextAwareCommand.
* src/PeerAbstractCommand.cc: Use btRuntime.
* src/BtContextAwareCommand.h: New class.
* src/FileEntry.h: Added accessor methods for following
variables.
(path): Made private.
(length): Made private.
(offset): Made private.
(extracted): Made private.
(requested): Made private.
(FileEntries): New definition.
(FileEntryHandle): New definition.
* src/FileEntry.cc: New file.
* src/HaveEraseCommand.h: Made subclass of
BtContextAwareCommand.
* src/HaveEraseCommand.cc: Use btRuntime, pieceStorage.
* src/PeerChokeCommand.h: Made subclass of
BtContextAwareCommand.
* src/PeerChokeCommand.cc: Use btRuntime, peerStorage,
pieceStorage.
* src/PieceStorage.h: New file.
* src/PeerInteractionCommand.h: Use btContext.
* src/PeerInteractionCommand.cc: Use pieceStorage, peerStorage,
btRuntime.
* src/DefaultBtProgressInfoFile.h: New file.
* src/DefaultBtProgressInfoFile.cc: New file.
* src/File.cc
(Util.h): New include.
(mkdirs): New function.
* src/MultiDiskAdaptor.h
(mkdir): New function.
* src/PeerListProcessor.h
(Peers): Removed.
* src/PeerInteraction.h
(torrentMan): Removed.
(btContext): New variable.
(peerStorage): New variable.
(pieceStorage): New variable.
(btAnnounce): New variable.
(getTorrentMan): Removed.
(getBtContext): New function.
* src/PeerInteraction.cc: Use btContext, peerStorage,
pieceStorage,
btAnnounce.
* src/HandshakeMessage.h
(TorrentMan.h): Removed.
* src/HandshakeMessage.cc: Use btContext.
* src/DefaultBtAnnounce.cc: New file.
* src/MultiDiskWriter.cc: Use the accessor methods of FileEntry.
* src/DefaultPieceStorage.cc: New file.
* src/DefaultBtContext.h: New file.
* src/TorrentRequestInfo.cc: Use btContext, pieceStorage.
Use the accessor methods of FileEntry.
* src/CookieBox.cc: Updated to use Util::slice().
* src/PieceMessage.cc: Use btContext, pieceStorage.
* src/common.h (SharedHandle.h): New include.
* src/PeerMessage.cc (PeerMessage): Added btContext,
peerStorage,
pieceStorage.
* src/TorrentAutoSaveCommand.h: Made subclass of
BtContextAwareCommand.
* src/DiskAdaptor.h
(topDir): Removed.
(getFileEntryFromPath): Changed the return type to
FileEntryHandle.
(setTopDir): Removed.
(getTopDir): Removed.
* src/BtContext.h: New file.
* src/DefaultPeerStorage.h: New file.
* src/PieceMessage.h (TorrentMan.h): Removed.
* src/RequestMessage.h (TorrentMan.h): Removed.
* src/TorrentDownloadEngine.h
(uploadLength): New variable.
(btContext): New variable.
(btRuntime): New variable.
(pieceStorage): New variable.
(peerStorage): New variable.
(btAnnounce): New variable.
(btProgressInfoFile): New variable.
(torrentMan): Removed.
(setBtContext): New function.
* src/TorrentDownloadEngine.cc: Use BtContext, BtRuntime,
pieceStorage,
peerStorage, btAnnounce, btProgressInfoFile.
* src/Piece.h
(toString): New function.
(Pieces): New type definition.
* src/Peer.h
(active): New variable.
(Peer): Added active.
(activate): Set active to true.
(deactivate): Set active to false.
(isActive): New function.
(Peers): New type definition.
* src/DirectDiskAdaptor.cc
(getFilePath): Use the accessor methods of FileEntry.
* src/TorrentConsoleDownloadEngine.h
(afterEachIteration): New function.
* src/TorrentConsoleDownloadEngine.cc
(haltRequested): New variable.
(sendStatistics): Use pieceStorage, btRuntime.
(afterEachIteration): New function.
* src/AnnounceList: AnnounceTier->AnnounceTierHandle.
* src/Directry.h
(Directory): New function.
(DirectoryHandle): New type definition.
* src/BtProgressInfoFile.h: New file.
* src/RequestMessage.cc: Use pieceStorage.
* src/BtRuntime.h: New file.
* src/DefaultBtContext.cc: New file.
* src/BitfieldMan.h
(getCompletedLength): New function(private).
(getCompletedLength): New function.
(getFilteredCompletedLength): New function.
* src/BitfieldMan.h
(getCompletedLength): New function(private).
(getCompletedLength): New function.
(getFilteredCompletedLength): New function.
* src/MultiDiskAdaptor.cc
(mkdir): New function.
(openFile): Call mkdir().
(initAndOpenFile): Call mkdir().
* src/CancelMessage.h
(TorrentMan.h): Removed.
* src/RejectMessage.h
(TorrentMan.h): Removed.
* src/DownloadEngineFactory.cc
(DefaultPieceStorage.h): New include.
(DefaultPeerStorage.h): New include.
(DefaultBtAnnounce.h): New include.
(DefaultBtProgressInfoFile.h): New include.
(newTorrentConsoleEngine): Rewritten.
* src/ShareRatioSeedCriteria.h
(torrentMan): Removed.
(btContext): New variable.
(peerStorage): New variable.
(btRuntime): New variable.
(evaluate): Use btContext, btRuntime, peerStorage.
* src/AnnounceTier.h: New file.
* src/BtAnnounce.h: New file.
* src/BtRegistry.h: New file.
* src/PeerInitiateConnectionCommand.h: Added btContext.
* src/PeerConnection.h (TorrentMan.h): Removed.
* src/PeerMessageFactory.cc: Use btContext, pieceStorage.
* src/Util.h
(slice): Added an argument.
* src/Util.cc
(slice): Added an argument to control whether trim is made or
not.
* src/PeerStorage.h: New file.
* src/BtRegistry.cc: New file.
* src/TrackerUpdateCommand.h: Made subclass of
BtContextAwareCommand.
* src/CopyDiskAdaptor.cc: Use the accessor methods of FileEntry.
* src/MultiDiskWriter.h: FileEntry -> FileEntryHandle
* src/PeerListenCommand.cc: Use btRuntime, peerStorage,
btContext.
* src/TorrentRequestInfo.h
(e): Removed.
(showFileEntry): Added an argument.
(getDownloadEngine): Return 0.
* src/DefaultBtAnnounce.h: New file.
* src/TorrentAutoSaveCommand.cc: Use btRuntime,
btProgressInfoFile.
* src/TrackerWatcherComand.cc: Use btRuntime, btAnnounce,
* src/PeerMessageFactory.h
(btContext): New variable.
(pieceStorage): New variable.
* src/TrackerUpdateCommand.cc: Use btRuntime, peerStorage,
btContext,
btAnnounce.
* src/DiskAdaptor.cc
(DiskAdaptor): Removed topDir.
(~DiskAdaptor): Removed topDir.
* src/PeerListenCommand.h: Made subclass of
BtContextAwareCommand.
* src/SeedCheckCommand.h: Made subclass of
BtContextAwareCommand.
* src/File.h (mkdirs): New function.
* src/DefaultPeerStorage): New file.
* src/DownloadEngineFactory.h
(newTorrentConsoleEngine): Use btContext.
* src/BtContextAwareCommand.cc: New file.
* src/PeerInitiateConnectionCommand.cc: Use btRuntime,
peerStorage.
* src/PeerMessage.h
(btContext): New variable.
(peerStorage): New variable.
(pieceStorage): New variable.
(setBtContext): New function.
* src/Directry.cc
(Directory): New function.
(createDir): Do nothing if name.size() == 0.
* src/AnnounceList.h
(AnnounceTier): Removed.
(AnnounceTiers): Removed.
* src/DefaultPieceStorage.h: New file.
* src/Piece.cc (toString): New function.
To fix typo:
* src/main.cc (showVersion): Fixed typo.
To fix compile warning:
* src/DelegatingPeerListProcessor.cc
(canHandle): Added "return false".
2006-11-05 15:04:17 +00:00
|
|
|
*
|
|
|
|
* In addition, as a special exception, the copyright holders give
|
|
|
|
* permission to link the code of portions of this program with the
|
|
|
|
* OpenSSL library under certain conditions as described in each
|
|
|
|
* individual source file, and distribute linked combinations
|
|
|
|
* including the two.
|
|
|
|
* You must obey the GNU General Public License in all respects
|
|
|
|
* for all of the code used other than OpenSSL. If you modify
|
|
|
|
* file(s) with this exception, you may extend this exception to your
|
|
|
|
* version of the file(s), but you are not obligated to do so. If you
|
|
|
|
* do not wish to do so, delete this exception statement from your
|
|
|
|
* version. If you delete this exception statement from all source
|
|
|
|
* files in the program, then also delete it here.
|
|
|
|
*/
|
|
|
|
/* copyright --> */
|
|
|
|
#include "FileEntry.h"
|
2009-06-23 15:35:45 +00:00
|
|
|
|
|
|
|
#include <cassert>
|
2009-06-29 08:42:58 +00:00
|
|
|
#include <algorithm>
|
2009-06-23 15:35:45 +00:00
|
|
|
|
2009-10-22 15:35:33 +00:00
|
|
|
#include "util.h"
|
2009-06-23 15:35:45 +00:00
|
|
|
#include "URISelector.h"
|
2010-11-20 08:21:36 +00:00
|
|
|
#include "Logger.h"
|
2009-06-29 08:42:58 +00:00
|
|
|
#include "LogFactory.h"
|
2010-03-06 08:29:53 +00:00
|
|
|
#include "wallclock.h"
|
2010-07-15 13:49:02 +00:00
|
|
|
#include "a2algo.h"
|
2010-09-11 12:09:19 +00:00
|
|
|
#include "uri.h"
|
2010-11-14 07:17:55 +00:00
|
|
|
#include "PeerStat.h"
|
2010-11-20 08:21:36 +00:00
|
|
|
#include "fmt.h"
|
2011-06-27 15:18:53 +00:00
|
|
|
#include "ServerStatMan.h"
|
|
|
|
#include "ServerStat.h"
|
2006-11-05 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
To divide TorrentMan into 6 classes: BtContext, BtRuntime,
PeerStorage, PieceStorage, BtAnnounce and BtProgressInfoFile
* src/TrackerWatcherComand.h: Made subclass of
BtContextAwareCommand.
* src/SeedCheckCommand.cc: Use pieceStorage, btRuntime
* src/PeerAbstractCommand.h: Made subclass of
BtContextAwareCommand.
* src/PeerAbstractCommand.cc: Use btRuntime.
* src/BtContextAwareCommand.h: New class.
* src/FileEntry.h: Added accessor methods for following
variables.
(path): Made private.
(length): Made private.
(offset): Made private.
(extracted): Made private.
(requested): Made private.
(FileEntries): New definition.
(FileEntryHandle): New definition.
* src/FileEntry.cc: New file.
* src/HaveEraseCommand.h: Made subclass of
BtContextAwareCommand.
* src/HaveEraseCommand.cc: Use btRuntime, pieceStorage.
* src/PeerChokeCommand.h: Made subclass of
BtContextAwareCommand.
* src/PeerChokeCommand.cc: Use btRuntime, peerStorage,
pieceStorage.
* src/PieceStorage.h: New file.
* src/PeerInteractionCommand.h: Use btContext.
* src/PeerInteractionCommand.cc: Use pieceStorage, peerStorage,
btRuntime.
* src/DefaultBtProgressInfoFile.h: New file.
* src/DefaultBtProgressInfoFile.cc: New file.
* src/File.cc
(Util.h): New include.
(mkdirs): New function.
* src/MultiDiskAdaptor.h
(mkdir): New function.
* src/PeerListProcessor.h
(Peers): Removed.
* src/PeerInteraction.h
(torrentMan): Removed.
(btContext): New variable.
(peerStorage): New variable.
(pieceStorage): New variable.
(btAnnounce): New variable.
(getTorrentMan): Removed.
(getBtContext): New function.
* src/PeerInteraction.cc: Use btContext, peerStorage,
pieceStorage,
btAnnounce.
* src/HandshakeMessage.h
(TorrentMan.h): Removed.
* src/HandshakeMessage.cc: Use btContext.
* src/DefaultBtAnnounce.cc: New file.
* src/MultiDiskWriter.cc: Use the accessor methods of FileEntry.
* src/DefaultPieceStorage.cc: New file.
* src/DefaultBtContext.h: New file.
* src/TorrentRequestInfo.cc: Use btContext, pieceStorage.
Use the accessor methods of FileEntry.
* src/CookieBox.cc: Updated to use Util::slice().
* src/PieceMessage.cc: Use btContext, pieceStorage.
* src/common.h (SharedHandle.h): New include.
* src/PeerMessage.cc (PeerMessage): Added btContext,
peerStorage,
pieceStorage.
* src/TorrentAutoSaveCommand.h: Made subclass of
BtContextAwareCommand.
* src/DiskAdaptor.h
(topDir): Removed.
(getFileEntryFromPath): Changed the return type to
FileEntryHandle.
(setTopDir): Removed.
(getTopDir): Removed.
* src/BtContext.h: New file.
* src/DefaultPeerStorage.h: New file.
* src/PieceMessage.h (TorrentMan.h): Removed.
* src/RequestMessage.h (TorrentMan.h): Removed.
* src/TorrentDownloadEngine.h
(uploadLength): New variable.
(btContext): New variable.
(btRuntime): New variable.
(pieceStorage): New variable.
(peerStorage): New variable.
(btAnnounce): New variable.
(btProgressInfoFile): New variable.
(torrentMan): Removed.
(setBtContext): New function.
* src/TorrentDownloadEngine.cc: Use BtContext, BtRuntime,
pieceStorage,
peerStorage, btAnnounce, btProgressInfoFile.
* src/Piece.h
(toString): New function.
(Pieces): New type definition.
* src/Peer.h
(active): New variable.
(Peer): Added active.
(activate): Set active to true.
(deactivate): Set active to false.
(isActive): New function.
(Peers): New type definition.
* src/DirectDiskAdaptor.cc
(getFilePath): Use the accessor methods of FileEntry.
* src/TorrentConsoleDownloadEngine.h
(afterEachIteration): New function.
* src/TorrentConsoleDownloadEngine.cc
(haltRequested): New variable.
(sendStatistics): Use pieceStorage, btRuntime.
(afterEachIteration): New function.
* src/AnnounceList: AnnounceTier->AnnounceTierHandle.
* src/Directry.h
(Directory): New function.
(DirectoryHandle): New type definition.
* src/BtProgressInfoFile.h: New file.
* src/RequestMessage.cc: Use pieceStorage.
* src/BtRuntime.h: New file.
* src/DefaultBtContext.cc: New file.
* src/BitfieldMan.h
(getCompletedLength): New function(private).
(getCompletedLength): New function.
(getFilteredCompletedLength): New function.
* src/BitfieldMan.h
(getCompletedLength): New function(private).
(getCompletedLength): New function.
(getFilteredCompletedLength): New function.
* src/MultiDiskAdaptor.cc
(mkdir): New function.
(openFile): Call mkdir().
(initAndOpenFile): Call mkdir().
* src/CancelMessage.h
(TorrentMan.h): Removed.
* src/RejectMessage.h
(TorrentMan.h): Removed.
* src/DownloadEngineFactory.cc
(DefaultPieceStorage.h): New include.
(DefaultPeerStorage.h): New include.
(DefaultBtAnnounce.h): New include.
(DefaultBtProgressInfoFile.h): New include.
(newTorrentConsoleEngine): Rewritten.
* src/ShareRatioSeedCriteria.h
(torrentMan): Removed.
(btContext): New variable.
(peerStorage): New variable.
(btRuntime): New variable.
(evaluate): Use btContext, btRuntime, peerStorage.
* src/AnnounceTier.h: New file.
* src/BtAnnounce.h: New file.
* src/BtRegistry.h: New file.
* src/PeerInitiateConnectionCommand.h: Added btContext.
* src/PeerConnection.h (TorrentMan.h): Removed.
* src/PeerMessageFactory.cc: Use btContext, pieceStorage.
* src/Util.h
(slice): Added an argument.
* src/Util.cc
(slice): Added an argument to control whether trim is made or
not.
* src/PeerStorage.h: New file.
* src/BtRegistry.cc: New file.
* src/TrackerUpdateCommand.h: Made subclass of
BtContextAwareCommand.
* src/CopyDiskAdaptor.cc: Use the accessor methods of FileEntry.
* src/MultiDiskWriter.h: FileEntry -> FileEntryHandle
* src/PeerListenCommand.cc: Use btRuntime, peerStorage,
btContext.
* src/TorrentRequestInfo.h
(e): Removed.
(showFileEntry): Added an argument.
(getDownloadEngine): Return 0.
* src/DefaultBtAnnounce.h: New file.
* src/TorrentAutoSaveCommand.cc: Use btRuntime,
btProgressInfoFile.
* src/TrackerWatcherComand.cc: Use btRuntime, btAnnounce,
* src/PeerMessageFactory.h
(btContext): New variable.
(pieceStorage): New variable.
* src/TrackerUpdateCommand.cc: Use btRuntime, peerStorage,
btContext,
btAnnounce.
* src/DiskAdaptor.cc
(DiskAdaptor): Removed topDir.
(~DiskAdaptor): Removed topDir.
* src/PeerListenCommand.h: Made subclass of
BtContextAwareCommand.
* src/SeedCheckCommand.h: Made subclass of
BtContextAwareCommand.
* src/File.h (mkdirs): New function.
* src/DefaultPeerStorage): New file.
* src/DownloadEngineFactory.h
(newTorrentConsoleEngine): Use btContext.
* src/BtContextAwareCommand.cc: New file.
* src/PeerInitiateConnectionCommand.cc: Use btRuntime,
peerStorage.
* src/PeerMessage.h
(btContext): New variable.
(peerStorage): New variable.
(pieceStorage): New variable.
(setBtContext): New function.
* src/Directry.cc
(Directory): New function.
(createDir): Do nothing if name.size() == 0.
* src/AnnounceList.h
(AnnounceTier): Removed.
(AnnounceTiers): Removed.
* src/DefaultPieceStorage.h: New file.
* src/Piece.cc (toString): New function.
To fix typo:
* src/main.cc (showVersion): Fixed typo.
To fix compile warning:
* src/DelegatingPeerListProcessor.cc
(canHandle): Added "return false".
2006-11-05 15:04:17 +00:00
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
namespace aria2 {
|
|
|
|
|
2012-03-22 16:34:37 +00:00
|
|
|
bool FileEntry::RequestFaster::operator()
|
|
|
|
(const SharedHandle<Request>& lhs,
|
|
|
|
const SharedHandle<Request>& rhs) const
|
|
|
|
{
|
|
|
|
if(!lhs->getPeerStat()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(!rhs->getPeerStat()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
int lspd = lhs->getPeerStat()->getAvgDownloadSpeed();
|
|
|
|
int rspd = rhs->getPeerStat()->getAvgDownloadSpeed();
|
|
|
|
return lspd > rspd || (lspd == rspd && lhs.get() < rhs.get());
|
|
|
|
}
|
|
|
|
|
2010-11-20 08:21:36 +00:00
|
|
|
FileEntry::FileEntry
|
|
|
|
(const std::string& path,
|
2012-06-25 14:35:24 +00:00
|
|
|
int64_t length,
|
|
|
|
int64_t offset,
|
2010-11-20 08:21:36 +00:00
|
|
|
const std::vector<std::string>& uris)
|
|
|
|
: path_(path),
|
|
|
|
uris_(uris.begin(), uris.end()),
|
|
|
|
length_(length),
|
|
|
|
offset_(offset),
|
|
|
|
requested_(true),
|
|
|
|
uniqueProtocol_(false),
|
|
|
|
maxConnectionPerServer_(1),
|
|
|
|
lastFasterReplace_(0)
|
|
|
|
{}
|
2009-06-29 08:42:58 +00:00
|
|
|
|
2010-11-20 08:21:36 +00:00
|
|
|
FileEntry::FileEntry()
|
|
|
|
: length_(0),
|
|
|
|
offset_(0),
|
|
|
|
requested_(false),
|
|
|
|
uniqueProtocol_(false),
|
|
|
|
maxConnectionPerServer_(1)
|
|
|
|
{}
|
2006-11-05 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
To divide TorrentMan into 6 classes: BtContext, BtRuntime,
PeerStorage, PieceStorage, BtAnnounce and BtProgressInfoFile
* src/TrackerWatcherComand.h: Made subclass of
BtContextAwareCommand.
* src/SeedCheckCommand.cc: Use pieceStorage, btRuntime
* src/PeerAbstractCommand.h: Made subclass of
BtContextAwareCommand.
* src/PeerAbstractCommand.cc: Use btRuntime.
* src/BtContextAwareCommand.h: New class.
* src/FileEntry.h: Added accessor methods for following
variables.
(path): Made private.
(length): Made private.
(offset): Made private.
(extracted): Made private.
(requested): Made private.
(FileEntries): New definition.
(FileEntryHandle): New definition.
* src/FileEntry.cc: New file.
* src/HaveEraseCommand.h: Made subclass of
BtContextAwareCommand.
* src/HaveEraseCommand.cc: Use btRuntime, pieceStorage.
* src/PeerChokeCommand.h: Made subclass of
BtContextAwareCommand.
* src/PeerChokeCommand.cc: Use btRuntime, peerStorage,
pieceStorage.
* src/PieceStorage.h: New file.
* src/PeerInteractionCommand.h: Use btContext.
* src/PeerInteractionCommand.cc: Use pieceStorage, peerStorage,
btRuntime.
* src/DefaultBtProgressInfoFile.h: New file.
* src/DefaultBtProgressInfoFile.cc: New file.
* src/File.cc
(Util.h): New include.
(mkdirs): New function.
* src/MultiDiskAdaptor.h
(mkdir): New function.
* src/PeerListProcessor.h
(Peers): Removed.
* src/PeerInteraction.h
(torrentMan): Removed.
(btContext): New variable.
(peerStorage): New variable.
(pieceStorage): New variable.
(btAnnounce): New variable.
(getTorrentMan): Removed.
(getBtContext): New function.
* src/PeerInteraction.cc: Use btContext, peerStorage,
pieceStorage,
btAnnounce.
* src/HandshakeMessage.h
(TorrentMan.h): Removed.
* src/HandshakeMessage.cc: Use btContext.
* src/DefaultBtAnnounce.cc: New file.
* src/MultiDiskWriter.cc: Use the accessor methods of FileEntry.
* src/DefaultPieceStorage.cc: New file.
* src/DefaultBtContext.h: New file.
* src/TorrentRequestInfo.cc: Use btContext, pieceStorage.
Use the accessor methods of FileEntry.
* src/CookieBox.cc: Updated to use Util::slice().
* src/PieceMessage.cc: Use btContext, pieceStorage.
* src/common.h (SharedHandle.h): New include.
* src/PeerMessage.cc (PeerMessage): Added btContext,
peerStorage,
pieceStorage.
* src/TorrentAutoSaveCommand.h: Made subclass of
BtContextAwareCommand.
* src/DiskAdaptor.h
(topDir): Removed.
(getFileEntryFromPath): Changed the return type to
FileEntryHandle.
(setTopDir): Removed.
(getTopDir): Removed.
* src/BtContext.h: New file.
* src/DefaultPeerStorage.h: New file.
* src/PieceMessage.h (TorrentMan.h): Removed.
* src/RequestMessage.h (TorrentMan.h): Removed.
* src/TorrentDownloadEngine.h
(uploadLength): New variable.
(btContext): New variable.
(btRuntime): New variable.
(pieceStorage): New variable.
(peerStorage): New variable.
(btAnnounce): New variable.
(btProgressInfoFile): New variable.
(torrentMan): Removed.
(setBtContext): New function.
* src/TorrentDownloadEngine.cc: Use BtContext, BtRuntime,
pieceStorage,
peerStorage, btAnnounce, btProgressInfoFile.
* src/Piece.h
(toString): New function.
(Pieces): New type definition.
* src/Peer.h
(active): New variable.
(Peer): Added active.
(activate): Set active to true.
(deactivate): Set active to false.
(isActive): New function.
(Peers): New type definition.
* src/DirectDiskAdaptor.cc
(getFilePath): Use the accessor methods of FileEntry.
* src/TorrentConsoleDownloadEngine.h
(afterEachIteration): New function.
* src/TorrentConsoleDownloadEngine.cc
(haltRequested): New variable.
(sendStatistics): Use pieceStorage, btRuntime.
(afterEachIteration): New function.
* src/AnnounceList: AnnounceTier->AnnounceTierHandle.
* src/Directry.h
(Directory): New function.
(DirectoryHandle): New type definition.
* src/BtProgressInfoFile.h: New file.
* src/RequestMessage.cc: Use pieceStorage.
* src/BtRuntime.h: New file.
* src/DefaultBtContext.cc: New file.
* src/BitfieldMan.h
(getCompletedLength): New function(private).
(getCompletedLength): New function.
(getFilteredCompletedLength): New function.
* src/BitfieldMan.h
(getCompletedLength): New function(private).
(getCompletedLength): New function.
(getFilteredCompletedLength): New function.
* src/MultiDiskAdaptor.cc
(mkdir): New function.
(openFile): Call mkdir().
(initAndOpenFile): Call mkdir().
* src/CancelMessage.h
(TorrentMan.h): Removed.
* src/RejectMessage.h
(TorrentMan.h): Removed.
* src/DownloadEngineFactory.cc
(DefaultPieceStorage.h): New include.
(DefaultPeerStorage.h): New include.
(DefaultBtAnnounce.h): New include.
(DefaultBtProgressInfoFile.h): New include.
(newTorrentConsoleEngine): Rewritten.
* src/ShareRatioSeedCriteria.h
(torrentMan): Removed.
(btContext): New variable.
(peerStorage): New variable.
(btRuntime): New variable.
(evaluate): Use btContext, btRuntime, peerStorage.
* src/AnnounceTier.h: New file.
* src/BtAnnounce.h: New file.
* src/BtRegistry.h: New file.
* src/PeerInitiateConnectionCommand.h: Added btContext.
* src/PeerConnection.h (TorrentMan.h): Removed.
* src/PeerMessageFactory.cc: Use btContext, pieceStorage.
* src/Util.h
(slice): Added an argument.
* src/Util.cc
(slice): Added an argument to control whether trim is made or
not.
* src/PeerStorage.h: New file.
* src/BtRegistry.cc: New file.
* src/TrackerUpdateCommand.h: Made subclass of
BtContextAwareCommand.
* src/CopyDiskAdaptor.cc: Use the accessor methods of FileEntry.
* src/MultiDiskWriter.h: FileEntry -> FileEntryHandle
* src/PeerListenCommand.cc: Use btRuntime, peerStorage,
btContext.
* src/TorrentRequestInfo.h
(e): Removed.
(showFileEntry): Added an argument.
(getDownloadEngine): Return 0.
* src/DefaultBtAnnounce.h: New file.
* src/TorrentAutoSaveCommand.cc: Use btRuntime,
btProgressInfoFile.
* src/TrackerWatcherComand.cc: Use btRuntime, btAnnounce,
* src/PeerMessageFactory.h
(btContext): New variable.
(pieceStorage): New variable.
* src/TrackerUpdateCommand.cc: Use btRuntime, peerStorage,
btContext,
btAnnounce.
* src/DiskAdaptor.cc
(DiskAdaptor): Removed topDir.
(~DiskAdaptor): Removed topDir.
* src/PeerListenCommand.h: Made subclass of
BtContextAwareCommand.
* src/SeedCheckCommand.h: Made subclass of
BtContextAwareCommand.
* src/File.h (mkdirs): New function.
* src/DefaultPeerStorage): New file.
* src/DownloadEngineFactory.h
(newTorrentConsoleEngine): Use btContext.
* src/BtContextAwareCommand.cc: New file.
* src/PeerInitiateConnectionCommand.cc: Use btRuntime,
peerStorage.
* src/PeerMessage.h
(btContext): New variable.
(peerStorage): New variable.
(pieceStorage): New variable.
(setBtContext): New function.
* src/Directry.cc
(Directory): New function.
(createDir): Do nothing if name.size() == 0.
* src/AnnounceList.h
(AnnounceTier): Removed.
(AnnounceTiers): Removed.
* src/DefaultPieceStorage.h: New file.
* src/Piece.cc (toString): New function.
To fix typo:
* src/main.cc (showVersion): Fixed typo.
To fix compile warning:
* src/DelegatingPeerListProcessor.cc
(canHandle): Added "return false".
2006-11-05 15:04:17 +00:00
|
|
|
|
|
|
|
FileEntry::~FileEntry() {}
|
|
|
|
|
2007-10-11 16:58:24 +00:00
|
|
|
FileEntry& FileEntry::operator=(const FileEntry& entry)
|
|
|
|
{
|
|
|
|
if(this != &entry) {
|
2010-06-21 13:51:56 +00:00
|
|
|
path_ = entry.path_;
|
|
|
|
length_ = entry.length_;
|
|
|
|
offset_ = entry.offset_;
|
|
|
|
requested_ = entry.requested_;
|
2007-10-11 16:58:24 +00:00
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
2007-11-21 16:14:40 +00:00
|
|
|
|
|
|
|
bool FileEntry::operator<(const FileEntry& fileEntry) const
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
return offset_ < fileEntry.offset_;
|
2007-11-21 16:14:40 +00:00
|
|
|
}
|
2008-02-08 15:53:45 +00:00
|
|
|
|
2009-03-11 15:37:40 +00:00
|
|
|
bool FileEntry::exists() const
|
|
|
|
{
|
|
|
|
return File(getPath()).exists();
|
|
|
|
}
|
|
|
|
|
2012-06-25 14:35:24 +00:00
|
|
|
int64_t FileEntry::gtoloff(int64_t goff) const
|
2009-06-23 15:35:45 +00:00
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
assert(offset_ <= goff);
|
|
|
|
return goff-offset_;
|
2009-06-23 15:35:45 +00:00
|
|
|
}
|
|
|
|
|
2010-02-28 12:30:11 +00:00
|
|
|
void FileEntry::getUris(std::vector<std::string>& uris) const
|
2009-06-23 15:35:45 +00:00
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
uris.insert(uris.end(), spentUris_.begin(), spentUris_.end());
|
|
|
|
uris.insert(uris.end(), uris_.begin(), uris_.end());
|
2009-06-23 15:35:45 +00:00
|
|
|
}
|
|
|
|
|
2010-10-30 16:02:15 +00:00
|
|
|
namespace {
|
2010-07-31 14:02:24 +00:00
|
|
|
template<typename InputIterator, typename OutputIterator>
|
2010-10-30 16:02:15 +00:00
|
|
|
OutputIterator enumerateInFlightHosts
|
2010-07-31 14:02:24 +00:00
|
|
|
(InputIterator first, InputIterator last, OutputIterator out)
|
2009-06-29 16:43:41 +00:00
|
|
|
{
|
|
|
|
for(; first != last; ++first) {
|
2012-10-14 09:05:34 +00:00
|
|
|
uri_split_result us;
|
|
|
|
if(uri_split(&us, (*first)->getUri().c_str()) == 0) {
|
|
|
|
*out++ = uri::getFieldString(us, USR_HOST, (*first)->getUri().c_str());
|
2010-09-11 12:09:19 +00:00
|
|
|
}
|
2009-06-29 16:43:41 +00:00
|
|
|
}
|
2010-07-31 14:02:24 +00:00
|
|
|
return out;
|
2009-06-29 16:43:41 +00:00
|
|
|
}
|
2010-10-30 16:02:15 +00:00
|
|
|
} // namespace
|
2009-06-29 16:43:41 +00:00
|
|
|
|
2009-06-23 15:35:45 +00:00
|
|
|
SharedHandle<Request>
|
2009-07-01 14:11:04 +00:00
|
|
|
FileEntry::getRequest
|
|
|
|
(const SharedHandle<URISelector>& selector,
|
2010-07-14 11:39:05 +00:00
|
|
|
bool uriReuse,
|
2010-07-15 13:49:02 +00:00
|
|
|
const std::vector<std::pair<size_t, std::string> >& usedHosts,
|
2009-07-01 14:11:04 +00:00
|
|
|
const std::string& referer,
|
|
|
|
const std::string& method)
|
2009-06-23 15:35:45 +00:00
|
|
|
{
|
|
|
|
SharedHandle<Request> req;
|
2010-08-31 11:17:38 +00:00
|
|
|
if(requestPool_.empty()) {
|
|
|
|
std::vector<std::string> inFlightHosts;
|
|
|
|
enumerateInFlightHosts(inFlightRequests_.begin(), inFlightRequests_.end(),
|
|
|
|
std::back_inserter(inFlightHosts));
|
|
|
|
for(int g = 0; g < 2; ++g) {
|
|
|
|
std::vector<std::string> pending;
|
|
|
|
std::vector<std::string> ignoreHost;
|
|
|
|
while(1) {
|
|
|
|
std::string uri = selector->select(this, usedHosts);
|
|
|
|
if(uri.empty()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
req.reset(new Request());
|
|
|
|
if(req->setUri(uri)) {
|
|
|
|
if(std::count(inFlightHosts.begin(),
|
|
|
|
inFlightHosts.end(),req->getHost())
|
2011-12-08 14:26:51 +00:00
|
|
|
>= maxConnectionPerServer_) {
|
2010-08-31 11:17:38 +00:00
|
|
|
pending.push_back(uri);
|
|
|
|
ignoreHost.push_back(req->getHost());
|
|
|
|
req.reset();
|
|
|
|
continue;
|
|
|
|
}
|
2013-01-08 12:42:42 +00:00
|
|
|
if(referer == "*") {
|
|
|
|
// Assuming uri has already been percent-encoded.
|
|
|
|
req->setReferer(uri);
|
|
|
|
} else {
|
|
|
|
req->setReferer(util::percentEncodeMini(referer));
|
|
|
|
}
|
2010-08-31 11:17:38 +00:00
|
|
|
req->setMethod(method);
|
|
|
|
spentUris_.push_back(uri);
|
2012-03-22 16:34:37 +00:00
|
|
|
inFlightRequests_.insert(req);
|
2010-08-31 11:17:38 +00:00
|
|
|
break;
|
|
|
|
} else {
|
2010-07-14 11:39:05 +00:00
|
|
|
req.reset();
|
2010-01-05 16:01:46 +00:00
|
|
|
}
|
2010-08-31 11:17:38 +00:00
|
|
|
}
|
|
|
|
uris_.insert(uris_.begin(), pending.begin(), pending.end());
|
2010-11-12 12:48:48 +00:00
|
|
|
if(g == 0 && uriReuse && !req && uris_.size() == pending.size()) {
|
2010-08-31 11:17:38 +00:00
|
|
|
// Reuse URIs other than ones in pending
|
|
|
|
reuseUri(ignoreHost);
|
2010-07-14 14:10:33 +00:00
|
|
|
} else {
|
2010-08-31 11:17:38 +00:00
|
|
|
break;
|
2010-07-14 14:10:33 +00:00
|
|
|
}
|
|
|
|
}
|
2011-01-17 12:19:45 +00:00
|
|
|
} else {
|
|
|
|
// Skip Request object if it is still
|
2011-08-09 16:17:28 +00:00
|
|
|
// sleeping(Request::getWakeTime() < global::wallclock()). If all
|
2011-01-17 12:19:45 +00:00
|
|
|
// pooled objects are sleeping, return first one. Caller should
|
|
|
|
// inspect returned object's getWakeTime().
|
2012-03-22 16:34:37 +00:00
|
|
|
RequestPool::iterator i = requestPool_.begin();
|
|
|
|
RequestPool::iterator eoi = requestPool_.end();
|
2011-01-17 12:19:45 +00:00
|
|
|
for(; i != eoi; ++i) {
|
2011-08-09 16:17:28 +00:00
|
|
|
if((*i)->getWakeTime() <= global::wallclock()) {
|
2011-01-17 12:19:45 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(i == eoi) {
|
|
|
|
i = requestPool_.begin();
|
|
|
|
}
|
|
|
|
req = *i;
|
|
|
|
requestPool_.erase(i);
|
2012-03-22 16:34:37 +00:00
|
|
|
inFlightRequests_.insert(req);
|
2010-11-20 08:21:36 +00:00
|
|
|
A2_LOG_DEBUG(fmt("Picked up from pool: %s", req->getUri().c_str()));
|
2009-06-23 15:35:45 +00:00
|
|
|
}
|
2009-06-29 16:43:41 +00:00
|
|
|
return req;
|
2009-06-23 15:35:45 +00:00
|
|
|
}
|
|
|
|
|
2009-06-29 15:18:21 +00:00
|
|
|
SharedHandle<Request>
|
|
|
|
FileEntry::findFasterRequest(const SharedHandle<Request>& base)
|
|
|
|
{
|
2010-07-11 14:17:05 +00:00
|
|
|
const int startupIdleTime = 10;
|
|
|
|
if(requestPool_.empty() ||
|
2011-08-09 16:17:28 +00:00
|
|
|
lastFasterReplace_.difference(global::wallclock()) < startupIdleTime) {
|
2009-06-29 15:18:21 +00:00
|
|
|
return SharedHandle<Request>();
|
|
|
|
}
|
2012-03-22 16:34:37 +00:00
|
|
|
const SharedHandle<PeerStat>& fastest =
|
|
|
|
(*requestPool_.begin())->getPeerStat();
|
2010-11-12 12:48:48 +00:00
|
|
|
if(!fastest) {
|
2009-06-29 15:18:21 +00:00
|
|
|
return SharedHandle<Request>();
|
|
|
|
}
|
|
|
|
const SharedHandle<PeerStat>& basestat = base->getPeerStat();
|
2009-07-11 15:57:06 +00:00
|
|
|
// TODO hard coded value. See PREF_STARTUP_IDLE_TIME
|
2010-11-12 12:48:48 +00:00
|
|
|
if(!basestat ||
|
2010-03-06 08:29:53 +00:00
|
|
|
(basestat->getDownloadStartTime().
|
2011-08-09 16:17:28 +00:00
|
|
|
difference(global::wallclock()) >= startupIdleTime &&
|
2009-06-29 15:18:21 +00:00
|
|
|
fastest->getAvgDownloadSpeed()*0.8 > basestat->calculateDownloadSpeed())){
|
2009-07-11 15:57:06 +00:00
|
|
|
// TODO we should consider that "fastest" is very slow.
|
2012-03-22 16:34:37 +00:00
|
|
|
SharedHandle<Request> fastestRequest = *requestPool_.begin();
|
|
|
|
requestPool_.erase(requestPool_.begin());
|
|
|
|
inFlightRequests_.insert(fastestRequest);
|
2011-08-09 16:17:28 +00:00
|
|
|
lastFasterReplace_ = global::wallclock();
|
2009-06-29 15:18:21 +00:00
|
|
|
return fastestRequest;
|
|
|
|
}
|
|
|
|
return SharedHandle<Request>();
|
|
|
|
}
|
|
|
|
|
2011-06-27 15:18:53 +00:00
|
|
|
SharedHandle<Request>
|
|
|
|
FileEntry::findFasterRequest
|
|
|
|
(const SharedHandle<Request>& base,
|
|
|
|
const std::vector<std::pair<size_t, std::string> >& usedHosts,
|
|
|
|
const SharedHandle<ServerStatMan>& serverStatMan)
|
|
|
|
{
|
|
|
|
const int startupIdleTime = 10;
|
2011-12-08 12:38:00 +00:00
|
|
|
const int SPEED_THRESHOLD = 20*1024;
|
2011-08-09 16:17:28 +00:00
|
|
|
if(lastFasterReplace_.difference(global::wallclock()) < startupIdleTime) {
|
2011-06-27 15:18:53 +00:00
|
|
|
return SharedHandle<Request>();
|
|
|
|
}
|
2011-06-28 12:27:31 +00:00
|
|
|
std::vector<std::string> inFlightHosts;
|
|
|
|
enumerateInFlightHosts(inFlightRequests_.begin(), inFlightRequests_.end(),
|
|
|
|
std::back_inserter(inFlightHosts));
|
2011-06-27 15:18:53 +00:00
|
|
|
const SharedHandle<PeerStat>& basestat = base->getPeerStat();
|
|
|
|
A2_LOG_DEBUG("Search faster server using ServerStat.");
|
|
|
|
// Use first 10 good URIs to introduce some randomness.
|
|
|
|
const size_t NUM_URI = 10;
|
|
|
|
std::vector<std::pair<SharedHandle<ServerStat>, std::string> > fastCands;
|
|
|
|
std::vector<std::string> normCands;
|
|
|
|
for(std::deque<std::string>::const_iterator i = uris_.begin(),
|
|
|
|
eoi = uris_.end(); i != eoi && fastCands.size() < NUM_URI; ++i) {
|
2012-10-14 09:05:34 +00:00
|
|
|
uri_split_result us;
|
|
|
|
if(uri_split(&us, (*i).c_str()) == -1) {
|
2011-06-27 15:18:53 +00:00
|
|
|
continue;
|
|
|
|
}
|
2012-10-14 09:05:34 +00:00
|
|
|
std::string host = uri::getFieldString(us, USR_HOST, (*i).c_str());
|
|
|
|
std::string protocol = uri::getFieldString(us, USR_SCHEME, (*i).c_str());
|
|
|
|
if(std::count(inFlightHosts.begin(), inFlightHosts.end(), host)
|
2011-12-08 14:26:51 +00:00
|
|
|
>= maxConnectionPerServer_) {
|
2011-06-28 12:27:31 +00:00
|
|
|
A2_LOG_DEBUG(fmt("%s has already used %d times, not considered.",
|
|
|
|
(*i).c_str(),
|
2011-12-08 14:26:51 +00:00
|
|
|
maxConnectionPerServer_));
|
2011-06-28 12:27:31 +00:00
|
|
|
continue;
|
|
|
|
}
|
2012-10-14 09:05:34 +00:00
|
|
|
if(findSecond(usedHosts.begin(), usedHosts.end(), host) !=
|
2011-06-27 15:18:53 +00:00
|
|
|
usedHosts.end()) {
|
|
|
|
A2_LOG_DEBUG(fmt("%s is in usedHosts, not considered", (*i).c_str()));
|
|
|
|
continue;
|
|
|
|
}
|
2012-10-14 09:05:34 +00:00
|
|
|
SharedHandle<ServerStat> ss = serverStatMan->find(host, protocol);
|
2011-06-27 15:18:53 +00:00
|
|
|
if(ss && ss->isOK()) {
|
|
|
|
if((basestat &&
|
|
|
|
ss->getDownloadSpeed() > basestat->calculateDownloadSpeed()*1.5) ||
|
|
|
|
(!basestat && ss->getDownloadSpeed() > SPEED_THRESHOLD)) {
|
|
|
|
fastCands.push_back(std::make_pair(ss, *i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!fastCands.empty()) {
|
|
|
|
std::sort(fastCands.begin(), fastCands.end(), ServerStatFaster());
|
|
|
|
SharedHandle<Request> fastestRequest(new Request());
|
2011-06-28 13:38:31 +00:00
|
|
|
const std::string& uri = fastCands.front().second;
|
|
|
|
A2_LOG_DEBUG(fmt("Selected %s from fastCands", uri.c_str()));
|
|
|
|
fastestRequest->setUri(uri);
|
2011-06-27 15:18:53 +00:00
|
|
|
fastestRequest->setReferer(base->getReferer());
|
2011-06-28 13:38:31 +00:00
|
|
|
uris_.erase(std::find(uris_.begin(), uris_.end(), uri));
|
|
|
|
spentUris_.push_back(uri);
|
2012-03-22 16:34:37 +00:00
|
|
|
inFlightRequests_.insert(fastestRequest);
|
2011-08-09 16:17:28 +00:00
|
|
|
lastFasterReplace_ = global::wallclock();
|
2011-06-27 15:18:53 +00:00
|
|
|
return fastestRequest;
|
|
|
|
}
|
|
|
|
A2_LOG_DEBUG("No faster server found.");
|
|
|
|
return SharedHandle<Request>();
|
|
|
|
}
|
|
|
|
|
2009-06-29 15:18:21 +00:00
|
|
|
void FileEntry::storePool(const SharedHandle<Request>& request)
|
|
|
|
{
|
|
|
|
const SharedHandle<PeerStat>& peerStat = request->getPeerStat();
|
2010-11-12 12:48:48 +00:00
|
|
|
if(peerStat) {
|
2009-06-29 15:18:21 +00:00
|
|
|
// We need to calculate average download speed here in order to
|
|
|
|
// store Request in the right position in the pool.
|
|
|
|
peerStat->calculateAvgDownloadSpeed();
|
|
|
|
}
|
2012-03-22 16:34:37 +00:00
|
|
|
requestPool_.insert(request);
|
2009-06-29 15:18:21 +00:00
|
|
|
}
|
|
|
|
|
2009-06-23 15:35:45 +00:00
|
|
|
void FileEntry::poolRequest(const SharedHandle<Request>& request)
|
|
|
|
{
|
|
|
|
removeRequest(request);
|
2010-03-06 14:21:43 +00:00
|
|
|
if(!request->removalRequested()) {
|
|
|
|
storePool(request);
|
|
|
|
}
|
2009-06-23 15:35:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool FileEntry::removeRequest(const SharedHandle<Request>& request)
|
|
|
|
{
|
2012-03-22 16:34:37 +00:00
|
|
|
return inFlightRequests_.erase(request) == 1;
|
2009-06-23 15:35:45 +00:00
|
|
|
}
|
|
|
|
|
2009-06-29 08:42:58 +00:00
|
|
|
void FileEntry::removeURIWhoseHostnameIs(const std::string& hostname)
|
|
|
|
{
|
|
|
|
std::deque<std::string> newURIs;
|
2010-06-21 13:51:56 +00:00
|
|
|
for(std::deque<std::string>::const_iterator itr = uris_.begin(),
|
|
|
|
eoi = uris_.end(); itr != eoi; ++itr) {
|
2012-10-14 09:05:34 +00:00
|
|
|
uri_split_result us;
|
|
|
|
if(uri_split(&us, (*itr).c_str()) == -1) {
|
2010-09-11 12:09:19 +00:00
|
|
|
continue;
|
|
|
|
}
|
2012-10-14 09:05:34 +00:00
|
|
|
if(us.fields[USR_HOST].len != hostname.size() ||
|
|
|
|
memcmp((*itr).c_str()+us.fields[USR_HOST].off, hostname.c_str(),
|
|
|
|
hostname.size()) != 0) {
|
2009-06-29 08:42:58 +00:00
|
|
|
newURIs.push_back(*itr);
|
|
|
|
}
|
|
|
|
}
|
2010-11-20 08:21:36 +00:00
|
|
|
A2_LOG_DEBUG(fmt("Removed %lu duplicate hostname URIs for path=%s",
|
2010-10-28 14:19:29 +00:00
|
|
|
static_cast<unsigned long>(uris_.size()-newURIs.size()),
|
2011-08-08 12:46:10 +00:00
|
|
|
getPath().c_str()));
|
2010-09-11 12:09:19 +00:00
|
|
|
uris_.swap(newURIs);
|
2009-06-29 08:42:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FileEntry::removeIdenticalURI(const std::string& uri)
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
uris_.erase(std::remove(uris_.begin(), uris_.end(), uri), uris_.end());
|
2009-06-29 08:42:58 +00:00
|
|
|
}
|
|
|
|
|
2010-11-28 07:52:02 +00:00
|
|
|
void FileEntry::addURIResult(std::string uri, error_code::Value result)
|
2009-06-29 08:42:58 +00:00
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
uriResults_.push_back(URIResult(uri, result));
|
2009-06-29 08:42:58 +00:00
|
|
|
}
|
|
|
|
|
2010-08-31 13:33:05 +00:00
|
|
|
namespace {
|
2009-06-29 08:42:58 +00:00
|
|
|
class FindURIResultByResult {
|
|
|
|
private:
|
2010-11-28 07:52:02 +00:00
|
|
|
error_code::Value r_;
|
2009-06-29 08:42:58 +00:00
|
|
|
public:
|
2010-11-28 07:52:02 +00:00
|
|
|
FindURIResultByResult(error_code::Value r):r_(r) {}
|
2009-06-29 08:42:58 +00:00
|
|
|
|
|
|
|
bool operator()(const URIResult& uriResult) const
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
return uriResult.getResult() == r_;
|
2009-06-29 08:42:58 +00:00
|
|
|
}
|
|
|
|
};
|
2010-10-30 14:53:40 +00:00
|
|
|
} // namespace
|
2009-06-29 08:42:58 +00:00
|
|
|
|
|
|
|
void FileEntry::extractURIResult
|
2010-11-28 07:52:02 +00:00
|
|
|
(std::deque<URIResult>& res, error_code::Value r)
|
2009-06-29 08:42:58 +00:00
|
|
|
{
|
|
|
|
std::deque<URIResult>::iterator i =
|
2010-06-21 13:51:56 +00:00
|
|
|
std::stable_partition(uriResults_.begin(), uriResults_.end(),
|
2010-01-05 16:01:46 +00:00
|
|
|
FindURIResultByResult(r));
|
2010-06-21 13:51:56 +00:00
|
|
|
std::copy(uriResults_.begin(), i, std::back_inserter(res));
|
|
|
|
uriResults_.erase(uriResults_.begin(), i);
|
2009-06-29 08:42:58 +00:00
|
|
|
}
|
|
|
|
|
2010-07-14 11:39:05 +00:00
|
|
|
void FileEntry::reuseUri(const std::vector<std::string>& ignore)
|
2009-07-04 13:36:56 +00:00
|
|
|
{
|
2010-11-20 08:21:36 +00:00
|
|
|
if(A2_LOG_DEBUG_ENABLED) {
|
2010-07-14 11:39:05 +00:00
|
|
|
for(std::vector<std::string>::const_iterator i = ignore.begin(),
|
|
|
|
eoi = ignore.end(); i != eoi; ++i) {
|
2010-11-20 08:21:36 +00:00
|
|
|
A2_LOG_DEBUG(fmt("ignore host=%s", (*i).c_str()));
|
2010-07-14 11:39:05 +00:00
|
|
|
}
|
|
|
|
}
|
2010-06-21 13:51:56 +00:00
|
|
|
std::deque<std::string> uris = spentUris_;
|
2009-07-04 13:36:56 +00:00
|
|
|
std::sort(uris.begin(), uris.end());
|
|
|
|
uris.erase(std::unique(uris.begin(), uris.end()), uris.end());
|
|
|
|
|
2010-06-21 13:51:56 +00:00
|
|
|
std::vector<std::string> errorUris(uriResults_.size());
|
|
|
|
std::transform(uriResults_.begin(), uriResults_.end(),
|
2010-01-05 16:01:46 +00:00
|
|
|
errorUris.begin(), std::mem_fun_ref(&URIResult::getURI));
|
2009-07-04 13:36:56 +00:00
|
|
|
std::sort(errorUris.begin(), errorUris.end());
|
|
|
|
errorUris.erase(std::unique(errorUris.begin(), errorUris.end()),
|
2010-01-05 16:01:46 +00:00
|
|
|
errorUris.end());
|
2010-11-20 08:21:36 +00:00
|
|
|
if(A2_LOG_DEBUG_ENABLED) {
|
2010-07-14 11:39:05 +00:00
|
|
|
for(std::vector<std::string>::const_iterator i = errorUris.begin(),
|
|
|
|
eoi = errorUris.end(); i != eoi; ++i) {
|
2010-11-20 08:21:36 +00:00
|
|
|
A2_LOG_DEBUG(fmt("error URI=%s", (*i).c_str()));
|
2010-07-14 11:39:05 +00:00
|
|
|
}
|
|
|
|
}
|
2010-02-28 12:30:11 +00:00
|
|
|
std::vector<std::string> reusableURIs;
|
2009-07-04 13:36:56 +00:00
|
|
|
std::set_difference(uris.begin(), uris.end(),
|
2010-01-05 16:01:46 +00:00
|
|
|
errorUris.begin(), errorUris.end(),
|
|
|
|
std::back_inserter(reusableURIs));
|
2010-07-14 11:39:05 +00:00
|
|
|
std::vector<std::string>::iterator insertionPoint = reusableURIs.begin();
|
|
|
|
for(std::vector<std::string>::iterator i = reusableURIs.begin(),
|
|
|
|
eoi = reusableURIs.end(); i != eoi; ++i) {
|
2012-10-14 09:05:34 +00:00
|
|
|
uri_split_result us;
|
|
|
|
if(uri_split(&us, (*i).c_str()) == 0 &&
|
|
|
|
std::find(ignore.begin(), ignore.end(),
|
|
|
|
uri::getFieldString(us, USR_HOST, (*i).c_str()))
|
|
|
|
== ignore.end()) {
|
2010-07-14 11:39:05 +00:00
|
|
|
if(i != insertionPoint) {
|
|
|
|
*insertionPoint = *i;
|
|
|
|
}
|
|
|
|
++insertionPoint;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
reusableURIs.erase(insertionPoint, reusableURIs.end());
|
2009-07-04 13:36:56 +00:00
|
|
|
size_t ininum = reusableURIs.size();
|
2010-11-20 08:21:36 +00:00
|
|
|
if(A2_LOG_DEBUG_ENABLED) {
|
|
|
|
A2_LOG_DEBUG(fmt("Found %u reusable URIs",
|
|
|
|
static_cast<unsigned int>(ininum)));
|
2010-03-08 14:34:32 +00:00
|
|
|
for(std::vector<std::string>::const_iterator i = reusableURIs.begin(),
|
|
|
|
eoi = reusableURIs.end(); i != eoi; ++i) {
|
2010-11-20 08:21:36 +00:00
|
|
|
A2_LOG_DEBUG(fmt("URI=%s", (*i).c_str()));
|
2010-03-08 14:34:32 +00:00
|
|
|
}
|
2010-02-09 12:20:20 +00:00
|
|
|
}
|
2010-07-14 11:39:05 +00:00
|
|
|
uris_.insert(uris_.end(), reusableURIs.begin(), reusableURIs.end());
|
2009-07-04 13:36:56 +00:00
|
|
|
}
|
|
|
|
|
2009-07-05 09:10:08 +00:00
|
|
|
void FileEntry::releaseRuntimeResource()
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
requestPool_.clear();
|
|
|
|
inFlightRequests_.clear();
|
2009-07-05 09:10:08 +00:00
|
|
|
}
|
|
|
|
|
2011-10-31 12:53:24 +00:00
|
|
|
namespace {
|
|
|
|
template<typename InputIterator>
|
|
|
|
void putBackUri
|
|
|
|
(std::deque<std::string>& uris,
|
|
|
|
InputIterator first,
|
|
|
|
InputIterator last)
|
|
|
|
{
|
|
|
|
for(; first != last; ++first) {
|
|
|
|
uris.push_front((*first)->getUri());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
void FileEntry::putBackRequest()
|
|
|
|
{
|
|
|
|
putBackUri(uris_, requestPool_.begin(), requestPool_.end());
|
|
|
|
putBackUri(uris_, inFlightRequests_.begin(), inFlightRequests_.end());
|
|
|
|
}
|
|
|
|
|
2010-10-30 16:02:15 +00:00
|
|
|
namespace {
|
2010-03-06 14:21:43 +00:00
|
|
|
template<typename InputIterator, typename T>
|
2010-10-30 16:02:15 +00:00
|
|
|
InputIterator findRequestByUri
|
2010-03-06 14:21:43 +00:00
|
|
|
(InputIterator first, InputIterator last, const T& uri)
|
|
|
|
{
|
|
|
|
for(; first != last; ++first) {
|
2010-03-07 10:36:42 +00:00
|
|
|
if(!(*first)->removalRequested() && (*first)->getUri() == uri) {
|
2010-03-06 14:21:43 +00:00
|
|
|
return first;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return last;
|
|
|
|
}
|
2010-10-30 16:02:15 +00:00
|
|
|
} // namespace
|
2010-03-06 14:21:43 +00:00
|
|
|
|
|
|
|
bool FileEntry::removeUri(const std::string& uri)
|
|
|
|
{
|
|
|
|
std::deque<std::string>::iterator itr =
|
2010-06-21 13:51:56 +00:00
|
|
|
std::find(spentUris_.begin(), spentUris_.end(), uri);
|
|
|
|
if(itr == spentUris_.end()) {
|
|
|
|
itr = std::find(uris_.begin(), uris_.end(), uri);
|
|
|
|
if(itr == uris_.end()) {
|
2010-03-06 14:21:43 +00:00
|
|
|
return false;
|
|
|
|
} else {
|
2010-06-21 13:51:56 +00:00
|
|
|
uris_.erase(itr);
|
2010-03-06 14:21:43 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else {
|
2010-06-21 13:51:56 +00:00
|
|
|
spentUris_.erase(itr);
|
2010-03-06 14:21:43 +00:00
|
|
|
SharedHandle<Request> req;
|
2012-03-22 16:34:37 +00:00
|
|
|
InFlightRequestSet::iterator riter =
|
2010-06-21 13:51:56 +00:00
|
|
|
findRequestByUri(inFlightRequests_.begin(), inFlightRequests_.end(), uri);
|
|
|
|
if(riter == inFlightRequests_.end()) {
|
2012-03-22 16:34:37 +00:00
|
|
|
RequestPool::iterator riter = findRequestByUri(requestPool_.begin(),
|
|
|
|
requestPool_.end(), uri);
|
2010-06-21 13:51:56 +00:00
|
|
|
if(riter == requestPool_.end()) {
|
2010-03-06 14:21:43 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
req = *riter;
|
2010-06-21 13:51:56 +00:00
|
|
|
requestPool_.erase(riter);
|
2010-03-06 14:21:43 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
req = *riter;
|
|
|
|
}
|
|
|
|
req->requestRemoval();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-20 11:42:33 +00:00
|
|
|
std::string FileEntry::getBasename() const
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
return File(path_).getBasename();
|
2010-06-20 11:42:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string FileEntry::getDirname() const
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
return File(path_).getDirname();
|
2010-06-20 11:42:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t FileEntry::setUris(const std::vector<std::string>& uris)
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
uris_.clear();
|
2010-06-20 11:42:33 +00:00
|
|
|
return addUris(uris.begin(), uris.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FileEntry::addUri(const std::string& uri)
|
|
|
|
{
|
2011-08-07 11:46:04 +00:00
|
|
|
std::string peUri = util::percentEncodeMini(uri);
|
2012-10-14 09:05:34 +00:00
|
|
|
if(uri_split(NULL, peUri.c_str()) == 0) {
|
2011-08-07 11:46:04 +00:00
|
|
|
uris_.push_back(peUri);
|
2010-06-20 11:42:33 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FileEntry::insertUri(const std::string& uri, size_t pos)
|
|
|
|
{
|
2011-08-07 11:46:04 +00:00
|
|
|
std::string peUri = util::percentEncodeMini(uri);
|
2012-10-14 09:05:34 +00:00
|
|
|
if(uri_split(NULL, peUri.c_str()) == 0) {
|
2010-06-21 13:51:56 +00:00
|
|
|
pos = std::min(pos, uris_.size());
|
2011-08-07 11:46:04 +00:00
|
|
|
uris_.insert(uris_.begin()+pos, peUri);
|
2010-06-20 11:42:33 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-14 07:17:55 +00:00
|
|
|
void FileEntry::setPath(const std::string& path)
|
|
|
|
{
|
|
|
|
path_ = path;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileEntry::setContentType(const std::string& contentType)
|
|
|
|
{
|
|
|
|
contentType_ = contentType;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t FileEntry::countInFlightRequest() const
|
|
|
|
{
|
|
|
|
return inFlightRequests_.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t FileEntry::countPooledRequest() const
|
|
|
|
{
|
|
|
|
return requestPool_.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileEntry::setOriginalName(const std::string& originalName)
|
|
|
|
{
|
|
|
|
originalName_ = originalName;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FileEntry::emptyRequestUri() const
|
|
|
|
{
|
|
|
|
return uris_.empty() && inFlightRequests_.empty() && requestPool_.empty();
|
|
|
|
}
|
|
|
|
|
2011-08-22 14:05:06 +00:00
|
|
|
void writeFilePath
|
|
|
|
(std::ostream& o,
|
|
|
|
const SharedHandle<FileEntry>& entry,
|
|
|
|
bool memory)
|
|
|
|
{
|
|
|
|
if(entry->getPath().empty()) {
|
|
|
|
std::vector<std::string> uris;
|
|
|
|
entry->getUris(uris);
|
|
|
|
if(uris.empty()) {
|
|
|
|
o << "n/a";
|
|
|
|
} else {
|
|
|
|
o << uris.front();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if(memory) {
|
|
|
|
o << "[MEMORY]" << File(entry->getPath()).getBasename();
|
|
|
|
} else {
|
|
|
|
o << entry->getPath();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
} // namespace aria2
|