FileDocCategorySizeDatePackage
ResourceDownloaderTimeoutImpl.javaAPI DocAzureus 3.0.3.46268Thu Feb 09 19:42:50 GMT 2006org.gudy.azureus2.pluginsimpl.local.utils.resourcedownloader

ResourceDownloaderTimeoutImpl

public class ResourceDownloaderTimeoutImpl extends ResourceDownloaderBaseImpl implements ResourceDownloaderListener
author
parg

Fields Summary
protected ResourceDownloaderBaseImpl
delegate
protected int
timeout_millis
protected boolean
cancelled
protected ResourceDownloaderBaseImpl
current_downloader
protected Object
result
protected AESemaphore
done_sem
protected long
size
Constructors Summary
public ResourceDownloaderTimeoutImpl(ResourceDownloaderBaseImpl _parent, ResourceDownloader _delegate, int _timeout_millis)

	
	
	
			
					
									 
	
		super( _parent );
		
		delegate			= (ResourceDownloaderBaseImpl)_delegate;
		
		delegate.setParent( this );

		timeout_millis		= _timeout_millis;
	
Methods Summary
public voidasyncDownload()

		
		try{
			this_mon.enter();
		
			if ( !cancelled ){
				
				current_downloader = delegate.getClone( this );
				
				informActivity( getLogIndent() + "Downloading: " + getName());
	
				current_downloader.addListener( this );
				
				current_downloader.asyncDownload();
			
				Thread t = new AEThread( "ResourceDownloaderTimeout")
					{
						public void
						runSupport()
						{
							try{
								Thread.sleep( timeout_millis );
								
								cancel(new ResourceDownloaderException( "Download timeout"));
								
							}catch( Throwable e ){
								
								Debug.printStackTrace( e );
							}
						}
					};
				
				t.setDaemon(true);
		
				t.start();
			}
		}finally{
			
			this_mon.exit();
		}
	
public voidasyncGetSize()

		
		try{
			this_mon.enter();
		
			if ( !cancelled ){
				
				current_downloader = delegate.getClone( this );
						
				Thread	size_thread = new AEThread( "ResourceDownloader:size getter" )
					{
						public void
						runSupport()
						{
							try{
								long	res = current_downloader.getSize();
								
								result	= new Long(res);
								
								setProperties( current_downloader );
								
								done_sem.release();
								
							}catch( ResourceDownloaderException e ){
								
								failed( current_downloader, e );
							}
						}
					};
					
				size_thread.setDaemon( true );
			
				size_thread.start();
				
				Thread t = new AEThread( "ResourceDownloaderTimeout")
					{
						public void
						runSupport()
						{
							try{
								Thread.sleep( timeout_millis );
								
								cancel(new ResourceDownloaderException( "getSize timeout"));
								
							}catch( Throwable e ){
								
								Debug.printStackTrace( e );
							}
						}
					};
				
				t.setDaemon(true);
		
				t.start();
			}
		}finally{
			
			this_mon.exit();
		}
	
public voidcancel()

		cancel( new ResourceDownloaderException( "Download cancelled"));
	
protected voidcancel(ResourceDownloaderException reason)

		setCancelled();
		
		try{
			this_mon.enter();
		
			result	= reason; 
			
			cancelled	= true;
		
			informFailed((ResourceDownloaderException)result );
			
			if ( current_downloader != null ){
				
				current_downloader.cancel();
			}
		}finally{
			
			this_mon.exit();
		}
	
public booleancompleted(ResourceDownloader downloader, java.io.InputStream data)

		if (informComplete( data )){
			
			result	= data;
			
			done_sem.release();
			
			return( true );
		}
		
		return( false );
	
public java.io.InputStreamdownload()

		asyncDownload();
		
		done_sem.reserve();
		
		if ( result instanceof InputStream ){
			
			return((InputStream)result);
		}
		
		throw((ResourceDownloaderException)result);
	
public voidfailed(ResourceDownloader downloader, ResourceDownloaderException e)

		result		= e;
		
		done_sem.release();
		
		informFailed( e );
	
public ResourceDownloaderBaseImplgetClone(ResourceDownloaderBaseImpl parent)

		ResourceDownloaderTimeoutImpl c = new ResourceDownloaderTimeoutImpl( getParent(), delegate.getClone( parent ), timeout_millis );
		
		c.setSize( size );
		
		c.setProperties( this );

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

		return( delegate.getName() + ": timeout=" + timeout_millis );
	
public longgetSize()

	
		if ( size != -2 ){
			
			return( size );
		}
		
		try{
			ResourceDownloaderTimeoutImpl x = new ResourceDownloaderTimeoutImpl( getParent(), delegate.getClone( this ), timeout_millis );
		
			addReportListener( x );
			
			size = x.getSizeSupport();
			
			setProperties( x );
			
		}finally{
			
			if ( size == -2 ){
				
				size = -1;
			}
			
			setSize( size );
		}
	
		return( size );
	
protected longgetSizeSupport()

		asyncGetSize();
		
		done_sem.reserve();
		
		if ( result instanceof Long ){
			
			return(((Long)result).longValue());
		}
		
		throw((ResourceDownloaderException)result);
	
protected voidsetProperty(java.lang.String name, java.lang.Object value)

		setPropertySupport( name, value );
		
		delegate.setProperty( name, value );
	
protected voidsetSize(long l)

		size	= l;
		
		if ( size >= 0 ){
			
			delegate.setSize( size );
		}