FileDocCategorySizeDatePackage
PeerForeignDelegate.javaAPI DocAzureus 3.0.3.416398Wed Sep 05 14:50:14 BST 2007org.gudy.azureus2.pluginsimpl.local.peers

PeerForeignDelegate

public class PeerForeignDelegate extends Object implements PEPeerTransport
author
parg
author
MjrTom 2005/Oct/08: Add _lastPiece

Fields Summary
protected volatile int
_lastPiece
private PeerManagerImpl
manager
private Peer
foreign
private com.aelitis.azureus.core.networkmanager.NetworkConnectionBase
network_connection
private long
create_time
private long
last_data_received_time
private long
last_data_message_received_time
private int
reserved_piece
private int
consecutive_no_requests
private com.aelitis.azureus.core.peermanager.piecepicker.util.BitFlags
bit_flags
private Map
data
private HashMap
peer_listeners
protected AEMonitor
this_mon
Constructors Summary
protected PeerForeignDelegate(PeerManagerImpl _manager, Peer _foreign)


	
	
				
						 
	
		manager		= _manager;
		foreign		= _foreign;
		
		PEPeerManager pm = manager.getDelegate();
		
		network_connection = new PeerForeignNetworkConnection( foreign );
				
		network_connection.addRateLimiter( pm.getUploadLimitedRateGroup(), true );
		network_connection.addRateLimiter( pm.getDownloadLimitedRateGroup(), false );
	
Methods Summary
public voidaddListener(PEPeerListener l)

		final PEPeer self =this;
		// add a listener to the foreign, then call our listeners when it calls us
		PeerListener2 core_listener = 
			new PeerListener2() 
			{
				public void
				eventOccurred(
					PeerEvent	event )
				{
					Object	data = event.getData();
					
					switch( event.getType() ){
						case PeerEvent.ET_STATE_CHANGED:{
							l.stateChanged(self, ((Integer)data).intValue());
							break;
						}
						case PeerEvent.ET_BAD_CHUNK:{
							Integer[] d = (Integer[])data;
							l.sentBadChunk(self, d[0].intValue(), d[1].intValue() );
							break;
						}
						case PeerEvent.ET_ADD_AVAILABILITY:{
							l.addAvailability(self, new BitFlags((boolean[])data));
							break;
						}
						case PeerEvent.ET_REMOVE_AVAILABILITY:{
							l.removeAvailability(self, new BitFlags((boolean[])data));
							break;
						}
					}
				}	
			};
    
			foreign.addListener( core_listener );
    
		if( peer_listeners == null ){
			
			peer_listeners = new HashMap();
		}
		
		peer_listeners.put( l, core_listener );
		
	
public voidaddRateLimiter(com.aelitis.azureus.core.networkmanager.LimitedRateGroup limiter, boolean upload)

		network_connection.addRateLimiter( limiter, upload );
	
public voidcheckInterested()
Nothing to do if called

public voidclearAvailabilityAdded()
Nothing to do if called

public voidclearRequestHint()

	
public voidcloseConnection(java.lang.String reason)

		try{
			foreign.close( reason, false, false );
			
		}finally{
			
			stop();
		}
	
protected voiddataReceived()

		last_data_received_time	= SystemTime.getCurrentTime();
	
public voiddoKeepAliveCheck()

  	
public voiddoPerformanceTuningCheck()

  	
public booleandoTimeoutChecks()

  		return false;
  	
public booleanequals(java.lang.Object other)

		if ( other instanceof PeerForeignDelegate ){
				
			return( foreign.equals(((PeerForeignDelegate)other).foreign ));
		}
			
		return( false );
	
public voidgenerateEvidence(IndentWriter writer)

		writer.println( "delegate: ip=" + getIp() + ",tcp=" + getTCPListenPort()+",udp="+getUDPListenPort()+",state=" + foreign.getState()+",foreign=" + foreign );
	
public com.aelitis.azureus.core.peermanager.piecepicker.util.BitFlagsgetAvailable()

		boolean[]	flags = foreign.getAvailable();
		
		if ( bit_flags == null || bit_flags.flags != flags ){
			
			bit_flags = new BitFlags( flags );
		}
		
		return( bit_flags );
	
public java.lang.StringgetClient()

		return( foreign.getClient());
	
public java.lang.StringgetClientNameFromExtensionHandshake()

return null;
public java.lang.StringgetClientNameFromPeerID()

return null;
public intgetConnectionState()

		int	peer_state = getPeerState();
	  
		if ( peer_state == Peer.CONNECTING ){
		  
			return( CONNECTION_CONNECTING );
		  
		}else if ( peer_state == Peer.HANDSHAKING ){
		  
			return( CONNECTION_WAITING_FOR_HANDSHAKE );
			  
		}else if ( peer_state == Peer.TRANSFERING ){
		  
			return( CONNECTION_FULLY_ESTABLISHED );
		  
		}else{
		
			return( CONNECTION_FULLY_ESTABLISHED );
		}
	
public intgetConsecutiveNoRequestCount()

  		 return( consecutive_no_requests );
  	 
public PEPeerControlgetControl()

		return((PEPeerControl)manager.getDelegate());
	
public java.lang.ObjectgetData(java.lang.String key)
To retreive arbitrary objects against a peer.

	  	if (data == null) return null;
	    return data.get(key);
	  
public intgetDownloadRateLimitBytesPerSecond()

 return network_connection.getDownloadLimit(); 
public java.lang.StringgetEncryption()

		return( "" );
	
public java.util.ListgetExpiredRequests()

		return( foreign.getExpiredRequests());
	
public byte[]getHandshakeReservedBytes()

		return foreign.getHandshakeReservedBytes();
	
public java.lang.StringgetIPHostName()

		return( foreign.getIp());
	
public byte[]getId()

		return( foreign.getId());
	
public intgetIncomingRequestCount()

		return( 0 );
	
public int[]getIncomingRequestedPieceNumbers()

		return( new int[0] );
	
public java.lang.StringgetIp()

		return( foreign.getIp());
	
public intgetLastPiece()

		return _lastPiece;
	
public PEPeerManagergetManager()

		return( manager.getDelegate());
	
public intgetMaxNbRequests()

		return( foreign.getMaximumNumberOfRequests());
	
public intgetMessagingMode()

		return PEPeer.MESSAGING_EXTERN;
	
public intgetNbRequests()

		return( foreign.getNumberOfRequests());
	
public intgetOutboundDataQueueSize()

			// don't know, assume all requests are queued and block size
		
		return( getOutgoingRequestCount() * DiskManager.BLOCK_SIZE );
	
public intgetOutgoingRequestCount()

		return( foreign.getRequests().size());
	
public int[]getOutgoingRequestedPieceNumbers()

		List	l = foreign.getRequests();
		
		int[]	res = new int[l.size()];
		
		for (int i=0;i<l.size();i++){
			
			res[i] = ((PeerReadRequest)l.get(i)).getPieceNumber();
		}
		
		return( res );
	
public PeerItemgetPeerItemIdentity()

		return PeerItemFactory.createPeerItem( 
				foreign.getIp(), 
				foreign.getTCPListenPort(), 
				PeerItemFactory.PEER_SOURCE_PLUGIN, 
				PeerItemFactory.HANDSHAKE_TYPE_PLAIN,
				foreign.getUDPListenPort(),
				PeerItemFactory.CRYPTO_LEVEL_1,
				0 );
	
public java.lang.StringgetPeerSource()

		return( PEPeerSource.PS_PLUGIN );
	
public intgetPeerState()

		int	peer_state = foreign.getState();
		
		return( peer_state );
	
public intgetPercentDoneInThousandNotation()

		return foreign.getPercentDoneInThousandNotation();
	
public intgetPercentDoneOfCurrentIncomingRequest()

		return( foreign.getPercentDoneOfCurrentIncomingRequest());
	
public intgetPercentDoneOfCurrentOutgoingRequest()

		return( foreign.getPercentDoneOfCurrentOutgoingRequest());	
	
public org.gudy.azureus2.plugins.network.ConnectiongetPluginConnection()

		return( foreign.getConnection());
	
public intgetPort()

		return( foreign.getPort());
	
public int[]getPriorityOffsets()

		return( foreign.getPriorityOffsets());
	
public int[]getRequestHint()

		return null;
	
public intgetRequestIndex(org.gudy.azureus2.core3.disk.DiskManagerReadRequest request)

		return( foreign.getRequests().indexOf( request ));
	
public intgetReservedPieceNumber()

		return( reserved_piece );
	
public longgetSnubbedTime()

		return foreign.getSnubbedTime();
	
public PEPeerStatsgetStats()

		return( ((PeerStatsImpl)foreign.getStats()).getDelegate());
	
public com.aelitis.azureus.core.peermanager.messaging.Message[]getSupportedMessages()

		org.gudy.azureus2.plugins.messaging.Message[] plug_msgs = foreign.getSupportedMessages();
    
		Message[] core_msgs = new Message[ plug_msgs.length ];
    
		for( int i=0; i < plug_msgs.length; i++ ) {
			core_msgs[i] = new MessageAdapter( plug_msgs[i] );
		}
    
		return core_msgs;
	
public intgetTCPListenPort()

  return foreign.getTCPListenPort();  
public longgetTimeSinceConnectionEstablished()

  		long	now = SystemTime.getCurrentTime();
  		
  		if ( now > create_time ){
  			
  			return( now - create_time );
  		}
  		
  		return( 0 );
  	
public longgetTimeSinceGoodDataReceived()

		if (last_data_received_time ==-1)
			return -1;	// never received
		long now =SystemTime.getCurrentTime();
		long time_since =now -last_data_received_time;

		if (time_since <0)
		{	// time went backwards
			last_data_received_time =now;
			time_since =0;
		}

		return time_since;
	
public longgetTimeSinceLastDataMessageReceived()

        if (last_data_message_received_time ==-1)
          return -1;	//never received
        
        final long now =SystemTime.getCurrentTime();
        if (last_data_message_received_time <now)
            last_data_message_received_time =now;   //time went backwards
        return now -last_data_message_received_time;
      
public longgetTimeSinceLastDataMessageSent()

  		return 0;
  	
public intgetUDPListenPort()

  return foreign.getUDPListenPort();  
public intgetUDPNonDataListenPort()

 return( foreign.getUDPNonDataListenPort()); 
public intgetUniqueAnnounce()

	    return -1;
	
public intgetUploadHint()

	    return 0;
	
public intgetUploadRateLimitBytesPerSecond()

 return network_connection.getUploadLimit(); 
public booleanhasReceivedBitField()

		return( true );
	
public inthashCode()

		return( foreign.hashCode());
	
public booleanisAvailabilityAdded()
Apaprently nothing significant to do if called

return false;
public booleanisChokedByMe()

		return( foreign.isChoking());
	
public booleanisChokingMe()

		return( foreign.isChoked());
	
public booleanisDownloadPossible()

    	return foreign.isDownloadPossible();
    
public booleanisIncoming()

		return( foreign.isIncoming());
	
public booleanisInterested()

		return( foreign.isInterested());
	
public booleanisInteresting()

		return( foreign.isInteresting());
	
public booleanisLANLocal()

		return( AddressUtils.isLANLocalAddress( foreign.getIp()) == AddressUtils.LAN_LOCAL_YES );
	
public booleanisOptimisticUnchoke()

		return( foreign.isOptimisticUnchoke());
	
public booleanisPieceAvailable(int pieceNumber)

		return foreign.isPieceAvailable(pieceNumber);
	
public booleanisSeed()

		return( foreign.isSeed());
	
public booleanisSnubbed()

		return( foreign.isSnubbed());
	
public booleanisStalledPendingLoad()

return( false );
public booleanisTCP()

		return( true );
	
public PEPeerTransportreconnect()

 return null; 
public voidremoveListener(PEPeerListener l)

		if ( peer_listeners != null ){
			
			Object core_listener = peer_listeners.remove( l );
    
			if ( core_listener != null ){
				
				if( core_listener instanceof PeerListener ) {
	      
					foreign.removeListener((PeerListener)core_listener );
					
				}else{
					
					foreign.removeListener((PeerListener2)core_listener );

				}
			}
		}
	
public voidremoveRateLimiter(com.aelitis.azureus.core.networkmanager.LimitedRateGroup limiter, boolean upload)

		network_connection.removeRateLimiter( limiter, upload );
	
public org.gudy.azureus2.core3.disk.DiskManagerReadRequestrequest(int pieceNumber, int pieceOffset, int pieceLength)

param
pieceNumber
param
pieceOffset
param
pieceLength
return
true is the piece is really requested

		DiskManagerReadRequest	request = manager.getDelegate().getDiskManager().createReadRequest( pieceNumber, pieceOffset, pieceLength );
		
		if ( foreign.addRequest( request )){
			
			return( request );
			
		}else{
			
			return( null );
		}
	
public voidrequestAllocationComplete()

		foreign.requestAllocationComplete();
	
public booleanrequestAllocationStarts(int[] base_priorities)

		return( foreign.requestAllocationStarts( base_priorities ));
	
public voidsendBadPiece(int piece_number)

	
public voidsendCancel(org.gudy.azureus2.core3.disk.DiskManagerReadRequest request)

		foreign.cancelRequest( request );
	
public voidsendChoke()
Should never be called

public voidsendHave(int piece)
Nothing to do if called

public booleansendRequestHint(int piece_number, int offset, int length, int life)

	
		return( false );
	
public voidsendUnChoke()
Should never be called

public voidsetConsecutiveNoRequestCount(int num)

  		 consecutive_no_requests = num;
  	 
public voidsetData(java.lang.String key, java.lang.Object value)
To store arbitrary objects against a peer.

	  	try{
	  		this_mon.enter();
	  	
	  		if (data == null) {
		  	  data = new HashMap();
		  	}
		    if (value == null) {
		      if (data.containsKey(key))
		        data.remove(key);
		    } else {
		      data.put(key, value);
		    }
	  	}finally{
	  		
	  		this_mon.exit();
	  	}
	  
public voidsetDownloadRateLimitBytesPerSecond(int bytes)

 network_connection.setDownloadLimit( bytes ); 
public voidsetHaveAggregationEnabled(boolean enabled)

	
public voidsetLastPiece(int pieceNumber)

		_lastPiece =pieceNumber;
	
public voidsetOptimisticUnchoke(boolean is_optimistic)

    foreign.setOptimisticUnchoke( is_optimistic );
  
public voidsetReservedPieceNumber(int pieceNumber)

  		reserved_piece	= pieceNumber;
  	
public voidsetSnubbed(boolean b)

		foreign.setSnubbed( b );
	
public voidsetUniqueAnnounce(int uniquePieceNumber)

public voidsetUploadHint(int timeToSpread)

public voidsetUploadRateLimitBytesPerSecond(int bytes)

 network_connection.setUploadLimit( bytes ); 
public voidstart()

		NetworkManager.getSingleton().startTransferProcessing( network_connection );
		
		NetworkManager.getSingleton().upgradeTransferProcessing( network_connection );
	
protected voidstop()

		NetworkManager.getSingleton().stopTransferProcessing( network_connection );
	
public booleansupportsMessaging()

		return foreign.supportsMessaging();
	
public booleantransferAvailable()

    	return( foreign.isTransferAvailable());
    
public voidupdatePeerExchange()