gps.protocol.BT
Class BTSession

java.lang.Object
  extended by gps.protocol.BT.BTSession
All Implemented Interfaces:
SimEventHandler

public class BTSession
extends java.lang.Object
implements SimEventHandler

BT Session corresponding to a downloading or uploading session for a document.

Version:
1.2, 6/20/2005
Author:
Weishuai Yang

Field Summary
static int COMPLETED
          session status - completed
static int FAILED
          session status - failed
static int MAX_ALLOWED_CONNECTION_NUM
          max allowed connection num
 boolean mRemoved
          if closed, set up the mClosed flag, and remove from hashmap
 int mStatus
          session status
static int PEER_CANDIDATE_LIST_LENGTH
          peer candidate list length
static int STOPPED
          session status - stopped
static int WORKING
          session status - working
 
Constructor Summary
BTSession(BTTorrent t, BTPeer agent)
          constructs a bt session from torrent
BTSession(java.lang.String key, BTPeer agent)
          constructs a bt session from document hash key
 
Method Summary
 boolean addNodeHasPiece(BTPeer node, int piece)
          update piece information in local database
 void announcePeer(int index)
          let all the connected peers know that this node finishes downloading a piece.
 void announceTracker()
          sends announcement to tracker
 void closeConnection(BTPeer p)
          close connection to peer
 void connectTo(BTPeer p, boolean chokedornot)
          set up a connection to a peer
 int countPeersWithPiece(int i)
          count peers with a specific piece
 BTPeer getAgent()
          gets the Agent object on which this session is running
 BTSocket getConnection(BTPeer p)
          gets connection to a peer
 java.util.LinkedHashMap getConnections()
          gets connections
 BTDocument getDocument()
          gets document that the session is downloading/uploading
 double getDownloadBetweenSnubbingDetection()
          gets download amount between snubbing detection
 java.util.LinkedList getPeerCandidateList()
          gets candidate list
 java.util.LinkedHashSet getUnchokedList()
          gets unchoked list
 boolean handle(SimEvent e)
          process event sent to this session
 void handleConnectionTimeout(BTEvent e)
          handles connection time event, cuts connection
 void handlePeerMessage(BTEvent e)
          handles all the peer message from peers except for hand shaking.
 void handlePMBitField(BTEvent e)
          handles the bitfield type peer message In this simulation, bitfiled message works as the reply of handshaking this is received by the initiator, and the connection is really setup.
 void handlePMCancel(BTEvent e)
          handles the cancel type peer message
 void handlePMChoke(BTEvent e)
          handles the choke type peer message
 void handlePMHave(BTEvent e)
          handles the have type peer message
 void handlePMInterested(BTEvent e)
          handles the interested type peer message
 void handlePMNotInterested(BTEvent e)
          handles the notInterested type peer message
 void handlePMPiece(BTEvent e)
          handles the piece type peer message
 void handlePMRequest(BTEvent e)
          handles the request type peer message
 void handlePMUnChoke(BTEvent e)
          handles the unchoke type peer message
 void handleTrackerResponse(BTEvent e)
          process tracker response evnet
 void queryTracker()
          the entry point of this session, after the session is initionalized, this function is called to start the communication with tracker and peers
 void sendChoked(boolean choked, BTPeer p)
          send choked type peer message
 void sendInterested(boolean interested, BTPeer p)
          send interested type peer message
 void sendRequest(int index, BTPeer p)
          send request for a specific piece to a peer
 void sendRequestTo(BTPeer p)
          send a piece request message to a peer
 void setDownloadBetweenSnubbingDetection(double d)
          sets download amount between snubbing detection
 void setTorrent(BTTorrent t)
          set torrent for existing session
 java.lang.String toString()
          gets string description
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

PEER_CANDIDATE_LIST_LENGTH

public static final int PEER_CANDIDATE_LIST_LENGTH
peer candidate list length

See Also:
Constant Field Values

MAX_ALLOWED_CONNECTION_NUM

public static final int MAX_ALLOWED_CONNECTION_NUM
max allowed connection num

See Also:
Constant Field Values

WORKING

public static final int WORKING
session status - working

See Also:
Constant Field Values

COMPLETED

public static final int COMPLETED
session status - completed

See Also:
Constant Field Values

STOPPED

public static final int STOPPED
session status - stopped

See Also:
Constant Field Values

FAILED

public static final int FAILED
session status - failed

See Also:
Constant Field Values

mStatus

public int mStatus
session status


mRemoved

public boolean mRemoved
if closed, set up the mClosed flag, and remove from hashmap

Constructor Detail

BTSession

public BTSession(BTTorrent t,
                 BTPeer agent)
constructs a bt session from torrent

Parameters:
t - torrent file
agent - peer

BTSession

public BTSession(java.lang.String key,
                 BTPeer agent)
constructs a bt session from document hash key

Parameters:
key - document hash key
agent - peer
Method Detail

getAgent

public BTPeer getAgent()
gets the Agent object on which this session is running

Returns:
agent

setTorrent

public void setTorrent(BTTorrent t)
set torrent for existing session

Parameters:
t - torrent

getDocument

public BTDocument getDocument()
gets document that the session is downloading/uploading

Returns:
document object

getConnections

public java.util.LinkedHashMap getConnections()
gets connections

Returns:
hash map of connections

getConnection

public BTSocket getConnection(BTPeer p)
gets connection to a peer

Parameters:
p - peer as an index
Returns:
connection

getPeerCandidateList

public java.util.LinkedList getPeerCandidateList()
gets candidate list

Returns:
candidate list

getUnchokedList

public java.util.LinkedHashSet getUnchokedList()
gets unchoked list

Returns:
unchoked list

getDownloadBetweenSnubbingDetection

public double getDownloadBetweenSnubbingDetection()
gets download amount between snubbing detection

Returns:
download amount

setDownloadBetweenSnubbingDetection

public void setDownloadBetweenSnubbingDetection(double d)
sets download amount between snubbing detection

Parameters:
d - new download amount

handle

public boolean handle(SimEvent e)
process event sent to this session

Specified by:
handle in interface SimEventHandler
Parameters:
e - the event object with some parameters in it
Returns:
true if already handled

handleTrackerResponse

public void handleTrackerResponse(BTEvent e)
process tracker response evnet

Parameters:
e - the event object

handlePeerMessage

public void handlePeerMessage(BTEvent e)
handles all the peer message from peers except for hand shaking. hand shaking is handled by btpeer.

Parameters:
e - the event object

handleConnectionTimeout

public void handleConnectionTimeout(BTEvent e)
handles connection time event, cuts connection

Parameters:
e - the event object

handlePMBitField

public void handlePMBitField(BTEvent e)
handles the bitfield type peer message In this simulation, bitfiled message works as the reply of handshaking this is received by the initiator, and the connection is really setup.

Parameters:
e - the event object

handlePMRequest

public void handlePMRequest(BTEvent e)
handles the request type peer message

Parameters:
e - the event object

handlePMPiece

public void handlePMPiece(BTEvent e)
handles the piece type peer message

Parameters:
e - the event object

handlePMHave

public void handlePMHave(BTEvent e)
handles the have type peer message

Parameters:
e - the event object

handlePMChoke

public void handlePMChoke(BTEvent e)
handles the choke type peer message

Parameters:
e - the event object

handlePMUnChoke

public void handlePMUnChoke(BTEvent e)
handles the unchoke type peer message

Parameters:
e - the event object

handlePMInterested

public void handlePMInterested(BTEvent e)
handles the interested type peer message

Parameters:
e - the event object

handlePMNotInterested

public void handlePMNotInterested(BTEvent e)
handles the notInterested type peer message

Parameters:
e - the event object

handlePMCancel

public void handlePMCancel(BTEvent e)
handles the cancel type peer message

Parameters:
e - the event object

queryTracker

public void queryTracker()
the entry point of this session, after the session is initionalized, this function is called to start the communication with tracker and peers


announceTracker

public void announceTracker()
sends announcement to tracker


announcePeer

public void announcePeer(int index)
let all the connected peers know that this node finishes downloading a piece.

Parameters:
index - piece index

connectTo

public void connectTo(BTPeer p,
                      boolean chokedornot)
set up a connection to a peer

Parameters:
p - destination peer
chokedornot - initially choked or not

closeConnection

public void closeConnection(BTPeer p)
close connection to peer

Parameters:
p - destination peer

sendRequestTo

public void sendRequestTo(BTPeer p)
send a piece request message to a peer

Parameters:
p - destination peer

sendRequest

public void sendRequest(int index,
                        BTPeer p)
send request for a specific piece to a peer

Parameters:
index - piece index
p - destination peer

sendInterested

public void sendInterested(boolean interested,
                           BTPeer p)
send interested type peer message

Parameters:
interested - interested or uninterested
p - destination peer

sendChoked

public void sendChoked(boolean choked,
                       BTPeer p)
send choked type peer message

Parameters:
choked - choked or unchoked
p - destination peer

addNodeHasPiece

public boolean addNodeHasPiece(BTPeer node,
                               int piece)
update piece information in local database

Parameters:
node - peer
piece - piece index
Returns:
success or not

countPeersWithPiece

public int countPeersWithPiece(int i)
count peers with a specific piece

Parameters:
i - piece index
Returns:
count

toString

public java.lang.String toString()
gets string description

Overrides:
toString in class java.lang.Object
Returns:
a string describing this session