FileDocCategorySizeDatePackage
NetworkConnectionImpl.javaAPI DocAzureus 3.0.3.410493Tue Jul 17 18:03:12 BST 2007com.aelitis.azureus.core.networkmanager.impl

NetworkConnectionImpl

public class NetworkConnectionImpl extends NetworkConnectionHelper implements NetworkConnection

Fields Summary
private final ConnectionEndpoint
connection_endpoint
private boolean
connect_with_crypto
private boolean
allow_fallback
private byte[]
shared_secrets
private ConnectionListener
connection_listener
private boolean
is_connected
private byte
is_lan_local
private final OutgoingMessageQueue
outgoing_message_queue
private final IncomingMessageQueueImpl
incoming_message_queue
private Transport
transport
private volatile ConnectionAttempt
connection_attempt
private volatile boolean
closed
Constructors Summary
public NetworkConnectionImpl(ConnectionEndpoint _target, com.aelitis.azureus.core.peermanager.messaging.MessageStreamEncoder encoder, com.aelitis.azureus.core.peermanager.messaging.MessageStreamDecoder decoder, boolean _connect_with_crypto, boolean _allow_fallback, byte[] _shared_secrets)
Constructor for new OUTbound connection. The connection is not yet established upon instantiation; use connect() to do so.

param
_remote_address to connect to
param
encoder default message stream encoder to use for the outgoing queue
param
decoder default message stream decoder to use for the incoming queue

  
                                                    
    
		  		    
		  		     
		  		   
  
	connection_endpoint	= _target;
    connect_with_crypto	= _connect_with_crypto;
    allow_fallback = _allow_fallback;
    shared_secrets = _shared_secrets;
    
    
    is_connected = false;
    outgoing_message_queue = new OutgoingMessageQueueImpl( encoder );
    incoming_message_queue = new IncomingMessageQueueImpl( decoder, this );
  
public NetworkConnectionImpl(Transport _transport, com.aelitis.azureus.core.peermanager.messaging.MessageStreamEncoder encoder, com.aelitis.azureus.core.peermanager.messaging.MessageStreamDecoder decoder)
Constructor for new INbound connection. The connection is assumed to be already established, by the given already-connected channel.

param
_remote_channel connected by
param
data_already_read bytestream already read during routing
param
encoder default message stream encoder to use for the outgoing queue
param
decoder default message stream decoder to use for the incoming queue

    transport = _transport;
    connection_endpoint = transport.getTransportEndpoint().getProtocolEndpoint().getConnectionEndpoint();
    is_connected = true;
    outgoing_message_queue = new OutgoingMessageQueueImpl( encoder );
    outgoing_message_queue.setTransport( transport );
    incoming_message_queue = new IncomingMessageQueueImpl( decoder, this );
  
Methods Summary
public voidclose()

  	NetworkManager.getSingleton().stopTransferProcessing( this );   
  	closed	= true;
    if ( connection_attempt != null ){
    	connection_attempt.abandon();
    }
    if ( transport != null ){
    	transport.close( "Tidy close" );
    }
    incoming_message_queue.destroy();
    outgoing_message_queue.destroy();  
    is_connected = false;
  
public voidconnect(ConnectionListener listener)

	  connect( null, listener );
  
public voidconnect(java.nio.ByteBuffer initial_outbound_data, ConnectionListener listener)

    this.connection_listener = listener;
    
    if( is_connected ){
    	
      connection_listener.connectStarted();
      
      connection_listener.connectSuccess( initial_outbound_data );
      
      return;
    }
    
    if ( connection_attempt != null ){
    	
    	Debug.out( "Connection attempt already active" );
    	
    	listener.connectFailure( new Throwable( "Connection attempt already active" ));
    	
    	return;
    }
    
    connection_attempt = 
    	connection_endpoint.connectOutbound( 
    			connect_with_crypto, 
    			allow_fallback, 
    			shared_secrets, 
    			initial_outbound_data,
    			new Transport.ConnectListener() {
			      public void connectAttemptStarted() {
			        connection_listener.connectStarted();
			      }
			      
			      public void connectSuccess( Transport	_transport, ByteBuffer remaining_initial_data ) {
			        is_connected = true;
			        transport	= _transport;
			        outgoing_message_queue.setTransport( transport );
			        connection_listener.connectSuccess( remaining_initial_data );
			        connection_attempt	= null;
			      }
			      
			      public void connectFailure( Throwable failure_msg ) {
			        is_connected = false;
			        connection_listener.connectFailure( failure_msg );
			      }
			    });
    
    if ( closed ){
    	
    	ConnectionAttempt	ca = connection_attempt;
    	
    	if ( ca != null ){
    		
    		ca.abandon();
    	}
    }
  
public TransportdetachTransport()

	  Transport	t = transport;
	  
	  transport = new bogusTransport( transport );
	  
	  close();
	  
	  return( t );
  
public voidenableEnhancedMessageProcessing(boolean enable)

    if( enable ) {
    	NetworkManager.getSingleton().upgradeTransferProcessing( this );
    }
    else {
      NetworkManager.getSingleton().downgradeTransferProcessing( this );
    }
  
public ConnectionEndpointgetEndpoint()

	  return( connection_endpoint );
  
public IncomingMessageQueuegetIncomingMessageQueue()

  return incoming_message_queue;  
public intgetMssSize()

	  if ( transport == null ){
		  
		  return( NetworkManager.getMinMssSize());
		  
	  }else{
		  
		  return( transport.getMssSize());
	  }
  
public OutgoingMessageQueuegetOutgoingMessageQueue()

  return outgoing_message_queue;  
public java.lang.StringgetString()

		return( "tran=" + (transport==null?"null":transport.getDescription()+",w_ready=" + transport.isReadyForWrite(null)+",r_ready=" + transport.isReadyForRead( null ))+ ",in=" + incoming_message_queue.getPercentDoneOfCurrentMessage() + 
				",out=" + outgoing_message_queue.getTotalSize() + ",owner=" + (connection_listener==null?"null":connection_listener.getDescription()));
	
public TransportgetTransport()

  return transport;  
public TransportBasegetTransportBase()

  return transport;  
public booleanisConnected()

		return is_connected;
	
public booleanisLANLocal()

		if ( is_lan_local == AddressUtils.LAN_LOCAL_MAYBE ){
			
			is_lan_local = AddressUtils.isLANLocalAddress( connection_endpoint.getNotionalAddress());
		}
		return( is_lan_local == AddressUtils.LAN_LOCAL_YES );
	
public voidnotifyOfException(java.lang.Throwable error)

    if( connection_listener != null ) {
      connection_listener.exceptionThrown( error );
    }
    else {
      Debug.out( "notifyOfException():: connection_listener == null for exception: " +error.getMessage() );
    }
  
public voidstartMessageProcessing()

  	NetworkManager.getSingleton().startTransferProcessing( this );
  
public java.lang.StringtoString()

    return( transport==null?connection_endpoint.getDescription():transport.getDescription() );