From c8f97d9c739c0d0c7a7d833686b856d6df16290e Mon Sep 17 00:00:00 2001
From: zzz <zzz@mail.i2p>
Date: Thu, 25 Mar 2010 19:05:45 +0000
Subject: [PATCH]     * i2psnark:       - Send numwant=0 if we don't need peers
       - Report returned complete and incomplete counts         if higher than
 peer count       - Allow missing peer list       - Log tweaks

---
 .../src/org/klomp/snark/TrackerClient.java    | 11 ++++++--
 .../java/src/org/klomp/snark/TrackerInfo.java | 28 ++++++++++++++++++-
 2 files changed, 35 insertions(+), 4 deletions(-)

diff --git a/apps/i2psnark/java/src/org/klomp/snark/TrackerClient.java b/apps/i2psnark/java/src/org/klomp/snark/TrackerClient.java
index c6f22a63df..cb72f2c1e0 100644
--- a/apps/i2psnark/java/src/org/klomp/snark/TrackerClient.java
+++ b/apps/i2psnark/java/src/org/klomp/snark/TrackerClient.java
@@ -150,7 +150,7 @@ public class TrackerClient extends I2PAppThread
                 continue;
              String dest = _util.lookup(url.substring(7, slash));
              if (dest == null) {
-                _log.error("Announce host unknown: [" + url + "]");
+                _log.error("Announce host unknown: [" + url.substring(7, slash) + "]");
                 continue;
              }
              if (primary.startsWith("http://" + dest))
@@ -258,7 +258,7 @@ public class TrackerClient extends I2PAppThread
                     tr.started = true;
 
                     Set peers = info.getPeers();
-                    tr.seenPeers = peers.size();
+                    tr.seenPeers = info.getPeerCount();
                     if (coordinator.trackerSeenPeers < tr.seenPeers) // update rising number quickly
                         coordinator.trackerSeenPeers = tr.seenPeers;
                     if ( (left > 0) && (!completed) ) {
@@ -269,6 +269,7 @@ public class TrackerClient extends I2PAppThread
                         Iterator it = ordered.iterator();
                         while (it.hasNext()) {
                           Peer cur = (Peer)it.next();
+                          // FIXME if id == us || dest == us continue;
                           // only delay if we actually make an attempt to add peer
                           if(coordinator.addPeer(cur)) {
                             int delay = DELAY_MUL;
@@ -356,6 +357,10 @@ public class TrackerClient extends I2PAppThread
       + "&downloaded=" + downloaded
       + "&left=" + left
       + ((! event.equals(NO_EVENT)) ? ("&event=" + event) : "");
+    if (left <= 0 || event.equals(STOPPED_EVENT) || !coordinator.needPeers())
+        s += "&numwant=0";
+    else
+        s += "&numwant=" + _util.getMaxConnections();
     _util.debug("Sending TrackerClient request: " + s, Snark.INFO);
       
     tr.lastRequestTime = System.currentTimeMillis();
@@ -430,7 +435,7 @@ public class TrackerClient extends I2PAppThread
            url.getPort() < 0;
   }
 
-  private class Tracker
+  private static class Tracker
   {
       String announce;
       boolean isPrimary;
diff --git a/apps/i2psnark/java/src/org/klomp/snark/TrackerInfo.java b/apps/i2psnark/java/src/org/klomp/snark/TrackerInfo.java
index c2a287270d..84198f12f0 100644
--- a/apps/i2psnark/java/src/org/klomp/snark/TrackerInfo.java
+++ b/apps/i2psnark/java/src/org/klomp/snark/TrackerInfo.java
@@ -23,6 +23,7 @@ package org.klomp.snark;
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.HashSet;
+import java.util.Collections;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
@@ -37,6 +38,8 @@ public class TrackerInfo
   private final String failure_reason;
   private final int interval;
   private final Set peers;
+  private int complete;
+  private int incomplete;
 
   public TrackerInfo(InputStream in, byte[] my_id, MetaInfo metainfo)
     throws IOException
@@ -68,11 +71,26 @@ public class TrackerInfo
           throw new InvalidBEncodingException("No interval given");
         else
           interval = beInterval.getInt();
+
         BEValue bePeers = (BEValue)m.get("peers");
         if (bePeers == null)
-          throw new InvalidBEncodingException("No peer list");
+          peers = Collections.EMPTY_SET;
         else
           peers = getPeers(bePeers.getList(), my_id, metainfo);
+
+        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) {}
       }
   }
 
@@ -115,6 +133,12 @@ public class TrackerInfo
     return peers;
   }
 
+  public int getPeerCount()
+  {
+    int pc = peers == null ? 0 : peers.size();
+    return Math.max(pc, complete + incomplete - 1);
+  }
+
   public String getFailureReason()
   {
     return failure_reason;
@@ -132,6 +156,8 @@ public class TrackerInfo
       return "TrackerInfo[FAILED: " + failure_reason + "]";
     else
       return "TrackerInfo[interval=" + interval
+        + (complete > 0 ? (", complete=" + complete) : "" )
+        + (incomplete > 0 ? (", incomplete=" + incomplete) : "" )
         + ", peers=" + peers + "]";
   }
 }
-- 
GitLab