* i2psnark:

- Don't stay interested if we run out of pieces
        to request (thanks sponge)
      - Enhance debug mode to show requests
This commit is contained in:
zzz
2010-10-27 13:29:27 +00:00
parent b9af4a8cf0
commit 983e7683fd
2 changed files with 33 additions and 11 deletions

View File

@ -117,10 +117,15 @@ public class Peer implements Comparable
} }
/** /**
* Returns socket (for debug printing) * @return socket debug string (for debug printing)
*/ */
public String getSocket() public String getSocket()
{ {
if (state != null) {
String r = state.getRequests();
if (r != null)
return sock.toString() + "<br>Requests: " + r;
}
return sock.toString(); return sock.toString();
} }

View File

@ -58,9 +58,6 @@ class PeerState
private final List outstandingRequests = new ArrayList(); private final List outstandingRequests = new ArrayList();
private Request lastRequest = null; private Request lastRequest = null;
// If we have te resend outstanding requests (true after we got choked).
private boolean resend = false;
private final static int MAX_PIPELINE = 5; // this is for outbound requests private final static int MAX_PIPELINE = 5; // this is for outbound requests
private final static int MAX_PIPELINE_BYTES = 128*1024; // this is for inbound requests private final static int MAX_PIPELINE_BYTES = 128*1024; // this is for inbound requests
public final static int PARTSIZE = 16*1024; // outbound request public final static int PARTSIZE = 16*1024; // outbound request
@ -91,14 +88,13 @@ class PeerState
if (_log.shouldLog(Log.DEBUG)) if (_log.shouldLog(Log.DEBUG))
_log.debug(peer + " rcv " + (choke ? "" : "un") + "choked"); _log.debug(peer + " rcv " + (choke ? "" : "un") + "choked");
boolean resend = choked && !choke;
choked = choke; choked = choke;
if (choked)
resend = true;
listener.gotChoke(peer, choke); listener.gotChoke(peer, choke);
if (!choked && interesting) if (interesting && !choked)
request(); request(resend);
} }
void interestedMessage(boolean interest) void interestedMessage(boolean interest)
@ -464,7 +460,7 @@ class PeerState
} }
// Starts or resumes requesting pieces. // Starts or resumes requesting pieces.
private void request() private void request(boolean resend)
{ {
// Are there outstanding requests that have to be resend? // Are there outstanding requests that have to be resend?
if (resend) if (resend)
@ -472,7 +468,6 @@ class PeerState
synchronized (this) { synchronized (this) {
out.sendRequests(outstandingRequests); out.sendRequests(outstandingRequests);
} }
resend = false;
} }
// Add/Send some more requests if necessary. // Add/Send some more requests if necessary.
@ -526,6 +521,7 @@ class PeerState
/** /**
* Starts requesting first chunk of next piece. Returns true if * Starts requesting first chunk of next piece. Returns true if
* something has been added to the requests, false otherwise. * something has been added to the requests, false otherwise.
* Caller should synchronize.
*/ */
private boolean requestNextPiece() private boolean requestNextPiece()
{ {
@ -587,6 +583,15 @@ class PeerState
} }
} }
// 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()
if (interesting && lastRequest == null) {
interesting = false;
out.sendInterest(false);
if (_log.shouldLog(Log.DEBUG))
_log.debug(peer + " nothing more to request, now uninteresting");
}
return false; return false;
} }
@ -601,7 +606,7 @@ class PeerState
out.sendInterest(interest); out.sendInterest(interest);
if (interesting && !choked) if (interesting && !choked)
request(); request(true); // we shouldnt have any pending requests, but if we do, resend them
} }
} }
@ -627,4 +632,16 @@ class PeerState
if (interesting && !choked) if (interesting && !choked)
out.retransmitRequests(outstandingRequests); out.retransmitRequests(outstandingRequests);
} }
/**
* debug
* @return string or null
* @since 0.8.1
*/
synchronized String getRequests() {
if (outstandingRequests.isEmpty())
return null;
else
return outstandingRequests.toString();
}
} }