org.klomp.snark

## Class TrackerClient

- java.lang.Object
- - org.klomp.snark.TrackerClient

- All Implemented Interfaces:
    Runnable

      public class TrackerClient
      extends Object
      implements Runnable

  Handles communication with BitTorrent trackers and DHT to discover new peers.

  This class manages tracker announcements and peer discovery by:

  - Announcing torrent status to HTTP/HTTPS trackers
  - Processing tracker responses and extracting peer information
  - Managing UDP tracker communication
  - Integrating with DHT for trackerless peer discovery
  - Handling Peer Exchange (PEX) when available
  - Managing announcement intervals and retry logic
  - Processing various tracker events (started, completed, stopped)

  Threading Model: The start() method creates a thread that runs one complete announcement cycle through all trackers, PEX, and DHT, then queues a new timed event and exits. This approach prevents thread accumulation while ensuring regular announcements.

  The client can be restarted after halt() and handles various tracker responses including registration failures and error conditions.

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `static int` | `PORT`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `TrackerClient(I2PSnarkUtil util, MetaInfo meta, String additionalTrackerURL, PeerCoordinator coordinator, Snark snark)` Call start() to start it.

  - ### Method Summary

    All Methods Static Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `halt(boolean fast)` Interrupts this Thread to stop it.
    `boolean` | `halted()`
    `static boolean` | `isValidAnnounce(String ann)`
    `void` | `reinitialize()` Call after editing torrent
    `void` | `run()` Setup the first time only, then one pass (usually) through the trackers, PEX, and DHT.
    `void` | `start()`
    `boolean` | `started()`
    `static String` | `urlencode(byte[] bs)` Very lazy byte[] to URL encoder.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### PORT

          public static final int PORT

      See Also:
        Constant Field Values

  - ### Constructor Detail

    - #### TrackerClient

          public TrackerClient(I2PSnarkUtil util,
                               MetaInfo meta,
                               String additionalTrackerURL,
                               PeerCoordinator coordinator,
                               Snark snark)

      Call start() to start it.
      Parameters:
        `meta` - null if in magnet mode
        `additionalTrackerURL` - may be null, from the ?tr= param in magnet mode, otherwise ignored

  - ### Method Detail

    - #### halt

          public void halt(boolean fast)

      Interrupts this Thread to stop it.
      Parameters:
        `fast` - if true, limit the life of the unannounce threads

    - #### halted

          public boolean halted()

    - #### isValidAnnounce

          public static boolean isValidAnnounce(String ann)

      Parameters:
        `ann` - an announce URL, may be null, returns false if null
      Returns:
        true for i2p hosts only

    - #### reinitialize

          public void reinitialize()

      Call after editing torrent

    - #### run

          public void run()

      Setup the first time only, then one pass (usually) through the trackers, PEX, and DHT. This will take several seconds to several minutes.
      Specified by:
        `run` in interface `Runnable`

    - #### start

          public void start()

    - #### started

          public boolean started()

    - #### urlencode

          public static String urlencode(byte[] bs)

      Very lazy byte[] to URL encoder. Just encodes almost everything, even some "normal" chars. By not encoding about 1/4 of the chars, we make random data like hashes about 16% smaller.

      RFC1738: 0-9a-zA-Z$-_.+!*'(), Us: 0-9a-zA-Z