Package org.bitcoinj.core
Class PeerAddress
- java.lang.Object
-
- org.bitcoinj.core.PeerAddress
-
public class PeerAddress extends java.lang.ObjectA PeerAddress holds an IP address and port number representing the network location of a peer in the Bitcoin P2P network. It exists primarily for serialization purposes.Instances of this class are not safe for use by multiple threads.
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description booleanequals(java.lang.Object o)java.net.InetAddressgetAddr()static java.net.InetAddressgetByAddress(byte[] addrBytes)java.lang.StringgetHostname()intgetMessageSize(int protocolVariant)Return the size of the serialized message, using a given protocol variant.intgetPort()ServicesgetServices()java.net.InetSocketAddressgetSocketAddress()longgetTime()Deprecated.usetime()inthashCode()static PeerAddressinet(java.net.InetAddress addr, int port, Services services, java.time.Instant time)Constructs a peer address from the given IP address, port, services and time.static PeerAddressinet(java.net.InetSocketAddress addr, Services services, java.time.Instant time)Constructs a peer address from the given IP address, port, services and time.static PeerAddresslocalhost(NetworkParameters params)static byte[]mapIntoIPv6(byte[] ip)Map given IPv4 address into IPv6 space.static PeerAddressread(java.nio.ByteBuffer payload, int protocolVariant)Deserialize this peer address from a given payload, using a given protocol variant.byte[]serialize(int protocolVariant)Allocates a byte array and writes this peer address into it, using a given protocol variant.static PeerAddresssimple(java.net.InetAddress addr, int port)Constructs a simple peer address from the given IP address and port, but without services.static PeerAddresssimple(java.net.InetSocketAddress addr)Constructs a simple peer address from the given socket address, but without services.java.time.Instanttime()Gets the time that the node was last seen as connected to the network.java.net.InetSocketAddresstoSocketAddress()java.lang.StringtoString()java.nio.ByteBufferwrite(java.nio.ByteBuffer buf, int protocolVariant)Write this peer address into the given buffer, using a given protocol variant.
-
-
-
Method Detail
-
simple
public static PeerAddress simple(java.net.InetAddress addr, int port)
Constructs a simple peer address from the given IP address and port, but without services. The time is set to current time.- Parameters:
addr- ip address of peerport- port the peer is listening on- Returns:
- simple peer address
-
simple
public static PeerAddress simple(java.net.InetSocketAddress addr)
Constructs a simple peer address from the given socket address, but without services. The time is set to current time.- Parameters:
addr- ip address and port of peer- Returns:
- simple peer address
-
inet
public static PeerAddress inet(java.net.InetAddress addr, int port, Services services, java.time.Instant time)
Constructs a peer address from the given IP address, port, services and time. Such addresses are used for `addr` and `addrv2` messages of types IPv4 and IPv6.- Parameters:
addr- ip address of the peerport- port the peer is listening onservices- node services the peer is providingtime- last-seen time of the peer
-
inet
public static PeerAddress inet(java.net.InetSocketAddress addr, Services services, java.time.Instant time)
Constructs a peer address from the given IP address, port, services and time. Such addresses are used for `addr` and `addrv2` messages of types IPv4 and IPv6.- Parameters:
addr- ip address and port of the peerservices- node services the peer is providingtime- last-seen time of the peer
-
read
public static PeerAddress read(java.nio.ByteBuffer payload, int protocolVariant) throws java.nio.BufferUnderflowException, ProtocolException
Deserialize this peer address from a given payload, using a given protocol variant. The variant can be 1 (AddressV1Message) or 2 (AddressV2Message).- Parameters:
payload- payload to deserialize fromprotocolVariant- variant of protocol to use for parsing- Returns:
- read message
- Throws:
java.nio.BufferUnderflowException- if the read message extends beyond the remaining bytes of the payloadProtocolException
-
localhost
public static PeerAddress localhost(NetworkParameters params)
-
write
public java.nio.ByteBuffer write(java.nio.ByteBuffer buf, int protocolVariant) throws java.nio.BufferOverflowExceptionWrite this peer address into the given buffer, using a given protocol variant. The variant can be 1 (AddressV1Message) or 2 (AddressV2Message)..- Parameters:
buf- buffer to write intoprotocolVariant- variant of protocol used- Returns:
- the buffer
- Throws:
java.nio.BufferOverflowException- if the peer addressdoesn't fit the remaining buffer
-
serialize
public byte[] serialize(int protocolVariant)
Allocates a byte array and writes this peer address into it, using a given protocol variant. The variant can be 1 (AddressV1Message) or 2 (AddressV2Message).- Parameters:
protocolVariant- variant of protocol used- Returns:
- byte array containing the peer address
-
getMessageSize
public int getMessageSize(int protocolVariant)
Return the size of the serialized message, using a given protocol variant. The variant can be 1 (AddressV1Message) or 2 (AddressV2Message).. Note that if the message was deserialized from a payload, this size can differ from the size of the original payload.- Parameters:
protocolVariant- variant of protocol used- Returns:
- size of the serialized message in bytes
-
getByAddress
public static java.net.InetAddress getByAddress(byte[] addrBytes)
-
mapIntoIPv6
public static byte[] mapIntoIPv6(byte[] ip)
Map given IPv4 address into IPv6 space.- Parameters:
ip- IPv4 to map into IPv6 space- Returns:
- mapped IP
-
getHostname
public java.lang.String getHostname()
-
getAddr
public java.net.InetAddress getAddr()
-
getSocketAddress
public java.net.InetSocketAddress getSocketAddress()
-
getPort
public int getPort()
-
getServices
public Services getServices()
-
time
public java.time.Instant time()
Gets the time that the node was last seen as connected to the network.- Returns:
- time that the node was last seen
-
getTime
@Deprecated public long getTime()
Deprecated.usetime()
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
equals
public boolean equals(java.lang.Object o)
- Overrides:
equalsin classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
toSocketAddress
public java.net.InetSocketAddress toSocketAddress()
-
-