FileDocCategorySizeDatePackage
ExternalSeedReaderGetRight.javaAPI DocAzureus 3.0.3.410519Tue Aug 14 23:26:42 BST 2007com.aelitis.azureus.plugins.extseed.impl.getright

ExternalSeedReaderGetRight

public class ExternalSeedReaderGetRight extends com.aelitis.azureus.plugins.extseed.impl.ExternalSeedReaderImpl

Fields Summary
private static final int
TARGET_REQUEST_SIZE_DEFAULT
private URL
url
private String
ip
private int
port
private com.aelitis.azureus.plugins.extseed.util.ExternalSeedHTTPDownloader[]
http_downloaders
private long[]
downloader_offsets
private long[]
downloader_lengths
private int
piece_size
private int
piece_group_size
Constructors Summary
protected ExternalSeedReaderGetRight(com.aelitis.azureus.plugins.extseed.ExternalSeedPlugin _plugin, org.gudy.azureus2.plugins.torrent.Torrent _torrent, URL _url, Map _params)

		
	
	
		 		
								
								
								 
	
		 
	
		super( _plugin, _torrent, _params );
				
		int target_request_size	= getIntParam( _params, "req_size", TARGET_REQUEST_SIZE_DEFAULT );
		
		url		= _url;
		
		ip		= url.getHost();
		port	= url.getPort();
		
		if ( port == -1 ){
			
			port = url.getDefaultPort();
		}
			
		piece_size = (int)getTorrent().getPieceSize();
		
		piece_group_size = target_request_size / piece_size;
		
		if ( piece_group_size == 0 ){
			
			piece_group_size	= 1;
		}
		
		TOTorrent	to_torrent = ((TorrentImpl)_torrent).getTorrent();

		String	ua = getUserAgent();
		
		if ( to_torrent.isSimpleTorrent()){
			
			http_downloaders = new ExternalSeedHTTPDownloader[]{ new ExternalSeedHTTPDownloader( url, ua )};
			
			downloader_offsets 	= new long[]{ 0 };
			downloader_lengths	= new long[]{ to_torrent.getSize() };
			
		}else{
			
			TOTorrentFile[]	files = to_torrent.getFiles();
			
			http_downloaders = new ExternalSeedHTTPDownloader[ files.length ];
			
			downloader_offsets 	= new long[ files.length ];
			downloader_lengths	= new long[ files.length ];

			long	offset	= 0;
			
				// encoding is a problem, assume ISO-8859-1
			
			String	base_url = url.toString();
			
			if ( base_url.endsWith( "/" )){
				
				base_url = base_url.substring( 0, base_url.length()-1 );
			}
			
			base_url += "/" + URLEncoder.encode( new String( to_torrent.getName(), "ISO-8859-1" ), "ISO-8859-1" );
			
			for (int i=0;i<files.length;i++ ){
				
				TOTorrentFile	file = files[i];
				
				long length = file.getLength();
					
				String	file_url_str = base_url;
				
				byte[][] bits = file.getPathComponents();
				
				for (int j=0;j<bits.length;j++){
					
					file_url_str += "/" + URLEncoder.encode( new String( bits[j], "ISO-8859-1" ), "ISO-8859-1" );
				}
				
				http_downloaders[i] = new ExternalSeedHTTPDownloader( new URL( file_url_str), ua );
				
				downloader_offsets[i]	= offset;
				downloader_lengths[i]	= length;
				
				offset += length;
			}
		}
	
Methods Summary
public java.lang.StringgetIP()

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

		return( "GR: " + url );
	
protected intgetPieceGroupSize()

		return( piece_group_size );
	
public intgetPort()

		return( port );
	
protected booleangetRequestCanSpanPieces()

		return( true );
	
protected voidreadData(com.aelitis.azureus.plugins.extseed.impl.ExternalSeedReaderRequest request)

		setReconnectDelay( RECONNECT_DEFAULT, false );
		
		long	request_start 	= request.getStartPieceNumber() * piece_size + request.getStartPieceOffset();
		int		request_length	= request.getLength();
		
		if ( http_downloaders.length == 1 ){
		
			ExternalSeedHTTPDownloader http_downloader = http_downloaders[0];
			
	        try{
				http_downloader.downloadRange( 
						request_start, 
						request_length,
						request,
						isTransient());
	
	        }catch( ExternalSeedException ese ){
	        	
	        	if ( http_downloader.getLastResponse() == 503 && http_downloader.getLast503RetrySecs() >= 0 ){
			
					int	retry_secs = http_downloader.getLast503RetrySecs();
					
					setReconnectDelay( retry_secs * 1000, true );
					
					throw( new ExternalSeedException( "Server temporarily unavailable, retrying in " + retry_secs + " seconds" ));
	        		
	        	}else{
	        		
	        		throw(ese);                	
	        	}
	        }
		}else{
			
			long	request_end = request_start + request_length;
			
			// System.out.println( "Req: start=" + request_start + ", len=" + request_length );
			
			final	byte[][] overlap_buffer 			= { null };
			final	int[]	 overlap_buffer_position 	= { 0 };
			
				// we've got to multiplex the (possible) multiple request buffers onto (possible) multi files
			
			for (int i=0;i<http_downloaders.length;i++){
				
				long	this_start 	= downloader_offsets[i];
				long	this_end	= this_start + downloader_lengths[i];

				if ( this_end <= request_start ){
					
					continue;
				}
					
				if ( this_start >= request_end ){
					
					break;
				}
				
				long	sub_request_start 	= Math.max( request_start, 	this_start );
				long	sub_request_end		= Math.min( request_end,	this_end );
	
				final int	sub_len = (int)( sub_request_end - sub_request_start );
				
				if ( sub_len == 0 ){
					
					continue;
				}
				
				ExternalSeedHTTPDownloader http_downloader = http_downloaders[i];
				
				// System.out.println( "    sub_req: start=" + sub_request_start + ", len=" + sub_len + ",url=" + http_downloader.getURL());
				
				ExternalSeedHTTPDownloaderListener sub_request = 
					new ExternalSeedHTTPDownloaderListener()
					{
						private int bytes_read;
							
						private byte[]	current_buffer 			= overlap_buffer[0];
						private int		current_buffer_position	= overlap_buffer_position[0];
						private int		current_buffer_length	= current_buffer==null?-1:Math.min( current_buffer.length, current_buffer_position + sub_len );
						
						public byte[]
			        	getBuffer()
			        	
			        		throws ExternalSeedException
			        	{
							if ( current_buffer == null ){
								
								current_buffer 			= request.getBuffer();
								current_buffer_position	= 0;
								current_buffer_length	= Math.min( current_buffer.length, sub_len - bytes_read );
							}
							
							return( current_buffer );
			        	}
			        	
			        	public void
			        	setBufferPosition(
			        		int	position )
			        	{
			        		current_buffer_position	= position;
			        		
			        		request.setBufferPosition( position );
			        	}
			        	
			        	public int
			        	getBufferPosition()
			        	{
			        		return( current_buffer_position );
			        	}
			        	
			        	public int
			        	getBufferLength()
			        	{
			        		return( current_buffer_length );
			        	}
			        	
			        	public int
			        	getPermittedBytes()
			        	
			        		throws ExternalSeedException
			        	{
			        		return( request.getPermittedBytes());
			        	}
			        	
			        	public void
			        	reportBytesRead(
			        		int		num )
			        	{
			        		bytes_read += num;
			        		
			        		request.reportBytesRead( num );
			        	}
			        	
			        	public void
			        	done()
			        	{
			        			// the current buffer is full up to the declared length
			        		
		        			int rem = current_buffer.length - current_buffer_length;
		        			
			        		if ( bytes_read == sub_len ){
			        			
			        				// this request is complete. save any partial buffer for
			        				// next request
			        			
			        			if ( rem == 0 ){
			        				
			        				overlap_buffer[0] 			= null;
			        				overlap_buffer_position[0]	= 0;
			        				
			        			}else{
			        				
			        				overlap_buffer[0]			= current_buffer;
			        				overlap_buffer_position[0]	= current_buffer_length;
			        			}
			        		}
			        		
			        			// prepare for next buffer if needed
			        		
			        		current_buffer = null;
			        		
			        		if ( rem == 0 ){
			        			
			        			request.done();
			        		}
			        	}
					};
					
		        try{
					http_downloader.downloadRange( 
							sub_request_start - this_start, 
							sub_len,
							sub_request,
							isTransient());
		
		        }catch( ExternalSeedException ese ){
		        	
		        	if ( http_downloader.getLastResponse() == 503 && http_downloader.getLast503RetrySecs() >= 0 ){
				
						int	retry_secs = http_downloader.getLast503RetrySecs();
						
						setReconnectDelay( retry_secs * 1000, true );
						
						throw( new ExternalSeedException( "Server temporarily unavailable, retrying in " + retry_secs + " seconds" ));
		        		
		        	}else{
		        		
		        		throw(ese);                	
		        	}
		        }
			}
		}
	
public booleansameAs(com.aelitis.azureus.plugins.extseed.ExternalSeedReader other)

		if ( other instanceof ExternalSeedReaderGetRight ){
			
			return( url.toString().equals(((ExternalSeedReaderGetRight)other).url.toString()));
		}
		
		return( false );