FileDocCategorySizeDatePackage
NetworkAdminSocksProxyImpl.javaAPI DocAzureus 3.0.3.45873Thu Jun 28 16:08:24 BST 2007com.aelitis.azureus.core.networkmanager.admin.impl

NetworkAdminSocksProxyImpl

public class NetworkAdminSocksProxyImpl extends Object implements com.aelitis.azureus.core.networkmanager.admin.NetworkAdminSocksProxy

Fields Summary
private final String
TARGET_HOST
private final int
TARGET_PORT
private String
host
private String
port
private String
user
private String
password
Constructors Summary
protected NetworkAdminSocksProxyImpl(String _host, String _port, String _user, String _password)

	
	
	
				
				
				
				 
	
		host		= _host;
		port		= _port;
		user		= _user;
		password	= _password;
 	
Methods Summary
public java.lang.StringgetHost()

		return( host );
	
public java.lang.StringgetName()

		return( host + ":" + port );
	
public java.lang.StringgetPort()

		return( port );
	
public java.lang.StringgetUser()

		return( user );
	
public java.lang.String[]getVersionsSupported()

		NetworkAdminException	failure = null;
		
		List	versions = new ArrayList();
		
		try{
			testVersion( "V4" );
			
			versions.add( "4" );
			
		}catch( NetworkAdminException e ){
			
			failure = e;
		}
		
		try{
			testVersion( "V4a" );
			
			versions.add( "4a" );
			
		}catch( NetworkAdminException e ){
			
			failure = e;
		}
		
		try{
			testVersion( "V5" );
			
			versions.add( "5" );
			
		}catch( NetworkAdminException e ){
			
			failure = e;
		}
		
		if ( versions.size() > 0 ){
	
			return((String[])versions.toArray( new String[versions.size()]));
			
		}
		
		throw( failure );
	
protected booleanisConfigured()

		return( host.length() > 0 );
	
protected voidtestVersion(java.lang.String version)

		final int RES_CONNECT_FAILED	= 0;
		final int RES_SOCKS_FAILED		= 1;
		final int RES_OK				= 3;

		final AESemaphore	sem = new AESemaphore( "NetworkAdminSocksProxy:test" );
		
		final int[]	result = { RES_CONNECT_FAILED };
		
		final NetworkAdminException[]	error = { null };
		
		try{
			InetSocketAddress		socks_address = new InetSocketAddress( InetAddress.getByName( host ), Integer.parseInt(port));
			
			final InetSocketAddress	target_address = new InetSocketAddress( TARGET_HOST, TARGET_PORT );
			
			TCPConnectionManager.ConnectListener connect_listener = 
				new TCPConnectionManager.ConnectListener() 
			{
				public void 
				connectAttemptStarted() 
				{	
				}
	
				public void 
				connectSuccess( 
					SocketChannel channel ) 
				{
					final TCPTransportImpl	transport = 
						new TCPTransportImpl(
								new ProtocolEndpointTCP( target_address ), false, false, null );
					
					transport.setFilter( TCPTransportHelperFilterFactory.createTransparentFilter( channel ));
	
					new ProxyLoginHandler( 
							transport, 
							target_address, 
							new ProxyLoginHandler.ProxyListener() 
							{
								public void 
								connectSuccess() 
								{
									transport.close( "Done" );
									
									result[0] 	= RES_OK;

									sem.release();
								}
	
								public void 
								connectFailure(
									Throwable failure_msg ) 
								{
									transport.close( "Proxy login failed" );
									
									result[0] 	= RES_SOCKS_FAILED;
									error[0]	= new NetworkAdminException( "Proxy connect failed", failure_msg );
									
									sem.release();
								}
							},
							version,
							user,
							password );
				}
	
				public void 
				connectFailure( 
					Throwable failure_msg ) 
				{
					result[0] 	= RES_CONNECT_FAILED;
					error[0]	= new NetworkAdminException( "Connect failed", failure_msg );
					
					sem.release();
				}
			};
	
			TCPNetworkManager.getSingleton().getConnectDisconnectManager().requestNewConnection(
					socks_address, connect_listener );
						
		}catch( Throwable e ){
			
			result[0] 	= RES_CONNECT_FAILED;
			error[0]	= new NetworkAdminException( "Connect failed", e );
			
			sem.release();
		}
		
		if ( !sem.reserve(10000)){
			
			result[0] 	= RES_CONNECT_FAILED;
			error[0] 	= new NetworkAdminException( "Connect timeout" );
		}
		
		if ( result[0] != RES_OK ){
				
			throw( error[0] );
		}