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 {
|
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
bool FileEntry::RequestFaster::
|
|
|
|
operator()(const std::shared_ptr<Request>& lhs,
|
|
|
|
const std::shared_ptr<Request>& rhs) const
|
2012-03-22 16:34:37 +00:00
|
|
|
{
|
2015-12-27 09:39:47 +00:00
|
|
|
if (!lhs->getPeerStat()) {
|
2012-03-22 16:34:37 +00:00
|
|
|
return false;
|
|
|
|
}
|
2015-12-27 09:39:47 +00:00
|
|
|
if (!rhs->getPeerStat()) {
|
2012-03-22 16:34:37 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
int lspd = lhs->getPeerStat()->getAvgDownloadSpeed();
|
|
|
|
int rspd = rhs->getPeerStat()->getAvgDownloadSpeed();
|
|
|
|
return lspd > rspd || (lspd == rspd && lhs.get() < rhs.get());
|
|
|
|
}
|
|
|
|
|
2014-06-04 13:43:29 +00:00
|
|
|
FileEntry::FileEntry(std::string path, int64_t length, int64_t offset,
|
2013-11-26 01:59:51 +00:00
|
|
|
const std::vector<std::string>& uris)
|
2015-12-27 09:39:47 +00:00
|
|
|
: length_(length),
|
|
|
|
offset_(offset),
|
|
|
|
uris_(uris.begin(), uris.end()),
|
|
|
|
path_(std::move(path)),
|
|
|
|
lastFasterReplace_(Timer::zero()),
|
|
|
|
maxConnectionPerServer_(1),
|
|
|
|
requested_(true),
|
|
|
|
uniqueProtocol_(false)
|
|
|
|
{
|
|
|
|
}
|
2009-06-29 08:42:58 +00:00
|
|
|
|
2010-11-20 08:21:36 +00:00
|
|
|
FileEntry::FileEntry()
|
2015-12-27 09:39:47 +00:00
|
|
|
: length_(0),
|
|
|
|
offset_(0),
|
|
|
|
maxConnectionPerServer_(1),
|
|
|
|
requested_(false),
|
|
|
|
uniqueProtocol_(false)
|
|
|
|
{
|
|
|
|
}
|
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
|
|
|
|
2016-07-31 00:18:43 +00:00
|
|
|
FileEntry::~FileEntry() = default;
|
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
|
|
|
|
2007-10-11 16:58:24 +00:00
|
|
|
FileEntry& FileEntry::operator=(const FileEntry& entry)
|
|
|
|
{
|
2015-12-27 09:39:47 +00:00
|
|
|
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
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
bool FileEntry::exists() const { return File(getPath()).exists(); }
|
2009-03-11 15:37:40 +00:00
|
|
|
|
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);
|
2015-12-27 09:39:47 +00:00
|
|
|
return goff - offset_;
|
2009-06-23 15:35:45 +00:00
|
|
|
}
|
|
|
|
|
2014-06-04 13:33:46 +00:00
|
|
|
std::vector<std::string> FileEntry::getUris() const
|
2009-06-23 15:35:45 +00:00
|
|
|
{
|
2014-06-04 13:33:46 +00:00
|
|
|
std::vector<std::string> uris(std::begin(spentUris_), std::end(spentUris_));
|
|
|
|
uris.insert(std::end(uris), std::begin(uris_), std::end(uris_));
|
|
|
|
return uris;
|
2009-06-23 15:35:45 +00:00
|
|
|
}
|
|
|
|
|
2010-10-30 16:02:15 +00:00
|
|
|
namespace {
|
2015-12-27 09:39:47 +00:00
|
|
|
template <typename InputIterator, typename OutputIterator>
|
|
|
|
OutputIterator enumerateInFlightHosts(InputIterator first, InputIterator last,
|
|
|
|
OutputIterator out)
|
2009-06-29 16:43:41 +00:00
|
|
|
{
|
2015-12-27 09:39:47 +00:00
|
|
|
for (; first != last; ++first) {
|
2012-10-14 09:05:34 +00:00
|
|
|
uri_split_result us;
|
2015-12-27 09:39:47 +00:00
|
|
|
if (uri_split(&us, (*first)->getUri().c_str()) == 0) {
|
2012-10-14 09:05:34 +00:00
|
|
|
*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
|
|
|
|
2016-07-07 14:51:51 +00:00
|
|
|
std::shared_ptr<Request> FileEntry::getRequestWithInFlightHosts(
|
2015-12-27 09:39:47 +00:00
|
|
|
URISelector* selector, bool uriReuse,
|
|
|
|
const std::vector<std::pair<size_t, std::string>>& usedHosts,
|
2016-07-07 14:51:51 +00:00
|
|
|
const std::string& referer, const std::string& method,
|
|
|
|
const std::vector<std::string>& inFlightHosts)
|
2009-06-23 15:35:45 +00:00
|
|
|
{
|
2013-06-21 16:10:38 +00:00
|
|
|
std::shared_ptr<Request> req;
|
2016-07-07 14:51:51 +00:00
|
|
|
|
|
|
|
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 = std::make_shared<Request>();
|
|
|
|
if (req->setUri(uri)) {
|
|
|
|
if (std::count(std::begin(inFlightHosts), std::end(inFlightHosts),
|
|
|
|
req->getHost()) >= maxConnectionPerServer_) {
|
|
|
|
pending.push_back(uri);
|
|
|
|
ignoreHost.push_back(req->getHost());
|
|
|
|
req.reset();
|
|
|
|
continue;
|
2010-08-31 11:17:38 +00:00
|
|
|
}
|
2016-07-07 14:51:51 +00:00
|
|
|
if (referer == "*") {
|
|
|
|
// Assuming uri has already been percent-encoded.
|
|
|
|
req->setReferer(uri);
|
2015-12-27 09:39:47 +00:00
|
|
|
}
|
|
|
|
else {
|
2016-07-07 14:51:51 +00:00
|
|
|
req->setReferer(util::percentEncodeMini(referer));
|
2010-01-05 16:01:46 +00:00
|
|
|
}
|
2016-07-07 14:51:51 +00:00
|
|
|
req->setMethod(method);
|
|
|
|
spentUris_.push_back(uri);
|
|
|
|
inFlightRequests_.insert(req);
|
|
|
|
break;
|
2015-12-27 09:39:47 +00:00
|
|
|
}
|
|
|
|
else {
|
2016-07-07 14:51:51 +00:00
|
|
|
req.reset();
|
2010-07-14 14:10:33 +00:00
|
|
|
}
|
|
|
|
}
|
2016-07-07 14:51:51 +00:00
|
|
|
uris_.insert(std::begin(uris_), std::begin(pending), std::end(pending));
|
|
|
|
if (g == 0 && uriReuse && !req && uris_.size() == pending.size()) {
|
|
|
|
// Reuse URIs other than ones in pending
|
|
|
|
reuseUri(ignoreHost);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-07-19 13:36:09 +00:00
|
|
|
break;
|
2015-12-27 09:39:47 +00:00
|
|
|
}
|
2016-07-19 13:36:09 +00:00
|
|
|
return req;
|
2016-07-07 14:51:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<Request> FileEntry::getRequest(
|
|
|
|
URISelector* selector, bool uriReuse,
|
|
|
|
const std::vector<std::pair<size_t, std::string>>& usedHosts,
|
|
|
|
const std::string& referer, const std::string& method)
|
|
|
|
{
|
|
|
|
std::shared_ptr<Request> req;
|
|
|
|
if (requestPool_.empty()) {
|
|
|
|
std::vector<std::string> inFlightHosts;
|
|
|
|
enumerateInFlightHosts(std::begin(inFlightRequests_),
|
|
|
|
std::end(inFlightRequests_),
|
|
|
|
std::back_inserter(inFlightHosts));
|
|
|
|
return getRequestWithInFlightHosts(selector, uriReuse, usedHosts, referer,
|
|
|
|
method, inFlightHosts);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip Request object if it is still
|
|
|
|
// sleeping(Request::getWakeTime() < global::wallclock()). If all
|
|
|
|
// pooled objects are sleeping, we may return first one. Caller
|
|
|
|
// should inspect returned object's getWakeTime().
|
|
|
|
auto i = std::begin(requestPool_);
|
|
|
|
for (; i != std::end(requestPool_); ++i) {
|
|
|
|
if ((*i)->getWakeTime() <= global::wallclock()) {
|
|
|
|
break;
|
2011-01-17 12:19:45 +00:00
|
|
|
}
|
2016-07-07 14:51:51 +00:00
|
|
|
}
|
|
|
|
if (i == std::end(requestPool_)) {
|
|
|
|
// all requests are sleeping; try to another URI
|
|
|
|
std::vector<std::string> inFlightHosts;
|
|
|
|
enumerateInFlightHosts(std::begin(inFlightRequests_),
|
|
|
|
std::end(inFlightRequests_),
|
|
|
|
std::back_inserter(inFlightHosts));
|
|
|
|
enumerateInFlightHosts(std::begin(requestPool_), std::end(requestPool_),
|
|
|
|
std::back_inserter(inFlightHosts));
|
|
|
|
|
|
|
|
req = getRequestWithInFlightHosts(selector, uriReuse, usedHosts, referer,
|
|
|
|
method, inFlightHosts);
|
|
|
|
if (!req || req->getUri() == (*std::begin(requestPool_))->getUri()) {
|
|
|
|
i = std::begin(requestPool_);
|
2011-01-17 12:19:45 +00:00
|
|
|
}
|
2016-07-07 14:51:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (i != std::end(requestPool_)) {
|
2011-01-17 12:19:45 +00:00
|
|
|
req = *i;
|
|
|
|
requestPool_.erase(i);
|
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
|
|
|
}
|
2016-07-07 14:51:51 +00:00
|
|
|
|
|
|
|
inFlightRequests_.insert(req);
|
|
|
|
|
2009-06-29 16:43:41 +00:00
|
|
|
return req;
|
2009-06-23 15:35:45 +00:00
|
|
|
}
|
|
|
|
|
2015-06-07 17:03:31 +00:00
|
|
|
namespace {
|
2015-06-21 06:17:00 +00:00
|
|
|
constexpr auto startupIdleTime = 10_s;
|
2015-06-07 17:03:31 +00:00
|
|
|
} // namespace
|
|
|
|
|
2013-06-21 16:10:38 +00:00
|
|
|
std::shared_ptr<Request>
|
|
|
|
FileEntry::findFasterRequest(const std::shared_ptr<Request>& base)
|
2009-06-29 15:18:21 +00:00
|
|
|
{
|
2015-12-27 09:39:47 +00:00
|
|
|
if (requestPool_.empty() ||
|
|
|
|
lastFasterReplace_.difference(global::wallclock()) < startupIdleTime) {
|
2013-07-04 12:44:09 +00:00
|
|
|
return nullptr;
|
2009-06-29 15:18:21 +00:00
|
|
|
}
|
2013-06-21 16:10:38 +00:00
|
|
|
const std::shared_ptr<PeerStat>& fastest =
|
2015-12-27 09:39:47 +00:00
|
|
|
(*requestPool_.begin())->getPeerStat();
|
|
|
|
if (!fastest) {
|
2013-07-04 12:44:09 +00:00
|
|
|
return nullptr;
|
2009-06-29 15:18:21 +00:00
|
|
|
}
|
2013-06-21 16:10:38 +00:00
|
|
|
const std::shared_ptr<PeerStat>& basestat = base->getPeerStat();
|
2009-07-11 15:57:06 +00:00
|
|
|
// TODO hard coded value. See PREF_STARTUP_IDLE_TIME
|
2015-12-27 09:39:47 +00:00
|
|
|
if (!basestat || (basestat->getDownloadStartTime().difference(
|
|
|
|
global::wallclock()) >= startupIdleTime &&
|
|
|
|
fastest->getAvgDownloadSpeed() * 0.8 >
|
|
|
|
basestat->calculateDownloadSpeed())) {
|
2009-07-11 15:57:06 +00:00
|
|
|
// TODO we should consider that "fastest" is very slow.
|
2013-06-21 16:10:38 +00:00
|
|
|
std::shared_ptr<Request> fastestRequest = *requestPool_.begin();
|
2012-03-22 16:34:37 +00:00
|
|
|
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;
|
|
|
|
}
|
2013-07-04 12:44:09 +00:00
|
|
|
return nullptr;
|
2009-06-29 15:18:21 +00:00
|
|
|
}
|
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
std::shared_ptr<Request> FileEntry::findFasterRequest(
|
|
|
|
const std::shared_ptr<Request>& base,
|
|
|
|
const std::vector<std::pair<size_t, std::string>>& usedHosts,
|
|
|
|
const std::shared_ptr<ServerStatMan>& serverStatMan)
|
2011-06-27 15:18:53 +00:00
|
|
|
{
|
2015-06-21 09:04:30 +00:00
|
|
|
constexpr int SPEED_THRESHOLD = 20_k;
|
2015-12-27 09:39:47 +00:00
|
|
|
if (lastFasterReplace_.difference(global::wallclock()) < startupIdleTime) {
|
2013-07-04 12:44:09 +00:00
|
|
|
return nullptr;
|
2011-06-27 15:18:53 +00:00
|
|
|
}
|
2011-06-28 12:27:31 +00:00
|
|
|
std::vector<std::string> inFlightHosts;
|
|
|
|
enumerateInFlightHosts(inFlightRequests_.begin(), inFlightRequests_.end(),
|
|
|
|
std::back_inserter(inFlightHosts));
|
2013-06-21 16:10:38 +00:00
|
|
|
const std::shared_ptr<PeerStat>& basestat = base->getPeerStat();
|
2011-06-27 15:18:53 +00:00
|
|
|
A2_LOG_DEBUG("Search faster server using ServerStat.");
|
|
|
|
// Use first 10 good URIs to introduce some randomness.
|
|
|
|
const size_t NUM_URI = 10;
|
2015-12-27 09:39:47 +00:00
|
|
|
std::vector<std::pair<std::shared_ptr<ServerStat>, std::string>> fastCands;
|
2011-06-27 15:18:53 +00:00
|
|
|
std::vector<std::string> normCands;
|
2015-12-27 09:39:47 +00:00
|
|
|
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;
|
2015-12-27 09:39:47 +00:00
|
|
|
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());
|
2015-12-27 09:39:47 +00:00
|
|
|
if (std::count(inFlightHosts.begin(), inFlightHosts.end(), host) >=
|
|
|
|
maxConnectionPerServer_) {
|
2011-06-28 12:27:31 +00:00
|
|
|
A2_LOG_DEBUG(fmt("%s has already used %d times, not considered.",
|
2015-12-27 09:39:47 +00:00
|
|
|
(*i).c_str(), maxConnectionPerServer_));
|
2011-06-28 12:27:31 +00:00
|
|
|
continue;
|
|
|
|
}
|
2015-12-27 09:39:47 +00:00
|
|
|
if (findSecond(usedHosts.begin(), usedHosts.end(), host) !=
|
|
|
|
usedHosts.end()) {
|
2011-06-27 15:18:53 +00:00
|
|
|
A2_LOG_DEBUG(fmt("%s is in usedHosts, not considered", (*i).c_str()));
|
|
|
|
continue;
|
|
|
|
}
|
2013-06-21 16:10:38 +00:00
|
|
|
std::shared_ptr<ServerStat> ss = serverStatMan->find(host, protocol);
|
2015-12-27 09:39:47 +00:00
|
|
|
if (ss && ss->isOK()) {
|
|
|
|
if ((basestat &&
|
|
|
|
ss->getDownloadSpeed() > basestat->calculateDownloadSpeed() * 1.5) ||
|
|
|
|
(!basestat && ss->getDownloadSpeed() > SPEED_THRESHOLD)) {
|
2011-06-27 15:18:53 +00:00
|
|
|
fastCands.push_back(std::make_pair(ss, *i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-12-27 09:39:47 +00:00
|
|
|
if (!fastCands.empty()) {
|
2011-06-27 15:18:53 +00:00
|
|
|
std::sort(fastCands.begin(), fastCands.end(), ServerStatFaster());
|
2014-09-12 15:07:21 +00:00
|
|
|
auto fastestRequest = std::make_shared<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()));
|
2014-05-27 16:52:29 +00:00
|
|
|
// Candidate URIs where already parsed when populating fastCands.
|
|
|
|
(void)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.");
|
2013-07-04 12:44:09 +00:00
|
|
|
return nullptr;
|
2011-06-27 15:18:53 +00:00
|
|
|
}
|
|
|
|
|
2013-06-21 16:10:38 +00:00
|
|
|
void FileEntry::storePool(const std::shared_ptr<Request>& request)
|
2009-06-29 15:18:21 +00:00
|
|
|
{
|
2013-06-21 16:10:38 +00:00
|
|
|
const std::shared_ptr<PeerStat>& peerStat = request->getPeerStat();
|
2015-12-27 09:39:47 +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
|
|
|
}
|
|
|
|
|
2013-06-21 16:10:38 +00:00
|
|
|
void FileEntry::poolRequest(const std::shared_ptr<Request>& request)
|
2009-06-23 15:35:45 +00:00
|
|
|
{
|
|
|
|
removeRequest(request);
|
2015-12-27 09:39:47 +00:00
|
|
|
if (!request->removalRequested()) {
|
2010-03-06 14:21:43 +00:00
|
|
|
storePool(request);
|
|
|
|
}
|
2009-06-23 15:35:45 +00:00
|
|
|
}
|
|
|
|
|
2013-06-21 16:10:38 +00:00
|
|
|
bool FileEntry::removeRequest(const std::shared_ptr<Request>& request)
|
2009-06-23 15:35:45 +00:00
|
|
|
{
|
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;
|
2015-12-27 09:39:47 +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;
|
2015-12-27 09:39:47 +00:00
|
|
|
if (uri_split(&us, (*itr).c_str()) == -1) {
|
2010-09-11 12:09:19 +00:00
|
|
|
continue;
|
|
|
|
}
|
2015-12-27 09:39:47 +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",
|
2015-12-27 09:39:47 +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_;
|
2015-12-27 09:39:47 +00:00
|
|
|
|
2009-06-29 08:42:58 +00:00
|
|
|
public:
|
2015-12-27 09:39:47 +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
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
void FileEntry::extractURIResult(std::deque<URIResult>& res,
|
|
|
|
error_code::Value r)
|
2009-06-29 08:42:58 +00:00
|
|
|
{
|
2013-08-21 03:06:53 +00:00
|
|
|
auto i = std::stable_partition(uriResults_.begin(), uriResults_.end(),
|
|
|
|
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
|
|
|
{
|
2015-12-27 09:39:47 +00:00
|
|
|
if (A2_LOG_DEBUG_ENABLED) {
|
|
|
|
for (const auto& i : ignore) {
|
2013-08-21 03:06:53 +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());
|
2015-12-27 09:39:47 +00:00
|
|
|
std::transform(uriResults_.begin(), uriResults_.end(), errorUris.begin(),
|
|
|
|
std::mem_fn(&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());
|
2015-12-27 09:39:47 +00:00
|
|
|
if (A2_LOG_DEBUG_ENABLED) {
|
|
|
|
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;
|
2015-12-27 09:39:47 +00:00
|
|
|
std::set_difference(uris.begin(), uris.end(), errorUris.begin(),
|
|
|
|
errorUris.end(), std::back_inserter(reusableURIs));
|
2013-08-21 03:06:53 +00:00
|
|
|
auto insertionPoint = reusableURIs.begin();
|
2015-12-27 09:39:47 +00:00
|
|
|
for (auto i = reusableURIs.begin(), eoi = reusableURIs.end(); i != eoi; ++i) {
|
2012-10-14 09:05:34 +00:00
|
|
|
uri_split_result us;
|
2015-12-27 09:39:47 +00:00
|
|
|
if (uri_split(&us, (*i).c_str()) == 0 &&
|
|
|
|
std::find(ignore.begin(), ignore.end(),
|
|
|
|
uri::getFieldString(us, USR_HOST, (*i).c_str())) ==
|
|
|
|
ignore.end()) {
|
|
|
|
if (i != insertionPoint) {
|
2010-07-14 11:39:05 +00:00
|
|
|
*insertionPoint = *i;
|
|
|
|
}
|
|
|
|
++insertionPoint;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
reusableURIs.erase(insertionPoint, reusableURIs.end());
|
2009-07-04 13:36:56 +00:00
|
|
|
size_t ininum = reusableURIs.size();
|
2015-12-27 09:39:47 +00:00
|
|
|
if (A2_LOG_DEBUG_ENABLED) {
|
|
|
|
A2_LOG_DEBUG(
|
|
|
|
fmt("Found %u reusable URIs", static_cast<unsigned int>(ininum)));
|
|
|
|
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 {
|
2015-12-27 09:39:47 +00:00
|
|
|
template <typename InputIterator>
|
|
|
|
void putBackUri(std::deque<std::string>& uris, InputIterator first,
|
|
|
|
InputIterator last)
|
2011-10-31 12:53:24 +00:00
|
|
|
{
|
2015-12-27 09:39:47 +00:00
|
|
|
for (; first != last; ++first) {
|
2011-10-31 12:53:24 +00:00
|
|
|
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 {
|
2015-12-27 09:39:47 +00:00
|
|
|
template <typename InputIterator, typename T>
|
|
|
|
InputIterator findRequestByUri(InputIterator first, InputIterator last,
|
|
|
|
const T& uri)
|
2010-03-06 14:21:43 +00:00
|
|
|
{
|
2015-12-27 09:39:47 +00:00
|
|
|
for (; first != last; ++first) {
|
|
|
|
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)
|
|
|
|
{
|
2013-08-21 03:06:53 +00:00
|
|
|
auto itr = std::find(spentUris_.begin(), spentUris_.end(), uri);
|
2015-12-27 09:39:47 +00:00
|
|
|
if (itr == spentUris_.end()) {
|
2010-06-21 13:51:56 +00:00
|
|
|
itr = std::find(uris_.begin(), uris_.end(), uri);
|
2015-12-27 09:39:47 +00:00
|
|
|
if (itr == uris_.end()) {
|
2010-03-06 14:21:43 +00:00
|
|
|
return false;
|
2013-08-21 03:06:53 +00:00
|
|
|
}
|
|
|
|
uris_.erase(itr);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
spentUris_.erase(itr);
|
|
|
|
std::shared_ptr<Request> req;
|
2013-08-25 16:00:46 +00:00
|
|
|
auto riter =
|
2015-12-27 09:39:47 +00:00
|
|
|
findRequestByUri(inFlightRequests_.begin(), inFlightRequests_.end(), uri);
|
|
|
|
if (riter == inFlightRequests_.end()) {
|
|
|
|
auto riter =
|
|
|
|
findRequestByUri(requestPool_.begin(), requestPool_.end(), uri);
|
|
|
|
if (riter == requestPool_.end()) {
|
2010-03-06 14:21:43 +00:00
|
|
|
return true;
|
|
|
|
}
|
2013-08-21 03:06:53 +00:00
|
|
|
req = *riter;
|
|
|
|
requestPool_.erase(riter);
|
2015-12-27 09:39:47 +00:00
|
|
|
}
|
|
|
|
else {
|
2013-08-21 03:06:53 +00:00
|
|
|
req = *riter;
|
2010-03-06 14:21:43 +00:00
|
|
|
}
|
2013-08-21 03:06:53 +00:00
|
|
|
req->requestRemoval();
|
|
|
|
return true;
|
2010-03-06 14:21:43 +00:00
|
|
|
}
|
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
std::string FileEntry::getBasename() const { return File(path_).getBasename(); }
|
2010-06-20 11:42:33 +00:00
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
std::string FileEntry::getDirname() const { 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);
|
2015-12-27 09:39:47 +00:00
|
|
|
if (uri_split(nullptr, 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;
|
2015-12-27 09:39:47 +00:00
|
|
|
}
|
|
|
|
else {
|
2010-06-20 11:42:33 +00:00
|
|
|
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);
|
2015-12-27 09:39:47 +00:00
|
|
|
if (uri_split(nullptr, peUri.c_str()) != 0) {
|
2010-06-20 11:42:33 +00:00
|
|
|
return false;
|
|
|
|
}
|
2013-08-21 03:06:53 +00:00
|
|
|
pos = std::min(pos, uris_.size());
|
2015-12-27 09:39:47 +00:00
|
|
|
uris_.insert(uris_.begin() + pos, peUri);
|
2013-08-21 03:06:53 +00:00
|
|
|
return true;
|
2010-06-20 11:42:33 +00:00
|
|
|
}
|
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
void FileEntry::setPath(std::string path) { path_ = std::move(path); }
|
2010-11-14 07:17:55 +00:00
|
|
|
|
2013-12-15 10:20:31 +00:00
|
|
|
void FileEntry::setContentType(std::string contentType)
|
2010-11-14 07:17:55 +00:00
|
|
|
{
|
2013-12-15 10:20:31 +00:00
|
|
|
contentType_ = std::move(contentType);
|
2010-11-14 07:17:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t FileEntry::countInFlightRequest() const
|
|
|
|
{
|
|
|
|
return inFlightRequests_.size();
|
|
|
|
}
|
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
size_t FileEntry::countPooledRequest() const { return requestPool_.size(); }
|
2010-11-14 07:17:55 +00:00
|
|
|
|
2013-12-15 10:20:31 +00:00
|
|
|
void FileEntry::setOriginalName(std::string originalName)
|
2010-11-14 07:17:55 +00:00
|
|
|
{
|
2013-12-15 10:20:31 +00:00
|
|
|
originalName_ = std::move(originalName);
|
2010-11-14 07:17:55 +00:00
|
|
|
}
|
|
|
|
|
2014-06-04 12:45:12 +00:00
|
|
|
void FileEntry::setSuffixPath(std::string suffixPath)
|
|
|
|
{
|
|
|
|
suffixPath_ = std::move(suffixPath);
|
|
|
|
}
|
|
|
|
|
2010-11-14 07:17:55 +00:00
|
|
|
bool FileEntry::emptyRequestUri() const
|
|
|
|
{
|
|
|
|
return uris_.empty() && inFlightRequests_.empty() && requestPool_.empty();
|
|
|
|
}
|
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
void writeFilePath(std::ostream& o, const std::shared_ptr<FileEntry>& entry,
|
|
|
|
bool memory)
|
2011-08-22 14:05:06 +00:00
|
|
|
{
|
2015-12-27 09:39:47 +00:00
|
|
|
if (entry->getPath().empty()) {
|
2014-06-04 13:33:46 +00:00
|
|
|
auto uris = entry->getUris();
|
2015-12-27 09:39:47 +00:00
|
|
|
if (uris.empty()) {
|
2011-08-22 14:05:06 +00:00
|
|
|
o << "n/a";
|
2015-12-27 09:39:47 +00:00
|
|
|
}
|
|
|
|
else {
|
2011-08-22 14:05:06 +00:00
|
|
|
o << uris.front();
|
|
|
|
}
|
2013-08-21 03:06:53 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
if (memory) {
|
2013-08-21 03:06:53 +00:00
|
|
|
o << "[MEMORY]" << File(entry->getPath()).getBasename();
|
2015-12-27 09:39:47 +00:00
|
|
|
}
|
|
|
|
else {
|
2013-08-21 03:06:53 +00:00
|
|
|
o << entry->getPath();
|
2011-08-22 14:05:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
} // namespace aria2
|