This commit is contained in:
zzz
2010-07-05 14:20:02 +00:00
parent 546a588aa5
commit 378c855902

View File

@ -62,7 +62,7 @@ public class PeerCoordinator implements PeerListener
private long downloaded_old[] = {-1,-1,-1,-1,-1,-1}; private long downloaded_old[] = {-1,-1,-1,-1,-1,-1};
// synchronize on this when changing peers or downloaders // synchronize on this when changing peers or downloaders
final List peers = new ArrayList(); final List<Peer> peers = new ArrayList();
/** estimate of the peers, without requiring any synchronization */ /** estimate of the peers, without requiring any synchronization */
volatile int peerCount; volatile int peerCount;
@ -72,7 +72,7 @@ public class PeerCoordinator implements PeerListener
private final byte[] id; private final byte[] id;
// Some random wanted pieces // Some random wanted pieces
private List wantedPieces; private List<Piece> wantedPieces;
private boolean halted = false; private boolean halted = false;
@ -117,7 +117,7 @@ public class PeerCoordinator implements PeerListener
public CoordinatorListener getListener() { return listener; } public CoordinatorListener getListener() { return listener; }
// for web page detailed stats // for web page detailed stats
public List peerList() public List<Peer> peerList()
{ {
synchronized(peers) synchronized(peers)
{ {
@ -135,8 +135,10 @@ public class PeerCoordinator implements PeerListener
return storage.complete(); return storage.complete();
} }
/** might be wrong */
public int getPeerCount() { return peerCount; } public int getPeerCount() { return peerCount; }
/** should be right */
public int getPeers() public int getPeers()
{ {
synchronized(peers) synchronized(peers)
@ -260,7 +262,7 @@ public class PeerCoordinator implements PeerListener
public void halt() public void halt()
{ {
halted = true; halted = true;
List removed = new ArrayList(); List<Peer> removed = new ArrayList();
synchronized(peers) synchronized(peers)
{ {
// Stop peer checker task. // Stop peer checker task.
@ -273,7 +275,7 @@ public class PeerCoordinator implements PeerListener
} }
while (!removed.isEmpty()) { while (!removed.isEmpty()) {
Peer peer = (Peer)removed.remove(0); Peer peer = removed.remove(0);
peer.disconnect(); peer.disconnect();
removePeerFromPieces(peer); removePeerFromPieces(peer);
} }
@ -340,9 +342,9 @@ public class PeerCoordinator implements PeerListener
// caller must synchronize on peers // caller must synchronize on peers
private static Peer peerIDInList(PeerID pid, List peers) private static Peer peerIDInList(PeerID pid, List peers)
{ {
Iterator it = peers.iterator(); Iterator<Peer> it = peers.iterator();
while (it.hasNext()) { while (it.hasNext()) {
Peer cur = (Peer)it.next(); Peer cur = it.next();
if (pid.sameID(cur.getPeerID())) if (pid.sameID(cur.getPeerID()))
return cur; return cur;
} }
@ -406,15 +408,15 @@ public class PeerCoordinator implements PeerListener
// linked list will contain all interested peers that we choke. // linked list will contain all interested peers that we choke.
// At the start are the peers that have us unchoked at the end the // At the start are the peers that have us unchoked at the end the
// other peer that are interested, but are choking us. // other peer that are interested, but are choking us.
List interested = new LinkedList(); List<Peer> interested = new LinkedList();
synchronized (peers) { synchronized (peers) {
int count = 0; int count = 0;
int unchokedCount = 0; int unchokedCount = 0;
int maxUploaders = allowedUploaders(); int maxUploaders = allowedUploaders();
Iterator it = peers.iterator(); Iterator<Peer> it = peers.iterator();
while (it.hasNext()) while (it.hasNext())
{ {
Peer peer = (Peer)it.next(); Peer peer = it.next();
if (peer.isChoking() && peer.isInterested()) if (peer.isChoking() && peer.isInterested())
{ {
count++; count++;
@ -430,7 +432,7 @@ public class PeerCoordinator implements PeerListener
while (uploaders < maxUploaders && !interested.isEmpty()) while (uploaders < maxUploaders && !interested.isEmpty())
{ {
Peer peer = (Peer)interested.remove(0); Peer peer = interested.remove(0);
if (_log.shouldLog(Log.DEBUG)) if (_log.shouldLog(Log.DEBUG))
_log.debug("Unchoke: " + peer); _log.debug("Unchoke: " + peer);
peer.setChoking(false); peer.setChoking(false);
@ -475,10 +477,10 @@ public class PeerCoordinator implements PeerListener
synchronized(wantedPieces) synchronized(wantedPieces)
{ {
Iterator it = wantedPieces.iterator(); Iterator<Piece> it = wantedPieces.iterator();
while (it.hasNext()) while (it.hasNext())
{ {
Piece p = (Piece)it.next(); Piece p = it.next();
int i = p.getId(); int i = p.getId();
if (bitfield.get(i)) { if (bitfield.get(i)) {
p.addPeer(peer); p.addPeer(peer);
@ -512,11 +514,11 @@ public class PeerCoordinator implements PeerListener
{ {
Piece piece = null; Piece piece = null;
Collections.sort(wantedPieces); // Sort in order of rarest first. Collections.sort(wantedPieces); // Sort in order of rarest first.
List requested = new ArrayList(); List<Piece> requested = new ArrayList();
Iterator it = wantedPieces.iterator(); Iterator<Piece> it = wantedPieces.iterator();
while (piece == null && it.hasNext()) while (piece == null && it.hasNext())
{ {
Piece p = (Piece)it.next(); Piece p = it.next();
if (havePieces.get(p.getId()) && !p.isRequested()) if (havePieces.get(p.getId()) && !p.isRequested())
{ {
piece = p; piece = p;
@ -536,10 +538,10 @@ public class PeerCoordinator implements PeerListener
return -1; // nothing to request and not in end game return -1; // nothing to request and not in end game
// let's not all get on the same piece // let's not all get on the same piece
Collections.shuffle(requested); Collections.shuffle(requested);
Iterator it2 = requested.iterator(); Iterator<Piece> it2 = requested.iterator();
while (piece == null && it2.hasNext()) while (piece == null && it2.hasNext())
{ {
Piece p = (Piece)it2.next(); Piece p = it2.next();
if (havePieces.get(p.getId())) if (havePieces.get(p.getId()))
{ {
piece = p; piece = p;
@ -659,11 +661,11 @@ public class PeerCoordinator implements PeerListener
// Disconnect from other seeders when we get the last piece // Disconnect from other seeders when we get the last piece
synchronized(peers) synchronized(peers)
{ {
List toDisconnect = new ArrayList(); List<Peer> toDisconnect = new ArrayList();
Iterator it = peers.iterator(); Iterator<Peer> it = peers.iterator();
while (it.hasNext()) while (it.hasNext())
{ {
Peer p = (Peer)it.next(); Peer p = it.next();
if (p.isConnected()) if (p.isConnected())
{ {
if (completed() && p.isCompleted()) if (completed() && p.isCompleted())
@ -675,7 +677,7 @@ public class PeerCoordinator implements PeerListener
it = toDisconnect.iterator(); it = toDisconnect.iterator();
while (it.hasNext()) while (it.hasNext())
{ {
Peer p = (Peer)it.next(); Peer p = it.next();
p.disconnect(true); p.disconnect(true);
} }
} }
@ -741,8 +743,8 @@ public class PeerCoordinator implements PeerListener
*/ */
public void removePeerFromPieces(Peer peer) { public void removePeerFromPieces(Peer peer) {
synchronized(wantedPieces) { synchronized(wantedPieces) {
for(Iterator iter = wantedPieces.iterator(); iter.hasNext(); ) { for(Iterator<Piece> iter = wantedPieces.iterator(); iter.hasNext(); ) {
Piece piece = (Piece)iter.next(); Piece piece = iter.next();
piece.removePeer(peer); piece.removePeer(peer);
} }
} }
@ -796,8 +798,8 @@ public class PeerCoordinator implements PeerListener
} }
synchronized(wantedPieces) synchronized(wantedPieces)
{ {
for(Iterator iter = wantedPieces.iterator(); iter.hasNext(); ) { for(Iterator<Piece> iter = wantedPieces.iterator(); iter.hasNext(); ) {
Piece piece = (Piece)iter.next(); Piece piece = iter.next();
if (piece.getId() == savedRequest.piece) { if (piece.getId() == savedRequest.piece) {
Request req = savedRequest; Request req = savedRequest;
piece.setRequested(true); piece.setRequested(true);
@ -822,9 +824,9 @@ public class PeerCoordinator implements PeerListener
// see if anybody else is requesting // see if anybody else is requesting
synchronized (peers) synchronized (peers)
{ {
Iterator it = peers.iterator(); Iterator<Peer> it = peers.iterator();
while (it.hasNext()) { while (it.hasNext()) {
Peer p = (Peer)it.next(); Peer p = it.next();
if (p.equals(peer)) if (p.equals(peer))
continue; continue;
if (p.state == null) if (p.state == null)
@ -842,9 +844,9 @@ public class PeerCoordinator implements PeerListener
// nobody is, so mark unrequested // nobody is, so mark unrequested
synchronized(wantedPieces) synchronized(wantedPieces)
{ {
Iterator it = wantedPieces.iterator(); Iterator<Piece> it = wantedPieces.iterator();
while (it.hasNext()) { while (it.hasNext()) {
Piece p = (Piece)it.next(); Piece p = it.next();
if (p.getId() == piece) { if (p.getId() == piece) {
p.setRequested(false); p.setRequested(false);
if (_log.shouldLog(Log.DEBUG)) if (_log.shouldLog(Log.DEBUG))