FileDocCategorySizeDatePackage
TRTrackerServerNATChecker.javaAPI DocAzureus 3.0.3.45964Thu Feb 09 19:42:54 GMT 2006org.gudy.azureus2.core3.tracker.server.impl

TRTrackerServerNATChecker

public class TRTrackerServerNATChecker extends Object
author
parg

Fields Summary
private static final LogIDs
LOGID
protected static TRTrackerServerNATChecker
singleton
protected static final int
THREAD_POOL_SIZE
protected static final int
CHECK_QUEUE_LIMIT
protected static int
check_timeout
protected boolean
enabled
protected ThreadPool
thread_pool
protected List
check_queue
protected AESemaphore
check_queue_sem
protected AEMonitor
check_queue_mon
protected AEMonitor
this_mon
Constructors Summary
protected TRTrackerServerNATChecker()


	
	
	
		final String	enable_param 	= "Tracker NAT Check Enable";
		final String	timeout_param	= "Tracker NAT Check Timeout";
		
		final String[]	params = { enable_param, timeout_param };
		
		for (int i=0;i<params.length;i++){
			
			COConfigurationManager.addParameterListener(
				params[i],
				new ParameterListener()
				{
					public void 
					parameterChanged(
						String parameter_name)
					{
						checkConfig( enable_param, timeout_param );
					}
				});
		}
		
		checkConfig( enable_param, timeout_param );
	
Methods Summary
protected booleanaddNATCheckRequest(java.lang.String host, int port, TRTrackerServerNatCheckerListener listener)

		
		if ((!enabled) || thread_pool == null ){
			
			return( false );
		}
		
		try{
			check_queue_mon.enter();
			
			if ( check_queue.size() > CHECK_QUEUE_LIMIT ){
				if (Logger.isEnabled())
					Logger.log(new LogEvent(LOGID, LogEvent.LT_WARNING,
							"NAT Check queue size too large, check for '" + host + ":" + port
									+ "' skipped"));
				//Debug.out( "NAT Check queue size too large, check skipped" );
				
				listener.NATCheckComplete( true );
				
			}else{
				
				check_queue.add(
					new ThreadPoolTask()
					{
						protected	Socket	socket;
						
						public void
						runSupport()
						{
							boolean	ok = false;
														
							try{
								InetSocketAddress address = 
									new InetSocketAddress( AEProxyFactory.getAddressMapper().internalise(host), port );
								
								socket = new Socket();
																
								socket.connect( address, check_timeout );
							
								ok	= true;
								
								socket.close();
								
								socket	= null;
								
							}catch( Throwable e ){
								
							}finally{
																
								listener.NATCheckComplete( ok );
								
								if ( socket != null ){
									
									try{
										socket.close();
										
									}catch( Throwable e ){
									}
								}
							}
						}
						
						public void
						interruptTask()
						{
							if ( socket != null ){
								
								try{
									socket.close();
									
								}catch( Throwable e ){
								}
							}					
						}
					});
				
				check_queue_sem.release();
			}
		}finally{
			
			check_queue_mon.exit();
		}
		
		return( true );
	
protected voidcheckConfig(java.lang.String enable_param, java.lang.String timeout_param)

		try{
			this_mon.enter();
		
			enabled = COConfigurationManager.getBooleanParameter( enable_param );
			
			check_timeout = COConfigurationManager.getIntParameter( timeout_param ) * 1000;
			
			if ( check_timeout < 1000 ){
				
				Debug.out( "NAT check timeout too small - " + check_timeout );
				
				check_timeout	= 1000;
			}
			
			if ( thread_pool == null ){
				
				thread_pool	= new ThreadPool("Tracker NAT Checker", THREAD_POOL_SIZE );
				
				thread_pool.setExecutionLimit( check_timeout );
				
				Thread	dispatcher_thread = 
					new AEThread( "Tracker NAT Checker Dispatcher" )
					{
						public void
						runSupport()
						{
							while(true){
								
								check_queue_sem.reserve();
								
								ThreadPoolTask	task;
								
								try{
									check_queue_mon.enter();
									
									task = (ThreadPoolTask)check_queue.remove(0);
								}finally{
									
									check_queue_mon.exit();
								}
								
								try{
									thread_pool.run( task );
									
								}catch( Throwable e ){
									
									Debug.printStackTrace( e );
								}
							}
						}
					};
					
				dispatcher_thread.setDaemon( true );
				
				dispatcher_thread.start();
				
			}else{
				
				thread_pool.setExecutionLimit( check_timeout );
			}
		}finally{
			
			this_mon.exit();
		}
	
protected static org.gudy.azureus2.core3.tracker.server.impl.TRTrackerServerNATCheckergetSingleton()

	
	  
	
	
		return( singleton );
	
protected booleanisEnabled()

		return( enabled );