FileDocCategorySizeDatePackage
VirtualBlockingServerChannelSelector.javaAPI DocAzureus 3.0.3.44905Sun Jun 18 05:22:42 BST 2006com.aelitis.azureus.core.networkmanager.impl.tcp

VirtualBlockingServerChannelSelector

public class VirtualBlockingServerChannelSelector extends Object implements com.aelitis.azureus.core.networkmanager.VirtualServerChannelSelector
Virtual server socket channel for listening and accepting incoming connections.

Fields Summary
private static final LogIDs
LOGID
private ServerSocketChannel
server_channel
private final InetSocketAddress
bind_address
private final int
receive_buffer_size
private final SelectListener
listener
protected AEMonitor
this_mon
private long
last_accept_time
Constructors Summary
public VirtualBlockingServerChannelSelector(InetSocketAddress _bind_address, int so_rcvbuf_size, SelectListener listener)
Create a new server listening on the given address and reporting to the given listener.

param
bind_address ip+port to listen on
param
so_rcvbuf_size new socket receive buffer size
param
listener to notify of incoming connections

  
  
                                        
           
    this.bind_address = _bind_address;
    this.receive_buffer_size = so_rcvbuf_size;
    this.listener = listener;
  
Methods Summary
protected voidaccept_loop()

    while( isRunning() ) {
      try {
        SocketChannel client_channel = server_channel.accept();
        last_accept_time = SystemTime.getCurrentTime();
        client_channel.configureBlocking( false );
        listener.newConnectionAccepted( server_channel, client_channel );
      }
      catch( AsynchronousCloseException e ) {
        /* is thrown when stop() is called */
      }
      catch( Throwable t ) {
        Debug.out( t );
        try {  Thread.sleep( 500 );  }catch( Exception e ) {  e.printStackTrace();  }
      }
      
    }
  
public java.net.InetAddressgetBoundToAddress()

  	if( server_channel != null ) {
  		return server_channel.socket().getInetAddress();
  	}
  	return null;
  
public longgetTimeOfLastAccept()

  	return last_accept_time;
  
public booleanisRunning()
Is this selector actively running

return
true if enabled, false if not running

  	if( server_channel != null && server_channel.isOpen() )  return true;
  	return false;
  
public voidstart()
Start the server and begin accepting incoming connections.

  	try{
  		this_mon.enter();
  	
	    if( !isRunning() ) {
	      try {
	        server_channel = ServerSocketChannel.open();
	        
	        server_channel.socket().setReuseAddress( true );
	        if( receive_buffer_size > 0 )  server_channel.socket().setReceiveBufferSize( receive_buffer_size );
	        
	        server_channel.socket().bind( bind_address, 1024 );
	        
	        if (Logger.isEnabled()) 	Logger.log(new LogEvent(LOGID, "TCP incoming server socket "	+ bind_address));
	        
	        AEThread accept_thread = new AEThread( "VServerSelector:port" + bind_address.getPort() ) {
	          public void runSupport() {
	            accept_loop();
	          }
	        };
	        accept_thread.setDaemon( true );
	        accept_thread.start();  
	      }
	      catch( Throwable t ) {
	      	Debug.out( t );
	      	Logger.log(new LogAlert(LogAlert.UNREPEATABLE,	"ERROR, unable to bind TCP incoming server socket to " +bind_address.getPort(), t));
	      }
	      
	      last_accept_time = SystemTime.getCurrentTime();  //init to now
	    }
  	}finally{
  		
  		this_mon.exit();
  	} 	
  
public voidstop()
Stop the server.

  	try{
  		this_mon.enter();

	    if( server_channel != null ) {
	      try {
	        server_channel.close();
	        server_channel = null;
	      }
	      catch( Throwable t ) {  Debug.out( t );  }
	    }
  	}finally{
  		
  		this_mon.exit();
  	}