FileDocCategorySizeDatePackage
IncomingConnectionManager.javaAPI DocAzureus 3.0.3.414610Tue Nov 21 16:13:12 GMT 2006com.aelitis.azureus.core.networkmanager.impl

IncomingConnectionManager

public class IncomingConnectionManager extends Object

Fields Summary
private static final LogIDs
LOGID
private static IncomingConnectionManager
singleton
private volatile Map
match_buffers_cow
private final AEMonitor
match_buffers_mon
private int
max_match_buffer_size
private int
max_min_match_buffer_size
private final ArrayList
connections
private final AEMonitor
connections_mon
Constructors Summary
protected IncomingConnectionManager()


	
	
	
			
		
		SimpleTimer.addPeriodicEvent( 
				"IncomingConnectionManager:timeouts",
				5000,
        new TimerEventPerformer() {
          public void perform( TimerEvent ev ) {
       
          	doTimeoutChecks();
          }
        }
     );
	
Methods Summary
public voidaddConnection(int local_port, TransportHelperFilter filter, com.aelitis.azureus.core.networkmanager.Transport new_transport)

		TransportHelper	transport_helper = filter.getHelper();
		
		if ( isEmpty()) {  //no match registrations, just close
			
			if ( Logger.isEnabled()){
			
		    	Logger.log(new LogEvent(LOGID, "Incoming connection from [" + transport_helper.getAddress() +
		    				 "] dropped because zero routing handlers registered"));
			}
			
			transport_helper.close( "No routing handler" );
		      
		    return;
		}
		 
	   	// note that the filter may have some data internally queued in it after the crypto handshake decode
		// (in particular the BT header). However, there should be some data right behind it that will trigger
		// a read-select below, thus giving prompt access to the queued data

		final IncomingConnection ic = new IncomingConnection( filter, getMaxMatchBufferSize());
				
		TransportHelper.selectListener	sel_listener = new SelectorListener( local_port, new_transport );
		
		try{  
			connections_mon.enter();
		
			connections.add( ic );
		 
			transport_helper.registerForReadSelects( sel_listener, ic );
			
		}finally{
			
			connections_mon.exit();  
		} 
		
			// might be stuff queued up in the filter - force one process cycle (NAT check in particular )
		
		sel_listener.selectSuccess( transport_helper, ic );
	
public voidaddSharedSecrets(byte[][] secrets)

		if ( secrets != null ){
			
			ProtocolDecoder.addSecrets( secrets );
		}
	
public java.lang.Object[]checkForMatch(TransportHelper transport, int incoming_port, java.nio.ByteBuffer to_check, boolean min_match)

 
	       //remember original values for later restore
	      int orig_position = to_check.position();
	      int orig_limit = to_check.limit();
	      
	      //rewind
	      to_check.position( 0 );

	      MatchListener listener 		= null;
	      Object		routing_data 	= null;
	      	    	  
	      for( Iterator i = match_buffers_cow.entrySet().iterator(); i.hasNext(); ) {
	        Map.Entry entry = (Map.Entry)i.next();
	        NetworkManager.ByteMatcher bm = (NetworkManager.ByteMatcher)entry.getKey();
	        MatchListener this_listener = (MatchListener)entry.getValue();
	        
	        int	specific_port = bm.getSpecificPort();
	        
	        if ( specific_port != -1 && specific_port != incoming_port ){
	        	
	        	continue;
	        }
	        
	        if ( min_match ){
	            if( orig_position < bm.minSize() ) {  //not enough bytes yet to compare
	  	          continue;
	  	        }
	  	                
	            routing_data = bm.minMatches( transport, to_check, incoming_port );
	            
	            if ( routing_data != null ){
	  	          listener = this_listener;
	  	          break;
	  	        }      	
	        }else{
		        if( orig_position < bm.matchThisSizeOrBigger() ) {  //not enough bytes yet to compare
		          continue;
		        }
		                
		        routing_data = bm.matches( transport, to_check, incoming_port );
		        
		        if ( routing_data != null ){
		          listener = this_listener;
		          break;
		        }
	        }
	      }

	      //restore original values in case the checks changed them
	      to_check.position( orig_position );
	      to_check.limit( orig_limit );
	      
	      if ( listener == null ){
	    	  
	    	  return( null );
	      }
	      
	      return( new Object[]{ listener, routing_data });
	  
public voidderegisterMatchBytes(NetworkManager.ByteMatcher to_remove)
Remove the given byte sequence match from the registration list.

param
to_remove byte sequence originally used to register

	    try {  match_buffers_mon.enter();
	      Map	new_match_buffers = new HashMap( match_buffers_cow );
	    
	      new_match_buffers.remove( to_remove );
	    
	      if( to_remove.maxSize() == max_match_buffer_size ) { //recalc longest buffer if necessary
	        max_match_buffer_size = 0;
	        for( Iterator i = new_match_buffers.keySet().iterator(); i.hasNext(); ) {
	          NetworkManager.ByteMatcher bm = (NetworkManager.ByteMatcher)i.next();
	          if( bm.maxSize() > max_match_buffer_size ) {
	            max_match_buffer_size = bm.maxSize();
	          }
	        }
	      }
	    
	      match_buffers_cow = new_match_buffers;
	      
	      removeSharedSecrets( to_remove.getSharedSecrets());

	    } finally {  match_buffers_mon.exit();  }  
	
protected voiddoTimeoutChecks()

		try{  connections_mon.enter();

		ArrayList to_close = null;
		
		long now = SystemTime.getCurrentTime();

		for( int i=0; i < connections.size(); i++ ){
			
			IncomingConnection ic = (IncomingConnection)connections.get( i );

			TransportHelper	transport_helper = ic.filter.getHelper();
			
			if( ic.last_read_time > 0 ) {  //at least one read op has occured
				if( now < ic.last_read_time ) {  //time went backwards!
					ic.last_read_time = now;
				}
				else if( now - ic.last_read_time > transport_helper.getReadTimeout()) {  
					if (Logger.isEnabled())
						Logger.log(new LogEvent(LOGID, "Incoming connection ["
								+ transport_helper.getAddress()
								+ "] forcibly timed out due to socket read inactivity ["
								+ ic.buffer.position() + " bytes read: "
								+ new String(ic.buffer.array()) + "]"));
					if( to_close == null )  to_close = new ArrayList();
					to_close.add( ic );
				}
			}
			else { //no bytes have been read yet
				if( now < ic.initial_connect_time ) {  //time went backwards!
					ic.initial_connect_time = now;
				}
				else if( now - ic.initial_connect_time > transport_helper.getConnectTimeout()) {  
					if (Logger.isEnabled())
						Logger.log(new LogEvent(LOGID, "Incoming connection ["
								+ transport_helper.getAddress()	+ "] forcibly timed out after "
								+ "60sec due to socket inactivity"));
					if( to_close == null )  to_close = new ArrayList();
					to_close.add( ic );
				}
			}
		}

		if( to_close != null ) {
			for( int i=0; i < to_close.size(); i++ ) {
				IncomingConnection ic = (IncomingConnection)to_close.get( i );
				removeConnection( ic, true );
			}
		}

		} finally {  connections_mon.exit();  }
	
public intgetMaxMatchBufferSize()

		return( max_match_buffer_size );
	
public intgetMaxMinMatchBufferSize()

		return( max_min_match_buffer_size );
	
public static com.aelitis.azureus.core.networkmanager.impl.IncomingConnectionManagergetSingleton()

	
	  
	
	
		return( singleton );
	
public booleanisEmpty()

		return( match_buffers_cow.isEmpty());
	
public voidregisterMatchBytes(NetworkManager.ByteMatcher matcher, com.aelitis.azureus.core.networkmanager.impl.IncomingConnectionManager$MatchListener listener)
Register the given byte sequence matcher to handle matching against new incoming connection initial data; i.e. the first bytes read from a connection must match in order for the given listener to be invoked.

param
matcher byte filter sequence
param
listener to call upon match

	    try {  match_buffers_mon.enter();
	    
	    	if( matcher.maxSize() > max_match_buffer_size ) {
	    		max_match_buffer_size = matcher.maxSize();
	    	}

	    	if ( matcher.minSize() > max_min_match_buffer_size ){
	    		max_min_match_buffer_size = matcher.minSize();
	    	}
	      
	    	Map	new_match_buffers = new HashMap( match_buffers_cow );
	      
	    	new_match_buffers.put( matcher, listener );
	      
	    	match_buffers_cow = new_match_buffers;
	    
	    	addSharedSecrets( matcher.getSharedSecrets());
	      
	    }finally {  
	    	match_buffers_mon.exit();  
	    }
	
protected voidremoveConnection(com.aelitis.azureus.core.networkmanager.impl.IncomingConnectionManager$IncomingConnection connection, boolean close_as_well)


		try{  
			connections_mon.enter();
		     
			connection.filter.getHelper().cancelReadSelects();
		
			connections.remove( connection );   //remove from connection list
		      
		}finally{
			
			connections_mon.exit();  
		}
		    
		if( close_as_well ) {
		
			connection.filter.getHelper().close( "Tidy close" );
		}
	
public voidremoveSharedSecrets(byte[][] secrets)

		if ( secrets != null ){
			
			ProtocolDecoder.removeSecrets( secrets );
		}