2005-10-19 22:02:37 +00:00
|
|
|
/* PeerState - Keeps track of the Peer state through connection callbacks.
|
|
|
|
Copyright (C) 2003 Mark J. Wielaard
|
|
|
|
|
|
|
|
This file is part of Snark.
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software Foundation,
|
|
|
|
Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package org.klomp.snark;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.List;
|
|
|
|
|
2010-04-12 19:07:53 +00:00
|
|
|
import net.i2p.I2PAppContext;
|
2005-12-19 13:34:52 +00:00
|
|
|
import net.i2p.util.Log;
|
|
|
|
|
2005-10-19 22:02:37 +00:00
|
|
|
class PeerState
|
|
|
|
{
|
2010-04-12 19:07:53 +00:00
|
|
|
private final Log _log = I2PAppContext.getGlobalContext().logManager().getLog(PeerState.class);
|
2005-10-19 22:02:37 +00:00
|
|
|
final Peer peer;
|
|
|
|
final PeerListener listener;
|
|
|
|
final MetaInfo metainfo;
|
|
|
|
|
|
|
|
// Interesting and choking describes whether we are interested in or
|
|
|
|
// are choking the other side.
|
|
|
|
boolean interesting = false;
|
|
|
|
boolean choking = true;
|
|
|
|
|
|
|
|
// Interested and choked describes whether the other side is
|
|
|
|
// interested in us or choked us.
|
|
|
|
boolean interested = false;
|
|
|
|
boolean choked = true;
|
|
|
|
|
|
|
|
// Package local for use by Peer.
|
|
|
|
long downloaded;
|
|
|
|
long uploaded;
|
|
|
|
|
|
|
|
BitField bitfield;
|
|
|
|
|
|
|
|
// Package local for use by Peer.
|
|
|
|
final PeerConnectionIn in;
|
|
|
|
final PeerConnectionOut out;
|
|
|
|
|
|
|
|
// Outstanding request
|
|
|
|
private final List outstandingRequests = new ArrayList();
|
|
|
|
private Request lastRequest = null;
|
|
|
|
|
|
|
|
// If we have te resend outstanding requests (true after we got choked).
|
|
|
|
private boolean resend = false;
|
|
|
|
|
2009-04-22 13:54:59 +00:00
|
|
|
private final static int MAX_PIPELINE = 5; // this is for outbound requests
|
2008-05-18 21:45:54 +00:00
|
|
|
private final static int MAX_PIPELINE_BYTES = 128*1024; // this is for inbound requests
|
2009-04-22 13:54:59 +00:00
|
|
|
public final static int PARTSIZE = 16*1024; // outbound request
|
2007-03-08 18:55:17 +00:00
|
|
|
private final static int MAX_PARTSIZE = 64*1024; // Don't let anybody request more than this
|
2005-10-19 22:02:37 +00:00
|
|
|
|
|
|
|
PeerState(Peer peer, PeerListener listener, MetaInfo metainfo,
|
|
|
|
PeerConnectionIn in, PeerConnectionOut out)
|
|
|
|
{
|
|
|
|
this.peer = peer;
|
|
|
|
this.listener = listener;
|
|
|
|
this.metainfo = metainfo;
|
|
|
|
|
|
|
|
this.in = in;
|
|
|
|
this.out = out;
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE Methods that inspect or change the state synchronize (on this).
|
|
|
|
|
|
|
|
void keepAliveMessage()
|
|
|
|
{
|
2005-12-19 13:34:52 +00:00
|
|
|
if (_log.shouldLog(Log.DEBUG))
|
|
|
|
_log.debug(peer + " rcv alive");
|
2005-10-19 22:02:37 +00:00
|
|
|
/* XXX - ignored */
|
|
|
|
}
|
|
|
|
|
|
|
|
void chokeMessage(boolean choke)
|
|
|
|
{
|
2005-12-19 13:34:52 +00:00
|
|
|
if (_log.shouldLog(Log.DEBUG))
|
|
|
|
_log.debug(peer + " rcv " + (choke ? "" : "un") + "choked");
|
2005-10-19 22:02:37 +00:00
|
|
|
|
|
|
|
choked = choke;
|
|
|
|
if (choked)
|
|
|
|
resend = true;
|
|
|
|
|
|
|
|
listener.gotChoke(peer, choke);
|
|
|
|
|
|
|
|
if (!choked && interesting)
|
|
|
|
request();
|
|
|
|
}
|
|
|
|
|
|
|
|
void interestedMessage(boolean interest)
|
|
|
|
{
|
2005-12-19 13:34:52 +00:00
|
|
|
if (_log.shouldLog(Log.DEBUG))
|
|
|
|
_log.debug(peer + " rcv " + (interest ? "" : "un")
|
|
|
|
+ "interested");
|
2005-10-19 22:02:37 +00:00
|
|
|
interested = interest;
|
|
|
|
listener.gotInterest(peer, interest);
|
|
|
|
}
|
|
|
|
|
|
|
|
void haveMessage(int piece)
|
|
|
|
{
|
2005-12-19 13:34:52 +00:00
|
|
|
if (_log.shouldLog(Log.DEBUG))
|
|
|
|
_log.debug(peer + " rcv have(" + piece + ")");
|
2005-10-19 22:02:37 +00:00
|
|
|
// Sanity check
|
|
|
|
if (piece < 0 || piece >= metainfo.getPieces())
|
|
|
|
{
|
|
|
|
// XXX disconnect?
|
2005-12-19 13:34:52 +00:00
|
|
|
if (_log.shouldLog(Log.WARN))
|
|
|
|
_log.warn("Got strange 'have: " + piece + "' message from " + peer);
|
2005-10-19 22:02:37 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
synchronized(this)
|
|
|
|
{
|
|
|
|
// Can happen if the other side never send a bitfield message.
|
|
|
|
if (bitfield == null)
|
|
|
|
bitfield = new BitField(metainfo.getPieces());
|
|
|
|
|
|
|
|
bitfield.set(piece);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (listener.gotHave(peer, piece))
|
|
|
|
setInteresting(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void bitfieldMessage(byte[] bitmap)
|
|
|
|
{
|
|
|
|
synchronized(this)
|
|
|
|
{
|
2005-12-19 13:34:52 +00:00
|
|
|
if (_log.shouldLog(Log.DEBUG))
|
|
|
|
_log.debug(peer + " rcv bitfield");
|
2005-10-19 22:02:37 +00:00
|
|
|
if (bitfield != null)
|
|
|
|
{
|
|
|
|
// XXX - Be liberal in what you except?
|
2005-12-19 13:34:52 +00:00
|
|
|
if (_log.shouldLog(Log.WARN))
|
|
|
|
_log.warn("Got unexpected bitfield message from " + peer);
|
2005-10-19 22:02:37 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXX - Check for weird bitfield and disconnect?
|
|
|
|
bitfield = new BitField(bitmap, metainfo.getPieces());
|
|
|
|
}
|
2010-04-10 15:26:23 +00:00
|
|
|
boolean interest = listener.gotBitField(peer, bitfield);
|
|
|
|
setInteresting(interest);
|
|
|
|
if (bitfield.complete() && !interest) {
|
|
|
|
// They are seeding and we are seeding,
|
|
|
|
// why did they contact us? (robert)
|
|
|
|
// Dump them quick before we send our whole bitmap
|
|
|
|
if (_log.shouldLog(Log.WARN))
|
2010-04-12 19:07:53 +00:00
|
|
|
_log.warn("Disconnecting seed that connects to seeds: " + peer);
|
2010-04-10 15:26:23 +00:00
|
|
|
peer.disconnect(true);
|
|
|
|
}
|
2005-10-19 22:02:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void requestMessage(int piece, int begin, int length)
|
|
|
|
{
|
2005-12-19 13:34:52 +00:00
|
|
|
if (_log.shouldLog(Log.DEBUG))
|
|
|
|
_log.debug(peer + " rcv request("
|
|
|
|
+ piece + ", " + begin + ", " + length + ") ");
|
2005-10-19 22:02:37 +00:00
|
|
|
if (choking)
|
|
|
|
{
|
2005-12-19 13:34:52 +00:00
|
|
|
if (_log.shouldLog(Log.INFO))
|
|
|
|
_log.info("Request received, but choking " + peer);
|
2005-10-19 22:02:37 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sanity check
|
|
|
|
if (piece < 0
|
|
|
|
|| piece >= metainfo.getPieces()
|
|
|
|
|| begin < 0
|
|
|
|
|| begin > metainfo.getPieceLength(piece)
|
|
|
|
|| length <= 0
|
2007-03-08 18:55:17 +00:00
|
|
|
|| length > MAX_PARTSIZE)
|
2005-10-19 22:02:37 +00:00
|
|
|
{
|
|
|
|
// XXX - Protocol error -> disconnect?
|
2005-12-19 13:34:52 +00:00
|
|
|
if (_log.shouldLog(Log.WARN))
|
|
|
|
_log.warn("Got strange 'request: " + piece
|
2005-10-19 22:02:37 +00:00
|
|
|
+ ", " + begin
|
|
|
|
+ ", " + length
|
2005-12-19 13:34:52 +00:00
|
|
|
+ "' message from " + peer);
|
2005-10-19 22:02:37 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-05-18 21:45:54 +00:00
|
|
|
// Limit total pipelined requests to MAX_PIPELINE bytes
|
|
|
|
// to conserve memory and prevent DOS
|
2010-04-12 19:07:53 +00:00
|
|
|
// Todo: limit number of requests also? (robert 64 x 4KB)
|
2008-05-18 21:45:54 +00:00
|
|
|
if (out.queuedBytes() + length > MAX_PIPELINE_BYTES)
|
|
|
|
{
|
|
|
|
if (_log.shouldLog(Log.WARN))
|
|
|
|
_log.warn("Discarding request over pipeline limit from " + peer);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-01-21 01:43:31 +00:00
|
|
|
byte[] pieceBytes = listener.gotRequest(peer, piece, begin, length);
|
2005-10-19 22:02:37 +00:00
|
|
|
if (pieceBytes == null)
|
|
|
|
{
|
|
|
|
// XXX - Protocol error-> diconnect?
|
2005-12-19 13:34:52 +00:00
|
|
|
if (_log.shouldLog(Log.WARN))
|
|
|
|
_log.warn("Got request for unknown piece: " + piece);
|
2005-10-19 22:02:37 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// More sanity checks
|
2007-01-21 01:43:31 +00:00
|
|
|
if (length != pieceBytes.length)
|
2005-10-19 22:02:37 +00:00
|
|
|
{
|
|
|
|
// XXX - Protocol error-> disconnect?
|
2005-12-19 13:34:52 +00:00
|
|
|
if (_log.shouldLog(Log.WARN))
|
|
|
|
_log.warn("Got out of range 'request: " + piece
|
2005-10-19 22:02:37 +00:00
|
|
|
+ ", " + begin
|
|
|
|
+ ", " + length
|
2005-12-19 13:34:52 +00:00
|
|
|
+ "' message from " + peer);
|
2005-10-19 22:02:37 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2005-12-19 13:34:52 +00:00
|
|
|
if (_log.shouldLog(Log.INFO))
|
|
|
|
_log.info("Sending (" + piece + ", " + begin + ", "
|
|
|
|
+ length + ")" + " to " + peer);
|
2005-10-19 22:02:37 +00:00
|
|
|
out.sendPiece(piece, begin, length, pieceBytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when some bytes have left the outgoing connection.
|
|
|
|
* XXX - Should indicate whether it was a real piece or overhead.
|
|
|
|
*/
|
|
|
|
void uploaded(int size)
|
|
|
|
{
|
|
|
|
uploaded += size;
|
|
|
|
listener.uploaded(peer, size);
|
|
|
|
}
|
|
|
|
|
2006-09-06 06:32:53 +00:00
|
|
|
// This is used to flag that we have to back up from the firstOutstandingRequest
|
|
|
|
// when calculating how far we've gotten
|
|
|
|
Request pendingRequest = null;
|
|
|
|
|
2005-10-19 22:02:37 +00:00
|
|
|
/**
|
|
|
|
* Called when a partial piece request has been handled by
|
|
|
|
* PeerConnectionIn.
|
|
|
|
*/
|
|
|
|
void pieceMessage(Request req)
|
|
|
|
{
|
|
|
|
int size = req.len;
|
|
|
|
downloaded += size;
|
|
|
|
listener.downloaded(peer, size);
|
|
|
|
|
2006-09-06 06:32:53 +00:00
|
|
|
pendingRequest = null;
|
|
|
|
|
2005-10-19 22:02:37 +00:00
|
|
|
// Last chunk needed for this piece?
|
|
|
|
if (getFirstOutstandingRequest(req.piece) == -1)
|
|
|
|
{
|
|
|
|
if (listener.gotPiece(peer, req.piece, req.bs))
|
|
|
|
{
|
2005-12-19 13:34:52 +00:00
|
|
|
if (_log.shouldLog(Log.DEBUG))
|
|
|
|
_log.debug("Got " + req.piece + ": " + peer);
|
2005-10-19 22:02:37 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2005-12-19 13:34:52 +00:00
|
|
|
if (_log.shouldLog(Log.WARN))
|
|
|
|
_log.warn("Got BAD " + req.piece + " from " + peer);
|
2005-10-19 22:02:37 +00:00
|
|
|
// XXX ARGH What now !?!
|
|
|
|
downloaded = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
synchronized private int getFirstOutstandingRequest(int piece)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < outstandingRequests.size(); i++)
|
|
|
|
if (((Request)outstandingRequests.get(i)).piece == piece)
|
|
|
|
return i;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when a piece message is being processed by the incoming
|
|
|
|
* connection. Returns null when there was no such request. It also
|
|
|
|
* requeues/sends requests when it thinks that they must have been
|
|
|
|
* lost.
|
|
|
|
*/
|
|
|
|
Request getOutstandingRequest(int piece, int begin, int length)
|
|
|
|
{
|
2005-12-19 13:34:52 +00:00
|
|
|
if (_log.shouldLog(Log.DEBUG))
|
|
|
|
_log.debug("getChunk("
|
2005-10-19 22:02:37 +00:00
|
|
|
+ piece + "," + begin + "," + length + ") "
|
2005-12-19 13:34:52 +00:00
|
|
|
+ peer);
|
2005-10-19 22:02:37 +00:00
|
|
|
|
|
|
|
int r = getFirstOutstandingRequest(piece);
|
|
|
|
|
|
|
|
// Unrequested piece number?
|
|
|
|
if (r == -1)
|
|
|
|
{
|
2005-12-19 13:34:52 +00:00
|
|
|
if (_log.shouldLog(Log.INFO))
|
|
|
|
_log.info("Unrequested 'piece: " + piece + ", "
|
2005-10-19 22:02:37 +00:00
|
|
|
+ begin + ", " + length + "' received from "
|
2005-12-19 13:34:52 +00:00
|
|
|
+ peer);
|
2005-10-19 22:02:37 +00:00
|
|
|
downloaded = 0; // XXX - punishment?
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lookup the correct piece chunk request from the list.
|
|
|
|
Request req;
|
|
|
|
synchronized(this)
|
|
|
|
{
|
|
|
|
req = (Request)outstandingRequests.get(r);
|
|
|
|
while (req.piece == piece && req.off != begin
|
|
|
|
&& r < outstandingRequests.size() - 1)
|
|
|
|
{
|
|
|
|
r++;
|
|
|
|
req = (Request)outstandingRequests.get(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Something wrong?
|
|
|
|
if (req.piece != piece || req.off != begin || req.len != length)
|
|
|
|
{
|
2005-12-19 13:34:52 +00:00
|
|
|
if (_log.shouldLog(Log.INFO))
|
|
|
|
_log.info("Unrequested or unneeded 'piece: "
|
2005-10-19 22:02:37 +00:00
|
|
|
+ piece + ", "
|
|
|
|
+ begin + ", "
|
|
|
|
+ length + "' received from "
|
2005-12-19 13:34:52 +00:00
|
|
|
+ peer);
|
2005-10-19 22:02:37 +00:00
|
|
|
downloaded = 0; // XXX - punishment?
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Report missing requests.
|
|
|
|
if (r != 0)
|
|
|
|
{
|
2005-12-19 13:34:52 +00:00
|
|
|
if (_log.shouldLog(Log.WARN))
|
|
|
|
_log.warn("Some requests dropped, got " + req
|
|
|
|
+ ", wanted for peer: " + peer);
|
2005-10-19 22:02:37 +00:00
|
|
|
for (int i = 0; i < r; i++)
|
|
|
|
{
|
|
|
|
Request dropReq = (Request)outstandingRequests.remove(0);
|
|
|
|
outstandingRequests.add(dropReq);
|
2006-09-06 06:32:53 +00:00
|
|
|
if (!choked)
|
2005-10-19 22:02:37 +00:00
|
|
|
out.sendRequest(dropReq);
|
2005-12-19 13:34:52 +00:00
|
|
|
if (_log.shouldLog(Log.WARN))
|
|
|
|
_log.warn("dropped " + dropReq + " with peer " + peer);
|
2005-10-19 22:02:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
outstandingRequests.remove(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Request more if necessary to keep the pipeline filled.
|
|
|
|
addRequest();
|
|
|
|
|
2006-09-06 06:32:53 +00:00
|
|
|
pendingRequest = req;
|
2005-10-19 22:02:37 +00:00
|
|
|
return req;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2006-09-06 06:32:53 +00:00
|
|
|
// get longest partial piece
|
|
|
|
Request getPartialRequest()
|
|
|
|
{
|
|
|
|
Request req = null;
|
|
|
|
for (int i = 0; i < outstandingRequests.size(); i++) {
|
|
|
|
Request r1 = (Request)outstandingRequests.get(i);
|
|
|
|
int j = getFirstOutstandingRequest(r1.piece);
|
|
|
|
if (j == -1)
|
|
|
|
continue;
|
|
|
|
Request r2 = (Request)outstandingRequests.get(j);
|
|
|
|
if (r2.off > 0 && ((req == null) || (r2.off > req.off)))
|
|
|
|
req = r2;
|
|
|
|
}
|
|
|
|
if (pendingRequest != null && req != null && pendingRequest.off < req.off) {
|
|
|
|
if (pendingRequest.off != 0)
|
|
|
|
req = pendingRequest;
|
|
|
|
else
|
|
|
|
req = null;
|
|
|
|
}
|
|
|
|
return req;
|
|
|
|
}
|
|
|
|
|
2006-09-13 23:02:07 +00:00
|
|
|
// return array of pieces terminated by -1
|
|
|
|
// remove most duplicates
|
|
|
|
// but still could be some duplicates, not guaranteed
|
|
|
|
int[] getRequestedPieces()
|
|
|
|
{
|
|
|
|
int size = outstandingRequests.size();
|
|
|
|
int[] arr = new int[size+2];
|
|
|
|
int pc = -1;
|
|
|
|
int pos = 0;
|
|
|
|
if (pendingRequest != null) {
|
|
|
|
pc = pendingRequest.piece;
|
|
|
|
arr[pos++] = pc;
|
|
|
|
}
|
|
|
|
Request req = null;
|
|
|
|
for (int i = 0; i < size; i++) {
|
|
|
|
Request r1 = (Request)outstandingRequests.get(i);
|
|
|
|
if (pc != r1.piece) {
|
|
|
|
pc = r1.piece;
|
|
|
|
arr[pos++] = pc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
arr[pos] = -1;
|
|
|
|
return(arr);
|
|
|
|
}
|
|
|
|
|
2005-10-19 22:02:37 +00:00
|
|
|
void cancelMessage(int piece, int begin, int length)
|
|
|
|
{
|
2005-12-19 13:34:52 +00:00
|
|
|
if (_log.shouldLog(Log.DEBUG))
|
|
|
|
_log.debug("Got cancel message ("
|
|
|
|
+ piece + ", " + begin + ", " + length + ")");
|
2005-10-19 22:02:37 +00:00
|
|
|
out.cancelRequest(piece, begin, length);
|
|
|
|
}
|
|
|
|
|
|
|
|
void unknownMessage(int type, byte[] bs)
|
|
|
|
{
|
2005-12-19 13:34:52 +00:00
|
|
|
if (_log.shouldLog(Log.WARN))
|
|
|
|
_log.warn("Warning: Ignoring unknown message type: " + type
|
|
|
|
+ " length: " + bs.length);
|
2005-10-19 22:02:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void havePiece(int piece)
|
|
|
|
{
|
2005-12-19 13:34:52 +00:00
|
|
|
if (_log.shouldLog(Log.DEBUG))
|
|
|
|
_log.debug("Tell " + peer + " havePiece(" + piece + ")");
|
2005-10-19 22:02:37 +00:00
|
|
|
|
|
|
|
synchronized(this)
|
|
|
|
{
|
|
|
|
// Tell the other side that we are no longer interested in any of
|
|
|
|
// the outstanding requests for this piece.
|
|
|
|
if (lastRequest != null && lastRequest.piece == piece)
|
|
|
|
lastRequest = null;
|
|
|
|
|
|
|
|
Iterator it = outstandingRequests.iterator();
|
|
|
|
while (it.hasNext())
|
|
|
|
{
|
|
|
|
Request req = (Request)it.next();
|
|
|
|
if (req.piece == piece)
|
|
|
|
{
|
|
|
|
it.remove();
|
|
|
|
// Send cancel even when we are choked to make sure that it is
|
|
|
|
// really never ever send.
|
|
|
|
out.sendCancel(req);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tell the other side that we really have this piece.
|
|
|
|
out.sendHave(piece);
|
|
|
|
|
|
|
|
// Request something else if necessary.
|
|
|
|
addRequest();
|
|
|
|
|
|
|
|
synchronized(this)
|
|
|
|
{
|
|
|
|
// Is the peer still interesting?
|
|
|
|
if (lastRequest == null)
|
|
|
|
setInteresting(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Starts or resumes requesting pieces.
|
|
|
|
private void request()
|
|
|
|
{
|
|
|
|
// Are there outstanding requests that have to be resend?
|
|
|
|
if (resend)
|
|
|
|
{
|
2005-12-30 20:57:53 +00:00
|
|
|
synchronized (this) {
|
|
|
|
out.sendRequests(outstandingRequests);
|
|
|
|
}
|
2005-10-19 22:02:37 +00:00
|
|
|
resend = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add/Send some more requests if necessary.
|
|
|
|
addRequest();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a new request to the outstanding requests list.
|
|
|
|
*/
|
2006-09-29 23:54:17 +00:00
|
|
|
synchronized private void addRequest()
|
2005-10-19 22:02:37 +00:00
|
|
|
{
|
|
|
|
boolean more_pieces = true;
|
|
|
|
while (more_pieces)
|
|
|
|
{
|
2006-09-29 23:54:17 +00:00
|
|
|
more_pieces = outstandingRequests.size() < MAX_PIPELINE;
|
2005-10-19 22:02:37 +00:00
|
|
|
// We want something and we don't have outstanding requests?
|
|
|
|
if (more_pieces && lastRequest == null)
|
|
|
|
more_pieces = requestNextPiece();
|
|
|
|
else if (more_pieces) // We want something
|
|
|
|
{
|
|
|
|
int pieceLength;
|
|
|
|
boolean isLastChunk;
|
2006-09-29 23:54:17 +00:00
|
|
|
pieceLength = metainfo.getPieceLength(lastRequest.piece);
|
|
|
|
isLastChunk = lastRequest.off + lastRequest.len == pieceLength;
|
2005-10-19 22:02:37 +00:00
|
|
|
|
|
|
|
// Last part of a piece?
|
|
|
|
if (isLastChunk)
|
|
|
|
more_pieces = requestNextPiece();
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int nextPiece = lastRequest.piece;
|
|
|
|
int nextBegin = lastRequest.off + PARTSIZE;
|
|
|
|
byte[] bs = lastRequest.bs;
|
|
|
|
int maxLength = pieceLength - nextBegin;
|
|
|
|
int nextLength = maxLength > PARTSIZE ? PARTSIZE
|
|
|
|
: maxLength;
|
|
|
|
Request req
|
|
|
|
= new Request(nextPiece, bs, nextBegin, nextLength);
|
|
|
|
outstandingRequests.add(req);
|
|
|
|
if (!choked)
|
|
|
|
out.sendRequest(req);
|
|
|
|
lastRequest = req;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-12-19 13:34:52 +00:00
|
|
|
if (_log.shouldLog(Log.DEBUG))
|
|
|
|
_log.debug(peer + " requests " + outstandingRequests);
|
2005-10-19 22:02:37 +00:00
|
|
|
}
|
|
|
|
|
2010-07-05 14:20:34 +00:00
|
|
|
/**
|
|
|
|
* Starts requesting first chunk of next piece. Returns true if
|
|
|
|
* something has been added to the requests, false otherwise.
|
|
|
|
*/
|
2005-10-19 22:02:37 +00:00
|
|
|
private boolean requestNextPiece()
|
|
|
|
{
|
|
|
|
// Check that we already know what the other side has.
|
|
|
|
if (bitfield != null)
|
|
|
|
{
|
2006-09-06 06:32:53 +00:00
|
|
|
// Check for adopting an orphaned partial piece
|
|
|
|
Request r = listener.getPeerPartial(bitfield);
|
|
|
|
if (r != null) {
|
2006-09-24 18:30:22 +00:00
|
|
|
// Check that r not already in outstandingRequests
|
|
|
|
int[] arr = getRequestedPieces();
|
|
|
|
boolean found = false;
|
|
|
|
for (int i = 0; arr[i] >= 0; i++) {
|
|
|
|
if (arr[i] == r.piece) {
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!found) {
|
|
|
|
outstandingRequests.add(r);
|
|
|
|
if (!choked)
|
|
|
|
out.sendRequest(r);
|
|
|
|
lastRequest = r;
|
|
|
|
return true;
|
|
|
|
}
|
2006-09-06 06:32:53 +00:00
|
|
|
}
|
2005-10-19 22:02:37 +00:00
|
|
|
int nextPiece = listener.wantPiece(peer, bitfield);
|
2005-12-19 13:34:52 +00:00
|
|
|
if (_log.shouldLog(Log.DEBUG))
|
|
|
|
_log.debug(peer + " want piece " + nextPiece);
|
2005-10-19 22:02:37 +00:00
|
|
|
if (nextPiece != -1
|
|
|
|
&& (lastRequest == null || lastRequest.piece != nextPiece))
|
|
|
|
{
|
2010-07-05 14:20:34 +00:00
|
|
|
// Fail safe to make sure we are interested
|
|
|
|
// When we transition into the end game we may not be interested...
|
|
|
|
if (!interesting) {
|
|
|
|
if (_log.shouldLog(Log.DEBUG))
|
|
|
|
_log.debug(peer + " transition to end game, setting interesting");
|
|
|
|
interesting = true;
|
|
|
|
out.sendInterest(true);
|
|
|
|
}
|
|
|
|
|
2005-10-19 22:02:37 +00:00
|
|
|
int piece_length = metainfo.getPieceLength(nextPiece);
|
2007-01-14 19:49:33 +00:00
|
|
|
//Catch a common place for OOMs esp. on 1MB pieces
|
|
|
|
byte[] bs;
|
|
|
|
try {
|
|
|
|
bs = new byte[piece_length];
|
|
|
|
} catch (OutOfMemoryError oom) {
|
|
|
|
_log.warn("Out of memory, can't request piece " + nextPiece, oom);
|
|
|
|
return false;
|
|
|
|
}
|
2005-10-19 22:02:37 +00:00
|
|
|
|
|
|
|
int length = Math.min(piece_length, PARTSIZE);
|
|
|
|
Request req = new Request(nextPiece, bs, 0, length);
|
|
|
|
outstandingRequests.add(req);
|
|
|
|
if (!choked)
|
|
|
|
out.sendRequest(req);
|
|
|
|
lastRequest = req;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
synchronized void setInteresting(boolean interest)
|
|
|
|
{
|
2005-12-19 13:34:52 +00:00
|
|
|
if (_log.shouldLog(Log.DEBUG))
|
|
|
|
_log.debug(peer + " setInteresting(" + interest + ")");
|
2005-10-19 22:02:37 +00:00
|
|
|
|
|
|
|
if (interest != interesting)
|
|
|
|
{
|
|
|
|
interesting = interest;
|
|
|
|
out.sendInterest(interest);
|
|
|
|
|
|
|
|
if (interesting && !choked)
|
|
|
|
request();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
synchronized void setChoking(boolean choke)
|
|
|
|
{
|
2005-12-19 13:34:52 +00:00
|
|
|
if (_log.shouldLog(Log.DEBUG))
|
|
|
|
_log.debug(peer + " setChoking(" + choke + ")");
|
2005-10-19 22:02:37 +00:00
|
|
|
|
|
|
|
if (choking != choke)
|
|
|
|
{
|
|
|
|
choking = choke;
|
|
|
|
out.sendChoke(choke);
|
|
|
|
}
|
|
|
|
}
|
2006-09-06 06:32:53 +00:00
|
|
|
|
2006-09-07 23:03:18 +00:00
|
|
|
void keepAlive()
|
2006-09-06 06:32:53 +00:00
|
|
|
{
|
|
|
|
out.sendAlive();
|
|
|
|
}
|
2006-09-16 21:07:28 +00:00
|
|
|
|
|
|
|
synchronized void retransmitRequests()
|
|
|
|
{
|
|
|
|
if (interesting && !choked)
|
|
|
|
out.retransmitRequests(outstandingRequests);
|
|
|
|
}
|
2005-10-19 22:02:37 +00:00
|
|
|
}
|