I2P Address: [http://git.idk.i2p]

Skip to content
Snippets Groups Projects
Commit 48b88862 authored by zzz's avatar zzz
Browse files

i2psnark: Add WebSeed support - WIP - not hooked in yet

parent 1097220d
No related branches found
No related tags found
No related merge requests found
......@@ -42,14 +42,14 @@ import org.klomp.snark.bencode.InvalidBEncodingException;
public class Peer implements Comparable<Peer>
{
private final Log _log = I2PAppContext.getGlobalContext().logManager().getLog(Peer.class);
protected final Log _log = I2PAppContext.getGlobalContext().logManager().getLog(getClass());
// Identifying property, the peer id of the other side.
private final PeerID peerID;
private final byte[] my_id;
private final byte[] infohash;
/** will start out null in magnet mode */
private MetaInfo metainfo;
protected MetaInfo metainfo;
private Map<String, BEValue> handshakeMap;
// The data in/output streams set during the handshake and used by
......@@ -61,8 +61,11 @@ public class Peer implements Comparable<Peer>
private final AtomicLong downloaded = new AtomicLong();
private final AtomicLong uploaded = new AtomicLong();
// Keeps state for in/out connections. Non-null when the handshake
// was successful, the connection setup and runs
/** `
* Keeps state for in/out connections. Non-null when the handshake
* was successful, the connection setup and runs.
* Do not access directly. All actions should be through Peer methods.
*/
volatile PeerState state;
/** shared across all peers on this torrent */
......@@ -77,8 +80,8 @@ public class Peer implements Comparable<Peer>
final static long CHECK_PERIOD = PeerCoordinator.CHECK_PERIOD; // 40 seconds
final static int RATE_DEPTH = PeerCoordinator.RATE_DEPTH; // make following arrays RATE_DEPTH long
private long uploaded_old[] = {-1,-1,-1};
private long downloaded_old[] = {-1,-1,-1};
private final long uploaded_old[] = {-1,-1,-1};
private final long downloaded_old[] = {-1,-1,-1};
private static final byte[] HANDSHAKE = DataHelper.getASCII("BitTorrent protocol");
// See BEP 4 for definitions
......@@ -341,7 +344,6 @@ public class Peer implements Comparable<Peer>
private byte[] handshake(InputStream in, OutputStream out)
throws IOException
{
din = new DataInputStream(in);
dout = new DataOutputStream(out);
// Handshake write - header
......@@ -366,6 +368,7 @@ public class Peer implements Comparable<Peer>
_log.debug("Wrote my shared hash and ID to " + toString());
// Handshake read - header
din = new DataInputStream(in);
byte b = din.readByte();
if (b != HANDSHAKE.length)
throw new IOException("Handshake failure, expected 19, got "
......@@ -789,4 +792,12 @@ public class Peer implements Comparable<Peer>
void setTotalCommentsSent(int count) {
_totalCommentsSent = count;
}
/**
* @return false
* @since 0.9.49
*/
public boolean isWebPeer() {
return false;
}
}
......@@ -26,6 +26,7 @@ import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Deque;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
......@@ -152,6 +153,10 @@ class PeerCoordinator implements PeerListener
private static final long COMMENT_REQ_INTERVAL = 12*60*60*1000L;
private static final long COMMENT_REQ_DELAY = 60*60*1000L;
private static final int MAX_COMMENT_NOT_REQ = 10;
/** hostname to expire time, sync on this */
private Map<String, Long> _webPeerBans;
private static final long WEBPEER_BAN_TIME = 30*60*1000L;
/**
* @param metainfo null if in magnet mode
......@@ -916,6 +921,7 @@ class PeerCoordinator implements PeerListener
// As connections are already up, new Pieces will
// not have their PeerID list populated, so do that.
for (Peer p : peers) {
// TODO don't access state directly
PeerState s = p.state;
if (s != null) {
BitField bf = s.bitfield;
......@@ -1299,6 +1305,7 @@ class PeerCoordinator implements PeerListener
if (++seeds >= 4)
break;
} else {
// TODO don't access state directly
PeerState state = pr.state;
if (state == null) continue;
BitField bf = state.bitfield;
......@@ -1336,6 +1343,7 @@ class PeerCoordinator implements PeerListener
// Temporary? So PeerState never calls wantPiece() directly for now...
Piece piece = wantPiece(peer, havePieces, true);
if (piece != null) {
// TODO padding
return new PartialPiece(piece, metainfo.getPieceLength(piece.getId()), _util.getTempDir());
}
if (_log.shouldLog(Log.DEBUG))
......@@ -1452,6 +1460,10 @@ class PeerCoordinator implements PeerListener
if (bev.getMap().get(ExtensionHandler.TYPE_PEX) != null) {
List<Peer> pList = peerList();
pList.remove(peer);
for (Iterator<Peer> iter = pList.iterator(); iter.hasNext(); ) {
if (iter.next().isWebPeer())
iter.remove();
}
if (!pList.isEmpty())
ExtensionHandler.sendPEX(peer, pList);
}
......@@ -1749,5 +1761,43 @@ class PeerCoordinator implements PeerListener
public I2PSnarkUtil getUtil() {
return _util;
}
/**
* Ban a web peer for this torrent, for while or permanently.
* @param host the host name
* @since 0.9.49
*/
public synchronized void banWebPeer(String host, boolean isPermanent) {
if (_webPeerBans == null)
_webPeerBans = new HashMap<String, Long>(4);
Long time;
if (isPermanent) {
time = Long.valueOf(Long.MAX_VALUE);
} else {
long now = _util.getContext().clock().now();
time = Long.valueOf(now + WEBPEER_BAN_TIME);
}
Long old = _webPeerBans.put(host, time);
if (old != null && old.longValue() > time)
_webPeerBans.put(host, old);
}
/**
* Is a web peer banned?
* @param host the host name
* @since 0.9.49
*/
public synchronized boolean isWebPeerBanned(String host) {
if (_webPeerBans == null)
return false;
Long time = _webPeerBans.get(host);
if (time == null)
return false;
long now = _util.getContext().clock().now();
boolean rv = time.longValue() > now;
if (!rv)
_webPeerBans.remove(host);
return rv;
}
}
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment