📄 globalmanagerimpl.java
字号:
public DownloadManager
addDownloadManager(
String torrent_file_name,
String savePath,
int initialState,
boolean persistent,
boolean for_seeding )
{
/* to recover the initial state for non-persistent downloads the simplest way is to do it here
*/
byte[] torrent_hash = null;
if (!persistent){
Map save_download_state = (Map)saved_download_manager_state.get(torrent_file_name);
if ( save_download_state != null ){
torrent_hash = (byte[])save_download_state.get( "torrent_hash" );
if ( save_download_state.containsKey( "state" )){
int saved_state = ((Long) save_download_state.get("state")).intValue();
if ( saved_state == DownloadManager.STATE_STOPPED ){
initialState = saved_state;
}
}
}
}
File torrentDir = null;
File fDest = null;
try {
File f = new File(torrent_file_name);
if ( !f.exists()){
throw( new IOException( "Torrent file '" + torrent_file_name + "' doesn't exist" ));
}
if ( !f.isFile()){
throw( new IOException( "Torrent '" + torrent_file_name + "' is not a file" ));
}
fDest = TorrentUtils.copyTorrentFileToSaveDir(f, persistent);
String fName = fDest.getCanonicalPath();
// now do the creation!
DownloadManager new_manager = DownloadManagerFactory.create(this, torrent_hash, fName, savePath, initialState, persistent, for_seeding );
DownloadManager manager = addDownloadManager(new_manager, true);
// if a different manager is returned then an existing manager for this torrent
// exists and the new one isn't needed (yuck)
if ( manager == null || manager != new_manager ) {
fDest.delete();
File backupFile = new File(fName + ".bak");
if(backupFile.exists())
backupFile.delete();
}
return( manager );
}
catch (IOException e) {
System.out.println( "DownloadManager::addDownloadManager: fails - td = " + torrentDir + ", fd = " + fDest );
Debug.printStackTrace( e );
DownloadManager manager = DownloadManagerFactory.create(this, torrent_hash, torrent_file_name, savePath, initialState, persistent, for_seeding );
return addDownloadManager(manager, true);
}
catch (Exception e) {
// get here on duplicate files, no need to treat as error
DownloadManager manager = DownloadManagerFactory.create(this, torrent_hash, torrent_file_name, savePath, initialState, persistent, for_seeding );
return addDownloadManager(manager, true);
}
}
protected DownloadManager
addDownloadManager(
DownloadManager download_manager,
boolean save)
{
if (!isStopping) {
try{
managers_mon.enter();
int existing_index = managers_cow.indexOf( download_manager );
if (existing_index != -1) {
DownloadManager existing = (DownloadManager)managers_cow.get(existing_index);
return( existing );
}
DownloadManagerStats dm_stats = download_manager.getStats();
String torrent_file_name = download_manager.getTorrentFileName();
Map save_download_state = (Map)saved_download_manager_state.get(torrent_file_name);
long saved_data_bytes_downloaded = 0;
long saved_data_bytes_uploaded = 0;
long saved_discarded = 0;
long saved_hashfails = 0;
long saved_SecondsDownloading = 0;
long saved_SecondsOnlySeeding = 0;
if ( save_download_state != null ){
// once the state's been used we remove it
saved_download_manager_state.remove( torrent_file_name );
int nbUploads = ((Long) save_download_state.get("uploads")).intValue();
int maxDL = save_download_state.get("maxdl")==null?0:((Long) save_download_state.get("maxdl")).intValue();
int maxUL = save_download_state.get("maxul")==null?0:((Long) save_download_state.get("maxul")).intValue();
Long lDownloaded = (Long) save_download_state.get("downloaded");
Long lUploaded = (Long) save_download_state.get("uploaded");
Long lCompleted = (Long) save_download_state.get("completed");
Long lDiscarded = (Long) save_download_state.get("discarded");
Long lHashFailsCount = (Long) save_download_state.get("hashfails"); // old method, number of fails
Long lHashFailsBytes = (Long) save_download_state.get("hashfailbytes"); // new method, bytes failed
dm_stats.setMaxUploads(nbUploads);
dm_stats.setDownloadRateLimitBytesPerSecond( maxDL );
dm_stats.setUploadRateLimitBytesPerSecond( maxUL );
if (lCompleted != null) {
dm_stats.setDownloadCompleted(lCompleted.intValue());
}
if (lDiscarded != null) {
saved_discarded = lDiscarded.longValue();
}
if ( lHashFailsBytes != null ){
saved_hashfails = lHashFailsBytes.longValue();
}else if ( lHashFailsCount != null) {
TOTorrent torrent = download_manager.getTorrent();
if ( torrent != null ){
saved_hashfails = lHashFailsCount.longValue() * torrent.getPieceLength();
}
}
Long lPosition = (Long) save_download_state.get("position");
// 2.2.0.1 - category moved to downloadstate - this here for
// migration purposes
String sCategory = null;
if (save_download_state.containsKey("category")){
try{
sCategory = new String((byte[]) save_download_state.get("category"), Constants.DEFAULT_ENCODING);
}catch( UnsupportedEncodingException e ){
Debug.printStackTrace(e);
}
}
if (sCategory != null) {
Category cat = CategoryManager.getCategory(sCategory);
if (cat != null) download_manager.getDownloadState().setCategory(cat);
}
boolean bCompleted = dm_stats.getDownloadCompleted(false) == 1000;
download_manager.setOnlySeeding(bCompleted);
if (lDownloaded != null && lUploaded != null) {
long lUploadedValue = lUploaded.longValue();
long lDownloadedValue = lDownloaded.longValue();
if ( bCompleted && (lDownloadedValue == 0)){
//Gudy : I say if the torrent is complete, let's simply set downloaded
//to size in order to see a meaningfull share-ratio
//Gudy : Bypass this horrible hack, and I don't care of first priority seeding...
/*
if (lDownloadedValue != 0 && ((lUploadedValue * 1000) / lDownloadedValue < minQueueingShareRatio) )
lUploadedValue = ( download_manager.getSize()+999) * minQueueingShareRatio / 1000;
*/
// Parg: quite a few users have complained that they want "open-for-seeding" torrents to
// have an infinite share ratio for seeding rules (i.e. so they're not first priority)
int dl_copies = COConfigurationManager.getIntParameter("StartStopManager_iAddForSeedingDLCopyCount");
lDownloadedValue = download_manager.getSize() * dl_copies;
download_manager.getDownloadState().setFlag( DownloadManagerState.FLAG_ONLY_EVER_SEEDED, true );
}
saved_data_bytes_downloaded = lDownloadedValue;
saved_data_bytes_uploaded = lUploadedValue;
}
if (lPosition != null)
download_manager.setPosition(lPosition.intValue());
// no longer needed code
// else if (dm_stats.getDownloadCompleted(false) < 1000)
// dm.setPosition(bCompleted ? numCompleted : numDownloading);
Long lSecondsDLing = (Long)save_download_state.get("secondsDownloading");
if (lSecondsDLing != null) {
saved_SecondsDownloading = lSecondsDLing.longValue();
}
Long lSecondsOnlySeeding = (Long)save_download_state.get("secondsOnlySeeding");
if (lSecondsOnlySeeding != null) {
saved_SecondsOnlySeeding = lSecondsOnlySeeding.longValue();
}
Long already_allocated = (Long)save_download_state.get( "allocated" );
if( already_allocated != null && already_allocated.intValue() == 1 ) {
download_manager.setDataAlreadyAllocated( true );
}
Long creation_time = (Long)save_download_state.get( "creationTime" );
if ( creation_time != null ){
long ct = creation_time.longValue();
if ( ct < SystemTime.getCurrentTime()){
download_manager.setCreationTime( ct );
}
}
//TODO: remove this try/catch. should only be needed for those upgrading from previous snapshot
try {
//load file priorities
List file_priorities = (List) save_download_state.get("file_priorities");
if ( file_priorities != null ) download_manager.setData( "file_priorities", file_priorities );
}
catch (Throwable t) { Debug.printStackTrace( t ); }
}else{
// no stats, bodge the uploaded for seeds
if ( dm_stats.getDownloadCompleted(false) == 1000 ){
int dl_copies = COConfigurationManager.getIntParameter("StartStopManager_iAddForSeedingDLCopyCount");
saved_data_bytes_downloaded = download_manager.getSize()*dl_copies;
}
}
dm_stats.restoreSessionTotals(
saved_data_bytes_downloaded,
saved_data_bytes_uploaded,
saved_discarded,
saved_hashfails,
saved_SecondsDownloading,
saved_SecondsOnlySeeding );
boolean isCompleted = download_manager.getStats().getDownloadCompleted(false) == 1000;
if (download_manager.getPosition() == -1) {
int endPosition = 0;
for (int i = 0; i < managers_cow.size(); i++) {
DownloadManager dm = (DownloadManager) managers_cow.get(i);
boolean dmIsCompleted = dm.getStats().getDownloadCompleted(false) == 1000;
if (dmIsCompleted == isCompleted)
endPosition++;
}
download_manager.setPosition(endPosition + 1);
}
// Even though when the DownloadManager was created, onlySeeding was
// most likely set to true for completed torrents (via the Initializer +
// readTorrent), there's a chance that the torrent file didn't have the
// resume data. If it didn't, but we marked it as complete in our
// downloads config file, we should set to onlySeeding
download_manager.setOnlySeeding(isCompleted);
List new_download_managers = new ArrayList( managers_cow );
new_download_managers.add(download_manager);
managers_cow = new_download_managers;
TOTorrent torrent = download_manager.getTorrent();
if ( torrent != null ){
try{
manager_map.put( new HashWrapper(torrent.getHash()), download_manager );
}catch( TOTorrentException e ){
Debug.printStackTrace( e );
}
}
listeners.dispatch( LDT_MANAGER_ADDED, download_manager );
download_manager.addListener(this);
if ( save_download_state != null ){
Long lForceStart = (Long) save_download_state.get("forceStart");
if (lForceStart == null) {
Long lStartStopLocked = (Long) save_download_state.get("startStopLocked");
if(lStartStopLocked != null) {
lForceStart = lStartStopLocked;
}
}
if(lForceStart != null) {
if(lForceStart.intValue() == 1) {
download_manager.setForceStart(true);
}
}
}
}finally{
managers_mon.exit();
}
if (save){
saveDownloads(false);
}
return( download_manager );
}
else {
Logger.log(new LogEvent(LOGID, LogEvent.LT_ERROR,
"Tried to add a DownloadManager after shutdown of GlobalManager."));
return( null );
}
}
public List getDownloadManagers() {
return managers_cow;
}
public DownloadManager getDownloadManager(TOTorrent torrent) {
try {
return getDownloadManager(torrent.getHash());
} catch (TOTorrentException e) {
return null;
}
}
public DownloadManager
getDownloadManager(byte[] hash)
{
return (DownloadManager)manager_map.get(new HashWrapper(hash));
}
public void
canDownloadManagerBeRemoved(
DownloadManager manager)
throws GlobalManagerDownloadRemovalVetoException
{
try{
removal_listeners.dispatchWithException( LDT_MANAGER_WBR, manager );
}catch( Throwable e ){
throw((GlobalManagerDownloadRemovalVetoException)e);
}
}
public void
removeDownloadManager(
DownloadManager manager)
throws GlobalManagerDownloadRemovalVetoException
{
// simple protection against people calling this twice
if ( !managers_cow.contains( manager )){
return;
}
canDownloadManagerBeRemoved( manager );
try{
managers_mon.enter();
List new_download_managers = new ArrayList( managers_cow );
new_download_managers.remove(manager);
managers_cow = new_download_managers;
TOTorrent torrent = manager.getTorrent();
if ( torrent != null ){
try{
manager_map.remove(new HashWrapper(torrent.getHash()));
}catch( TOTorrentException e ){
Debug.printStackTrace( e );
}
}
manager.destroy();
}finally{
managers_mon.exit();
}
fixUpDownloadManagerPositions();
listeners.dispatch( LDT_MANAGER_REMOVED, manager );
manager.removeListener(this);
saveDownloads( false );
DownloadManagerState dms = manager.getDownloadState();
if ( dms.getCategory() != null){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -