java.lang.Object
org.klomp.snark.PeerID
- All Implemented Interfaces:
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
FieldsModifier and TypeFieldDescriptionprivate StringCached display string.private DestinationThe peer's I2P destination.private byte[]Hash of the peer's destination, for the shared cache.private final intPrecomputed hashCode.private byte[]The 20-byte peer id, null if the peer sent none.private final intThe peer's torrent port, for HTTP trackers.private booleanWhether we have tried to get the dest from the hash; only do once.private final I2PSnarkUtilSupplies the shared destination hash cache.Constructor Summary
ConstructorsConstructorDescriptionPeerID(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 TypeMethodDescriptionprivate intComputes the hashCode from the id bytes.intCompares port, address and id.booleanTwo PeerIDs are equal when they have the same dest hash.The destination.byte[]The dest hash.byte[]getID()Returns the peer ID bytes.intgetPort()Returns the port number.inthashCode()The hash code of a PeerID is the hashcode of the desthash.static Stringidencode(byte[] bs) Encode an id as a hex encoded string and remove leading zeros.booleanReturns true if and only if this peerID and the given peerID have the same destination hash.voidsetID(byte[] xid) Store the peer ID bytes, for connecting out to the peer based on its dest hash.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.
Field Details
id
private byte[] idThe 20-byte peer id, null if the peer sent none.address
The peer's I2P destination.port
private final int portThe peer's torrent port, for HTTP trackers.destHash
private byte[] destHashHash of the peer's destination, for the shared cache.triedDestLookup
private boolean triedDestLookupWhether we have tried to get the dest from the hash; only do once.hash
private final int hashPrecomputed hashCode.util
Supplies the shared destination hash cache._toStringCache
Cached display string.
Constructor Details
PeerID
Creates a PeerID from a peer ID byte array and destination address.- Parameters:
id- the peer ID bytesaddress- the destination address
PeerID
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
Creates a PeerID from a destHash.- Parameters:
destHashParam- the destination hash, which must be exactly 32 bytesutil- 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
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
sameID
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
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.compareTo
Compares port, address and id.- Specified by:
compareToin interfaceComparable<PeerID>- Parameters:
pid- the peer ID to compare to- Returns:
- a negative, zero, or positive integer
idencode
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