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

VirtualNonBlockingServerChannelSelector

public class VirtualNonBlockingServerChannelSelector 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 List
server_channels
private final InetAddress
bind_address
private int
start_port
private int
num_ports
private final int
receive_buffer_size
private final VirtualBlockingServerChannelSelector.SelectListener
listener
protected AEMonitor
this_mon
private long
last_accept_time
Constructors Summary
public VirtualNonBlockingServerChannelSelector(InetSocketAddress bind_address, int so_rcvbuf_size, VirtualBlockingServerChannelSelector.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.getAddress(), bind_address.getPort(), 1, so_rcvbuf_size, listener );
	
public VirtualNonBlockingServerChannelSelector(InetAddress _bind_address, int _start_port, int _num_ports, int _so_rcvbuf_size, VirtualBlockingServerChannelSelector.SelectListener _listener)

		bind_address		= _bind_address;
		start_port			= _start_port;
		num_ports			= _num_ports;
		receive_buffer_size	= _so_rcvbuf_size;
		listener			= _listener;
	
Methods Summary
public java.net.InetAddressgetBoundToAddress()

	  if ( server_channels.size() == 0 ){
		  return( null);
	  }
	  ServerSocketChannel	server_channel = (ServerSocketChannel)server_channels.get(0);
 	
	  return server_channel.socket().getInetAddress();
  
public longgetTimeOfLastAccept()

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

return
true if enabled, false if not running

	  if ( server_channels.size() == 0 ){
		  return( false);
	  }
	  
	  ServerSocketChannel	server_channel = (ServerSocketChannel)server_channels.get(0);
	  
  	  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() ) {
	    	for (int i=start_port;i<start_port+num_ports;i++){
	      
	    		try {
	    			final ServerSocketChannel	server_channel = ServerSocketChannel.open();
	        
	    			server_channels.add( server_channel );
	    			
	    			server_channel.socket().setReuseAddress( true );
	    			
	    			if( receive_buffer_size > 0 )  server_channel.socket().setReceiveBufferSize( receive_buffer_size );
	        	    			
	    			server_channel.socket().bind( new InetSocketAddress( bind_address, i ), 1024 );
	        
	    			if (Logger.isEnabled()) 	Logger.log(new LogEvent(LOGID, "TCP incoming server socket "	+ bind_address));
	        
	    			server_channel.configureBlocking( false );
	        
			        VirtualAcceptSelector.getSingleton().register( 
			        		server_channel,
			        		new VirtualAcceptSelector.AcceptListener()
			        		{
			        			public void 
			        			newConnectionAccepted( 
			        				SocketChannel channel )
			        			{
			        			    last_accept_time = SystemTime.getCurrentTime();
			        			    
			        				listener.newConnectionAccepted( server_channel, channel );
			        			}
			        		});
	    		}catch( Throwable t ) {
	    			Debug.out( t );
	    			Logger.log(new LogAlert(LogAlert.UNREPEATABLE,	"ERROR, unable to bind TCP incoming server socket to " +i, t));
	    		}
	    	}
	      
	    	last_accept_time = SystemTime.getCurrentTime();  //init to now
	    }
  	}finally{
  		
  		this_mon.exit();
  	} 	
  
public voidstop()
Stop the server.

  	try{
  		this_mon.enter();

	    for (int i=0;i<server_channels.size();i++){
	      try {
	    	  ServerSocketChannel	server_channel = (ServerSocketChannel)server_channels.get(i);
	    	
	    	  VirtualAcceptSelector.getSingleton().cancel( server_channel );
	        
	    	  server_channel.close();
	       
	      }
	      catch( Throwable t ) {  Debug.out( t );  }
	    }
	    
	    server_channels.clear();
  	}finally{
  		
  		this_mon.exit();
  	}