From 643687472aff141c253265e8f50aa872c1b85581 Mon Sep 17 00:00:00 2001 From: zzz <zzz@mail.i2p> Date: Sat, 30 Oct 2010 15:28:29 +0000 Subject: [PATCH] - Only add wanted pieces to wanted list at startup - Make sure lastRequest is null when it should be - Logging tweaks --- .../src/org/klomp/snark/PeerCoordinator.java | 6 +++++- .../java/src/org/klomp/snark/PeerState.java | 19 +++++++++++++------ 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/apps/i2psnark/java/src/org/klomp/snark/PeerCoordinator.java b/apps/i2psnark/java/src/org/klomp/snark/PeerCoordinator.java index 6e7956078d..0fa74e9542 100644 --- a/apps/i2psnark/java/src/org/klomp/snark/PeerCoordinator.java +++ b/apps/i2psnark/java/src/org/klomp/snark/PeerCoordinator.java @@ -105,11 +105,15 @@ public class PeerCoordinator implements PeerListener public void setWantedPieces() { // Make a list of pieces + // FIXME synchronize, clear and re-add instead? + // Don't replace something we are synchronizing on. wantedPieces = new ArrayList(); BitField bitfield = storage.getBitField(); int[] pri = storage.getPiecePriorities(); 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); if (pri != null) p.setPriority(pri[i]); diff --git a/apps/i2psnark/java/src/org/klomp/snark/PeerState.java b/apps/i2psnark/java/src/org/klomp/snark/PeerState.java index cfa1575af4..cadcbf8668 100644 --- a/apps/i2psnark/java/src/org/klomp/snark/PeerState.java +++ b/apps/i2psnark/java/src/org/klomp/snark/PeerState.java @@ -56,6 +56,7 @@ class PeerState // Outstanding request private final List<Request> outstandingRequests = new ArrayList(); + /** the tail (NOT the head) of the request queue */ private Request lastRequest = null; private final static int MAX_PIPELINE = 5; // this is for outbound requests @@ -565,11 +566,10 @@ class PeerState } } int nextPiece = listener.wantPiece(peer, bitfield); - if (_log.shouldLog(Log.DEBUG)) - _log.debug(peer + " want piece " + nextPiece); - if (nextPiece != -1 - && (lastRequest == null || lastRequest.piece != nextPiece)) - { + if (nextPiece != -1 + && (lastRequest == null || lastRequest.piece != nextPiece)) { + if (_log.shouldLog(Log.DEBUG)) + _log.debug(peer + " want piece " + nextPiece); // Fail safe to make sure we are interested // When we transition into the end game we may not be interested... if (!interesting) { @@ -596,9 +596,16 @@ class PeerState out.sendRequest(req); lastRequest = req; 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 // because we are asking other peers. Set not-interesting now rather than // wait for those other requests to be satisfied via havePiece() -- GitLab