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

📄 trtrackerserverimpl.java

📁 这是一个基于java编写的torrent的P2P源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * File    : TRTrackerServerImpl.java
 * Created : 19-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.core3.tracker.server.impl;

/**
 * @author parg
 *
 */


import java.util.*;
import java.net.URL;
import java.net.URLDecoder;

import org.gudy.azureus2.core3.util.*;
import org.gudy.azureus2.core3.config.*;
import org.gudy.azureus2.core3.ipfilter.*;
import org.gudy.azureus2.core3.tracker.server.*;

public abstract class 
TRTrackerServerImpl 
	implements 	TRTrackerServer
{
	public static  int RETRY_MINIMUM_SECS			= 60;
	public static  int RETRY_MINIMUM_MILLIS			= RETRY_MINIMUM_SECS*1000;
	public static  int CLIENT_TIMEOUT_MULTIPLIER	= 3;
	
	public static int TIMEOUT_CHECK 				= RETRY_MINIMUM_MILLIS*CLIENT_TIMEOUT_MULTIPLIER;
	
	public static int		max_peers_to_send			= 0;
	public static boolean	send_peer_ids				= true;
	public static int		announce_cache_period		= TRTrackerServer.DEFAULT_ANNOUNCE_CACHE_PERIOD;
	public static int		scrape_cache_period			= TRTrackerServer.DEFAULT_SCRAPE_CACHE_PERIOD;
	public static int		announce_cache_threshold	= TRTrackerServer.DEFAULT_ANNOUNCE_CACHE_PEER_THRESHOLD;
	public static int		max_seed_retention			= 0;
	public static int		seed_limit					= 0;
	public static boolean	full_scrape_enable			= true;
	
	public static boolean	all_networks_permitted		= true;
	public static String[]	permitted_networks			= {};
	
	public static boolean	support_experimental_extensions;
	
	public static String	redirect_on_not_found		= "";
	
		// torrent map is static across all protocol servers
	
	private static Map		torrent_map = new HashMap(); 
	
	protected AEMonitor class_mon 	= new AEMonitor( "TRTrackerServer:class" );


	static{

		COConfigurationManager.addListener(
			new COConfigurationListener()
			{
				public void
				configurationSaved()
				{
					readConfig();
				}
			});
		
		readConfig();
	}

	protected static void
	readConfig()
	{
		send_peer_ids = COConfigurationManager.getBooleanParameter( "Tracker Send Peer IDs" );
				
		max_peers_to_send = COConfigurationManager.getIntParameter( "Tracker Max Peers Returned" );
				
		scrape_cache_period = COConfigurationManager.getIntParameter( "Tracker Scrape Cache", TRTrackerServer.DEFAULT_SCRAPE_CACHE_PERIOD );
				
		announce_cache_period = COConfigurationManager.getIntParameter( "Tracker Announce Cache", TRTrackerServer.DEFAULT_ANNOUNCE_CACHE_PERIOD );
						
		announce_cache_threshold = COConfigurationManager.getIntParameter( "Tracker Announce Cache Min Peers", TRTrackerServer.DEFAULT_ANNOUNCE_CACHE_PEER_THRESHOLD );

		max_seed_retention = COConfigurationManager.getIntParameter( "Tracker Max Seeds Retained", 0 );
		
		seed_limit = COConfigurationManager.getIntParameter( "Tracker Max Seeds", 0 );
		
		List	nets = new ArrayList();
		
		for (int i=0;i<AENetworkClassifier.AT_NETWORKS.length;i++){
			
			String	net = AENetworkClassifier.AT_NETWORKS[i];
			
			boolean	enabled = 
				COConfigurationManager.getBooleanParameter(
						"Tracker Network Selection Default." + net );
			
			if ( enabled ){
				
				nets.add( net );
			}
		}
		
		String[]	s_nets = new String[nets.size()];
		
		nets.toArray(s_nets);
		
		permitted_networks	= s_nets;
		
		all_networks_permitted = s_nets.length == AENetworkClassifier.AT_NETWORKS.length;
		
		full_scrape_enable = COConfigurationManager.getBooleanParameter( "Tracker Server Full Scrape Enable" );

		redirect_on_not_found = COConfigurationManager.getStringParameter( "Tracker Server Not Found Redirect" ).trim();
		
		support_experimental_extensions = COConfigurationManager.getBooleanParameter( "Tracker Server Support Experimental Extensions" );
	}
	
	protected static boolean
	getSendPeerIds()
	{
		return( send_peer_ids );
	}
	
	protected static int
	getMaxPeersToSend()
	{
		return( max_peers_to_send );
	}
	
	protected static int
	getScrapeCachePeriod()
	{
		return( scrape_cache_period );
	}
	
	protected static int
	getAnnounceCachePeriod()
	{
		return( announce_cache_period );
	}
	
	protected static int
	getAnnounceCachePeerThreshold()
	{
		return( announce_cache_threshold );
	}
	
	protected static int
	getMaxSeedRetention()
	{
		return( max_seed_retention );
	}
	
	protected static int
	getSeedLimit()
	{
		return( seed_limit );
	}
	
	public static boolean
	isFullScrapeEnabled()
	{
		return( full_scrape_enable );
	}
	
	protected static boolean
	getAllNetworksSupported()
	{
		return( all_networks_permitted );
	}
	
	protected static String[]
	getPermittedNetworks()
	{
		return( permitted_networks );
	}
	
	public static boolean
	supportsExtensions()
	{
		return( support_experimental_extensions );
	}
	
	protected IpFilter	ip_filter	= IpFilterManagerFactory.getSingleton().getIPFilter();
	
	private long		current_announce_retry_interval;
	private long		current_scrape_retry_interval;
	private long		current_total_clients;
	
	private int		current_min_poll_interval;
	
	private TRTrackerServerStatsImpl	stats = new TRTrackerServerStatsImpl( this );
		
	private String	name;
	private boolean	web_password_enabled;
	private boolean	web_password_https_only;
	
	private boolean	tracker_password_enabled;
	private String	password_user;
	private byte[]	password_pw;
	private boolean	compact_enabled;
	private boolean	key_enabled;
	

	protected Vector	listeners 			= new Vector();
	private List		auth_listeners		= new ArrayList();
	
	private Vector	request_listeners 	= new Vector();
	
	protected AEMonitor this_mon 	= new AEMonitor( "TRTrackerServer" );

	private COConfigurationListener		config_listener;
	private boolean						destroyed;
	
	private Set							biased_peers;
	
	private boolean						is_ready;
	
	public
	TRTrackerServerImpl(
		String		_name,
		boolean		_start_up_ready )
	{
		name		= _name==null?DEFAULT_NAME:_name;
		is_ready	= _start_up_ready;
		
		
		config_listener = 
			new COConfigurationListener()
			{
				public void
				configurationSaved()
				{
					readConfigSettings();
				}
			};
		
		COConfigurationManager.addListener( config_listener );

		readConfigSettings();
					
		current_min_poll_interval	= COConfigurationManager.getIntParameter("Tracker Poll Interval Min", DEFAULT_MIN_RETRY_DELAY );
		
		if ( current_min_poll_interval < RETRY_MINIMUM_SECS ){
			
			current_min_poll_interval = RETRY_MINIMUM_SECS;
		}
		
		current_announce_retry_interval = current_min_poll_interval;		

		int	scrape_percentage 		= COConfigurationManager.getIntParameter("Tracker Scrape Retry Percentage", DEFAULT_SCRAPE_RETRY_PERCENTAGE );		
	
		current_scrape_retry_interval	= (current_announce_retry_interval*scrape_percentage)/100;
		
		Thread timer_thread = 
			new AEThread("TrackerServer:timer.loop")
			{
				public void
				runSupport( )
				{
					timerLoop();
				}
			};
		
		timer_thread.setDaemon( true );
		
		timer_thread.start();
	}
	
	protected void
	readConfigSettings()
	{		
		web_password_enabled 		= COConfigurationManager.getBooleanParameter("Tracker Password Enable Web");
		tracker_password_enabled 	= COConfigurationManager.getBooleanParameter("Tracker Password Enable Torrent");

		web_password_https_only		= COConfigurationManager.getBooleanParameter("Tracker Password Web HTTPS Only");
		
		if ( web_password_enabled || tracker_password_enabled ){
			
			password_user	= COConfigurationManager.getStringParameter("Tracker Username", "");
			password_pw		= COConfigurationManager.getByteParameter("Tracker Password", new byte[0]);
		}
		
		compact_enabled = COConfigurationManager.getBooleanParameter("Tracker Compact Enable" );
		
		key_enabled = COConfigurationManager.getBooleanParameter("Tracker Key Enable Server");
	}

	public void
	setReady()
	{
		is_ready	= true;
	}
	
	public final boolean
	isReady()
	{
		return( is_ready );
	}
	
	public void
	setBiasedPeers(
		Set		peers )
	{
		if ( biased_peers != null && peers.equals( biased_peers )){
			
			return;
		}
		
		String	str = "";
		
		Iterator	it = peers.iterator();
		
		while( it.hasNext()){
			
			str += " " + it.next();
		}
		
		System.out.println( "biased peers: " + str );
		
		try{
			class_mon.enter();
		
			biased_peers = new HashSet( peers );
			
			Iterator	tit = torrent_map.values().iterator();
			
			while(tit.hasNext()){
							
				TRTrackerServerTorrentImpl	this_torrent = (TRTrackerServerTorrentImpl)tit.next();
				
				this_torrent.updateBiasedPeers( biased_peers );
			}
			
		}finally{
			
			class_mon.exit();
		}
	}
	
	protected Set
	getBiasedPeers()
	{
		return( biased_peers );
	}
	
	public boolean
	isWebPasswordEnabled()
	{
		return( web_password_enabled || auth_listeners.size() > 0 );
	}
	
	public boolean
	isTrackerPasswordEnabled()
	{
		return( tracker_password_enabled || auth_listeners.size() > 0 );
	}
	
	public boolean
	isWebPasswordHTTPSOnly()
	{
		return( web_password_https_only );
	}
	
	public boolean
	hasExternalAuthorisation()
	{
		return( auth_listeners.size() > 0 );
	}	
	
	public boolean
	hasInternalAuthorisation()
	{
		return( web_password_enabled || tracker_password_enabled );
	}
	
	public boolean
	performExternalAuthorisation(
		URL			resource,
		String		user,
		String		password )
	{
		for (int i=0;i<auth_listeners.size();i++){
			
			try{
				
				if ( ((TRTrackerServerAuthenticationListener)auth_listeners.get(i)).authenticate( resource, user, password )){
					
					return( true );
				}
			}catch( Throwable e ){
				
				Debug.printStackTrace( e );
			}
		}
		
		return( false );
	}
	
	public byte[]
	performExternalAuthorisation(
		URL			resource,
		String		user )
	{
		for (int i=0;i<auth_listeners.size();i++){
			
			try{
				
				byte[] sha_pw =  ((TRTrackerServerAuthenticationListener)auth_listeners.get(i)).authenticate( resource, user );
					
				if ( sha_pw != null ){
					
					return( sha_pw );
				}
			}catch( Throwable e ){
				
				Debug.printStackTrace( e );
			}
		}
		
		return( null );
	}
	
	public String
	getName()
	{
		return( name );
	}
	
	public boolean
	isCompactEnabled()
	{
		return( compact_enabled );
	}
	public boolean
	isKeyEnabled()
	{
		return( key_enabled );
	}
	
	public String
	getUsername()
	{
		return( password_user );
	}
	
	public byte[]
	getPassword()
	{
		return( password_pw );
	}
	
	public long
	getMinAnnounceRetryInterval()
	{
		return( current_min_poll_interval );
	}
	
	public long
	getAnnounceRetryInterval(
		TRTrackerServerTorrentImpl	torrent )
	{		
		long	clients = current_total_clients;
		
		if ( clients == 0 ){
			
			return( current_announce_retry_interval );
		}
		
		long	res = ( torrent.getPeerCount() * current_announce_retry_interval ) / clients;
		
		if ( res < current_min_poll_interval ){
			
			res = current_min_poll_interval;
		}
		
		return( res );
	}
	
	public long
	getScrapeRetryInterval(
		TRTrackerServerTorrentImpl	torrent )
	{		
		long	clients = current_total_clients;
		
		if ( torrent == null || clients == 0 ){
			
			return( current_scrape_retry_interval );
		}
		
		long	res = ( torrent.getPeerCount() * current_scrape_retry_interval ) / clients;
		
		if ( res < current_min_poll_interval ){
			
			res = current_min_poll_interval;
		}
		
		return( res );
	}
	
	public long
	getMinScrapeRetryInterval()
	{
		return( current_min_poll_interval );
	}

⌨️ 快捷键说明

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