FileDocCategorySizeDatePackage
AENetworkClassifier.javaAPI DocAzureus 3.0.3.44328Thu Feb 09 19:42:44 GMT 2006org.gudy.azureus2.core3.util

AENetworkClassifier

public class AENetworkClassifier extends Object
author
parg

Fields Summary
public static final String
AT_PUBLIC
public static final String
AT_I2P
public static final String
AT_TOR
public static final String[]
AT_NETWORKS
private static List
listeners
Constructors Summary
Methods Summary
public static voidaddListener(AENetworkClassifierListener l)

		listeners.add(l);
	
public static java.lang.StringcategoriseAddress(java.lang.String str)

	
	  
	
			 
	
		int	last_dot = str.lastIndexOf('.");
		
		if ( last_dot == -1 ){
			
			return( AT_PUBLIC );	// no idea really, treat as normal
		}
		
		String	dom = str.substring(last_dot+1).toLowerCase();
		
		if ( dom.equals( "i2p" )){
			
			return( AT_I2P );
			
		}else if ( dom.equals( "onion" )){
			
			return( AT_TOR );
		}
		
		return( AT_PUBLIC );
	
public static java.lang.String[]getNetworks(TOTorrent torrent, java.lang.String display_name)

			// go through all the announce URL and find all networks
		
		List	urls = new ArrayList();
		
		urls.add( torrent.getAnnounceURL());
		
		TOTorrentAnnounceURLSet[] sets = torrent.getAnnounceURLGroup().getAnnounceURLSets();
		
		for (int i=0;i<sets.length;i++){
			
			URL[]	u = sets[i].getAnnounceURLs();
			
			for (int j=0;j<u.length;j++){
				
				urls.add( u[j] );
			}
		}
		
		List	available_networks = new ArrayList();
		
		for (int i=0;i<urls.size();i++){
			
			URL	u = (URL)urls.get(i);
					
			String	network = categoriseAddress( u.getHost());
			
			if ( !available_networks.contains( network )){
				
				available_networks.add( network );
			}
		}
		
		if ( available_networks.size() == 1 && available_networks.get(0) == AT_PUBLIC ){
			
			return( new String[]{ AT_PUBLIC });
		}
		
		
		boolean	prompt = COConfigurationManager.getBooleanParameter( "Network Selection Prompt" );
		
		List	res = new ArrayList();

		if ( prompt && listeners.size() > 0 ){

			String[]	t_nets = new String[available_networks.size()];
			
			available_networks.toArray( t_nets );

			for (int i=0;i<listeners.size();i++){
				
				try{
					String[]	selected = ((AENetworkClassifierListener)listeners.get(i)).selectNetworks(
											display_name,
											t_nets );
					
					if ( selected != null ){
						
						for (int j=0;j<selected.length;j++){
							
							res.add( selected[j] );
						}
					}
				}catch( Throwable e ){
					
					Debug.printStackTrace(e);
				}
			}
			
		}else{
				// use enabled defaults to proceed
			
			
			for (int i=0;i<available_networks.size();i++){
				
				if ( COConfigurationManager.getBooleanParameter( "Network Selection Default." + available_networks.get(i))){
			
					res.add( available_networks.get(i));
				}
			}
		}
		
		String[]	x = new String[res.size()];
		
		res.toArray( x );
		
		return( x );
	
public static voidremoveListener(AENetworkClassifierListener l)

		listeners.remove(l);