trackerBT
Class TrackerService

java.lang.Object
  extended by simple.http.serve.Component
      extended by simple.http.load.Service
          extended by trackerBT.TrackerService
All Implemented Interfaces:
simple.http.serve.Resource

public class TrackerService
extends simple.http.load.Service

Service called to answer a peer requesting information about peers sharing a given torrent


Field Summary
private static int DONE
           
private static int INTERNAL_ERROR
           
private static int MALFORMED_REQUEST
           
private static java.lang.String[] MESSAGE
           
private static int MISSING_DL
           
private static int MISSING_EVENT
           
private static int MISSING_FILEID
           
private static int MISSING_LEFT
           
private static int MISSING_PEERID
           
private static int MISSING_PORT
           
private static int MISSING_UL
           
(package private)  org.jdom.Document peerList
           
private static int UNKNOWN_TORRENT
           
 
Fields inherited from class simple.http.load.Service
engine
 
Fields inherited from class simple.http.serve.Component
context
 
Constructor Summary
TrackerService(simple.http.serve.Context context)
          Default constructor for the tracker service, requested by the Simple server
 
Method Summary
private  void cleanPeerList(org.jdom.Element root)
          Cleans the peer list.
private  byte[] createAnswer(int err, java.util.List l)
          Encode the answer that will be sent to the peer.
private  boolean isNewTorrent(org.jdom.Element root, java.lang.String hash)
          Search if the torrent requested is registered to this tracker
private  java.util.HashMap parseURI(java.lang.String uri)
          Method called to parse the Request URI and retrieve the parameters in it
private  java.util.List peerSharing(org.jdom.Element root, java.lang.String hash, java.lang.String peerid)
          Returns the list of the peer currently sharing a given torrent
 void process(simple.http.Request req, simple.http.Response resp)
          Method called when a request is received at the tracker address It constructs the response message according to the parameters of the request.
 int processPeerList(java.util.Map param, java.util.List peers)
          Process the peer list according to the given parameters.
private  void registerPeer(org.jdom.Element root, java.lang.String id, java.lang.String ip, java.lang.String port, java.lang.String hash)
          Register or update the remote peer in the local peer database, represented by a XML document
private  boolean torrentExists(java.lang.String id)
          Look if the given torrent is already registered on this tracker
 
Methods inherited from class simple.http.load.Service
prepare
 
Methods inherited from class simple.http.serve.Component
handle, handle, handle, process, process
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DONE

private static final int DONE
See Also:
Constant Field Values

INTERNAL_ERROR

private static final int INTERNAL_ERROR
See Also:
Constant Field Values

MALFORMED_REQUEST

private static final int MALFORMED_REQUEST
See Also:
Constant Field Values

MESSAGE

private static final java.lang.String[] MESSAGE

MISSING_DL

private static final int MISSING_DL
See Also:
Constant Field Values

MISSING_EVENT

private static final int MISSING_EVENT
See Also:
Constant Field Values

MISSING_FILEID

private static final int MISSING_FILEID
See Also:
Constant Field Values

MISSING_LEFT

private static final int MISSING_LEFT
See Also:
Constant Field Values

MISSING_PEERID

private static final int MISSING_PEERID
See Also:
Constant Field Values

MISSING_PORT

private static final int MISSING_PORT
See Also:
Constant Field Values

MISSING_UL

private static final int MISSING_UL
See Also:
Constant Field Values

peerList

org.jdom.Document peerList

UNKNOWN_TORRENT

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

TrackerService

public TrackerService(simple.http.serve.Context context)
Default constructor for the tracker service, requested by the Simple server

Parameters:
context - Context that was provided to the Simple server
Method Detail

cleanPeerList

private void cleanPeerList(org.jdom.Element root)
Cleans the peer list. It removes all peers that haven't been updated from more than 5 minutes. It's necessary since these peers may not be active anymore.

Parameters:
root - The root of the XML document that represents the peers currently sharing a file and announced to this tracker

createAnswer

private byte[] createAnswer(int err,
                            java.util.List l)
Encode the answer that will be sent to the peer.

Parameters:
err - Represent the status of the response. If different from 0, then it means that an error occured and the response will be a message that explains what goes wrong. If equal to zero, then the answer a bencoded dictionary containing the peers sharing the file, and contained in the List l
l - The list of peers sharing the file
Returns:
byte[] The response bytes

isNewTorrent

private boolean isNewTorrent(org.jdom.Element root,
                             java.lang.String hash)
Search if the torrent requested is registered to this tracker

Parameters:
root - Root element of the XML document representing the torrents database
hash - The SHA1 hash of the requested torrent, i.e. the torrent id
Returns:
boolean true if the torrent is not registered, false otherwise

parseURI

private java.util.HashMap parseURI(java.lang.String uri)
                            throws java.io.UnsupportedEncodingException
Method called to parse the Request URI and retrieve the parameters in it

Parameters:
uri - The URI to be parsed
Returns:
A HashMap containing the parameters names (keys) and corresponding values
Throws:
java.io.UnsupportedEncodingException

peerSharing

private java.util.List peerSharing(org.jdom.Element root,
                                   java.lang.String hash,
                                   java.lang.String peerid)
Returns the list of the peer currently sharing a given torrent

Parameters:
root - Root element of the XML document representing the peer database
hash - Id of the requested file
peerid - Id of the peer
Returns:
List List of all peers sharing the file

process

public void process(simple.http.Request req,
                    simple.http.Response resp)
             throws java.io.IOException
Method called when a request is received at the tracker address It constructs the response message according to the parameters of the request.

Specified by:
process in class simple.http.serve.Component
Parameters:
req - Request message received from a peer with its parameters. Parameters must be: -- peer_id: the id of the peer that addresses the request -- info_hash: the SHA1 hash of the torrent the peer is sharing and requesting -- port: the port the peer is listening on -- ip: (optional) the ip address of the peer -- updated: total amount already uploaded by the peer -- downloaded: total amount already downloaded by the peer -- left: number of bytes left to complete download. O means peer is a seed
resp - Response message that will be constructed and returned. The response is a bencoded dictionary containing the list of all peers currently sharing the file given in parameter
Throws:
java.io.IOException

processPeerList

public int processPeerList(java.util.Map param,
                           java.util.List peers)
Process the peer list according to the given parameters. Processing the list includes: - cleaning the too old peers - retrieving peers currently sharing the file - register or update the peer in the XML document

Parameters:
param - Parameters needed to process the peers list. Parameters must be: info_hash, peer_id, port, ip
peers - List where all peers sharing the file will be returned.
Returns:
int Error message representing the status of the response. 0 means no problem occured. Otherwise, the number represents the error that occured.

registerPeer

private void registerPeer(org.jdom.Element root,
                          java.lang.String id,
                          java.lang.String ip,
                          java.lang.String port,
                          java.lang.String hash)
Register or update the remote peer in the local peer database, represented by a XML document

Parameters:
root - The root element of the XML document representing the peer database
id - The id of the peer
ip - The ip address of the peer
port - The listening port of the peer
hash - The file(s) that the peer is sharing

torrentExists

private boolean torrentExists(java.lang.String id)
Look if the given torrent is already registered on this tracker

Parameters:
id - The file id
Returns:
boolean true if the torrent is registered, false otherwise