FileDocCategorySizeDatePackage
MultiPeerDownloader.javaAPI DocAzureus 3.0.3.46252Fri Mar 09 12:55:48 GMT 2007com.aelitis.azureus.core.networkmanager.impl

MultiPeerDownloader

public class MultiPeerDownloader extends Object implements RateControlledEntity

Fields Summary
private volatile ArrayList
connections_cow
private final org.gudy.azureus2.core3.util.AEMonitor
connections_mon
private final RateHandler
main_handler
private int
next_position
Constructors Summary
public MultiPeerDownloader(RateHandler main_handler)
Create new downloader using the given "global" rate handler to limit all peers managed by this downloader.

param
main_handler

  
  
                        
       
    this.main_handler = main_handler;
  
Methods Summary
public voidaddPeerConnection(NetworkConnectionBase connection)
Add the given connection to the downloader.

param
connection to add

    try {  connections_mon.enter();
      //copy-on-write
      ArrayList conn_new = new ArrayList( connections_cow.size() + 1 );
      conn_new.addAll( connections_cow );
      conn_new.add( connection );
      connections_cow = conn_new;
    }
    finally{ connections_mon.exit();  }
  
public booleancanProcess(EventWaiter waiter)

    if( main_handler.getCurrentNumBytesAllowed() < 1/*NetworkManager.getTcpMssSize()*/ )  return false;

    return true;
  
public booleandoProcessing(EventWaiter waiter)

    int num_bytes_allowed = main_handler.getCurrentNumBytesAllowed();
    if( num_bytes_allowed < 1 )  return false;

    ArrayList connections = connections_cow;
    int num_checked = 0;
    int num_bytes_remaining = num_bytes_allowed;

    while( num_bytes_remaining > 0 && num_checked < connections.size() ) {
      next_position = next_position >= connections.size() ? 0 : next_position;  //make circular
      
      NetworkConnectionBase connection = (NetworkConnectionBase)connections.get( next_position );
      next_position++;
      num_checked++;
      
      if( connection.getTransportBase().isReadyForRead( waiter ) ) {
    	int	mss = connection.getMssSize();
        int allowed = num_bytes_remaining > mss ? mss : num_bytes_remaining;
          
        int bytes_read = 0;
          
        try {
          bytes_read = connection.getIncomingMessageQueue().receiveFromTransport( allowed );
        }
        catch( Throwable e ) {
          
          if( AEDiagnostics.TRACE_CONNECTION_DROPS ) {
            if( e.getMessage() == null ) {
              Debug.out( "null read exception message: ", e );
            }
            else {
              if( e.getMessage().indexOf( "end of stream on socket read" ) == -1 &&
                  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( "An established connection was aborted by the software in your host machine" ) == -1 ) {
                  
                System.out.println( "MP: read exception [" +connection.getTransportBase().getDescription()+ "]: " +e.getMessage() );
              }
            }
          }

          if (! (e instanceof IOException )){
        	
        	  Debug.printStackTrace(e);
          }
          
          connection.notifyOfException( e );
        }

        num_bytes_remaining -= bytes_read;
      }
    }
    
    int total_bytes_read = num_bytes_allowed - num_bytes_remaining;
    if( total_bytes_read > 0 ) {
      main_handler.bytesProcessed( total_bytes_read );
      return true;
    }

    return false;  //zero bytes read
  
public longgetBytesReadyToWrite()

	  return( 0 );
  
public intgetConnectionCount()

	  return(connections_cow.size());
  
public intgetPriority()

  return RateControlledEntity.PRIORITY_HIGH;  
public intgetReadyConnectionCount(EventWaiter waiter)

	  int	res = 0;
	  
	  for (Iterator it=connections_cow.iterator();it.hasNext();){
	      
	      NetworkConnectionBase connection = (NetworkConnectionBase)it.next();
	      
	      if ( connection.getTransportBase().isReadyForRead( waiter )){
	    	  
	    	  res++;
	      }
	  }
	  
	  return( res );
  
public java.lang.StringgetString()

	  String	str = "";
  
	  for (Iterator it=connections_cow.iterator();it.hasNext();){
	      
	      NetworkConnectionBase connection = (NetworkConnectionBase)it.next();
	      
	      str += (str.length()==0?"":",") + connection.getString();
	  }
	  
	  return( "MPD: " + str );
  
public booleanremovePeerConnection(NetworkConnectionBase connection)
Remove the given connection from the downloader.

param
connection to remove
return
true if the connection was found and removed, false if not removed

    try {  connections_mon.enter();
      //copy-on-write
      ArrayList conn_new = new ArrayList( connections_cow );
      boolean removed = conn_new.remove( connection );
      if( !removed ) return false;
      connections_cow = conn_new;
      return true;
    }
    finally{ connections_mon.exit();  }