FileDocCategorySizeDatePackage
AZTorrentSession.javaAPI DocAzureus 3.0.3.411573Sun Mar 04 21:08:16 GMT 2007com.aelitis.azureus.core.peermanager.download.session.impl

AZTorrentSession

public class AZTorrentSession extends Object implements TorrentSession

Fields Summary
private static int
next_session_id
private static final AEMonitor
session_mon
private int
local_session_id
private int
remote_session_id
private final com.aelitis.azureus.core.peermanager.download.TorrentDownload
download
private final com.aelitis.azureus.core.peermanager.connection.AZPeerConnection
peer
private final TorrentSessionListener
listener
private TimerEvent
syn_timeout_timer
private final IncomingMessageQueue.MessageQueueListener
incoming_q_listener
private final OutgoingMessageQueue.MessageQueueListener
sent_message_listener
Constructors Summary
public AZTorrentSession(com.aelitis.azureus.core.peermanager.download.TorrentDownload download, com.aelitis.azureus.core.peermanager.connection.AZPeerConnection peer, TorrentSessionListener listener, int remote_id, Map incoming_syn)

  
  
  

               
    this.download = download;
    this.peer = peer;
    this.remote_session_id = remote_id;
    this.listener = listener;
    
    try{ session_mon.enter();
      local_session_id = next_session_id;
      next_session_id++;
    }
    finally{ session_mon.exit();  }
    
    peer.getNetworkConnection().getIncomingMessageQueue().registerQueueListener( incoming_q_listener );
    peer.getNetworkConnection().getOutgoingMessageQueue().registerQueueListener( sent_message_listener );
    
    authenticate( incoming_syn );
  
Methods Summary
private voidauthenticate(java.util.Map incoming_syn)

    if( incoming_syn == null ) {  //outgoing session
      //send out the session request
      AZSessionSyn syn = new AZSessionSyn( download.getInfoHash(), local_session_id, download.getSessionAuthenticator().createSessionSyn( peer ), (byte)1 );
      peer.getNetworkConnection().getOutgoingMessageQueue().addMessage( syn, false );
      
      //set a timeout timer in case the other peer forgets to send an ACK or END reply
      syn_timeout_timer = SimpleTimer.addEvent( "AZTorrentSession:SynTImer", SystemTime.getCurrentTime() + 60*1000, new TimerEventPerformer() {
        public void  perform( TimerEvent  event ) {
          end( "No session ACK received after 60sec, request timed out.", true );
        }
      });
    }
    else {  //incoming session
      try{
        Map ack_reply = download.getSessionAuthenticator().verifySessionSyn( peer, incoming_syn );
        
        //send out the session acceptance
        AZSessionAck ack = new AZSessionAck( download.getInfoHash(), local_session_id, ack_reply, (byte)1 );
        peer.getNetworkConnection().getOutgoingMessageQueue().addMessage( ack, false );
        
        listener.sessionIsEstablished();  //notify of readiness
      }
      catch( AuthenticatorException ae ) {  //on syn authentication error
        end( "AuthenticatorException:: " +ae.getMessage(), true );  //send end message
      }      
    }
  
private voiddestroy()

    if( syn_timeout_timer != null )  syn_timeout_timer.cancel();  //abort timeout check if running
    peer.getNetworkConnection().getIncomingMessageQueue().cancelQueueListener( incoming_q_listener );
    peer.getNetworkConnection().getOutgoingMessageQueue().cancelQueueListener( sent_message_listener );
  
private voidend(java.lang.String end_reason, boolean notify_listener)

    System.out.println( "endSession:: " +end_reason );
    
    //send end notice to remote peer
    AZSessionEnd end = new AZSessionEnd( download.getInfoHash(), end_reason, (byte)1 );
    peer.getNetworkConnection().getOutgoingMessageQueue().addMessage( end, false );
    
    if( notify_listener ) {
      listener.sessionIsEnded( end_reason );  //notify listener of termination
    }
    
    destroy();
  
public voidendSession(java.lang.String reason)

    end( reason, false );
  
public voidsendSessionBitfield(DirectByteBuffer bitfield)

    AZSessionBitfield bitf = new AZSessionBitfield( remote_session_id, bitfield, (byte)1 );
    peer.getNetworkConnection().getOutgoingMessageQueue().addMessage( bitf, false );
  
public voidsendSessionCancel(int piece_number, int piece_offset, int length)

    AZSessionCancel can = new AZSessionCancel( remote_session_id, piece_number, piece_offset, length, (byte)1 );
    peer.getNetworkConnection().getOutgoingMessageQueue().addMessage( can, false );
  
public voidsendSessionHave(int[] piece_numbers)

    AZSessionHave have = new AZSessionHave( remote_session_id, piece_numbers, (byte)1 );
    peer.getNetworkConnection().getOutgoingMessageQueue().addMessage( have, false );
  
public java.lang.ObjectsendSessionPiece(int piece_number, int piece_offset, DirectByteBuffer data)

    try{
      DirectByteBuffer encoded = download.getSessionAuthenticator().encodeSessionData( peer, data );
      AZSessionPiece piece = new AZSessionPiece( remote_session_id, piece_number, piece_offset, encoded, (byte)1 );
      peer.getNetworkConnection().getOutgoingMessageQueue().addMessage( piece, false );
      return piece;
    }
    catch( AuthenticatorException ae ) {
      data.returnToPool();
      end( "AuthenticatorException:: " +ae.getMessage(), true );  //send end message
      return null;
    }
  
public voidsendSessionRequest(byte unchoke_id, int piece_number, int piece_offset, int length)

    AZSessionRequest req = new AZSessionRequest( remote_session_id, unchoke_id, piece_number, piece_offset, length, (byte)1 );
    peer.getNetworkConnection().getOutgoingMessageQueue().addMessage( req, false );