Package org.ldk.batteries
Class NioPeerHandler
- java.lang.Object
-
- org.ldk.batteries.NioPeerHandler
-
public class NioPeerHandler extends Object
A NioPeerHandler maps LDK's PeerHandler to Java's NIO I/O interface. It spawns a single background thread which processes socket events and provides the data to LDK for decryption and processing.
-
-
Constructor Summary
Constructors Constructor Description NioPeerHandler(PeerManager manager)Constructs a new peer handler, spawning a thread to monitor for socket events.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidbind_listener(SocketAddress socket_address)Binds a listening socket to the given address, accepting incoming connections and handling them on the background thread.voidcheck_events()Calls process_events on the PeerManager immediately.voidconnect(byte[] their_node_id, SocketAddress remote, int timeout_ms)Connect to a peer given their node id and socket address.voiddisconnect(byte[] their_node_id)Disconnects any connections currently open with the peer with the given node id.voidinterrupt()Interrupt the background thread, stopping all peer handling.
-
-
-
Constructor Detail
-
NioPeerHandler
public NioPeerHandler(PeerManager manager) throws IOException
Constructs a new peer handler, spawning a thread to monitor for socket events. The background thread will call the PeerManager's timer_tick_occured() function for you on an appropriate schedule.- Parameters:
manager- The LDK PeerManager which connection data will be provided to.- Throws:
IOException- If an internal java.nio error occurs.
-
-
Method Detail
-
connect
public void connect(byte[] their_node_id, SocketAddress remote, int timeout_ms) throws IOExceptionConnect to a peer given their node id and socket address. Blocks until a connection is established (or returns IOException) and then the connection handling runs in the background.- Parameters:
their_node_id- A valid 33-byte public key representing the peer's Lightning Node ID. If this is invalid, undefined behavior (read: Segfault, etc) may occur.remote- The socket address to connect to.timeout_ms- The amount of time, in milliseconds, up to which we will wait for connection to complete.- Throws:
IOException- If connecting to the remote endpoint fails or internal java.nio errors occur.
-
disconnect
public void disconnect(byte[] their_node_id)
Disconnects any connections currently open with the peer with the given node id.- Parameters:
their_node_id- must be a valid 33-byte public key
-
bind_listener
public void bind_listener(SocketAddress socket_address) throws IOException
Binds a listening socket to the given address, accepting incoming connections and handling them on the background thread.- Parameters:
socket_address- The address to bind the listening socket to.- Throws:
IOException- if binding the listening socket fail.
-
interrupt
public void interrupt()
Interrupt the background thread, stopping all peer handling. Disconnection events to the PeerHandler are not made, potentially leaving the PeerHandler in an inconsistent state.
-
check_events
public void check_events()
Calls process_events on the PeerManager immediately. Normally process_events is polled regularly to check for new messages which need to be sent, but you can interrupt the poll and check immediately by calling this function.
-
-