FileDocCategorySizeDatePackage
ADVMessageFactory.javaAPI DocAzureus 3.0.3.47839Mon Mar 05 11:39:46 GMT 2007com.aelitis.azureus.core.peermanager.messaging.advanced

ADVMessageFactory

public class ADVMessageFactory extends Object
Factory for handling ADV message creation.

Fields Summary
public static final byte
MESSAGE_VERSION_INITIAL
private static final byte
bss
private static final Map
legacy_data
private static Message[]
registered_messages
private static final HashMap
mapping_table
Constructors Summary
Methods Summary
public static MessagecreateADVMessage(int id, int sub_id, DirectByteBuffer stream_payload)
Construct a new ADV message instance from the given message raw byte stream.

param
stream_payload data
return
decoded/deserialized ADV message
throws
MessageException if message creation failed. NOTE: Does not auto-return given direct buffer on thrown exception.

    int id_length = stream_payload.getInt( bss );

    if( id_length < 1 || id_length > 1024 || id_length > stream_payload.remaining( bss ) - 1 ) {
      byte bt_id = stream_payload.get( (byte)0, 0 );
      throw new MessageException( "invalid ADV id length given: " +id_length+ ", stream_payload.remaining(): " +stream_payload.remaining( bss )+ ", BT id?=" +bt_id );
    }
    
    byte[] id_bytes = new byte[ id_length ];
    
    stream_payload.get( bss, id_bytes );
    
    byte version = stream_payload.get( bss );
    
    return MessageManager.getSingleton().createMessage( id_bytes, stream_payload, version );
  
public static com.aelitis.azureus.core.networkmanager.RawMessagecreateADVRawMessage(Message base_message)
Create the proper ADV raw message from the given base message.

param
base_message to create from
return
ADV raw message

    byte[] id_bytes = base_message.getID().getBytes();
    DirectByteBuffer[] payload = base_message.getData();
    
    int payload_size = 0;
    for( int i=0; i < payload.length; i++ ) {
      payload_size += payload[i].remaining( bss );
    }
    
    //create and fill header buffer
    DirectByteBuffer header = DirectByteBufferPool.getBuffer( DirectByteBuffer.AL_MSG_AZ_HEADER, 9 + id_bytes.length );
    header.putInt( bss, 5 + id_bytes.length + payload_size );
    header.putInt( bss, id_bytes.length );
    header.put( bss, id_bytes );
    //header.put( bss, base_message.getVersion() );
    header.flip( bss );
    
    DirectByteBuffer[] raw_buffs = new DirectByteBuffer[ payload.length + 1 ];
    raw_buffs[0] = header;
    for( int i=0; i < payload.length; i++ ) {
      raw_buffs[i+1] = payload[i];
    }
     
    LegacyData ld = (LegacyData)legacy_data.get( base_message.getID() );  //determine if a legacy BT message
    
    if( ld != null ) {  //legacy message, use pre-configured values
      return new RawMessageImpl( base_message, raw_buffs, ld.priority, ld.is_no_delay, ld.to_remove );
    }
    
    //standard message, ensure that protocol messages have wire priority over data payload messages
    int priority = base_message.getType() == Message.TYPE_DATA_PAYLOAD ? RawMessage.PRIORITY_LOW : RawMessage.PRIORITY_NORMAL;
    
    return new RawMessageImpl( base_message, raw_buffs, priority, true, null );
  
public static voidinit()
Initialize the factory, i.e. register the messages with the message manager.

  
  
  
                
      
/*    try {

    }
    catch( MessageException me ) {  me.printStackTrace();  }
*/
  
private static voidrefreshMappingTables()

  	Message[] new_msgs = MessageManager.getSingleton().getRegisteredMessages();
  	
  	if( !Arrays.equals( registered_messages, new_msgs ) ) {  //cached table is out of date
  		registered_messages = new_msgs;
  		mapping_table.clear();
  		
  		for( int i=0; i < registered_messages.length; i++ ) {
  			
  			String feature_id = registered_messages[i].getFeatureID();
  			int sub_id = registered_messages[i].getFeatureSubID();
  			
  			
  			byte[] raw_message_id = (byte[])mapping_table.get( feature_id );
  			
  			if( raw_message_id == null ) {
  				raw_message_id = new byte[3];
  				mapping_table.put( feature_id, raw_message_id );
  			}
  			
  			if( raw_message_id.length != 3 )  Debug.out( "raw_message_id.length[" +raw_message_id.length+ "] != 3" );
  			
  			  			
  			
  			
  			
  			
  		}
  		
  	}
  
private static voidregisterGenericMapPayloadMessageType(java.lang.String type_id)
Register a generic map payload type with the factory.

param
type_id to register
throws
MessageException on registration error

  	MessageManager.getSingleton().registerMessageType( new AZGenericMapPayload( type_id, null, MESSAGE_VERSION_INITIAL ) );