Package org.bitcoinj.core
Class PeerAddress
- java.lang.Object
-
- org.bitcoinj.core.Message
-
- org.bitcoinj.core.ChildMessage
-
- org.bitcoinj.core.PeerAddress
-
public class PeerAddress extends ChildMessage
A 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.
This class abuses the protocol version contained in its serializer. It can only contain 0 (format within
VersionMessage
), 1 (AddressV1Message
) or 2 (AddressV2Message
).Instances of this class are not safe for use by multiple threads.
-
-
Field Summary
-
Fields inherited from class org.bitcoinj.core.ChildMessage
parent
-
Fields inherited from class org.bitcoinj.core.Message
MAX_SIZE, params, payload, serializer
-
-
Constructor Summary
Constructors Constructor Description PeerAddress(NetworkParameters params, java.lang.String hostname, int port)
Constructs a peer address from a stringified hostname+port.PeerAddress(NetworkParameters params, java.net.InetAddress addr)
Constructs a peer address from the given IP address.PeerAddress(NetworkParameters params, java.net.InetAddress addr, int port)
Constructs a peer address from the given IP address and port.PeerAddress(NetworkParameters params, java.net.InetAddress addr, int port, java.math.BigInteger services)
Constructs a peer address from the given IP address, port and services.PeerAddress(NetworkParameters params, java.net.InetAddress addr, int port, java.math.BigInteger services, MessageSerializer serializer)
Construct a peer address from a memorized or hardcoded address.PeerAddress(NetworkParameters params, java.net.InetSocketAddress addr)
Constructs a peer address from anInetSocketAddress
.PeerAddress(NetworkParameters params, java.nio.ByteBuffer payload, Message parent, MessageSerializer serializer)
Construct a peer address from a serialized payload.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description protected void
bitcoinSerializeToStream(java.io.OutputStream stream)
Serializes this message to the provided stream.boolean
equals(java.lang.Object o)
java.net.InetAddress
getAddr()
java.lang.String
getHostname()
int
getPort()
java.math.BigInteger
getServices()
java.net.InetSocketAddress
getSocketAddress()
long
getTime()
Deprecated.usetime()
int
hashCode()
static PeerAddress
localhost(NetworkParameters params)
protected void
parse()
java.util.Optional<java.time.Instant>
time()
Gets the time that the node was last seen as connected to the network, or empty if that time isn't known (for old `addr` messages).java.net.InetSocketAddress
toSocketAddress()
java.lang.String
toString()
-
Methods inherited from class org.bitcoinj.core.ChildMessage
setParent, unCache
-
Methods inherited from class org.bitcoinj.core.Message
bitcoinSerialize, getHash, getMessageSize, getParams, readByte, readByteArray, readBytes, readHash, readInt32, readInt64, readStr, readUint32, readUint64, readVarInt, skipBytes, unsafeBitcoinSerialize
-
-
-
-
Constructor Detail
-
PeerAddress
public PeerAddress(NetworkParameters params, java.nio.ByteBuffer payload, Message parent, MessageSerializer serializer) throws ProtocolException
Construct a peer address from a serialized payload.- Parameters:
params
- NetworkParameters object.payload
- Bitcoin protocol formatted byte array containing message content.serializer
- the serializer to use for this message.- Throws:
ProtocolException
-
PeerAddress
public PeerAddress(NetworkParameters params, java.net.InetAddress addr, int port, java.math.BigInteger services, MessageSerializer serializer)
Construct a peer address from a memorized or hardcoded address.
-
PeerAddress
public PeerAddress(NetworkParameters params, java.net.InetAddress addr, int port, java.math.BigInteger services)
Constructs a peer address from the given IP address, port and services. Version number is default for the given parameters.
-
PeerAddress
public PeerAddress(NetworkParameters params, java.net.InetAddress addr, int port)
Constructs a peer address from the given IP address and port. Version number is default for the given parameters.
-
PeerAddress
public PeerAddress(NetworkParameters params, java.net.InetAddress addr)
Constructs a peer address from the given IP address. Port and version number are default for the given parameters.
-
PeerAddress
public PeerAddress(NetworkParameters params, java.net.InetSocketAddress addr)
Constructs a peer address from anInetSocketAddress
. An InetSocketAddress can take in as parameters an InetAddress or a String hostname. If you want to connect to a .onion, set the hostname to the .onion address.
-
PeerAddress
public PeerAddress(NetworkParameters params, java.lang.String hostname, int port)
Constructs a peer address from a stringified hostname+port. Use this if you want to connect to a Tor .onion address.
-
-
Method Detail
-
localhost
public static PeerAddress localhost(NetworkParameters params)
-
bitcoinSerializeToStream
protected void bitcoinSerializeToStream(java.io.OutputStream stream) throws java.io.IOException
Description copied from class:Message
Serializes this message to the provided stream. If you just want the raw bytes use bitcoinSerialize().- Overrides:
bitcoinSerializeToStream
in classMessage
- Throws:
java.io.IOException
-
parse
protected void parse() throws java.nio.BufferUnderflowException, ProtocolException
- Specified by:
parse
in classMessage
- Throws:
java.nio.BufferUnderflowException
ProtocolException
-
getHostname
public java.lang.String getHostname()
-
getAddr
public java.net.InetAddress getAddr()
-
getSocketAddress
public java.net.InetSocketAddress getSocketAddress()
-
getPort
public int getPort()
-
getServices
public java.math.BigInteger getServices()
-
time
public java.util.Optional<java.time.Instant> time()
Gets the time that the node was last seen as connected to the network, or empty if that time isn't known (for old `addr` messages).- Returns:
- time that the node was last seen, or empty if unknown
-
getTime
@Deprecated public long getTime()
Deprecated.usetime()
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
equals
public boolean equals(java.lang.Object o)
- Overrides:
equals
in classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
toSocketAddress
public java.net.InetSocketAddress toSocketAddress()
-
-