⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 downloadmanagerimpl.java

📁 这是一个基于java编写的torrent的P2P源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * File    : DownloadManagerImpl.java
 * Created : 06-Jan-2004
 * By      : parg
 * 
 * Azureus - a Java Bittorrent client
 *
 * 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.
 *
 * 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 ( see the LICENSE file ).
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package org.gudy.azureus2.pluginsimpl.local.download;

/**
 * @author parg
 *
 */

import java.util.*;
import java.io.File;
import java.net.URL;

import com.aelitis.azureus.core.*;
import com.aelitis.azureus.core.util.CopyOnWriteList;

import org.gudy.azureus2.plugins.torrent.*;
import org.gudy.azureus2.plugins.ui.UIManagerEvent;
import org.gudy.azureus2.pluginsimpl.local.torrent.*;
import org.gudy.azureus2.pluginsimpl.local.ui.UIManagerImpl;
import org.gudy.azureus2.plugins.download.DownloadException;
import org.gudy.azureus2.plugins.download.Download;
import org.gudy.azureus2.plugins.download.DownloadManagerListener;
import org.gudy.azureus2.plugins.download.DownloadManagerStats;
import org.gudy.azureus2.plugins.download.DownloadRemovalVetoException;
import org.gudy.azureus2.plugins.download.DownloadWillBeAddedListener;

import org.gudy.azureus2.core3.torrent.*;
import org.gudy.azureus2.core3.config.*;
import org.gudy.azureus2.core3.global.*;
import org.gudy.azureus2.core3.disk.DiskManager;
import org.gudy.azureus2.core3.download.*;
import org.gudy.azureus2.core3.util.*;


public class 
DownloadManagerImpl
	implements org.gudy.azureus2.plugins.download.DownloadManager, DownloadManagerInitialisationAdapter
{
	protected static DownloadManagerImpl	singleton;
	protected static AEMonitor				class_mon	= new AEMonitor( "DownloadManager:class");
	
	public static DownloadManagerImpl
	getSingleton(
		AzureusCore	azureus_core )
	{
		try{
			class_mon.enter();
	
			if ( singleton == null ){
				
				singleton = new DownloadManagerImpl( azureus_core );
			}
			
			return( singleton );
			
		}finally{
			
			class_mon.exit();
		}
	}
	
	private AzureusCore				azureus_core;
	private GlobalManager			global_manager;
	private DownloadManagerStats	stats;
	
	private List			listeners		= new ArrayList();
	private CopyOnWriteList	dwba_listeners	= new CopyOnWriteList();
	private AEMonitor		listeners_mon	= new AEMonitor( "DownloadManager:L");
	
	private List			downloads		= new ArrayList();
	private Map				pending_dls		= new HashMap();
	private Map				download_map	= new HashMap();
		
	protected
	DownloadManagerImpl(
		AzureusCore	_azureus_core )
	{
		azureus_core	= _azureus_core;
		global_manager	= _azureus_core.getGlobalManager();
		
		stats = new DownloadManagerStatsImpl( global_manager );
		
		global_manager.addListener(
			new GlobalManagerListener()
			{
				public void
				downloadManagerAdded(
					DownloadManager	dm )
				{
					addDownloadManager( dm );
				}
				
				public void
				downloadManagerRemoved(
					DownloadManager	dm )
				{
					List			listeners_ref	= null;
					DownloadImpl	dl				= null;
					
					try{
						listeners_mon.enter();
						
						dl = (DownloadImpl)download_map.get( dm );
						
						if ( dl == null ){
							
							System.out.println( "DownloadManager:unknown manager removed");
							
						}else{
						
							downloads.remove( dl );
							
							download_map.remove( dm );
							
							pending_dls.remove( dm );
							
							dl.destroy();
						
							listeners_ref = listeners;
						}
						
					}finally{
						
						listeners_mon.exit();
					}
					
					if ( dl != null ){
							
						for (int i=0;i<listeners_ref.size();i++){
								
							((DownloadManagerListener)listeners_ref.get(i)).downloadRemoved( dl );
						}
					}
				}
				
				public void
				destroyInitiated()
				{
				}				
				
				public void
				destroyed()
				{	
				}
                
                
                public void seedingStatusChanged( boolean seeding_only_mode ){
                  //TODO
                }           
			});
		
		global_manager.addDownloadWillBeRemovedListener(
			new GlobalManagerDownloadWillBeRemovedListener()
			{
				public void
				downloadWillBeRemoved(
					DownloadManager	dm )
				
					throws GlobalManagerDownloadRemovalVetoException
				{					
					DownloadImpl	download = (DownloadImpl)download_map.get( dm );
				
					if ( download != null ){
					
						try{ 
							download.isRemovable();
							
						}catch( DownloadRemovalVetoException e ){
													
							throw( new GlobalManagerDownloadRemovalVetoException( e.getMessage(),e.isSilent()));
						}		
					}
				}
			});
	}
	
	public void 
	addDownload(
		final File fileName ) 
	{
		UIManagerImpl.fireEvent( UIManagerEvent.ET_OPEN_TORRENT_VIA_FILE, fileName );
	}

	public void 
	addDownload(
		final URL	url) 
	{
		addDownload(url,null,true);
	}
	
	public void 
	addDownload(
		URL		url,
		boolean	auto_download )
	
		throws DownloadException
	{
		addDownload(url,null,auto_download);	
	}
	
	public void 
	addDownload(
		final URL	url,
		final URL 	referrer) 
	{
		addDownload(url,referrer,true);
	}
	
	public void 
	addDownload(
		final URL	url,
		final URL 	referrer,
		boolean		auto_download )
	{
		UIManagerImpl.fireEvent( UIManagerEvent.ET_OPEN_TORRENT_VIA_URL, new Object[]{ url, referrer, new Boolean( auto_download )});
	}
	
	protected void
	addDownloadManager(
		DownloadManager	dm )
	{
		List			listeners_ref 	= null;
		DownloadImpl	dl				= null;
		
		try{
			listeners_mon.enter();
			
			if ( download_map.get(dm) == null ){
	
				dl = (DownloadImpl)pending_dls.remove( dm );
				
				if ( dl == null ){
					
					dl = new DownloadImpl(dm);
				}
				
				downloads.add( dl );
				
				download_map.put( dm, dl );
				
				listeners_ref = listeners;
			}
		}finally{
			
			listeners_mon.exit();
		}
		
		if ( dl != null ){
			
			for (int i=0;i<listeners_ref.size();i++){
					
				try{
					((DownloadManagerListener)listeners_ref.get(i)).downloadAdded( dl );
					
				}catch( Throwable e ){
						
					Debug.printStackTrace( e );
				}
			}
		}
	}
	
	public Download
	addDownload(
		Torrent		torrent )
	
		throws DownloadException
	{	 
	    return( addDownload( torrent, null, null ));
	}
	
	public Download
	addDownload(
		Torrent		torrent,
		File		torrent_file,
		File		data_location )
	
		throws DownloadException
	{
		if ( torrent_file == null ){
			
		    String torrent_dir = null;
		    
		    if( COConfigurationManager.getBooleanParameter("Save Torrent Files",true)){
		    	
		      try{
		      	
		      	torrent_dir = COConfigurationManager.getDirectoryParameter("General_sDefaultTorrent_Directory");
		        
		      }catch(Exception egnore){}
		    }
		    
		    if ( torrent_dir == null || torrent_dir.length() == 0 ){
		    	
		    	throw( new DownloadException("DownloadManager::addDownload: default torrent save directory must be configured" ));
		    }
		
		    torrent_file = new File( torrent_dir + File.separator + torrent.getName() + ".torrent" );
		    
		    try{
		    	torrent.writeToFile( torrent_file );
		    	
		    }catch( TorrentException e ){
		    	
		    	throw( new DownloadException("DownloadManager::addDownload: failed to write torrent to '" + torrent_file.toString() + "'", e ));	    	
		    }
		}
		
		else {
			if (!torrent_file.exists()) {
				throw new DownloadException("DownloadManager::addDownload: torrent file does not exist - " + torrent_file.toString()); 
			}
			else if (!torrent_file.isFile()) {
				throw new DownloadException("DownloadManager::addDownload: torrent filepath given is not a file - " + torrent_file.toString());
			}
		}
		
		if ( data_location == null ){
			
		    boolean useDefDataDir = COConfigurationManager.getBooleanParameter("Use default data dir");
		    
		    String data_dir = null;
		    
		    if (useDefDataDir){
		    	
		    	data_dir = COConfigurationManager.getStringParameter("Default save path");
		    }
		    
		    if ( data_dir == null || data_dir.length() == 0 ){
		    	
		    	throw( new DownloadException("DownloadManager::addDownload: default data save directory must be configured" ));
		    }
		    
		    data_location = new File(data_dir); 
		    
		    FileUtil.mkdirs(data_location);
		}

		byte[] hash = null;
		try {
			hash = torrent.getHash();
		} catch (Exception e) { }
		
		DownloadManager dm = global_manager.addDownloadManager(
				torrent_file.toString(), hash, data_location.toString(),
				getInitialState(), true);
		
		if ( dm == null ){
			
			throw( new DownloadException( "DownloadManager::addDownload - failed"));
		}
		
		addDownloadManager( dm );
		
		return( getDownload( dm ));
	}

	public Download
	addNonPersistentDownload(
		Torrent		torrent,
		File		torrent_file,
		File		data_location )
	
		throws DownloadException
	{

		byte[] hash = null;
		try {
			hash = torrent.getHash();
		} catch (Exception e) { }

		DownloadManager dm = global_manager.addDownloadManager(
				torrent_file.toString(), hash, data_location.toString(),
				getInitialState(), false);
		
		if ( dm == null ){
			
			throw( new DownloadException( "DownloadManager::addDownload - failed"));
		}
		
		addDownloadManager( dm );
		
		return( getDownload( dm ));
	}
	
	protected int
	getInitialState()
	{
	  	boolean	default_start_stopped = COConfigurationManager.getBooleanParameter( "Default Start Torrents Stopped" );

        return( default_start_stopped?DownloadManager.STATE_STOPPED:DownloadManager.STATE_WAITING);
	}
	
	protected DownloadImpl
	getDownload(
		DownloadManager	dm )
	
		throws DownloadException
	{
		DownloadImpl dl = (DownloadImpl)download_map.get(dm);
		
		if ( dl == null ){
			
			throw( new DownloadException("DownloadManager::getDownload: download not found"));
		}
		
		return( dl );
	}

	/**
	 * Retrieve the plugin Downlaod object related to the DowloadManager
	 * 
	 * @param dm DownloadManager to find
	 * @return plugin object
	 * @throws DownloadException
	 */
	public static DownloadImpl
	getDownloadStatic(
		DownloadManager	dm )
	
		throws DownloadException
	{
		if ( singleton != null ){
			
			return( singleton.getDownload( dm ));
		}
		
		throw( new DownloadException( "DownloadManager not initialised"));
	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -