FileDocCategorySizeDatePackage
BTHandshake.javaAPI DocAzureus 3.0.3.47008Tue Sep 04 15:08:38 BST 2007com.aelitis.azureus.core.peermanager.messaging.bittorrent

BTHandshake

public class BTHandshake extends Object implements com.aelitis.azureus.core.networkmanager.RawMessage, BTMessage
BitTorrent handshake message.

Fields Summary
public static final String
PROTOCOL
public static final byte[]
BT_RESERVED
public static final byte[]
AZ_RESERVED
public static final boolean
LTEP_ENABLED
private DirectByteBuffer
buffer
private String
description
private final byte[]
reserved_bytes
private final byte[]
datahash_bytes
private final byte[]
peer_id_bytes
private final byte
version
Constructors Summary
public BTHandshake(byte[] data_hash, byte[] peer_id, boolean set_reserve_bit, byte version)
Used for outgoing handshake message.

param
data_hash
param
peer_id
param
set_reserve_bit

  
  
                
             
    this( set_reserve_bit ? AZ_RESERVED : BT_RESERVED, data_hash, peer_id, version );
  
private BTHandshake(byte[] reserved, byte[] data_hash, byte[] peer_id, byte version)

    this.reserved_bytes = reserved;
    this.datahash_bytes = data_hash;
    this.peer_id_bytes = peer_id;
    this.version = version;
    /*
    for( int i=0; i < reserved.length; i++ ) {  //locate any reserved bits
      for( int x=7; x >= 0; x-- ) {
        byte b = (byte) (reserved[i] >> x);
        int val = b & 0x01;
        if( val == 1 ) {
          String id = new String(peer_id);
          
          if( id.startsWith( "-AZ23" ) )  break;
          if( id.startsWith( "exbc" ) )  break;
          if( id.startsWith( "FUTB" ) )  break;
          
          System.out.println( "BT_HANDSHAKE:: reserved bit @ [" +i+ "/" +(7 - x)+ "] for [" +id+ "]" );
        }
      }
    }
    */
    
  
Methods Summary
private voidconstructBuffer()

    buffer = DirectByteBufferPool.getBuffer( DirectByteBuffer.AL_MSG_BT_HAND, 68 );
    buffer.put( DirectByteBuffer.SS_MSG, (byte)PROTOCOL.length() );
    buffer.put( DirectByteBuffer.SS_MSG, PROTOCOL.getBytes() );
    buffer.put( DirectByteBuffer.SS_MSG, reserved_bytes );
    buffer.put( DirectByteBuffer.SS_MSG, datahash_bytes );
    buffer.put( DirectByteBuffer.SS_MSG, peer_id_bytes );
    buffer.flip( DirectByteBuffer.SS_MSG );
  
public Messagedeserialize(DirectByteBuffer data, byte version)

    
    if( data == null ) {
      throw new MessageException( "[" +getID() + "] decode error: data == null" );
    }
    
    if( data.remaining( DirectByteBuffer.SS_MSG ) != 68 ) {
      throw new MessageException( "[" +getID() + "] decode error: payload.remaining[" +data.remaining( DirectByteBuffer.SS_MSG )+ "] != 68" );
    }
    
    if( data.get( DirectByteBuffer.SS_MSG ) != (byte)PROTOCOL.length() ) {
      throw new MessageException( "[" +getID() + "] decode error: payload.get() != (byte)PROTOCOL.length()" );
    }
    
    byte[] header = new byte[ PROTOCOL.getBytes().length ];
    data.get( DirectByteBuffer.SS_MSG, header );
    
    if( !PROTOCOL.equals( new String( header ) ) ) {
      throw new MessageException( "[" +getID() + "] decode error: invalid protocol given: " + new String( header ) );
    }
    
    byte[] reserved = new byte[ 8 ];
    data.get( DirectByteBuffer.SS_MSG, reserved );          
    
    byte[] infohash = new byte[ 20 ];
    data.get( DirectByteBuffer.SS_MSG, infohash );
    
    byte[] peerid = new byte[ 20 ];
    data.get( DirectByteBuffer.SS_MSG, peerid );
    
    data.returnToPool();
    
    return new BTHandshake( reserved, infohash, peerid, version );
  
public voiddestroy()

    if( buffer != null )  buffer.returnToPool();    
  
public MessagegetBaseMessage()

  return this;  
public DirectByteBuffer[]getData()

 
    if( buffer == null ) {
      constructBuffer();
    }
    
    return new DirectByteBuffer[]{ buffer };
  
public byte[]getDataHash()

  return datahash_bytes;  
public java.lang.StringgetDescription()

    if( description == null ) {
      description = BTMessage.ID_BT_HANDSHAKE + " of dataID: " +ByteFormatter.nicePrint( datahash_bytes, true ) + " peerID: " +PeerClassifier.getPrintablePeerID( peer_id_bytes );
    }
    
    return description; 
  
public java.lang.StringgetFeatureID()

  return BTMessage.BT_FEATURE_ID;  
public intgetFeatureSubID()

  return BTMessage.SUBID_BT_HANDSHAKE;  
public java.lang.StringgetID()

  return BTMessage.ID_BT_HANDSHAKE;  
public byte[]getIDBytes()

  return BTMessage.ID_BT_HANDSHAKE_BYTES;  
public byte[]getPeerId()

  return peer_id_bytes;  
public intgetPriority()

  return RawMessage.PRIORITY_HIGH;  
public DirectByteBuffer[]getRawData()

    if( buffer == null ) {
      constructBuffer();
    }
    
    return new DirectByteBuffer[]{ buffer };
  
public byte[]getReserved()

  return reserved_bytes;  
public intgetType()

  return Message.TYPE_PROTOCOL_PAYLOAD;  
public bytegetVersion()

 return version; 
public booleanisNoDelay()

  return true;  
public Message[]messagesToRemove()

  return null;