FileDocCategorySizeDatePackage
UDPTransportHelper.javaAPI DocAzureus 3.0.3.411202Mon Mar 05 17:13:52 GMT 2007com.aelitis.azureus.core.networkmanager.impl.udp

UDPTransportHelper

public class UDPTransportHelper extends Object implements com.aelitis.azureus.core.networkmanager.impl.TransportHelper

Fields Summary
public static final int
READ_TIMEOUT
public static final int
CONNECT_TIMEOUT
private UDPConnectionManager
manager
private UDPSelector
selector
private InetSocketAddress
address
private UDPTransport
transport
private boolean
incoming
private UDPConnection
connection
private selectListener
read_listener
private Object
read_attachment
private boolean
read_selects_paused
private selectListener
write_listener
private Object
write_attachment
private boolean
write_selects_paused
private boolean
closed
private IOException
failed
private ByteBuffer
pending_partial_write
private Map
user_data
Constructors Summary
protected UDPTransportHelper(UDPConnectionManager _manager, InetSocketAddress _address, UDPTransport _transport)

	
	
	
			
				
					 
	
		 
	
			// outgoing
	
		manager		= _manager;
		address 	= _address;
		transport	= _transport;
		
		incoming	= false;
		
		connection 	= manager.registerOutgoing( this );
		
		selector	= connection.getSelector();

	
protected UDPTransportHelper(UDPConnectionManager _manager, InetSocketAddress _address, UDPConnection _connection)

			// incoming
			
		manager		= _manager;
		address 	= _address;
		connection = _connection;
	
		incoming	= true;
		
		selector	= connection.getSelector();
	
Methods Summary
protected voidcanRead()

    	fireReadSelect();
    
protected voidcanWrite()

    	fireWriteSelect();
    
public synchronized voidcancelReadSelects()

    	selector.cancel( this, read_listener );
    	
    	read_selects_paused	= true;
      	read_listener		= null;
    	read_attachment		= null;
    
public synchronized voidcancelWriteSelects()

       	selector.cancel( this, write_listener );
        
    	write_selects_paused	= true;
     	write_listener			= null;
    	write_attachment		= null;
    
public voidclose(java.lang.String reason)

    	synchronized( this ){
    		
       		closed	= true;
       		
    		fireReadSelect();
      		fireWriteSelect();
      	}
    	
    	connection.closeSupport( reason );
    
public booleandelayWrite(java.nio.ByteBuffer buffer)

			// TODO: support this one day?
		
		return false;
	
public voidfailed(java.lang.Throwable reason)

    	synchronized( this ){
        		
    		if ( reason instanceof IOException ){
    			
    			failed = (IOException)reason;
    			
    		}else{
    			
    			failed	= new IOException( Debug.getNestedExceptionMessageAndStack(reason));
    		}
    	
    		fireReadSelect();
    		fireWriteSelect();
    	}
    	
    	connection.failedSupport( reason );
    
protected voidfireReadSelect()

     	synchronized( this ){
     		 
	   		if ( read_listener != null && !read_selects_paused ){
	   			
	   			if ( failed != null  ){
	   				
	   	 			selector.ready( this, read_listener, read_attachment, failed );
	   	 		  
	   			}else if ( closed ){
	   				
	   	   			selector.ready( this, read_listener, read_attachment, new Throwable( "Transport closed" ));
	   	   		 
	   			}else if ( connection.canRead()){
	   				
	   	 			selector.ready( this, read_listener, read_attachment );
	   			}
	   		}
     	}
    
protected voidfireWriteSelect()

      	synchronized( this ){
       	    
	   		if ( write_listener != null && !write_selects_paused ){
	   			
	   			if ( failed != null  ){
	   				
	   				write_selects_paused	= true;
	   				
	   	 			selector.ready( this, write_listener, write_attachment, failed );
	   	 		  
	   			}else if ( closed ){
	   				
	   				write_selects_paused	= true;

	   	   			selector.ready( this, write_listener, write_attachment, new Throwable( "Transport closed" ));

	   			}else if ( connection.canWrite()){
	   				
	   				write_selects_paused	= true;
	   					   				
	   	 			selector.ready( this, write_listener, write_attachment );
	   			}
	   		}
    	}
    
public java.net.InetSocketAddressgetAddress()

		return( address );
	
public intgetConnectTimeout()

		return( CONNECT_TIMEOUT );
	
protected UDPConnectiongetConnection()

		return( connection );
	
protected intgetMss()

		if ( transport == null ){
			
			return( UDPNetworkManager.getUdpMssSize());
		}
		
		return( transport.getMssSize());
	
public java.lang.StringgetName()

		return( " (UDP)" );
	
public intgetReadTimeout()

		return( READ_TIMEOUT );
	
protected UDPTransportgetTransport()

		return( transport );
	
public synchronized java.lang.ObjectgetUserData(java.lang.Object key)

		if ( user_data == null ){

			return(null);

		}

		return( user_data.get( key ));
	
public booleanhasDelayedWrite()

		return( false );
	
public booleanisIncoming()

		return( incoming );
	
public booleanminimiseOverheads()

		return( UDPNetworkManager.MINIMISE_OVERHEADS );
	
public synchronized voidpauseReadSelects()

    	if ( read_listener != null ){
    		
    		selector.cancel( this, read_listener );
    	}
    	
    	read_selects_paused	= true;
    
public synchronized voidpauseWriteSelects()

    	if ( write_listener != null ){
    		
    		selector.cancel( this, write_listener );
    	}
    	
    	write_selects_paused = true;
    
protected voidpoll()

	   	synchronized( this ){
	   		
	   		fireReadSelect();
	   		
	   		fireWriteSelect();
	   	}
	
public intread(java.nio.ByteBuffer buffer)

		synchronized( this ){
			
			if ( failed != null ){
				
				throw( failed );
			}
			
			if ( closed ){
				
				throw( new IOException( "Transport closed" ));
			}
		}
		
    	return( connection.read( buffer ));
    
public longread(java.nio.ByteBuffer[] buffers, int array_offset, int length)

		synchronized( this ){
			
			if ( failed != null ){
				
				throw( failed );
			}
			
			if ( closed ){
				
				throw( new IOException( "Transport closed" ));
			}
		}
		
    	long	total = 0;
    	
    	for (int i=array_offset;i<array_offset+length;i++){
    		
    		ByteBuffer	buffer = buffers[i];
    		
    		int	max = buffer.remaining();
    		
    		int	read = connection.read( buffer );
    		
    		total += read;
    		
    		if ( read < max ){
    		
    			break;
    		}
    	}
    	//System.out.println( "total = " + total );
    	return( total );
    
public voidregisterForReadSelects(selectListener listener, java.lang.Object attachment)

    	synchronized( this ){
    		
	    	read_listener		= listener;
	    	read_attachment		= attachment;
    	}
    	
    	resumeReadSelects();
    
public voidregisterForWriteSelects(selectListener listener, java.lang.Object attachment)

    	synchronized( this ){
    		
	      	write_listener		= listener;
	    	write_attachment	= attachment;  
    	} 
    	
    	resumeWriteSelects();
    
public synchronized voidresumeReadSelects()

    	read_selects_paused = false;
    	
    	fireReadSelect();
    
public synchronized voidresumeWriteSelects()

    	write_selects_paused = false;
    	    	
    	fireWriteSelect();
    
public voidsetTrace(boolean on)

	
protected voidsetTransport(UDPTransport _transport)

		transport	= _transport;
	
public synchronized voidsetUserData(java.lang.Object key, java.lang.Object data)

		if ( user_data == null ){

			user_data = new HashMap();
		}

		user_data.put( key, data );
	
public intwrite(java.nio.ByteBuffer buffer, boolean partial_write)

		synchronized( this ){
			
			if ( failed != null ){
				
				throw( failed );
			}
			
			if ( closed ){
				
				throw( new IOException( "Transport closed" ));
			}
		}
		
		if ( partial_write ){
			
			if ( pending_partial_write == null ){
				
				if ( buffer.remaining() < UDPConnectionSet.MIN_WRITE_PAYLOAD ){
				
					ByteBuffer	copy = ByteBuffer.allocate( buffer.remaining());
					
					copy.put( buffer );
					
					copy.position( 0 );
					
					pending_partial_write = copy;
					
					return( copy.remaining());
				}
			}
		}
		
		if ( pending_partial_write != null ){
			
			try{
				int	pw_len = pending_partial_write.remaining();
				
				int	written = connection.write( new ByteBuffer[]{ pending_partial_write, buffer }, 0, 2 );
				
				if ( written >= pw_len ){
					
					return( written - pw_len );
					
				}else{
					
					return( 0 );
				}
				
			}finally{
				
				if ( pending_partial_write.remaining() == 0 ){
					
					pending_partial_write = null;
				}
			}
			
		}else{
			
			return( connection.write( new ByteBuffer[]{ buffer }, 0, 1 ));
		}
	
public longwrite(java.nio.ByteBuffer[] buffers, int array_offset, int length)

		synchronized( this ){
			
			if ( failed != null ){
				
				throw( failed );
			}
			
			if ( closed ){
				
				throw( new IOException( "Transport closed" ));
			}
		}
		
		if ( pending_partial_write != null ){
			
			ByteBuffer[]	buffers2 = new ByteBuffer[length+1];
			
			buffers2[0] = pending_partial_write;
			
			int	pos = 1;

			for (int i=array_offset;i<array_offset+length;i++){
				
				buffers2[pos++] = buffers[i];
			}
			
			try{
				int	pw_len = pending_partial_write.remaining();
				
				int written = connection.write( buffers2, 0, buffers2.length );
				
				if ( written >= pw_len ){
					
					return( written - pw_len );
					
				}else{
					
					return( 0 );
				}
				
			}finally{
				
				if ( pending_partial_write.remaining() == 0 ){
					
					pending_partial_write = null;
				}
			}
		}else{
		
			return( connection.write( buffers, array_offset, length ));
		}