FileDocCategorySizeDatePackage
EntityHandler.javaAPI DocAzureus 3.0.3.47828Wed Nov 01 21:13:26 GMT 2006com.aelitis.azureus.core.networkmanager.impl

EntityHandler

public class EntityHandler extends Object
Manages transfer entities on behalf of peer connections. Each entity handler has a global pool which manages all connections by default. Connections can also be "upgraded" to a higher connection control level, i.e. each connection has its own specialized entity for performance purposes.

Fields Summary
private final HashMap
upgraded_connections
private final org.gudy.azureus2.core3.util.AEMonitor
lock
private final MultiPeerUploader
global_uploader
private final MultiPeerDownloader
global_downloader
private boolean
global_registered
private final int
handler_type
Constructors Summary
public EntityHandler(int type, RateHandler rate_handler)
Create a new entity handler using the given rate handler.

param
type read or write type handler
param
rate_handler global max rate handler

  
  
                            
         
    this.handler_type = type;
    if( handler_type == TransferProcessor.TYPE_UPLOAD ) {
      global_uploader = new MultiPeerUploader( rate_handler );
      global_downloader = null;
    }
    else {  //download type
      global_downloader = new MultiPeerDownloader( rate_handler );
      global_uploader = null;
    }
  
Methods Summary
public voidcancelPeerConnection(NetworkConnectionBase connection)
Remove a peer connection from the entity handler.

param
connection to cancel

    if( handler_type == TransferProcessor.TYPE_UPLOAD ) {
      if( !global_uploader.removePeerConnection( connection ) ) {  //if not found in the pool entity
        SinglePeerUploader upload_entity = (SinglePeerUploader)upgraded_connections.remove( connection );  //check for it in the upgraded list
        if( upload_entity != null ) {
          NetworkManager.getSingleton().removeWriteEntity( upload_entity );  //cancel from write processing
        }
      }
    }
    else {
      if( !global_downloader.removePeerConnection( connection ) ) {  //if not found in the pool entity
        SinglePeerDownloader download_entity = (SinglePeerDownloader)upgraded_connections.remove( connection );  //check for it in the upgraded list
        if( download_entity != null ) {
          NetworkManager.getSingleton().removeReadEntity( download_entity );  //cancel from read processing
        }
      }
    }

  
public voiddowngradePeerConnection(NetworkConnectionBase connection)
Downgrade (return) a peer connection back into the general pool.

param
connection to downgrade back into the global entity

    try {  lock.enter();
      if( handler_type == TransferProcessor.TYPE_UPLOAD ) {
        SinglePeerUploader upload_entity = (SinglePeerUploader)upgraded_connections.remove( connection );  //remove from the upgraded list  
        if( upload_entity != null ) {
          NetworkManager.getSingleton().removeWriteEntity( upload_entity );  //cancel from write processing
        }
        else {
          Debug.out( "upload_entity == null" );
        }
        global_uploader.addPeerConnection( connection );  //move back to the general pool
      }
      else {
        SinglePeerDownloader download_entity = (SinglePeerDownloader)upgraded_connections.remove( connection );  //remove from the upgraded list  
        if( download_entity != null ) {
          NetworkManager.getSingleton().removeReadEntity( download_entity );  //cancel from read processing
        }
        else {
          Debug.out( "download_entity == null" );
        }
        global_downloader.addPeerConnection( connection );  //move back to the general pool
      } 
    }
    finally {  lock.exit();  }
  
public voidregisterPeerConnection(NetworkConnectionBase connection)
Register a peer connection for management by the handler.

param
connection to add to the global pool

    try {  lock.enter();
      if( !global_registered ) {
        if( handler_type == TransferProcessor.TYPE_UPLOAD ) {
          NetworkManager.getSingleton().addWriteEntity( global_uploader );  //register global upload entity
        }
        else {
          NetworkManager.getSingleton().addReadEntity( global_downloader );  //register global download entity
        }
        
        global_registered = true;
      }
    }
    finally {  lock.exit();  }
    
    if( handler_type == TransferProcessor.TYPE_UPLOAD ) {
      global_uploader.addPeerConnection( connection );
    }
    else {
      global_downloader.addPeerConnection( connection );
    }
  
public voidupgradePeerConnection(NetworkConnectionBase connection, RateHandler handler)
Upgrade a peer connection from the general pool to its own high-speed entity.

param
connection to upgrade from global management
param
handler individual connection rate handler

   
    try {  lock.enter();
      if( handler_type == TransferProcessor.TYPE_UPLOAD ) {
        SinglePeerUploader upload_entity = new SinglePeerUploader( connection, handler );
        if( !global_uploader.removePeerConnection( connection ) ) {  //remove it from the general upload pool
          Debug.out( "upgradePeerConnection:: upload entity not found/removed !" );
        }
        NetworkManager.getSingleton().addWriteEntity( upload_entity );  //register it for write processing
        upgraded_connections.put( connection, upload_entity );  //add it to the upgraded list
      }
      else {
        SinglePeerDownloader download_entity = new SinglePeerDownloader( connection, handler );
        if( !global_downloader.removePeerConnection( connection ) ) {  //remove it from the general upload pool
          Debug.out( "upgradePeerConnection:: download entity not found/removed !" );
        }
        NetworkManager.getSingleton().addReadEntity( download_entity );  //register it for read processing
        upgraded_connections.put( connection, download_entity );  //add it to the upgraded list
      }
    }
    finally {  lock.exit();  }