FileDocCategorySizeDatePackage
IPToHostNameResolver.javaAPI DocAzureus 3.0.3.43897Thu Feb 01 09:44:16 GMT 2007org.gudy.azureus2.core3.util

IPToHostNameResolver

public class IPToHostNameResolver extends Object
author
parg

Fields Summary
protected static Thread
resolver_thread
protected static List
request_queue
protected static AEMonitor
request_mon
protected static AESemaphore
request_semaphore
Constructors Summary
Methods Summary
public static IPToHostNameResolverRequestaddResolverRequest(java.lang.String ip, IPToHostNameResolverListener l)

	
	  
	
									
			 
	
		try{
			request_mon.enter();
			
			IPToHostNameResolverRequest	request = new IPToHostNameResolverRequest( ip, l );
			
			request_queue.add( request );
			
			request_semaphore.release();
			
			if ( resolver_thread == null ){
				
				resolver_thread = 
					new AEThread("IPToHostNameResolver")
					{
						public void
						runSupport()
						{
							while(true){
								
								try{
									request_semaphore.reserve();
									
									IPToHostNameResolverRequest	req;
									
									try{
										request_mon.enter();
										
										req	= (IPToHostNameResolverRequest)request_queue.remove(0);
										
									}finally{
										
										request_mon.exit();
									}
									
									IPToHostNameResolverListener	listener = req.getListener();
									
										// if listener is null the request has been cancelled
									
									if ( listener != null ){
										
										try{
											InetAddress addr = InetAddress.getByName( req.getIP());
												
											req.getListener().IPResolutionComplete( addr.getHostName(), true );
												
										}catch( Throwable e ){
											
											req.getListener().IPResolutionComplete( req.getIP(), false );
											
										}
									}
								}catch( Throwable e ){
									
									Debug.printStackTrace( e );
								}
							}
						}
					};
					
				resolver_thread.setDaemon( true );	
					
				resolver_thread.start();
			}
			
			return( request );
			
		}finally{
			
			request_mon.exit();
		}
	
public static java.lang.StringsyncResolve(java.lang.String ip, int timeout)

		final AESemaphore	sem = new AESemaphore( "IPToHostNameREsolver:sync" );
		
		final Object[]	result = {null};
				
		addResolverRequest(
			ip, 
			new IPToHostNameResolverListener()
			{
				public void
				IPResolutionComplete(
					String		resolved_ip,
					boolean		succeeded )
				{
					try{
						synchronized( result ){

							if ( succeeded ){
									
								result[0] = resolved_ip;
							}
						}
					}finally{
						
						sem.release();
					}
				}
			});
			
		if ( !sem.reserve( timeout )){
			
			throw( new Exception( "Timeout" ));
		}
				
		synchronized( result ){

			if ( result[0] != null ){
				
				return((String)result[0]);
			}
			
			throw( new UnknownHostException( ip ));
		}