2005-10-19 22:02:37 +00:00
|
|
|
/* TrackerInfo - Holds information returned by a tracker, mainly the peer list.
|
|
|
|
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.IOException;
|
|
|
|
import java.io.InputStream;
|
2008-07-16 13:42:54 +00:00
|
|
|
import java.util.HashSet;
|
2010-03-25 19:05:45 +00:00
|
|
|
import java.util.Collections;
|
2005-10-19 22:02:37 +00:00
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Set;
|
|
|
|
|
2008-07-16 13:42:54 +00:00
|
|
|
import org.klomp.snark.bencode.BDecoder;
|
|
|
|
import org.klomp.snark.bencode.BEValue;
|
|
|
|
import org.klomp.snark.bencode.InvalidBEncodingException;
|
2005-10-19 22:02:37 +00:00
|
|
|
|
2010-07-09 16:32:31 +00:00
|
|
|
/**
|
|
|
|
* The data structure for the tracker response.
|
|
|
|
* Handles both traditional and compact formats.
|
|
|
|
* Compact format 1 - a list of hashes - implemented
|
|
|
|
* Compact format 2 - One big string of concatenated hashes - unimplemented
|
|
|
|
*/
|
2005-10-19 22:02:37 +00:00
|
|
|
public class TrackerInfo
|
|
|
|
{
|
|
|
|
private final String failure_reason;
|
|
|
|
private final int interval;
|
2010-07-09 16:32:31 +00:00
|
|
|
private final Set<Peer> peers;
|
2010-03-25 19:05:45 +00:00
|
|
|
private int complete;
|
|
|
|
private int incomplete;
|
2005-10-19 22:02:37 +00:00
|
|
|
|
|
|
|
public TrackerInfo(InputStream in, byte[] my_id, MetaInfo metainfo)
|
|
|
|
throws IOException
|
|
|
|
{
|
|
|
|
this(new BDecoder(in), my_id, metainfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
public TrackerInfo(BDecoder be, byte[] my_id, MetaInfo metainfo)
|
|
|
|
throws IOException
|
|
|
|
{
|
|
|
|
this(be.bdecodeMap().getMap(), my_id, metainfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
public TrackerInfo(Map m, byte[] my_id, MetaInfo metainfo)
|
|
|
|
throws IOException
|
|
|
|
{
|
|
|
|
BEValue reason = (BEValue)m.get("failure reason");
|
|
|
|
if (reason != null)
|
|
|
|
{
|
|
|
|
failure_reason = reason.getString();
|
|
|
|
interval = -1;
|
|
|
|
peers = null;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
failure_reason = null;
|
|
|
|
BEValue beInterval = (BEValue)m.get("interval");
|
|
|
|
if (beInterval == null)
|
|
|
|
throw new InvalidBEncodingException("No interval given");
|
|
|
|
else
|
|
|
|
interval = beInterval.getInt();
|
2010-03-25 19:05:45 +00:00
|
|
|
|
2005-10-19 22:02:37 +00:00
|
|
|
BEValue bePeers = (BEValue)m.get("peers");
|
|
|
|
if (bePeers == null)
|
2010-03-25 19:05:45 +00:00
|
|
|
peers = Collections.EMPTY_SET;
|
2005-10-19 22:02:37 +00:00
|
|
|
else
|
2010-07-09 16:32:31 +00:00
|
|
|
// if compact is going to be one big string instead, try/catch here
|
2005-10-19 22:02:37 +00:00
|
|
|
peers = getPeers(bePeers.getList(), my_id, metainfo);
|
2010-03-25 19:05:45 +00:00
|
|
|
|
|
|
|
BEValue bev = (BEValue)m.get("complete");
|
|
|
|
if (bev != null) try {
|
|
|
|
complete = bev.getInt();
|
|
|
|
if (complete < 0)
|
|
|
|
complete = 0;
|
|
|
|
} catch (InvalidBEncodingException ibe) {}
|
|
|
|
|
|
|
|
bev = (BEValue)m.get("incomplete");
|
|
|
|
if (bev != null) try {
|
|
|
|
incomplete = bev.getInt();
|
|
|
|
if (incomplete < 0)
|
|
|
|
incomplete = 0;
|
|
|
|
} catch (InvalidBEncodingException ibe) {}
|
2005-10-19 22:02:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-09 16:32:31 +00:00
|
|
|
/******
|
|
|
|
public static Set<Peer> getPeers(InputStream in, byte[] my_id, MetaInfo metainfo)
|
2005-10-19 22:02:37 +00:00
|
|
|
throws IOException
|
|
|
|
{
|
|
|
|
return getPeers(new BDecoder(in), my_id, metainfo);
|
|
|
|
}
|
|
|
|
|
2010-07-09 16:32:31 +00:00
|
|
|
public static Set<Peer> getPeers(BDecoder be, byte[] my_id, MetaInfo metainfo)
|
2005-10-19 22:02:37 +00:00
|
|
|
throws IOException
|
|
|
|
{
|
|
|
|
return getPeers(be.bdecodeList().getList(), my_id, metainfo);
|
|
|
|
}
|
2010-07-09 16:32:31 +00:00
|
|
|
******/
|
2005-10-19 22:02:37 +00:00
|
|
|
|
2010-07-09 16:32:31 +00:00
|
|
|
private static Set<Peer> getPeers(List<BEValue> l, byte[] my_id, MetaInfo metainfo)
|
2005-10-19 22:02:37 +00:00
|
|
|
throws IOException
|
|
|
|
{
|
2010-07-09 16:32:31 +00:00
|
|
|
Set<Peer> peers = new HashSet(l.size());
|
2005-10-19 22:02:37 +00:00
|
|
|
|
2010-07-09 16:32:31 +00:00
|
|
|
for (BEValue bev : l) {
|
2008-05-26 13:13:26 +00:00
|
|
|
PeerID peerID;
|
|
|
|
try {
|
2010-07-09 16:32:31 +00:00
|
|
|
// Case 1 - non-compact - A list of dictionaries (maps)
|
|
|
|
peerID = new PeerID(bev.getMap());
|
2008-05-26 13:13:26 +00:00
|
|
|
} catch (InvalidBEncodingException ibe) {
|
2010-07-09 16:32:31 +00:00
|
|
|
try {
|
|
|
|
// Case 2 - compact - A list of 32-byte binary strings (hashes)
|
|
|
|
peerID = new PeerID(bev.getBytes());
|
|
|
|
} catch (InvalidBEncodingException ibe2) {
|
|
|
|
// don't let one bad entry spoil the whole list
|
|
|
|
//Snark.debug("Discarding peer from list: " + ibe, Snark.ERROR);
|
|
|
|
continue;
|
|
|
|
}
|
2008-05-26 13:13:26 +00:00
|
|
|
}
|
2005-10-19 22:02:37 +00:00
|
|
|
peers.add(new Peer(peerID, my_id, metainfo));
|
|
|
|
}
|
|
|
|
|
|
|
|
return peers;
|
|
|
|
}
|
|
|
|
|
2010-07-09 16:32:31 +00:00
|
|
|
public Set<Peer> getPeers()
|
2005-10-19 22:02:37 +00:00
|
|
|
{
|
|
|
|
return peers;
|
|
|
|
}
|
|
|
|
|
2010-03-25 19:05:45 +00:00
|
|
|
public int getPeerCount()
|
|
|
|
{
|
|
|
|
int pc = peers == null ? 0 : peers.size();
|
|
|
|
return Math.max(pc, complete + incomplete - 1);
|
|
|
|
}
|
|
|
|
|
2005-10-19 22:02:37 +00:00
|
|
|
public String getFailureReason()
|
|
|
|
{
|
|
|
|
return failure_reason;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getInterval()
|
|
|
|
{
|
|
|
|
return interval;
|
|
|
|
}
|
|
|
|
|
2009-08-11 21:58:56 +00:00
|
|
|
@Override
|
2005-10-19 22:02:37 +00:00
|
|
|
public String toString()
|
|
|
|
{
|
|
|
|
if (failure_reason != null)
|
|
|
|
return "TrackerInfo[FAILED: " + failure_reason + "]";
|
|
|
|
else
|
|
|
|
return "TrackerInfo[interval=" + interval
|
2010-03-25 19:05:45 +00:00
|
|
|
+ (complete > 0 ? (", complete=" + complete) : "" )
|
|
|
|
+ (incomplete > 0 ? (", incomplete=" + incomplete) : "" )
|
2005-10-19 22:02:37 +00:00
|
|
|
+ ", peers=" + peers + "]";
|
|
|
|
}
|
|
|
|
}
|