FileDocCategorySizeDatePackage
MessageManagerImpl.javaAPI DocAzureus 3.0.3.411542Tue Nov 21 16:13:12 GMT 2006org.gudy.azureus2.pluginsimpl.local.messaging

MessageManagerImpl

public class MessageManagerImpl extends Object implements MessageManager, com.aelitis.azureus.core.nat.NATTraversalHandler

Fields Summary
private static MessageManagerImpl
instance
private final HashMap
compat_checks
private final DownloadManagerListener
download_manager_listener
private com.aelitis.azureus.core.AzureusCore
core
private Map
message_handlers
Constructors Summary
private MessageManagerImpl(com.aelitis.azureus.core.AzureusCore _core)

  
      
  
	  core	= _core;
	  
	  core.getNATTraverser().registerHandler( this );
  
Methods Summary
public voidcancelCompatiblePeersLocation(MessageManagerListener orig_listener)

    for( Iterator it = compat_checks.values().iterator(); it.hasNext(); ) {
      MessageManagerListener listener = (MessageManagerListener)it.next();
      
      if( listener == orig_listener ) {
        it.remove();
        break;
      }
    }
  
public voidderegisterMessageType(Message message)

    com.aelitis.azureus.core.peermanager.messaging.MessageManager.getSingleton().deregisterMessageType( new MessageAdapter( message ) );
  
protected org.gudy.azureus2.plugins.messaging.generic.GenericMessageHandlergetHandler(java.lang.String type)

		synchronized( message_handlers ){

			return((GenericMessageHandler)message_handlers.get( type ));
		}
  
public com.aelitis.azureus.core.nat.NATTraversergetNATTraverser()

	  return( core.getNATTraverser());
  
public java.lang.StringgetName()

		return( "Generic Messaging" );
	
public static synchronized org.gudy.azureus2.pluginsimpl.local.messaging.MessageManagerImplgetSingleton(com.aelitis.azureus.core.AzureusCore core)

  
  
  
  
  
  
      
    
  
	  if ( instance == null ){
		  
		  instance = new MessageManagerImpl( core );
	  }
		  
	  return instance;
  
public intgetType()

		return( NATTraverser.TRAVERSE_REASON_GENERIC_MESSAGING );
	
public voidlocateCompatiblePeers(org.gudy.azureus2.plugins.PluginInterface plug_interface, Message message, MessageManagerListener listener)

    compat_checks.put( message, listener );  //TODO need to copy-on-write?
    
    if( compat_checks.size() == 1 ) {  //only register global peer locator listener once
      plug_interface.getDownloadManager().addListener( download_manager_listener );
    }
  
public java.util.Mapprocess(java.net.InetSocketAddress originator, java.util.Map message)

		return( GenericMessageConnectionIndirect.receive( this, originator, message ));
	
public org.gudy.azureus2.plugins.messaging.generic.GenericMessageRegistrationregisterGenericMessageType(java.lang.String _type, java.lang.String description, int stream_crypto, org.gudy.azureus2.plugins.messaging.generic.GenericMessageHandler handler)

	  
	final String	type 		= "AEGEN:" + _type;
	final byte[]	type_bytes 	= type.getBytes();
	
	final byte[][]	shared_secrets = new byte[][]{ new SHA1Simple().calculateHash( type_bytes ) };
		
	synchronized( message_handlers ){
		
		message_handlers.put( type, handler );
	}
	
	final NetworkManager.ByteMatcher matcher = 
			new NetworkManager.ByteMatcher()
			{
				public int
				matchThisSizeOrBigger()
				{
					return( maxSize());
				}
				
				public int 
				maxSize() 
				{  
					return type_bytes.length;  
				}
				
				public int 
				minSize()
				{ 
					return maxSize(); 
				}
	
				public Object 
				matches( 
					TransportHelper transport, ByteBuffer to_compare, int port ) 
				{             
					int old_limit = to_compare.limit();
					
					to_compare.limit( to_compare.position() + maxSize() );
					
					boolean matches = to_compare.equals( ByteBuffer.wrap( type_bytes ) );
					
					to_compare.limit( old_limit );  //restore buffer structure
					
					return matches?"":null;
				}
				
				public Object 
				minMatches( 
					TransportHelper transport, ByteBuffer to_compare, int port ) 
				{ 
					return( matches( transport, to_compare, port )); 
				} 
				
				public byte[][] 
				getSharedSecrets()
				{ 
					return( shared_secrets ); 
				}
				
			   	public int 
				getSpecificPort()
				{
					return( -1 );
				}
			};
			
	NetworkManager.getSingleton().requestIncomingConnectionRouting(
				matcher,
				new NetworkManager.RoutingListener() 
				{
					public void 
					connectionRouted( 
						final NetworkConnection connection, Object routing_data ) 
					{  	
						try{
							ByteBuffer[]	skip_buffer = { ByteBuffer.allocate(type_bytes.length) };
							
							connection.getTransport().read( skip_buffer, 0, 1 );

							if ( skip_buffer[0].remaining() != 0 ){
								
								Debug.out( "incomplete read" );
							}
							
							GenericMessageEndpointImpl endpoint		= new GenericMessageEndpointImpl( connection.getEndpoint());

							GenericMessageConnectionDirect direct_connection = 
								GenericMessageConnectionDirect.receive( 
										endpoint, 
										type, 
										description,
										stream_crypto,
										shared_secrets );
								
							GenericMessageConnectionImpl new_connection = new GenericMessageConnectionImpl( MessageManagerImpl.this, direct_connection );

							direct_connection.connect( connection );
							
							if ( handler.accept( new_connection )){
								
								new_connection.accepted();

							}else{
								
								connection.close();
							}	
							
						}catch( Throwable e ){
							
							Debug.printStackTrace(e);
							
							connection.close();
						}
					}
					
					public boolean
					autoCryptoFallback()
					{
						return( stream_crypto != MessageManager.STREAM_ENCRYPTION_RC4_REQUIRED );
					}
				},
				new MessageStreamFactory() {
					public MessageStreamEncoder createEncoder() {  return new GenericMessageEncoder();}
					public MessageStreamDecoder createDecoder() {  return new GenericMessageDecoder(type, description);}
				});
		
	return( 
		new GenericMessageRegistration()
		{
			public GenericMessageEndpoint
			createEndpoint(
				InetSocketAddress	notional_target )
			{
				return( new GenericMessageEndpointImpl( notional_target ));
			}
			
			public GenericMessageConnection
			createConnection(
				GenericMessageEndpoint	endpoint )
			
				throws MessageException
			{
				return( new GenericMessageConnectionImpl( MessageManagerImpl.this, type, description, (GenericMessageEndpointImpl)endpoint, stream_crypto, shared_secrets ));
			}
			
			public void
			cancel()
			{
				NetworkManager.getSingleton().cancelIncomingConnectionRouting( matcher );
				
				synchronized( message_handlers ){
					
					message_handlers.remove( type );
				}
			}
		});
  
public voidregisterMessageType(Message message)

    try {
      com.aelitis.azureus.core.peermanager.messaging.MessageManager.getSingleton().registerMessageType( new MessageAdapter( message ) );
    }
    catch( com.aelitis.azureus.core.peermanager.messaging.MessageException me ) {
      throw new MessageException( me.getMessage() );
    }