- Only add wanted pieces to wanted list at startup

- Make sure lastRequest is null when it should be
- Logging tweaks
This commit is contained in:
zzz
2010-10-30 15:28:29 +00:00
parent c76058efc3
commit 643687472a
2 changed files with 18 additions and 7 deletions

View File

@ -105,11 +105,15 @@ public class PeerCoordinator implements PeerListener
public void setWantedPieces() public void setWantedPieces()
{ {
// Make a list of pieces // Make a list of pieces
// FIXME synchronize, clear and re-add instead?
// Don't replace something we are synchronizing on.
wantedPieces = new ArrayList(); wantedPieces = new ArrayList();
BitField bitfield = storage.getBitField(); BitField bitfield = storage.getBitField();
int[] pri = storage.getPiecePriorities(); int[] pri = storage.getPiecePriorities();
for(int i = 0; i < metainfo.getPieces(); i++) { for(int i = 0; i < metainfo.getPieces(); i++) {
if (!bitfield.get(i)) { // only add if we don't have and the priority is >= 0
if ((!bitfield.get(i)) &&
(pri == null || pri[i] >= 0)) {
Piece p = new Piece(i); Piece p = new Piece(i);
if (pri != null) if (pri != null)
p.setPriority(pri[i]); p.setPriority(pri[i]);

View File

@ -56,6 +56,7 @@ class PeerState
// Outstanding request // Outstanding request
private final List<Request> outstandingRequests = new ArrayList(); private final List<Request> outstandingRequests = new ArrayList();
/** the tail (NOT the head) of the request queue */
private Request lastRequest = null; private Request lastRequest = null;
private final static int MAX_PIPELINE = 5; // this is for outbound requests private final static int MAX_PIPELINE = 5; // this is for outbound requests
@ -565,11 +566,10 @@ class PeerState
} }
} }
int nextPiece = listener.wantPiece(peer, bitfield); int nextPiece = listener.wantPiece(peer, bitfield);
if (_log.shouldLog(Log.DEBUG)) if (nextPiece != -1
_log.debug(peer + " want piece " + nextPiece); && (lastRequest == null || lastRequest.piece != nextPiece)) {
if (nextPiece != -1 if (_log.shouldLog(Log.DEBUG))
&& (lastRequest == null || lastRequest.piece != nextPiece)) _log.debug(peer + " want piece " + nextPiece);
{
// Fail safe to make sure we are interested // Fail safe to make sure we are interested
// When we transition into the end game we may not be interested... // When we transition into the end game we may not be interested...
if (!interesting) { if (!interesting) {
@ -596,9 +596,16 @@ class PeerState
out.sendRequest(req); out.sendRequest(req);
lastRequest = req; lastRequest = req;
return true; return true;
} } else {
if (_log.shouldLog(Log.DEBUG))
_log.debug(peer + " no more pieces to request");
}
} }
// failsafe
if (outstandingRequests.isEmpty())
lastRequest = null;
// If we are not in the end game, we may run out of things to request // If we are not in the end game, we may run out of things to request
// because we are asking other peers. Set not-interesting now rather than // because we are asking other peers. Set not-interesting now rather than
// wait for those other requests to be satisfied via havePiece() // wait for those other requests to be satisfied via havePiece()