java.lang.Object
org.klomp.snark.PeerID
All Implemented Interfaces:
Comparable<PeerID>

public class PeerIDextends Object implements Comparable<PeerID>
Store the address information about a peer. Prior to 0.8.1, an instantiation required a peer ID, and full Destination address. Starting with 0.8.1, to support compact tracker responses, a PeerID can be instantiated with a Destination Hash alone. The full destination lookup is deferred until getAddress() is called, and the PeerID is not required. Equality is now determined solely by the dest hash.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private String
    Cached display string.
    private Destination
    The peer's I2P destination.
    private byte[]
    Hash of the peer's destination, for the shared cache.
    private final int
    Precomputed hashCode.
    private byte[]
    The 20-byte peer id, null if the peer sent none.
    private final int
    The peer's torrent port, for HTTP trackers.
    private boolean
    Whether we have tried to get the dest from the hash; only do once.
    private final I2PSnarkUtil
    Supplies the shared destination hash cache.
  • Constructor Summary

    Constructors
    Constructor
    Description
    PeerID(byte[] id, Destination address)
    Creates a PeerID from a peer ID byte array and destination address.
    PeerID(byte[] destHashParam, I2PSnarkUtil util)
    Creates a PeerID from a destHash.
    Creates a PeerID from a Map containing BEncoded peer id, ip and port.
  • Method Summary

    Modifier and Type
    Method
    Description
    private int
    Computes the hashCode from the id bytes.
    int
    Compares port, address and id.
    boolean
    Two PeerIDs are equal when they have the same dest hash.
    The destination.
    byte[]
    The dest hash.
    byte[]
    Returns the peer ID bytes.
    int
    Returns the port number.
    int
    The hash code of a PeerID is the hashcode of the desthash.
    static String
    idencode(byte[] bs)
    Encode an id as a hex encoded string and remove leading zeros.
    boolean
    Returns true if and only if this peerID and the given peerID have the same destination hash.
    void
    setID(byte[] xid)
    Store the peer ID bytes, for connecting out to the peer based on its dest hash.
    Returns the String "id@address" where id is the first 4 chars of the base64 encoded id and address is the first 6 chars of the base64 dest (was the base64 hash of the dest) which should match what the bytemonsoon tracker reports on its web pages.

    Methods inherited from class Object

    clone, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • id

      private byte[] id
      The 20-byte peer id, null if the peer sent none.
    • address

      private Destination address
      The peer's I2P destination.
    • port

      private final int port
      The peer's torrent port, for HTTP trackers.
    • destHash

      private byte[] destHash
      Hash of the peer's destination, for the shared cache.
    • triedDestLookup

      private boolean triedDestLookup
      Whether we have tried to get the dest from the hash; only do once.
    • hash

      private final int hash
      Precomputed hashCode.
    • util

      private final I2PSnarkUtil util
      Supplies the shared destination hash cache.
    • _toStringCache

      private String _toStringCache
      Cached display string.
  • Constructor Details

    • PeerID

      public PeerID(byte[] id, Destination address)
      Creates a PeerID from a peer ID byte array and destination address.
      Parameters:
      id - the peer ID bytes
      address - the destination address
    • PeerID

      public PeerID(Map<String,BEValue> m) throws InvalidBEncodingException
      Creates a PeerID from a Map containing BEncoded peer id, ip and port. Non-compact tracker response.
      Parameters:
      m - the map containing peer id and ip entries
      Throws:
      InvalidBEncodingException - if the map entries are missing or invalid
    • PeerID

      public PeerID(byte[] destHashParam, I2PSnarkUtil util) throws InvalidBEncodingException
      Creates a PeerID from a destHash.
      Parameters:
      destHashParam - the destination hash, which must be exactly 32 bytes
      util - for eventual destination lookup
      Throws:
      InvalidBEncodingException - if destHashParam is not 32 bytes long
  • Method Details

    • getID

      public byte[] getID()
      Returns the peer ID bytes.
      Returns:
      the peer ID, or null if not yet set
    • setID

      public void setID(byte[] xid)
      Store the peer ID bytes, for connecting out to the peer based on its dest hash.
      Parameters:
      xid - the peer ID bytes
    • getAddress

      public Destination getAddress()
      The destination. If this PeerId was instantiated with a destHash, and we have not yet done so, lookup the full destination, which may take up to 10 seconds.
      Returns:
      Dest or null if unknown
    • getPort

      public int getPort()
      Returns the port number.
      Returns:
      the TCP port this peer is reachable on
    • getDestHash

      public byte[] getDestHash()
      The dest hash.
      Returns:
      the 20-byte I2P destination hash, or null
    • calculateHash

      private int calculateHash()
      Computes the hashCode from the id bytes.
      Returns:
      the hash code computed from the id bytes
    • hashCode

      public int hashCode()
      The hash code of a PeerID is the hashcode of the desthash.
      Overrides:
      hashCode in class Object
    • sameID

      public boolean sameID(PeerID pid)
      Returns true if and only if this peerID and the given peerID have the same destination hash.
      Parameters:
      pid - the peer to compare destination hashes with
      Returns:
      true if both peers hash to the same destination
    • equals

      public boolean equals(Object o)
      Two PeerIDs are equal when they have the same dest hash.
      Overrides:
      equals in class Object
    • toString

      public String toString()
      Returns the String "id@address" where id is the first 4 chars of the base64 encoded id and address is the first 6 chars of the base64 dest (was the base64 hash of the dest) which should match what the bytemonsoon tracker reports on its web pages.
      Overrides:
      toString in class Object
    • compareTo

      public int compareTo(PeerID pid)
      Compares port, address and id.
      Specified by:
      compareTo in interface Comparable<PeerID>
      Parameters:
      pid - the peer ID to compare to
      Returns:
      a negative, zero, or positive integer
    • idencode

      public static String idencode(byte[] bs)
      Encode an id as a hex encoded string and remove leading zeros.
      Parameters:
      bs - the peer id bytes, each rendered as two hex digits
      Returns:
      the hex digits with any leading zero bytes omitted