jBittorrentAPI
Class DownloadTask

java.lang.Object
  extended by java.lang.Thread
      extended by jBittorrentAPI.DownloadTask
All Implemented Interfaces:
java.lang.Runnable, java.util.EventListener, IncomingListener, OutgoingListener

public class DownloadTask
extends java.lang.Thread
implements IncomingListener, OutgoingListener

Class representing a task that downloads pieces from a remote peer


Nested Class Summary
 
Nested classes/interfaces inherited from class java.lang.Thread
java.lang.Thread.State, java.lang.Thread.UncaughtExceptionHandler
 
Field Summary
static int BAD_HANDSHAKE
           
 byte[] bitfield
           
static int CONNECTION_REFUSED
           
private  long creationTime
           
private  long downloaded
           
private static int DOWNLOADING
           
private  Piece downloadPiece
           
private  byte[] fileID
           
private static int IDLE
           
private  boolean initiate
           
private  java.io.InputStream is
           
private  boolean isDownloading
           
private  boolean isUploading
           
private  javax.swing.event.EventListenerList listeners
           
private  LogManager lm
           
private  long lmrt
           
static int MALFORMED_MESSAGE
           
 MessageReceiver mr
           
 MessageSender ms
           
private  byte[] myID
           
private  int offset
           
private  java.io.OutputStream os
           
 Peer peer
           
private  java.net.Socket peerConnection
           
private  java.util.LinkedList<java.lang.Integer> pendingRequest
           
private static int READY_2_DL
           
private  boolean run
           
private  int state
           
static int TASK_COMPLETED
           
static int TIMEOUT
           
static int UNKNOWN_HOST
           
private  long updateTime
           
private  long uploaded
           
private static int WAIT_BFORHAVE
           
private static int WAIT_BLOCK
           
private static int WAIT_HS
           
private static int WAIT_UNCHOKE
           
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
DownloadTask(Peer peer, byte[] fileID, byte[] myID, boolean init, byte[] bitfield)
          Start the downloading process from the remote peer in parameter
DownloadTask(Peer peer, byte[] fileID, byte[] myID, boolean init, byte[] bitfield, java.net.Socket s)
          Start the downloading process from the remote peer in parameter
 
Method Summary
 void addDTListener(DTListener listener)
           
private  void changeState(int newState)
          Change the state of the task.
 int checkDownloaded()
          Returns the total amount of bytes downloaded by this task so far
private  void clear()
          Clear the piece currently downloading
 void connectionClosed()
          Fired when the connection to the remote peer has been closed.
 void end()
          Stops this thread by setting the 'run' variable to false and closing the communication thread (Message receiver and sender).
protected  void finalize()
           
private  void fireAddActiveTask(java.lang.String id, DownloadTask dt)
          Fired to inform that this task has completed the handshake and is now ready to communicate with the remote peer
private  void firePeerAvailability(java.lang.String id, java.util.BitSet hasPiece)
          Fired to inform that the availability of this peer has changed
private  void firePeerReady(java.lang.String id)
          Fired to inform that this task is ready to download
private  void firePeerRequest(java.lang.String peerID, int piece, int begin, int length)
          Fired to inform that the peer requests a piece block
private  void firePieceCompleted(int piece, boolean complete)
          Fired to inform that the given piece has been completed or not
private  void firePieceRequested(int piece, boolean requested)
          Fired to inform if the given piece is requested or not...
private  void fireTaskCompleted(java.lang.String id, int reason)
          Fired to inform that the task is finished for a certain reason
 DTListener[] getDTListeners()
           
 Peer getPeer()
          Returns this peer object
private  void initConnection()
          Inits the connection to the remote peer.
 void keepAliveSent()
          Fired when a keep-alive message has been sent by the MessageSender.
 void messageReceived(Message m)
          According to the message type, change the state of the task (peer) and take the necessary actions
 void removeDTListener(DTListener listener)
           
 void requestPiece(Piece p)
          Request a peer to the peer
 void run()
           
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

BAD_HANDSHAKE

public static final int BAD_HANDSHAKE
See Also:
Constant Field Values

bitfield

public byte[] bitfield

CONNECTION_REFUSED

public static final int CONNECTION_REFUSED
See Also:
Constant Field Values

creationTime

private long creationTime

downloaded

private long downloaded

DOWNLOADING

private static final int DOWNLOADING
See Also:
Constant Field Values

downloadPiece

private Piece downloadPiece

fileID

private byte[] fileID

IDLE

private static final int IDLE
See Also:
Constant Field Values

initiate

private final boolean initiate

is

private java.io.InputStream is

isDownloading

private boolean isDownloading

isUploading

private boolean isUploading

listeners

private final javax.swing.event.EventListenerList listeners

lm

private LogManager lm

lmrt

private long lmrt

MALFORMED_MESSAGE

public static final int MALFORMED_MESSAGE
See Also:
Constant Field Values

mr

public MessageReceiver mr

ms

public MessageSender ms

myID

private byte[] myID

offset

private int offset

os

private java.io.OutputStream os

peer

public Peer peer

peerConnection

private java.net.Socket peerConnection

pendingRequest

private java.util.LinkedList<java.lang.Integer> pendingRequest

READY_2_DL

private static final int READY_2_DL
See Also:
Constant Field Values

run

private boolean run

state

private int state

TASK_COMPLETED

public static final int TASK_COMPLETED
See Also:
Constant Field Values

TIMEOUT

public static final int TIMEOUT
See Also:
Constant Field Values

UNKNOWN_HOST

public static final int UNKNOWN_HOST
See Also:
Constant Field Values

updateTime

private long updateTime

uploaded

private long uploaded

WAIT_BFORHAVE

private static final int WAIT_BFORHAVE
See Also:
Constant Field Values

WAIT_BLOCK

private static final int WAIT_BLOCK
See Also:
Constant Field Values

WAIT_HS

private static final int WAIT_HS
See Also:
Constant Field Values

WAIT_UNCHOKE

private static final int WAIT_UNCHOKE
See Also:
Constant Field Values
Constructor Detail

DownloadTask

public DownloadTask(Peer peer,
                    byte[] fileID,
                    byte[] myID,
                    boolean init,
                    byte[] bitfield)
Start the downloading process from the remote peer in parameter

Parameters:
peer - The peer to connect to
fileID - The file to be downloaded
myID - The id of the current client
init - True if this client initiate the connection, false otherwise
bitfield - The pieces currently owned by this client

DownloadTask

public DownloadTask(Peer peer,
                    byte[] fileID,
                    byte[] myID,
                    boolean init,
                    byte[] bitfield,
                    java.net.Socket s)
Start the downloading process from the remote peer in parameter

Parameters:
peer - The peer to connect to
fileID - The file to be downloaded
myID - The id of the current client
init - True if this client initiate the connection, false otherwise
s - Only set if this client receive a connection request from the remote peer
bitfield - The piece currently ownend by this client
Method Detail

addDTListener

public void addDTListener(DTListener listener)

changeState

private void changeState(int newState)
Change the state of the task. State depends on the previously received messages This is here that are taken the most important decisions about the messages to be sent to the remote peer

Parameters:
newState - The new state of the download task

checkDownloaded

public int checkDownloaded()
Returns the total amount of bytes downloaded by this task so far

Returns:
int

clear

private void clear()
Clear the piece currently downloading


connectionClosed

public void connectionClosed()
Fired when the connection to the remote peer has been closed. This method clear this task data and send a message to the DownloadManager, informing it that this peer connection has been closed, resulting in the deletion of this task

Specified by:
connectionClosed in interface OutgoingListener

end

public void end()
Stops this thread by setting the 'run' variable to false and closing the communication thread (Message receiver and sender). Closes the connection to the remote peer


finalize

protected void finalize()
                 throws java.lang.Throwable
Overrides:
finalize in class java.lang.Object
Throws:
java.lang.Throwable

fireAddActiveTask

private void fireAddActiveTask(java.lang.String id,
                               DownloadTask dt)
Fired to inform that this task has completed the handshake and is now ready to communicate with the remote peer

Parameters:
id - String
dt - DownloadTask

firePeerAvailability

private void firePeerAvailability(java.lang.String id,
                                  java.util.BitSet hasPiece)
Fired to inform that the availability of this peer has changed

Parameters:
id - String
hasPiece - BitSet

firePeerReady

private void firePeerReady(java.lang.String id)
Fired to inform that this task is ready to download

Parameters:
id - String

firePeerRequest

private void firePeerRequest(java.lang.String peerID,
                             int piece,
                             int begin,
                             int length)
Fired to inform that the peer requests a piece block

Parameters:
peerID - String
piece - int
begin - int
length - int

firePieceCompleted

private void firePieceCompleted(int piece,
                                boolean complete)
Fired to inform that the given piece has been completed or not

Parameters:
piece - int
complete - boolean

firePieceRequested

private void firePieceRequested(int piece,
                                boolean requested)
Fired to inform if the given piece is requested or not...

Parameters:
piece - int
requested - boolean

fireTaskCompleted

private void fireTaskCompleted(java.lang.String id,
                               int reason)
Fired to inform that the task is finished for a certain reason

Parameters:
id - String
reason - Reason why the task ended

getDTListeners

public DTListener[] getDTListeners()

getPeer

public Peer getPeer()
Returns this peer object

Returns:
Peer

initConnection

private void initConnection()
                     throws java.net.UnknownHostException,
                            java.io.IOException
Inits the connection to the remote peer. Also init the message sender and receiver. If necessary, starts the handshake with the peer

Throws:
java.net.UnknownHostException - If the remote peer is unknown
java.io.IOException - If the connection to the remote peer fails (reset, ...)

keepAliveSent

public void keepAliveSent()
Fired when a keep-alive message has been sent by the MessageSender. If at the time the keep-alive was sent, this peer has not received any message from the remote peer since more that 3 minutes, the remote peer is considered as dead, and we close the connection, then inform the DownloadManager that this connection timed out... Otherwise, inform the DownloadManager that this task is still alive and has not been used for a long time...

Specified by:
keepAliveSent in interface OutgoingListener

messageReceived

public void messageReceived(Message m)
According to the message type, change the state of the task (peer) and take the necessary actions

Specified by:
messageReceived in interface IncomingListener
Parameters:
m - Message

removeDTListener

public void removeDTListener(DTListener listener)

requestPiece

public void requestPiece(Piece p)
Request a peer to the peer

Parameters:
p - The piece to be requested to the peer

run

public void run()
Specified by:
run in interface java.lang.Runnable
Overrides:
run in class java.lang.Thread