FileDocCategorySizeDatePackage
NATTraverser.javaAPI DocAzureus 3.0.3.45835Wed Aug 09 11:40:00 BST 2006com.aelitis.azureus.core.nat

NATTraverser

public class NATTraverser extends Object implements com.aelitis.azureus.core.dht.nat.DHTNATPuncherAdapter

Fields Summary
public static final int
TRAVERSE_REASON_PEER_DATA
public static final int
TRAVERSE_REASON_GENERIC_MESSAGING
private static final int
MAX_QUEUE_SIZE
private com.aelitis.azureus.core.AzureusCore
core
private com.aelitis.azureus.core.dht.nat.DHTNATPuncher
puncher
private org.gudy.azureus2.core3.util.ThreadPool
thread_pool
private Map
handlers
Constructors Summary
public NATTraverser(com.aelitis.azureus.core.AzureusCore _core)

	
	
	
				 
	
		core	= _core;
	
Methods Summary
public NATTraversalattemptTraversal(NATTraversalHandler handler, java.net.InetSocketAddress target, java.util.Map request, boolean sync, NATTraversalObserver listener)

		final NATTraversal traversal = 
			new NATTraversal()
			{
				private boolean cancelled;
				
				public void 
				cancel() 
				{
					cancelled	= true;
				}
				
				public boolean
				isCancelled()
				{
					return( cancelled );
				}
			};
			
		if ( sync ){
			
			syncTraverse( handler, target, request, listener );
			
		}else{
			
			if ( thread_pool.getQueueSize() >= MAX_QUEUE_SIZE ){
				
				Debug.out( "NATTraversal queue full" );
				
				listener.failed( NATTraversalObserver.FT_QUEUE_FULL );
				
			}else{
				
				thread_pool.run(
					new AERunnable()
					{
						public void 
						runSupport() 
						{
							if ( traversal.isCancelled()){
								
								listener.failed( NATTraversalObserver.FT_CANCELLED );
								
							}else{
								
								syncTraverse( handler, target, request, listener );
							}
						}
					});
			}
		}
		
		return( traversal );
	
public java.util.MapgetClientData(java.net.InetSocketAddress originator, java.util.Map originator_data)

		
		Long	type = (Long)originator_data.get( "_travreas" );
		
		if ( type != null ){
			
			NATTraversalHandler	handler;
			
			synchronized( handlers ){
				
				handler = (NATTraversalHandler)handlers.get( new Integer( type.intValue()));
			}
			
			
			if ( handler != null ){
				
				return( handler.process( originator, originator_data ));
			}
		}
		
		return( null );
	
public voidregisterHandler(NATTraversalHandler handler)

		synchronized( handlers ){
			
			handlers.put( new Integer(handler.getType()), handler );
		}
	
public java.util.MapsendMessage(NATTraversalHandler handler, java.net.InetSocketAddress rendezvous, java.net.InetSocketAddress target, java.util.Map message)

		if ( puncher == null ){
			
			throw( new NATTraversalException( "Puncher unavailable" ));
		}
		
		message.put( "_travreas", new Long( handler.getType()));

		Map	reply = puncher.sendMessage( rendezvous, target, message );
		
		if ( reply == null ){
			
			throw( new NATTraversalException( "Send message failed" ));
		}
		
		return( reply );
	
protected voidsyncTraverse(NATTraversalHandler handler, java.net.InetSocketAddress target, java.util.Map request, NATTraversalObserver listener)

		try{
			int	type = handler.getType();
			
			synchronized( this ){
				
				if ( puncher == null ){
			
					PluginInterface dht_pi = 
						core.getPluginManager().getPluginInterfaceByClass( DHTPlugin.class );
					
					if ( dht_pi != null ){
						
						DHTPlugin dht_plugin = (DHTPlugin)dht_pi.getPlugin();
						
						if ( dht_plugin.isEnabled()){
							
							DHT[]	dhts = dht_plugin.getDHTs();

							if ( dhts.length > 0 ){
								
								puncher = dhts[dhts.length-1].getNATPuncher();
							}
						}
					}
				}
				
				if ( puncher == null ){
					
					listener.disabled();
					
					return;
				}
			}

			if ( request == null ){
				
				request = new HashMap();
			}
			
			request.put( "_travreas", new Long( type ));
			
			InetSocketAddress[]	target_a = { target };
			
			DHTTransportContact[]	rendezvous_used = {null};
			
			Map	reply = puncher.punch( handler.getName(), target_a, rendezvous_used, request );
			
			if ( reply == null ){
				
				if ( rendezvous_used[0] == null ){
					
					listener.failed( NATTraversalObserver.FT_NO_RENDEZVOUS );
					
				}else{
					
					listener.failed( new Exception( "NAT traversal failed" ));
				}
				
			}else{
				
				listener.succeeded( rendezvous_used[0].getAddress(), target_a[0], reply );
			}
		}catch( Throwable e ){
			
			listener.failed( e );
		}