Disable returning non-compact peer list
We can no longer do it in a standard way, as we don't store the full destination. Since we can't bencode Peer correctly any more, Peer no longer extends HashMap, and we don't need the String destcache either. This saves lots more space.
This commit is contained in:
@ -16,38 +16,24 @@ package net.i2p.zzzot;
|
||||
*
|
||||
*/
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.concurrent.ConcurrentMap;
|
||||
|
||||
import net.i2p.data.Base32;
|
||||
import net.i2p.data.Destination;
|
||||
import net.i2p.data.Hash;
|
||||
|
||||
/*
|
||||
* A single peer for a single torrent.
|
||||
* Save a couple stats, and implements
|
||||
* a Map so we can BEncode it
|
||||
* So it's like PeerID but in reverse - we make a Map from the
|
||||
* data. PeerID makes the data from a Map.
|
||||
* Save a couple stats. We no longer support non-compact
|
||||
* announces, so this is no longer a Map that can be BEncoded.
|
||||
* See announce.jsp.
|
||||
*/
|
||||
public class Peer extends HashMap<String, Object> {
|
||||
public class Peer {
|
||||
|
||||
private final Hash hash;
|
||||
private long lastSeen;
|
||||
private long bytesLeft;
|
||||
private static final Integer PORT = Integer.valueOf(6881);
|
||||
|
||||
public Peer(byte[] id, Destination address, ConcurrentMap<String, String> destCache) {
|
||||
super(3);
|
||||
if (id.length != 20)
|
||||
throw new IllegalArgumentException("Bad peer ID length: " + id.length);
|
||||
put("peer id", id);
|
||||
put("port", PORT);
|
||||
// cache the 520-byte address strings
|
||||
String dest = address.toBase32().substring(0, 52);
|
||||
String oldDest = destCache.putIfAbsent(dest, dest);
|
||||
if (oldDest != null)
|
||||
dest = oldDest;
|
||||
put("ip", dest);
|
||||
public Peer(byte[] id, Destination address) {
|
||||
hash = address.calculateHash();
|
||||
}
|
||||
|
||||
public void setLeft(long l) {
|
||||
@ -67,7 +53,6 @@ public class Peer extends HashMap<String, Object> {
|
||||
* @since 0.20
|
||||
*/
|
||||
public byte[] getHashBytes() {
|
||||
String ip = (String) get("ip");
|
||||
return Base32.decode(ip);
|
||||
return hash.getData();
|
||||
}
|
||||
}
|
||||
|
@ -303,8 +303,7 @@ public class UDPHandler implements I2PSessionMuxedListener {
|
||||
} else {
|
||||
Peer p = peers.get(pid);
|
||||
if (p == null) {
|
||||
ConcurrentMap<String, String> destCache = _zzzot.getDestCache();
|
||||
p = new Peer(pid.getData(), from, destCache);
|
||||
p = new Peer(pid.getData(), from);
|
||||
Peer p2 = peers.putIfAbsent(pid, p);
|
||||
if (p2 != null)
|
||||
p = p2;
|
||||
|
@ -32,7 +32,6 @@ class ZzzOT {
|
||||
private final I2PAppContext _context;
|
||||
private final Torrents _torrents;
|
||||
private final Cleaner _cleaner;
|
||||
private final ConcurrentHashMap<String, String> _destCache = new ConcurrentHashMap<String, String>();
|
||||
private final long EXPIRE_TIME;
|
||||
|
||||
private static final String PROP_INTERVAL = "interval";
|
||||
@ -79,11 +78,6 @@ class ZzzOT {
|
||||
return _torrents;
|
||||
}
|
||||
|
||||
/** @since 0.9.14 */
|
||||
ConcurrentHashMap<String, String> getDestCache() {
|
||||
return _destCache;
|
||||
}
|
||||
|
||||
void start() {
|
||||
_cleaner.forceReschedule(CLEAN_TIME);
|
||||
long[] r = new long[] { 5*60*1000 };
|
||||
@ -95,7 +89,6 @@ class ZzzOT {
|
||||
void stop() {
|
||||
_cleaner.cancel();
|
||||
_torrents.clear();
|
||||
_destCache.clear();
|
||||
_context.statManager().removeRateStat("plugin.zzzot.announces");
|
||||
_context.statManager().removeRateStat("plugin.zzzot.peers");
|
||||
_context.statManager().removeRateStat("plugin.zzzot.torrents");
|
||||
@ -128,9 +121,6 @@ class ZzzOT {
|
||||
else
|
||||
peers += recent;
|
||||
}
|
||||
if (_runCount.incrementAndGet() % (DEST_CACHE_CLEAN_TIME / CLEAN_TIME) == 0) {
|
||||
_destCache.clear();
|
||||
}
|
||||
_context.statManager().addRateData("plugin.zzzot.announces", _torrents.getAnnounces() / (CLEAN_TIME / (60*1000L)));
|
||||
_context.statManager().addRateData("plugin.zzzot.peers", peers);
|
||||
_context.statManager().addRateData("plugin.zzzot.torrents", _torrents.size());
|
||||
|
@ -166,18 +166,6 @@ public class ZzzOTController implements ClientApp {
|
||||
return ctrlr._zzzot.getTorrents();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return null if not running
|
||||
* @since 0.9.14
|
||||
*/
|
||||
public static ConcurrentMap<String, String> getDestCache() {
|
||||
ZzzOTController ctrlr = getThis();
|
||||
if (ctrlr == null)
|
||||
return null;
|
||||
return ctrlr._zzzot.getDestCache();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return announces per minute, 0 if not running
|
||||
* @since 0.20.0
|
||||
|
@ -204,8 +204,7 @@
|
||||
// fixme same peer id, different dest
|
||||
Peer p = peers.get(pid);
|
||||
if (p == null) {
|
||||
ConcurrentMap<String, String> destCache = ZzzOTController.getDestCache();
|
||||
p = new Peer(pid.getData(), d, destCache);
|
||||
p = new Peer(pid.getData(), d);
|
||||
// don't add if spoofed
|
||||
if (matchIP) {
|
||||
Peer p2 = peers.putIfAbsent(pid, p);
|
||||
@ -248,7 +247,15 @@
|
||||
System.arraycopy(peerlist.get(i).getHashBytes(), 0, peerhashes, i * 32, 32);
|
||||
m.put("peers", peerhashes);
|
||||
} else if (ALLOW_NONCOMPACT_RESPONSE) {
|
||||
m.put("peers", peerlist);
|
||||
// This requires the Peer entries to be Maps
|
||||
// so they can be bencoded, but we don't save
|
||||
// the full Destination any more, and Peer does not.
|
||||
// extend HashMap, to greatly reduce memory usage.
|
||||
// We could create a Map here with the b32 as the IP,
|
||||
// but that's nonstandard. So if non-compact is enabled,
|
||||
// don't return any peers.
|
||||
//m.put("peers", peerlist);
|
||||
m.put("peers", java.util.Collections.EMPTY_LIST);
|
||||
} else {
|
||||
// won't get here
|
||||
}
|
||||
|
Reference in New Issue
Block a user