diff --git a/src/java/net/i2p/zzzot/Peer.java b/src/java/net/i2p/zzzot/Peer.java index 23ae348..b36a131 100644 --- a/src/java/net/i2p/zzzot/Peer.java +++ b/src/java/net/i2p/zzzot/Peer.java @@ -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 { +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 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 { * @since 0.20 */ public byte[] getHashBytes() { - String ip = (String) get("ip"); - return Base32.decode(ip); + return hash.getData(); } } diff --git a/src/java/net/i2p/zzzot/UDPHandler.java b/src/java/net/i2p/zzzot/UDPHandler.java index e68b7ae..765d107 100644 --- a/src/java/net/i2p/zzzot/UDPHandler.java +++ b/src/java/net/i2p/zzzot/UDPHandler.java @@ -303,8 +303,7 @@ public class UDPHandler implements I2PSessionMuxedListener { } else { Peer p = peers.get(pid); if (p == null) { - ConcurrentMap 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; diff --git a/src/java/net/i2p/zzzot/ZzzOT.java b/src/java/net/i2p/zzzot/ZzzOT.java index f52e08d..cee43e2 100644 --- a/src/java/net/i2p/zzzot/ZzzOT.java +++ b/src/java/net/i2p/zzzot/ZzzOT.java @@ -32,7 +32,6 @@ class ZzzOT { private final I2PAppContext _context; private final Torrents _torrents; private final Cleaner _cleaner; - private final ConcurrentHashMap _destCache = new ConcurrentHashMap(); 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 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()); diff --git a/src/java/net/i2p/zzzot/ZzzOTController.java b/src/java/net/i2p/zzzot/ZzzOTController.java index 2d5864e..179e333 100644 --- a/src/java/net/i2p/zzzot/ZzzOTController.java +++ b/src/java/net/i2p/zzzot/ZzzOTController.java @@ -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 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 diff --git a/src/jsp/announce.jsp b/src/jsp/announce.jsp index afa527f..a6ffe12 100644 --- a/src/jsp/announce.jsp +++ b/src/jsp/announce.jsp @@ -204,8 +204,7 @@ // fixme same peer id, different dest Peer p = peers.get(pid); if (p == null) { - ConcurrentMap 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 }