jBittorrentAPI
Class PeerUpdater

java.lang.Object
  extended by java.lang.Thread
      extended by jBittorrentAPI.PeerUpdater
All Implemented Interfaces:
java.lang.Runnable

public class PeerUpdater
extends java.lang.Thread

Class providing methods to enable communication between the client and a tracker. Provide method to decode and parse tracker response.


Nested Class Summary
 
Nested classes/interfaces inherited from class java.lang.Thread
java.lang.Thread.State, java.lang.Thread.UncaughtExceptionHandler
 
Field Summary
private  long downloaded
           
private  boolean end
           
private  java.lang.String event
           
private  boolean first
           
private  byte[] id
           
private  int interval
           
private  long left
           
private  javax.swing.event.EventListenerList listeners
           
private  int listeningPort
           
private  int minInterval
           
private  java.util.LinkedHashMap<java.lang.String,Peer> peerList
           
private  TorrentFile torrent
           
private  long uploaded
           
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
PeerUpdater(byte[] id, TorrentFile torrent)
           
 
Method Summary
 void addPeerUpdateListener(PeerUpdateListener listener)
          Adds a PeerUpdateListener to the list of listeners, enabling communication with this object
 java.util.Map contactTracker(byte[] id, TorrentFile t, long dl, long ul, long left, java.lang.String event)
          Contact the tracker according to the HTTP/HTTPS tracker protocol and using the information in the TorrentFile.
 void end()
          Stops the update process.
protected  void fireUpdateFailed(int error, java.lang.String message)
          Sends a message to all listeners with an error code and a String representing the reason why the last try to contact tracker failed
protected  void fireUpdatePeerList(java.util.LinkedHashMap l)
          Sends a message to all listeners with a HashMap containg the list of all peers present in the last tracker response
 long getDownloaded()
          Returns the number of bytes that have been downloaded so far
 java.lang.String getEvent()
          Returns the current event of the client
 int getInterval()
          Returns the last interval for updates received from the tracker
 long getLeft()
          Returns the number of bytes still to download to complete task
 java.util.LinkedHashMap getList()
          Returns the list of peers in its current state
 int getMinInterval()
          Returns the last minimal interval for updates received from the tracker
 PeerUpdateListener[] getPeerUpdateListeners()
          Returns the list of object that are currently listening to this PeerUpdater
 long getUploaded()
          Returns the number of bytes that have been uploaded so far
 java.util.LinkedHashMap<java.lang.String,Peer> processResponse(java.util.Map m)
          Process the map representing the tracker response, which should contain either an error message or the peers list and other information such as the interval before next update, aso
 void removePeerUpdateListener(PeerUpdateListener listener)
          Removes a PeerUpdateListener from the list of listeners
 void run()
          Thread method that regularly contact the tracker and process its response
 void setDownloaded(long dl)
          Sets the # of bytes downloaded so far
 void setEvent(java.lang.String event)
          Sets the current state of the client
 void setInterval(int interval)
          Sets the interval between tracker update
 void setLeft(long left)
          Sets the # of bytes still to download
 void setListeningPort(int port)
           
 void setMinInterval(int minInt)
          Sets the mininterval between tracker update
 void setUploaded(long ul)
          Sets the # of bytes uploaded so far
 void updateParameters(int dl, int ul, java.lang.String event)
          Update the parameters for future tracker communication
 
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, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

downloaded

private long downloaded

end

private boolean end

event

private java.lang.String event

first

private boolean first

id

private byte[] id

interval

private int interval

left

private long left

listeners

private final javax.swing.event.EventListenerList listeners

listeningPort

private int listeningPort

minInterval

private int minInterval

peerList

private java.util.LinkedHashMap<java.lang.String,Peer> peerList

torrent

private TorrentFile torrent

uploaded

private long uploaded
Constructor Detail

PeerUpdater

public PeerUpdater(byte[] id,
                   TorrentFile torrent)
Method Detail

addPeerUpdateListener

public void addPeerUpdateListener(PeerUpdateListener listener)
Adds a PeerUpdateListener to the list of listeners, enabling communication with this object

Parameters:
listener - PeerUpdateListener

contactTracker

public java.util.Map contactTracker(byte[] id,
                                    TorrentFile t,
                                    long dl,
                                    long ul,
                                    long left,
                                    java.lang.String event)
Contact the tracker according to the HTTP/HTTPS tracker protocol and using the information in the TorrentFile.

Parameters:
id - byte[]
t - TorrentFile
dl - long
ul - long
left - long
event - String
Returns:
A Map containing the decoded tracker response

end

public void end()
Stops the update process. This methods sends one last message to the tracker saying this client stops sharing the file and it also exits the run method


fireUpdateFailed

protected void fireUpdateFailed(int error,
                                java.lang.String message)
Sends a message to all listeners with an error code and a String representing the reason why the last try to contact tracker failed

Parameters:
error - int
message - String

fireUpdatePeerList

protected void fireUpdatePeerList(java.util.LinkedHashMap l)
Sends a message to all listeners with a HashMap containg the list of all peers present in the last tracker response

Parameters:
l - LinkedHashMap

getDownloaded

public long getDownloaded()
Returns the number of bytes that have been downloaded so far

Returns:
int

getEvent

public java.lang.String getEvent()
Returns the current event of the client

Returns:
int

getInterval

public int getInterval()
Returns the last interval for updates received from the tracker

Returns:
int

getLeft

public long getLeft()
Returns the number of bytes still to download to complete task

Returns:
int

getList

public java.util.LinkedHashMap getList()
Returns the list of peers in its current state

Returns:
LinkedHashMap

getMinInterval

public int getMinInterval()
Returns the last minimal interval for updates received from the tracker

Returns:
int

getPeerUpdateListeners

public PeerUpdateListener[] getPeerUpdateListeners()
Returns the list of object that are currently listening to this PeerUpdater

Returns:
PeerUpdateListener[]

getUploaded

public long getUploaded()
Returns the number of bytes that have been uploaded so far

Returns:
int

processResponse

public java.util.LinkedHashMap<java.lang.String,Peer> processResponse(java.util.Map m)
Process the map representing the tracker response, which should contain either an error message or the peers list and other information such as the interval before next update, aso

Parameters:
m - The tracker response as a Map
Returns:
LinkedHashMap A HashMap containing the peers and their ID as keys

removePeerUpdateListener

public void removePeerUpdateListener(PeerUpdateListener listener)
Removes a PeerUpdateListener from the list of listeners

Parameters:
listener - PeerUpdateListener

run

public void run()
Thread method that regularly contact the tracker and process its response

Specified by:
run in interface java.lang.Runnable
Overrides:
run in class java.lang.Thread

setDownloaded

public void setDownloaded(long dl)
Sets the # of bytes downloaded so far

Parameters:
dl - long

setEvent

public void setEvent(java.lang.String event)
Sets the current state of the client

Parameters:
event - String

setInterval

public void setInterval(int interval)
Sets the interval between tracker update

Parameters:
interval - int

setLeft

public void setLeft(long left)
Sets the # of bytes still to download

Parameters:
left - long

setListeningPort

public void setListeningPort(int port)

setMinInterval

public void setMinInterval(int minInt)
Sets the mininterval between tracker update

Parameters:
minInt - int

setUploaded

public void setUploaded(long ul)
Sets the # of bytes uploaded so far

Parameters:
ul - long

updateParameters

public void updateParameters(int dl,
                             int ul,
                             java.lang.String event)
Update the parameters for future tracker communication

Parameters:
dl - int
ul - int
event - String