2005-10-19 22:02:37 +00:00
|
|
|
/* PeerCheckTasks - TimerTask that checks for good/bad up/downloaders.
|
|
|
|
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.*;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* TimerTask that checks for good/bad up/downloader. Works together
|
|
|
|
* with the PeerCoordinator to select which Peers get (un)choked.
|
|
|
|
*/
|
|
|
|
class PeerCheckerTask extends TimerTask
|
|
|
|
{
|
|
|
|
private final long KILOPERSECOND = 1024*(PeerCoordinator.CHECK_PERIOD/1000);
|
|
|
|
|
|
|
|
private final PeerCoordinator coordinator;
|
|
|
|
|
|
|
|
PeerCheckerTask(PeerCoordinator coordinator)
|
|
|
|
{
|
|
|
|
this.coordinator = coordinator;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void run()
|
|
|
|
{
|
|
|
|
synchronized(coordinator.peers)
|
|
|
|
{
|
2007-03-08 18:55:17 +00:00
|
|
|
Iterator it = coordinator.peers.iterator();
|
|
|
|
if ((!it.hasNext()) || coordinator.halted()) {
|
|
|
|
coordinator.peerCount = 0;
|
|
|
|
coordinator.interestedAndChoking = 0;
|
|
|
|
coordinator.setRateHistory(0, 0);
|
|
|
|
coordinator.uploaders = 0;
|
|
|
|
if (coordinator.halted())
|
|
|
|
cancel();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2005-10-19 22:02:37 +00:00
|
|
|
// Calculate total uploading and worst downloader.
|
|
|
|
long worstdownload = Long.MAX_VALUE;
|
|
|
|
Peer worstDownloader = null;
|
|
|
|
|
|
|
|
int peers = 0;
|
|
|
|
int uploaders = 0;
|
|
|
|
int downloaders = 0;
|
2007-01-14 19:49:33 +00:00
|
|
|
int removedCount = 0;
|
2005-10-19 22:02:37 +00:00
|
|
|
|
|
|
|
long uploaded = 0;
|
|
|
|
long downloaded = 0;
|
|
|
|
|
|
|
|
// Keep track of peers we remove now,
|
|
|
|
// we will add them back to the end of the list.
|
|
|
|
List removed = new ArrayList();
|
2007-03-08 18:55:17 +00:00
|
|
|
int uploadLimit = coordinator.allowedUploaders();
|
2005-10-19 22:02:37 +00:00
|
|
|
while (it.hasNext())
|
|
|
|
{
|
|
|
|
Peer peer = (Peer)it.next();
|
|
|
|
|
|
|
|
// Remove dying peers
|
|
|
|
if (!peer.isConnected())
|
|
|
|
{
|
|
|
|
it.remove();
|
2005-10-21 01:09:31 +00:00
|
|
|
coordinator.removePeerFromPieces(peer);
|
2005-12-16 03:00:48 +00:00
|
|
|
coordinator.peerCount = coordinator.peers.size();
|
2005-10-19 22:02:37 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
peers++;
|
|
|
|
|
|
|
|
if (!peer.isChoking())
|
|
|
|
uploaders++;
|
|
|
|
if (!peer.isChoked() && peer.isInteresting())
|
|
|
|
downloaders++;
|
|
|
|
|
|
|
|
long upload = peer.getUploaded();
|
|
|
|
uploaded += upload;
|
|
|
|
long download = peer.getDownloaded();
|
|
|
|
downloaded += download;
|
2007-03-01 02:22:14 +00:00
|
|
|
peer.setRateHistory(upload, download);
|
2005-10-19 22:02:37 +00:00
|
|
|
peer.resetCounters();
|
|
|
|
|
|
|
|
if (Snark.debug >= Snark.DEBUG)
|
|
|
|
{
|
|
|
|
Snark.debug(peer + ":", Snark.DEBUG);
|
|
|
|
Snark.debug(" ul: " + upload/KILOPERSECOND
|
|
|
|
+ " dl: " + download/KILOPERSECOND
|
|
|
|
+ " i: " + peer.isInterested()
|
|
|
|
+ " I: " + peer.isInteresting()
|
|
|
|
+ " c: " + peer.isChoking()
|
|
|
|
+ " C: " + peer.isChoked(),
|
|
|
|
Snark.DEBUG);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we are at our max uploaders and we have lots of other
|
|
|
|
// interested peers try to make some room.
|
|
|
|
// (Note use of coordinator.uploaders)
|
2007-03-08 18:55:17 +00:00
|
|
|
if (((coordinator.uploaders == uploadLimit
|
|
|
|
&& coordinator.interestedAndChoking > 0)
|
|
|
|
|| coordinator.uploaders > uploadLimit)
|
2005-10-19 22:02:37 +00:00
|
|
|
&& !peer.isChoking())
|
|
|
|
{
|
|
|
|
// Check if it still wants pieces from us.
|
|
|
|
if (!peer.isInterested())
|
|
|
|
{
|
|
|
|
if (Snark.debug >= Snark.INFO)
|
|
|
|
Snark.debug("Choke uninterested peer: " + peer,
|
|
|
|
Snark.INFO);
|
|
|
|
peer.setChoking(true);
|
|
|
|
uploaders--;
|
|
|
|
coordinator.uploaders--;
|
|
|
|
|
|
|
|
// Put it at the back of the list
|
|
|
|
it.remove();
|
|
|
|
removed.add(peer);
|
|
|
|
}
|
2007-01-14 19:49:33 +00:00
|
|
|
else if (peer.isInteresting() && peer.isChoked())
|
2005-10-19 22:02:37 +00:00
|
|
|
{
|
|
|
|
// If they are choking us make someone else a downloader
|
|
|
|
if (Snark.debug >= Snark.DEBUG)
|
|
|
|
Snark.debug("Choke choking peer: " + peer, Snark.DEBUG);
|
2007-01-21 00:35:09 +00:00
|
|
|
peer.setChoking(true);
|
|
|
|
uploaders--;
|
|
|
|
coordinator.uploaders--;
|
|
|
|
removedCount++;
|
|
|
|
|
|
|
|
// Put it at the back of the list
|
|
|
|
it.remove();
|
|
|
|
removed.add(peer);
|
|
|
|
}
|
|
|
|
else if (!peer.isInteresting() && !coordinator.completed())
|
|
|
|
{
|
|
|
|
// If they aren't interesting make someone else a downloader
|
|
|
|
if (Snark.debug >= Snark.DEBUG)
|
|
|
|
Snark.debug("Choke uninteresting peer: " + peer, Snark.DEBUG);
|
2005-10-19 22:02:37 +00:00
|
|
|
peer.setChoking(true);
|
|
|
|
uploaders--;
|
|
|
|
coordinator.uploaders--;
|
2007-01-14 19:49:33 +00:00
|
|
|
removedCount++;
|
2005-10-19 22:02:37 +00:00
|
|
|
|
|
|
|
// Put it at the back of the list
|
|
|
|
it.remove();
|
|
|
|
removed.add(peer);
|
|
|
|
}
|
|
|
|
else if (peer.isInteresting()
|
|
|
|
&& !peer.isChoked()
|
|
|
|
&& download == 0)
|
|
|
|
{
|
|
|
|
// We are downloading but didn't receive anything...
|
|
|
|
if (Snark.debug >= Snark.DEBUG)
|
|
|
|
Snark.debug("Choke downloader that doesn't deliver:"
|
|
|
|
+ peer, Snark.DEBUG);
|
|
|
|
peer.setChoking(true);
|
|
|
|
uploaders--;
|
|
|
|
coordinator.uploaders--;
|
2007-01-14 19:49:33 +00:00
|
|
|
removedCount++;
|
2005-10-19 22:02:37 +00:00
|
|
|
|
|
|
|
// Put it at the back of the list
|
|
|
|
it.remove();
|
|
|
|
removed.add(peer);
|
|
|
|
}
|
2007-01-14 19:49:33 +00:00
|
|
|
else if (peer.isInteresting() && !peer.isChoked() &&
|
|
|
|
download < worstdownload)
|
2005-10-19 22:02:37 +00:00
|
|
|
{
|
|
|
|
// Make sure download is good if we are uploading
|
|
|
|
worstdownload = download;
|
|
|
|
worstDownloader = peer;
|
|
|
|
}
|
2007-01-14 19:49:33 +00:00
|
|
|
else if (upload < worstdownload && coordinator.completed())
|
|
|
|
{
|
|
|
|
// Make sure upload is good if we are seeding
|
|
|
|
worstdownload = upload;
|
|
|
|
worstDownloader = peer;
|
|
|
|
}
|
2005-10-19 22:02:37 +00:00
|
|
|
}
|
2006-09-16 21:07:28 +00:00
|
|
|
peer.retransmitRequests();
|
2006-09-06 06:32:53 +00:00
|
|
|
peer.keepAlive();
|
2005-10-19 22:02:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Resync actual uploaders value
|
|
|
|
// (can shift a bit by disconnecting peers)
|
|
|
|
coordinator.uploaders = uploaders;
|
|
|
|
|
2007-01-14 19:49:33 +00:00
|
|
|
// Remove the worst downloader if needed. (uploader if seeding)
|
2007-03-08 18:55:17 +00:00
|
|
|
if (((uploaders == uploadLimit
|
|
|
|
&& coordinator.interestedAndChoking > 0)
|
|
|
|
|| uploaders > uploadLimit)
|
2005-10-19 22:02:37 +00:00
|
|
|
&& worstDownloader != null)
|
|
|
|
{
|
|
|
|
if (Snark.debug >= Snark.DEBUG)
|
|
|
|
Snark.debug("Choke worst downloader: " + worstDownloader,
|
|
|
|
Snark.DEBUG);
|
|
|
|
|
|
|
|
worstDownloader.setChoking(true);
|
|
|
|
coordinator.uploaders--;
|
2007-01-14 19:49:33 +00:00
|
|
|
removedCount++;
|
2005-10-19 22:02:37 +00:00
|
|
|
|
|
|
|
// Put it at the back of the list
|
|
|
|
coordinator.peers.remove(worstDownloader);
|
2005-12-16 03:00:48 +00:00
|
|
|
coordinator.peerCount = coordinator.peers.size();
|
2005-10-19 22:02:37 +00:00
|
|
|
removed.add(worstDownloader);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Optimistically unchoke a peer
|
|
|
|
coordinator.unchokePeer();
|
|
|
|
|
|
|
|
// Put peers back at the end of the list that we removed earlier.
|
|
|
|
coordinator.peers.addAll(removed);
|
2005-12-16 03:00:48 +00:00
|
|
|
coordinator.peerCount = coordinator.peers.size();
|
2007-01-14 19:49:33 +00:00
|
|
|
coordinator.interestedAndChoking += removedCount;
|
2006-09-03 09:12:22 +00:00
|
|
|
|
|
|
|
// store the rates
|
|
|
|
coordinator.setRateHistory(uploaded, downloaded);
|
|
|
|
|
2005-10-19 22:02:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|