2005-10-19 22:02:37 +00:00
|
|
|
/* TrackerClient - Class that informs a tracker and gets new peers.
|
|
|
|
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.io.*;
|
|
|
|
import java.net.*;
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
import org.klomp.snark.bencode.*;
|
2005-12-16 23:18:56 +00:00
|
|
|
import net.i2p.util.I2PThread;
|
2005-12-13 09:38:51 +00:00
|
|
|
import net.i2p.util.Log;
|
2005-10-19 22:02:37 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Informs metainfo tracker of events and gets new peers for peer
|
|
|
|
* coordinator.
|
|
|
|
*
|
|
|
|
* @author Mark Wielaard (mark@klomp.org)
|
|
|
|
*/
|
2005-12-16 23:18:56 +00:00
|
|
|
public class TrackerClient extends I2PThread
|
2005-10-19 22:02:37 +00:00
|
|
|
{
|
2005-12-13 09:38:51 +00:00
|
|
|
private static final Log _log = new Log(TrackerClient.class);
|
2005-10-19 22:02:37 +00:00
|
|
|
private static final String NO_EVENT = "";
|
|
|
|
private static final String STARTED_EVENT = "started";
|
|
|
|
private static final String COMPLETED_EVENT = "completed";
|
|
|
|
private static final String STOPPED_EVENT = "stopped";
|
|
|
|
|
|
|
|
private final static int SLEEP = 5; // 5 minutes.
|
|
|
|
|
|
|
|
private final MetaInfo meta;
|
|
|
|
private final PeerCoordinator coordinator;
|
|
|
|
private final int port;
|
|
|
|
|
|
|
|
private boolean stop;
|
2005-12-16 03:00:48 +00:00
|
|
|
private boolean started;
|
2005-10-19 22:02:37 +00:00
|
|
|
|
|
|
|
private long interval;
|
|
|
|
private long lastRequestTime;
|
|
|
|
|
|
|
|
public TrackerClient(MetaInfo meta, PeerCoordinator coordinator)
|
|
|
|
{
|
|
|
|
// Set unique name.
|
|
|
|
super("TrackerClient-" + urlencode(coordinator.getID()));
|
|
|
|
this.meta = meta;
|
|
|
|
this.coordinator = coordinator;
|
|
|
|
|
|
|
|
this.port = 6881; //(port == -1) ? 9 : port;
|
|
|
|
|
|
|
|
stop = false;
|
2005-12-16 03:00:48 +00:00
|
|
|
started = false;
|
2005-10-19 22:02:37 +00:00
|
|
|
}
|
|
|
|
|
2005-12-15 08:58:30 +00:00
|
|
|
public void start() {
|
2005-12-16 03:00:48 +00:00
|
|
|
if (stop) throw new RuntimeException("Dont rerun me, create a copy");
|
2005-12-15 08:58:30 +00:00
|
|
|
super.start();
|
2005-12-16 03:00:48 +00:00
|
|
|
started = true;
|
2005-12-15 08:58:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public boolean halted() { return stop; }
|
2005-12-16 03:00:48 +00:00
|
|
|
public boolean started() { return started; }
|
2005-12-15 08:58:30 +00:00
|
|
|
|
2005-10-19 22:02:37 +00:00
|
|
|
/**
|
|
|
|
* Interrupts this Thread to stop it.
|
|
|
|
*/
|
|
|
|
public void halt()
|
|
|
|
{
|
|
|
|
stop = true;
|
|
|
|
this.interrupt();
|
|
|
|
}
|
|
|
|
|
2005-12-16 23:18:56 +00:00
|
|
|
private boolean verifyConnected() {
|
|
|
|
while (!stop && !I2PSnarkUtil.instance().connected()) {
|
|
|
|
boolean ok = I2PSnarkUtil.instance().connect();
|
|
|
|
if (!ok) {
|
|
|
|
try { Thread.sleep(30*1000); } catch (InterruptedException ie) {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return !stop && I2PSnarkUtil.instance().connected();
|
|
|
|
}
|
|
|
|
|
2005-10-19 22:02:37 +00:00
|
|
|
public void run()
|
|
|
|
{
|
|
|
|
// XXX - Support other IPs
|
2005-12-13 09:38:51 +00:00
|
|
|
String announce = meta.getAnnounce(); //I2PSnarkUtil.instance().rewriteAnnounce(meta.getAnnounce());
|
2005-10-19 22:02:37 +00:00
|
|
|
String infoHash = urlencode(meta.getInfoHash());
|
|
|
|
String peerID = urlencode(coordinator.getID());
|
|
|
|
|
2005-12-13 09:38:51 +00:00
|
|
|
_log.debug("Announce: [" + meta.getAnnounce() + "] infoHash: " + infoHash
|
|
|
|
+ " xmitAnnounce: [" + announce + "]");
|
|
|
|
|
2005-10-19 22:02:37 +00:00
|
|
|
long uploaded = coordinator.getUploaded();
|
|
|
|
long downloaded = coordinator.getDownloaded();
|
|
|
|
long left = coordinator.getLeft();
|
|
|
|
|
|
|
|
boolean completed = (left == 0);
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2005-12-16 23:18:56 +00:00
|
|
|
if (!verifyConnected()) return;
|
2005-10-19 22:02:37 +00:00
|
|
|
boolean started = false;
|
|
|
|
while (!started)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
// Send start.
|
|
|
|
TrackerInfo info = doRequest(announce, infoHash, peerID,
|
|
|
|
uploaded, downloaded, left,
|
|
|
|
STARTED_EVENT);
|
2005-12-16 03:00:48 +00:00
|
|
|
Set peers = info.getPeers();
|
|
|
|
coordinator.trackerSeenPeers = peers.size();
|
2005-12-15 08:58:30 +00:00
|
|
|
if (!completed) {
|
2005-12-16 03:00:48 +00:00
|
|
|
Iterator it = peers.iterator();
|
2005-12-15 08:58:30 +00:00
|
|
|
while (it.hasNext()) {
|
|
|
|
Peer cur = (Peer)it.next();
|
|
|
|
coordinator.addPeer(cur);
|
|
|
|
int delay = 3000;
|
|
|
|
int c = ((int)cur.getPeerID().getAddress().calculateHash().toBase64().charAt(0)) % 10;
|
|
|
|
try { Thread.sleep(delay * c); } catch (InterruptedException ie) {}
|
|
|
|
}
|
|
|
|
}
|
2005-10-19 22:02:37 +00:00
|
|
|
started = true;
|
2005-12-16 03:00:48 +00:00
|
|
|
coordinator.trackerProblems = null;
|
2005-10-19 22:02:37 +00:00
|
|
|
}
|
|
|
|
catch (IOException ioe)
|
|
|
|
{
|
|
|
|
// Probably not fatal (if it doesn't last to long...)
|
|
|
|
Snark.debug
|
|
|
|
("WARNING: Could not contact tracker at '"
|
|
|
|
+ announce + "': " + ioe, Snark.WARNING);
|
2005-12-16 03:00:48 +00:00
|
|
|
coordinator.trackerProblems = ioe.getMessage();
|
2005-10-19 22:02:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!started && !stop)
|
|
|
|
{
|
|
|
|
Snark.debug(" Retrying in one minute...", Snark.DEBUG);
|
|
|
|
try
|
|
|
|
{
|
|
|
|
// Sleep one minutes...
|
|
|
|
Thread.sleep(60*1000);
|
|
|
|
}
|
|
|
|
catch(InterruptedException interrupt)
|
|
|
|
{
|
|
|
|
// ignore
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-12-22 10:04:12 +00:00
|
|
|
Random r = new Random();
|
2005-10-19 22:02:37 +00:00
|
|
|
while(!stop)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
// Sleep some minutes...
|
2005-12-22 10:04:12 +00:00
|
|
|
int delay = SLEEP*60*1000 + r.nextInt(120*1000);
|
|
|
|
Thread.sleep(delay);
|
2005-10-19 22:02:37 +00:00
|
|
|
}
|
|
|
|
catch(InterruptedException interrupt)
|
|
|
|
{
|
|
|
|
// ignore
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stop)
|
|
|
|
break;
|
2005-12-16 23:18:56 +00:00
|
|
|
|
|
|
|
if (!verifyConnected()) return;
|
2005-10-19 22:02:37 +00:00
|
|
|
|
|
|
|
uploaded = coordinator.getUploaded();
|
|
|
|
downloaded = coordinator.getDownloaded();
|
|
|
|
left = coordinator.getLeft();
|
|
|
|
|
|
|
|
// First time we got a complete download?
|
|
|
|
String event;
|
|
|
|
if (!completed && left == 0)
|
|
|
|
{
|
|
|
|
completed = true;
|
|
|
|
event = COMPLETED_EVENT;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
event = NO_EVENT;
|
|
|
|
|
|
|
|
// Only do a request when necessary.
|
|
|
|
if (event == COMPLETED_EVENT
|
|
|
|
|| coordinator.needPeers()
|
|
|
|
|| System.currentTimeMillis() > lastRequestTime + interval)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
TrackerInfo info = doRequest(announce, infoHash, peerID,
|
|
|
|
uploaded, downloaded, left,
|
|
|
|
event);
|
|
|
|
|
2005-12-16 03:00:48 +00:00
|
|
|
Set peers = info.getPeers();
|
|
|
|
coordinator.trackerSeenPeers = peers.size();
|
2005-12-15 08:58:30 +00:00
|
|
|
if ( (left > 0) && (!completed) ) {
|
|
|
|
// we only want to talk to new people if we need things
|
|
|
|
// from them (duh)
|
2005-12-20 02:01:37 +00:00
|
|
|
List ordered = new ArrayList(peers);
|
|
|
|
Collections.shuffle(ordered);
|
|
|
|
Iterator it = ordered.iterator();
|
2005-12-15 08:58:30 +00:00
|
|
|
while (it.hasNext()) {
|
|
|
|
Peer cur = (Peer)it.next();
|
|
|
|
coordinator.addPeer(cur);
|
|
|
|
int delay = 3000;
|
|
|
|
int c = ((int)cur.getPeerID().getAddress().calculateHash().toBase64().charAt(0)) % 10;
|
|
|
|
try { Thread.sleep(delay * c); } catch (InterruptedException ie) {}
|
|
|
|
}
|
|
|
|
}
|
2005-10-19 22:02:37 +00:00
|
|
|
}
|
|
|
|
catch (IOException ioe)
|
|
|
|
{
|
|
|
|
// Probably not fatal (if it doesn't last to long...)
|
|
|
|
Snark.debug
|
|
|
|
("WARNING: Could not contact tracker at '"
|
|
|
|
+ announce + "': " + ioe, Snark.WARNING);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Throwable t)
|
|
|
|
{
|
2005-12-18 05:39:52 +00:00
|
|
|
I2PSnarkUtil.instance().debug("TrackerClient: " + t, Snark.ERROR, t);
|
|
|
|
if (t instanceof OutOfMemoryError)
|
|
|
|
throw (OutOfMemoryError)t;
|
2005-10-19 22:02:37 +00:00
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2005-12-16 23:18:56 +00:00
|
|
|
if (!verifyConnected()) return;
|
2005-10-19 22:02:37 +00:00
|
|
|
TrackerInfo info = doRequest(announce, infoHash, peerID, uploaded,
|
|
|
|
downloaded, left, STOPPED_EVENT);
|
|
|
|
}
|
|
|
|
catch(IOException ioe) { /* ignored */ }
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private TrackerInfo doRequest(String announce, String infoHash,
|
|
|
|
String peerID, long uploaded,
|
|
|
|
long downloaded, long left, String event)
|
|
|
|
throws IOException
|
|
|
|
{
|
|
|
|
String s = announce
|
|
|
|
+ "?info_hash=" + infoHash
|
|
|
|
+ "&peer_id=" + peerID
|
|
|
|
+ "&port=" + port
|
2005-12-13 09:38:51 +00:00
|
|
|
+ "&ip=" + I2PSnarkUtil.instance().getOurIPString() + ".i2p"
|
2005-10-19 22:02:37 +00:00
|
|
|
+ "&uploaded=" + uploaded
|
|
|
|
+ "&downloaded=" + downloaded
|
|
|
|
+ "&left=" + left
|
|
|
|
+ ((event != NO_EVENT) ? ("&event=" + event) : "");
|
|
|
|
if (Snark.debug >= Snark.INFO)
|
|
|
|
Snark.debug("Sending TrackerClient request: " + s, Snark.INFO);
|
|
|
|
|
|
|
|
File fetched = I2PSnarkUtil.instance().get(s);
|
|
|
|
if (fetched == null) {
|
|
|
|
throw new IOException("Error fetching " + s);
|
|
|
|
}
|
|
|
|
|
2005-12-16 08:24:21 +00:00
|
|
|
InputStream in = null;
|
2005-12-16 03:00:48 +00:00
|
|
|
try {
|
2005-12-16 08:24:21 +00:00
|
|
|
in = new FileInputStream(fetched);
|
2005-10-19 22:02:37 +00:00
|
|
|
|
2005-12-16 03:00:48 +00:00
|
|
|
TrackerInfo info = new TrackerInfo(in, coordinator.getID(),
|
|
|
|
coordinator.getMetaInfo());
|
|
|
|
if (Snark.debug >= Snark.INFO)
|
|
|
|
Snark.debug("TrackerClient response: " + info, Snark.INFO);
|
|
|
|
lastRequestTime = System.currentTimeMillis();
|
|
|
|
|
|
|
|
String failure = info.getFailureReason();
|
|
|
|
if (failure != null)
|
|
|
|
throw new IOException(failure);
|
|
|
|
|
|
|
|
interval = info.getInterval() * 1000;
|
|
|
|
return info;
|
|
|
|
} finally {
|
2005-12-16 08:24:21 +00:00
|
|
|
if (in != null) try { in.close(); } catch (IOException ioe) {}
|
2005-12-16 03:00:48 +00:00
|
|
|
fetched.delete();
|
|
|
|
}
|
2005-10-19 22:02:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Very lazy byte[] to URL encoder. Just encodes everything, even
|
|
|
|
* "normal" chars.
|
|
|
|
*/
|
|
|
|
static String urlencode(byte[] bs)
|
|
|
|
{
|
|
|
|
StringBuffer sb = new StringBuffer(bs.length*3);
|
|
|
|
for (int i = 0; i < bs.length; i++)
|
|
|
|
{
|
|
|
|
int c = bs[i] & 0xFF;
|
|
|
|
sb.append('%');
|
|
|
|
if (c < 16)
|
|
|
|
sb.append('0');
|
|
|
|
sb.append(Integer.toHexString(c));
|
|
|
|
}
|
|
|
|
|
|
|
|
return sb.toString();
|
|
|
|
}
|
|
|
|
}
|