FileDocCategorySizeDatePackage
SinglePeerUploader.javaAPI DocAzureus 3.0.3.44892Wed Jul 18 11:51:54 BST 2007com.aelitis.azureus.core.networkmanager.impl

SinglePeerUploader

public class SinglePeerUploader extends Object implements RateControlledEntity
A fast write entity backed by a single peer connection.

Fields Summary
private final com.aelitis.azureus.core.networkmanager.NetworkConnectionBase
connection
private final RateHandler
rate_handler
Constructors Summary
public SinglePeerUploader(com.aelitis.azureus.core.networkmanager.NetworkConnectionBase connection, RateHandler rate_handler)

    this.connection = connection;
    this.rate_handler = rate_handler;
  
Methods Summary
public booleancanProcess(com.aelitis.azureus.core.networkmanager.EventWaiter waiter)

    if( !connection.getTransportBase().isReadyForWrite(waiter) )  {
      return false;  //underlying transport not ready
    }
    if( connection.getOutgoingMessageQueue().getTotalSize() < 1 ) {
      return false;  //no data to send
    }
    if( rate_handler.getCurrentNumBytesAllowed() < 1 ) {
      return false;  //not allowed to send any bytes
    }
    return true;
  
public booleandoProcessing(com.aelitis.azureus.core.networkmanager.EventWaiter waiter)

    if( !connection.getTransportBase().isReadyForWrite(waiter) )  {
      //Debug.out("dW:not ready"); happens sometimes, just live with it as non-fatal
      return false;
    }
    
    int num_bytes_allowed = rate_handler.getCurrentNumBytesAllowed();
    if( num_bytes_allowed < 1 )  {
      return false;
    }
    
    int num_bytes_available = connection.getOutgoingMessageQueue().getTotalSize();
    if( num_bytes_available < 1 ) {
      if ( !connection.getOutgoingMessageQueue().isDestroyed()){
    	  //Debug.out("dW:not avail"); happens sometimes, just live with it as non-fatal
      }
      return false;
    }
    
    int num_bytes_to_write = num_bytes_allowed > num_bytes_available ? num_bytes_available : num_bytes_allowed;
    
    //int mss = NetworkManager.getTcpMssSize();
    //if( num_bytes_to_write > mss )  num_bytes_to_write = mss;
    
    int written = 0;
    try {
      written = connection.getOutgoingMessageQueue().deliverToTransport( num_bytes_to_write, false );
    }
    catch( Throwable e ) {
      
      if( AEDiagnostics.TRACE_CONNECTION_DROPS ) {
        if( e.getMessage() == null ) {
          Debug.out( "null write exception message: ", e );
        }
        else {
          if( e.getMessage().indexOf( "An existing connection was forcibly closed by the remote host" ) == -1 &&
              e.getMessage().indexOf( "Connection reset by peer" ) == -1 &&
              e.getMessage().indexOf( "Broken pipe" ) == -1 &&
              e.getMessage().indexOf( "An established connection was aborted by the software in your host machine" ) == -1 ) {
            
            System.out.println( "SP: write exception [" +connection.getTransportBase().getDescription()+ "]: " +e.getMessage() );
          }
        }
      }

      if (! (e instanceof IOException )){
      	
    	  Debug.printStackTrace(e);
      }
      
      connection.notifyOfException( e );
      return false;
    }
    
    if( written < 1 )  {
      return false;
    }
    
    rate_handler.bytesProcessed( written );
    return true;
  
public longgetBytesReadyToWrite()

	  return( connection.getOutgoingMessageQueue().getTotalSize());
  
public intgetConnectionCount()

	  return( 1 );
  
public intgetPriority()

    return RateControlledEntity.PRIORITY_NORMAL;
  
public intgetReadyConnectionCount(com.aelitis.azureus.core.networkmanager.EventWaiter waiter)

	  if ( connection.getTransportBase().isReadyForWrite(waiter)){
		  
		  return( 1 );
	  }
	  
	  return( 0 );
  
public java.lang.StringgetString()

	  return( "SPU: bytes_allowed=" + rate_handler.getCurrentNumBytesAllowed() + " " + connection.getString());