FileDocCategorySizeDatePackage
PeerManagerImpl.javaAPI DocAzureus 3.0.3.411956Fri Sep 07 15:45:12 BST 2007org.gudy.azureus2.pluginsimpl.local.peers

PeerManagerImpl

public class PeerManagerImpl extends Object implements PeerManager
author
parg

Fields Summary
private static final String
PEPEER_DATA_KEY
protected PEPeerManager
manager
protected static org.gudy.azureus2.core3.util.AEMonitor
pm_map_mon
private Map
foreign_map
private Map
listener_map
protected org.gudy.azureus2.core3.util.AEMonitor
this_mon
private final org.gudy.azureus2.core3.disk.DiskManagerPiece[]
dm_pieces
private final PEPiece[]
pe_pieces
private pieceFacade[]
piece_facades
private boolean
destroyed
Constructors Summary
protected PeerManagerImpl(PEPeerManager _manager)

	
	
	
			 
	
		manager	= _manager;
		
		dm_pieces	= _manager.getDiskManager().getPieces();
		pe_pieces	= _manager.getPieces();
		
		manager.addListener(
			new PEPeerManagerListener()
			{
				 public void
				 peerAdded(
					PEPeerManager	manager, 
					PEPeer 			peer )
				 {
					 
				 }
				 
				 public void 
				 peerRemoved( 
					PEPeerManager 	manager, 
					PEPeer 			peer )
				 {
					 PeerImpl	dele = getPeerForPEPeer( peer );
					 
					 if ( dele != null ){
						 
						 dele.closed();
					 }
				 }
				 
				public void
				destroyed()
				{	
					synchronized( foreign_map ){
						
						destroyed	= true;
						
						Iterator it = foreign_map.values().iterator();
						
						while( it.hasNext()){
							
							try{
								((PeerForeignDelegate)it.next()).stop();
								
							}catch( Throwable e ){
								
								Debug.printStackTrace( e );
							}
						}
					}
				}
			});
	
Methods Summary
public voidaddListener(PeerManagerListener l)

		try{
			this_mon.enter();
		
			final Map	peer_map = new HashMap();
			
      PEPeerManagerListener core_listener = new PEPeerManagerListener() {
        public void peerAdded( PEPeerManager manager, PEPeer peer ) {
          PeerImpl pi = getPeerForPEPeer( peer );
          peer_map.put( peer, pi );
          l.peerAdded( PeerManagerImpl.this, pi );
        }

        public void peerRemoved( PEPeerManager manager, PEPeer peer ) {
          PeerImpl  pi = (PeerImpl)peer_map.remove( peer );
          
          if ( pi == null ){
            // somewhat inconsistently we get told here about the removal of
            // peers that never connected (and weren't added)
            // Debug.out( "PeerManager: peer not found");
          }
          else{         
            l.peerRemoved( PeerManagerImpl.this, pi );
          }
        }
        public void
        destroyed()
        {
        }
      };
      
			listener_map.put( l, core_listener );
		
			manager.addListener( core_listener );
		}finally{
			
			this_mon.exit();
		}
	
public voidaddPeer(Peer peer)

			// no private check here, we come through here for webseeds for example
		
		manager.addPeer(mapForeignPeer( peer ));
	
public voidaddPeer(java.lang.String ip_address, int tcp_port)

		checkIfPrivate();
		
		manager.addPeer( ip_address, tcp_port, 0, NetworkManager.getCryptoRequired( NetworkManager.CRYPTO_OVERRIDE_NONE ));
	
public voidaddPeer(java.lang.String ip_address, int tcp_port, boolean use_crypto)

		checkIfPrivate();
		
		manager.addPeer( ip_address, tcp_port, 0, use_crypto );
	
public voidaddPeer(java.lang.String ip_address, int tcp_port, int udp_port, boolean use_crypto)

		checkIfPrivate();
		
		manager.addPeer( ip_address, tcp_port, udp_port, use_crypto );
	
protected voidcheckIfPrivate()

		Download dl;
		
		try{
			dl = getDownload();
			
		}catch( Throwable e ){
			
			// if this didn't work then nothing much else will so just fall through
			
			return;
		}
		
		Torrent t = dl.getTorrent();
		
		if ( t != null ){
			
			if ( t.isPrivate()){
				
				throw( new RuntimeException( "Torrent is private, peer addition not permitted" ));
			}
		}
	
public PeerStatscreatePeerStats(Peer peer)

		PEPeer	delegate = mapForeignPeer( peer );
		
		return( new PeerStatsImpl( this, peer, manager.createPeerStats( delegate )));
	
public PEPeerManagergetDelegate()

		return( manager );
	
public org.gudy.azureus2.plugins.disk.DiskManagergetDiskManager()

		return( new DiskManagerImpl( manager.getDiskManager()));
	
public DownloadgetDownload()

		return( DownloadManagerImpl.getDownloadStatic( manager.getDiskManager().getTorrent()));
	
public intgetDownloadRateLimitBytesPerSecond()

		return( manager.getDownloadRateLimitBytesPerSecond());
	
public static PeerImplgetPeerForPEPeer(PEPeer pe_peer)

		PeerImpl	peer = (PeerImpl)pe_peer.getData( PEPEER_DATA_KEY );
		
		if ( peer == null ){
			
			peer = new PeerImpl( pe_peer );
			
			pe_peer.setData( PEPEER_DATA_KEY, peer );
		}
		
		return( peer );
	
public static org.gudy.azureus2.pluginsimpl.local.peers.PeerManagerImplgetPeerManager(PEPeerManager _manager)


	  
	
			 
	
		try{
			pm_map_mon.enter();
			
			PeerManagerImpl	res = (PeerManagerImpl)_manager.getData( "PluginPeerManager" );
			
			if ( res == null ){
				
				res = new PeerManagerImpl( _manager );
				
				_manager.setData( "PluginPeerManager", res );
			}
			
			return( res );
		}finally{
			
			pm_map_mon.exit();
		}
	
public Peer[]getPeers()

		List	l = manager.getPeers();
		
		Peer[]	res= new Peer[l.size()];
		
			// this is all a bit shagged as we should maintain the PEPeer -> Peer link rather
			// than continually creating new PeerImpls...
		
		for (int i=0;i<res.length;i++){
			
			res[i] = getPeerForPEPeer((PEPeer)l.get(i));
		}
		
		return( res );
	
public Peer[]getPeers(java.lang.String address)

		List	l = manager.getPeers( address );
		
		Peer[]	res= new Peer[l.size()];
		
			// this is all a bit shagged as we should maintain the PEPeer -> Peer link rather
			// than continually creating new PeerImpls...
		
		for (int i=0;i<res.length;i++){
			
			res[i] = getPeerForPEPeer((PEPeer)l.get(i));
		}
		
		return( res );
	
public PeerDescriptor[]getPendingPeers(java.lang.String address)

		return( manager.getPendingPeers( address ));
	
public Piece[]getPieces()

		if ( piece_facades == null ){
			
			pieceFacade[]	pf = new pieceFacade[manager.getDiskManager().getNbPieces()];
			
			for (int i=0;i<pf.length;i++){
				
				pf[i] = new pieceFacade(i);
			}
			
			piece_facades	= pf;
		}
		
		return( piece_facades );
	
public PeerManagerStatsgetStats()

		return(new PeerManagerStatsImpl( manager));
	
public longgetTimeSinceConnectionEstablished(Peer peer)

		if ( peer instanceof PeerImpl ){
			
			return(((PeerImpl)peer).getDelegate().getTimeSinceConnectionEstablished());
		}else{
			PeerForeignDelegate	delegate = lookupForeignPeer( peer );
			
			if ( delegate != null ){
				
				return( delegate.getTimeSinceConnectionEstablished());
				
			}else{
				
				return( 0 );
			}
		}
	
public intgetUploadRateLimitBytesPerSecond()

		return( manager.getUploadRateLimitBytesPerSecond());
	
public booleanisSeeding()

		// this is the wrong thing to check for seeding..
		return( manager.getDiskManager().getRemainingExcludingDND() == 0 ); //yuck
	
public booleanisSuperSeeding()

		return( manager.isSuperSeedMode());
	
protected PeerForeignDelegatelookupForeignPeer(Peer _foreign)

		return((PeerForeignDelegate)_foreign.getUserData( PeerManagerImpl.class ));
	
public PEPeermapForeignPeer(Peer _foreign)

		if ( _foreign instanceof PeerImpl ){
			
			return(((PeerImpl)_foreign).getDelegate());
		}
		
		synchronized( foreign_map ){
			
			PEPeer	local = (PEPeer)foreign_map.get( _foreign );
			
			if( local == null ){
				
				if ( destroyed ){
					
					Debug.out( "Peer added to destroyed peer manager" );
					
					return( null );
				}
				
				local 	= new PeerForeignDelegate( this, _foreign );
				
				_foreign.setUserData( PeerManagerImpl.class, local );
				
				foreign_map.put( _foreign, local );
			}
			
			return( local );
		}
	
public java.util.ListmapForeignPeers(Peer[] _foreigns)

		List	res = new ArrayList();
		
		for (int i=0;i<_foreigns.length;i++){
		
			PEPeer	local = mapForeignPeer( _foreigns[i]);
			
				// could already be there if torrent contains two identical seeds (for whatever reason)
			
			if ( !res.contains( local )){
				
				res.add( local );
			}
		}
		
		return( res );
	
public voidremoveListener(PeerManagerListener l)

		try{
			this_mon.enter();
		
			PEPeerManagerListener core_listener	= (PEPeerManagerListener)listener_map.remove( l );
		
			if ( core_listener != null ){
				manager.removeListener( core_listener );
			}
      
		}finally{
			this_mon.exit();
		}
	
public voidremovePeer(Peer peer)

		manager.removePeer(mapForeignPeer( peer ));
	
protected voidremovePeer(Peer peer, java.lang.String reason)

		manager.removePeer(mapForeignPeer( peer ), reason );
	
public voidrequestCancelled(PeerReadRequest request, Peer sender)

		manager.requestCanceled((DiskManagerReadRequest)request );
	
public voidrequestComplete(PeerReadRequest request, org.gudy.azureus2.plugins.utils.PooledByteBuffer data, Peer sender)

		manager.writeBlock( 
			request.getPieceNumber(), 
			request.getOffset(), 
			((PooledByteBufferImpl)data).getBuffer(), 
			mapForeignPeer( sender ),
            false);
		
		PeerForeignDelegate	delegate = lookupForeignPeer( sender );
		
		if ( delegate != null ){
			
			delegate.dataReceived();
		}