FileDocCategorySizeDatePackage
TRNonBlockingServerProcessor.javaAPI DocAzureus 3.0.3.45930Wed Jun 13 14:06:26 BST 2007org.gudy.azureus2.core3.tracker.server.impl.tcp.nonblocking

TRNonBlockingServerProcessor

public abstract class TRNonBlockingServerProcessor extends org.gudy.azureus2.core3.tracker.server.impl.tcp.TRTrackerServerProcessorTCP
author
parg

Fields Summary
private static final int
READ_BUFFER_INITIAL
private static final int
READ_BUFFER_INCREMENT
private static final int
READ_BUFFER_LIMIT
private SocketChannel
socket_channel
private long
start_time
private ByteBuffer
read_buffer
private String
request_header
private ByteBuffer
write_buffer
Constructors Summary
protected TRNonBlockingServerProcessor(org.gudy.azureus2.core3.tracker.server.impl.tcp.TRTrackerServerTCP _server, SocketChannel _socket)

	
	
	
				
					 
	
		super( _server );
		
		socket_channel		= _socket;
		
		start_time	= SystemTime.getCurrentTime(); 
		
		read_buffer = ByteBuffer.allocate( READ_BUFFER_INITIAL );
		
		// System.out.println( "created: " + System.currentTimeMillis());
	
Methods Summary
protected voidasyncProcessComplete(java.io.ByteArrayOutputStream response)

		write_buffer = ByteBuffer.wrap( response.toByteArray());
	
		((TRNonBlockingServer)getServer()).readyToWrite( this );
	
protected voidclosed()

		// System.out.println( "close: " + System.currentTimeMillis());
	
protected voidcompleted()

		// System.out.println( "complete: " + System.currentTimeMillis());
	
protected voidfailed()

	
	
protected java.nio.channels.SocketChannelgetSocketChannel()

		return( socket_channel );
	
protected longgetStartTime()

		return( start_time );
	
public voidinterruptTask()

	
protected abstract java.io.ByteArrayOutputStreamprocess(java.lang.String input_header, java.lang.String lowercase_input_header, java.lang.String url_path, java.net.InetSocketAddress client_address, boolean announce_and_scrape_only, java.io.InputStream is)

protected intprocessRead()

		if ( read_buffer.remaining() == 0 ){
			
			int	capacity = read_buffer.capacity();
			
			if ( capacity == READ_BUFFER_LIMIT ){
				
				return( -1 );
				
			}else{
				
				read_buffer.position(0);
				
				byte[]	data = new byte[capacity];
				
				read_buffer.get( data );
				
				read_buffer = ByteBuffer.allocate( capacity + READ_BUFFER_INCREMENT );
				
				read_buffer.put( data );
			}
		}
		
		try{
			int	len = socket_channel.read( read_buffer );
			
			// System.out.println( "read op[" + len + "]: " + System.currentTimeMillis());


			if ( len < 0 ){
				
				return( -1 );
				
			}else if ( len == 0 ){
				
				return( 2 );	// no progress
			}
			
			byte[]	data = read_buffer.array();
						
			for (int i=read_buffer.position()-4;i>=0;i--){
				
				if ( 	data[i]   == CR &&
						data[i+1] == FF &&
						data[i+2] == CR &&
						data[i+3] == FF ){
					
					request_header = new String(data,0,read_buffer.position());
					
					// System.out.println( "read done: " + System.currentTimeMillis());
					
					getServer().runProcessor( this );
					
					return( 0 );				
				}
			}
			
			return( 1 );
			
		}catch( IOException e ){
			
			return( -1 );
		}
	
protected intprocessWrite()

		if ( write_buffer == null ){
			
			return( -1 );
		}
		
		if ( !write_buffer.hasRemaining()){
			
			return( 0 );
		}
		
		try{
			int	written = socket_channel.write( write_buffer );
			
			if ( written == 0 ){
				
				return( 2 );
			}
			
			if ( write_buffer.hasRemaining()){
				
				return( 1 );
			}
			
			return( 0 );
			
		}catch( IOException e ){
			
			return( -1 );
		}
	
public voidrunSupport()

		boolean	async = false;
		
		try{
			String	url = request_header.substring(4).trim();
			
			int	pos = url.indexOf( " " );
									
			url = url.substring(0,pos);
				
			ByteArrayOutputStream	response = 
				process( 	request_header,
							request_header.toLowerCase(),
							url, 
							(InetSocketAddress)socket_channel.socket().getRemoteSocketAddress(),
							TRTrackerServerImpl.restrict_non_blocking_requests,
							new ByteArrayInputStream(new byte[0]));
			
			if ( response == null ){
				
				async = true;
				
			}else{
				
				write_buffer = ByteBuffer.wrap( response.toByteArray());
			}
		}catch( Throwable e ){
			
			
		}finally{
			
			if ( !async ){
				
				((TRNonBlockingServer)getServer()).readyToWrite( this );
			}
		}