java.lang.Object
org.klomp.snark.TorrentDest

public class TorrentDestextends Object
Bundles the transient I2P destination, session, server socket, DHT instance, and UDP tracker client for one torrent or for a pool of torrents, so that all of a torrent's network activity goes out through its own identity. Created lazily when a torrent starts, destroyed when its last torrent stops, and recreated by the pooling logic when a pool index is reused. Not persisted; a new destination is generated on each start.
  • Field Summary

    Modifier and Type
    Field
    Description
    private int
    Tunnel quantity most recently pushed to this pool's session by IdleChecker, so each pool's floor can be judged against what it was actually given rather than against the client-wide configured option.
    private final I2PAppContext
    The context the destination runs in.
    private TorrentKRPC
    The DHT node serving the torrent, if any.
    private final String
    Base64-encoded info hash of the first torrent assigned, for thread names and logs.
    private final I2PSocketManager
    Opens this destination's sockets.
    private final Map<String,String>
    Assigned torrent names by Base64-encoded info hash key, for the pool tooltip.
    private final int
    -1 when dedicated to a single torrent, else the shared pool index.
    private final int
    Sequential pool number for the "I2PSnark - Pool N" nickname, -1 when dedicated.
    private final I2PServerSocket
    Accepts incoming connections for the torrent.
    private final Map<String,InfoHash>
    Assigned torrents by Base64-encoded info hash key, guarded by synchronized methods.
    The UDP tracker client, if any.
    static final String
    Session property carrying the pool's torrent names, for the /tunnels pool tooltip.
  • Constructor Summary

    Constructors
    Constructor
    Description
    TorrentDest(I2PAppContext ctx, String key, int poolIndex, int poolNum, I2PSocketManager mgr, I2PServerSocket serverSocket)
    Assign a torrent to this destination.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    assign(String key, InfoHash ih, String name)
    Assign a torrent to this destination, serving its infohash on the DHT instance when one already runs, and updating the pool tooltip and tunnel nickname.
    void
    Stop the DHT instance and UDP tracker client and destroy the destination.
    int
    The tunnel quantity last applied to this pool's own session, which is also its floor.
    getDHT(KRPC shared)
    Lazily create the DHT instance on this destination's session, sharing the main instance's routing table, tracker, and blacklist, serving tracker queries only for the torrents assigned here.
    The Base64-encoded info hash of the first torrent assigned.
    This router's own destination.
    int
    Identifies the shared tunnel pool serving this destination.
    int
    The sequential pool number shown in the tunnel nickname "I2PSnark - Pool <n>", or -1 when dedicated to a single torrent.
    The inbound server socket for this destination.
    The socket manager of the transient destination this pool's traffic uses.
    Lazily create the UDP tracker client on this destination's session.
    boolean
    Whether any torrent is assigned to this destination, so it still needs its tunnel floor even with no peers connected yet.
    void
    Record the tunnel quantity pushed to this pool's session.
    boolean
    Remove a torrent from this destination, stopping its DHT serving when an instance runs and updating the pool tooltip and tunnel nickname.
    private void
    Push the pool's torrent names to the session's i2psnark.poolMembers property, which the console reads to render the pool tooltip on the tunnels page.

    Methods inherited from class Object

    clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • _context

      private final I2PAppContext _context
      The context the destination runs in.
    • _key

      private final String _key
      Base64-encoded info hash of the first torrent assigned, for thread names and logs.
    • _poolIndex

      private final int _poolIndex
      -1 when dedicated to a single torrent, else the shared pool index.
    • _poolNum

      private final int _poolNum
      Sequential pool number for the "I2PSnark - Pool N" nickname, -1 when dedicated.
    • _mgr

      private final I2PSocketManager _mgr
      Opens this destination's sockets.
    • _serverSocket

      private final I2PServerSocket _serverSocket
      Accepts incoming connections for the torrent.
    • PROP_POOL_MEMBERS

      public static final String PROP_POOL_MEMBERS
      Session property carrying the pool's torrent names, for the /tunnels pool tooltip.
      See Also:
    • _appliedTunnelQuantity

      private volatile int _appliedTunnelQuantity
      Tunnel quantity most recently pushed to this pool's session by IdleChecker, so each pool's floor can be judged against what it was actually given rather than against the client-wide configured option. Starts at the floor, which is how I2PSnarkUtil creates a pool.
    • _torrents

      private final Map<String,InfoHash> _torrents
      Assigned torrents by Base64-encoded info hash key, guarded by synchronized methods.
    • _names

      private final Map<String,String> _names
      Assigned torrent names by Base64-encoded info hash key, for the pool tooltip.
    • _dht

      private TorrentKRPC _dht
      The DHT node serving the torrent, if any.
    • _udpTracker

      private UDPTrackerClient _udpTracker
      The UDP tracker client, if any.
  • Constructor Details

    • TorrentDest

      public TorrentDest(I2PAppContext ctx, String key, int poolIndex, int poolNum, I2PSocketManager mgr, I2PServerSocket serverSocket)
      Assign a torrent to this destination.
      Parameters:
      ctx - the application context supplying the logging and timing services this destination and its DHT and tracker clients need
      key - Base64 encoding of the first torrent's info hash
      poolIndex - the shared pool index, or -1 for a dedicated destination
      poolNum - the sequential pool number for the nickname, -1 when dedicated
      mgr - the socket manager of the new transient destination; must be connected
      serverSocket - the destination's server socket, or null
  • Method Details

    • getKey

      public String getKey()
      The Base64-encoded info hash of the first torrent assigned.
      Returns:
      the info hash as base64
    • getPoolIndex

      public int getPoolIndex()
      Identifies the shared tunnel pool serving this destination.
      Returns:
      the pool index, or -1 when dedicated to a single torrent
    • getPoolNum

      public int getPoolNum()
      The sequential pool number shown in the tunnel nickname "I2PSnark - Pool <n>", or -1 when dedicated to a single torrent.
      Returns:
      the pool number, or -1 when dedicated
    • hasTorrents

      public boolean hasTorrents()
      Whether any torrent is assigned to this destination, so it still needs its tunnel floor even with no peers connected yet.
      Returns:
      true if at least one torrent is assigned
    • getAppliedTunnelQuantity

      public int getAppliedTunnelQuantity()
      The tunnel quantity last applied to this pool's own session, which is also its floor.
      Returns:
      the applied quantity in each direction, never below 2
    • setAppliedTunnelQuantity

      public void setAppliedTunnelQuantity(int qty)
      Record the tunnel quantity pushed to this pool's session.
      Parameters:
      qty - the quantity in each direction
    • getSocketManager

      public I2PSocketManager getSocketManager()
      The socket manager of the transient destination this pool's traffic uses.
      Returns:
      the socket manager, never null
    • getServerSocket

      public I2PServerSocket getServerSocket()
      The inbound server socket for this destination.
      Returns:
      the server socket, or null when the destination has none
    • getMyDestination

      public Destination getMyDestination()
      This router's own destination.
      Returns:
      null if the session is not open
    • assign

      public void assign(String key, InfoHash ih, String name)
      Assign a torrent to this destination, serving its infohash on the DHT instance when one already runs, and updating the pool tooltip and tunnel nickname. The nickname changes only when the pool crosses the one-to-two torrent boundary; further members keep the pool name until the pool reverts to a single torrent.
      Parameters:
      key - the Base64-encoded info hash key
      ih - the torrent's info hash
      name - the torrent name, shown in the pool tooltip
    • unassign

      public boolean unassign(String key)
      Remove a torrent from this destination, stopping its DHT serving when an instance runs and updating the pool tooltip and tunnel nickname. The nickname reverts to the remaining torrent's name when the pool drops back to a single torrent.
      Parameters:
      key - the Base64-encoded info hash key
      Returns:
      true when no torrents remain assigned
    • updatePoolProps

      private void updatePoolProps()
      Push the pool's torrent names to the session's i2psnark.poolMembers property, which the console reads to render the pool tooltip on the tunnels page. The property must carry the inbound./outbound. prefix, because the router only stores prefixed session options in the pool settings. Also sets the tunnel nickname: the single torrent's name while one torrent is assigned, the "I2PSnark - Pool N" name once a second joins and until the pool reverts to one. Dedicated destinations have no tooltip and skip the update.
    • getDHT

      public TorrentKRPC getDHT(KRPC shared)
      Lazily create the DHT instance on this destination's session, sharing the main instance's routing table, tracker, and blacklist, serving tracker queries only for the torrents assigned here.
      Parameters:
      shared - the main DHT instance, or null if the DHT is disabled
      Returns:
      the DHT instance, or null if disabled
    • getUDPTracker

      public UDPTrackerClient getUDPTracker(I2PSnarkUtil util)
      Lazily create the UDP tracker client on this destination's session.
      Parameters:
      util - the shared util, for destination lookups
      Returns:
      the tracker client, or null if the session is gone
    • destroy

      public void destroy()
      Stop the DHT instance and UDP tracker client and destroy the destination. Call only when no torrents remain assigned.