FileDocCategorySizeDatePackage
ADVHeaderReader.javaAPI DocAzureus 3.0.3.44560Thu Feb 09 19:42:58 GMT 2006com.aelitis.azureus.core.peermanager.messaging.advanced

ADVHeaderReader

public class ADVHeaderReader extends Object

Fields Summary
private static final byte
SS
private static final int
SHORT_HEADER_SIZE
private static final int
LONG_HEADER_SIZE
private final org.gudy.azureus2.core3.util.DirectByteBuffer
short_header_buff
private final org.gudy.azureus2.core3.util.DirectByteBuffer
long_header_buff
private boolean
reading_handshake
private boolean
reading_short_header
private int
message_length
private int
message_id
private int
message_sub_id
Constructors Summary
protected ADVHeaderReader()

	
	
	  
		/*nothing*/
	
Methods Summary
public voiddestroy()

		short_header_buff.returnToPool();
		long_header_buff.returnToPool();
	
public intgetMessageID()

		return message_id;
	
public intgetMessageLength()

		return message_length;
	
public intgetMessageSubID()

		return message_sub_id;
	
public java.nio.ByteBuffergetReadBuffer()

		if( reading_short_header ) {
			return short_header_buff.getBuffer( SS );
		}
		
		return long_header_buff.getBuffer( SS );
	
public booleanisHeaderReadComplete()

		boolean completed = false;

		if( reading_short_header ) {
			short_header_buff.limit( SS, SHORT_HEADER_SIZE );  //ensure proper limit
			if( short_header_buff.position( SS ) == SHORT_HEADER_SIZE ) {
				completed = true;
			}
		}
		else {
			long_header_buff.limit( SS, LONG_HEADER_SIZE );  //ensure proper limit
			if( long_header_buff.position( SS ) == LONG_HEADER_SIZE ) {
				completed = true;
			}
		}
	
		
		
		if( completed ) {
			if( reading_short_header ) {				
				message_length = short_header_buff.get( SS, 0 );
				
				System.out.println( "NOTE: short message length = " +message_length );
				
				if( message_length == 0xFF ) {  //long part of header still needs to be read
					reading_short_header = false;
					completed = false;
				}
				else {					
					message_id = short_header_buff.get( SS, 1 ) >>> 4;  //use leftmost 4 bits	
					message_sub_id = short_header_buff.get( SS, 1 ) & 0x0F;  //use rightmost 4 bits	
				}
			}
			else if( reading_handshake ) {
				long_header_buff.position( SS, 0 ); //rewind
				message_length = long_header_buff.getInt( SS );				
				
				System.out.println( "NOTE: handshake message length = " +message_length );				
				
				message_id = -1;  //negative to signify stream handshake
				
				reading_handshake = false;
				reading_short_header = true;
			}
			else {  //reading long part of header			
				message_length = (short_header_buff.get( SS, 1 ) << 20) |
												 (long_header_buff.get( SS, 0 )  << 12) |
												 (long_header_buff.get( SS, 1 )  << 4)  |
												 (long_header_buff.get( SS, 2 )  >>> 4);

				System.out.println( "NOTE: long message length = " +message_length );
				
				message_id = (long_header_buff.get( SS, 2 ) << 4) |
				             (long_header_buff.get( SS, 3 ) >> 4 );
				
				message_sub_id = long_header_buff.get( SS, 3 ) & 0x0F;		
				
				reading_short_header = true;
			}
		}

		
		if( completed ) {			
			short_header_buff.position( SS, 0 );  //reset buffer positions for next read round
			long_header_buff.position( SS, 0 );
		}
		
		return completed;