]> git.bitcoin.ninja Git - ldk-java/blob - src/main/java/org/ldk/structs/PeerManager.java
[Java] Update auto-generated bindings to LDK 0.0.123
[ldk-java] / src / main / java / org / ldk / structs / PeerManager.java
1 package org.ldk.structs;
2
3 import org.ldk.impl.bindings;
4 import org.ldk.enums.*;
5 import org.ldk.util.*;
6 import java.util.Arrays;
7 import java.lang.ref.Reference;
8 import javax.annotation.Nullable;
9
10
11 /**
12  * A PeerManager manages a set of peers, described by their [`SocketDescriptor`] and marshalls
13  * socket events into messages which it passes on to its [`MessageHandler`].
14  * 
15  * Locks are taken internally, so you must never assume that reentrancy from a
16  * [`SocketDescriptor`] call back into [`PeerManager`] methods will not deadlock.
17  * 
18  * Calls to [`read_event`] will decode relevant messages and pass them to the
19  * [`ChannelMessageHandler`], likely doing message processing in-line. Thus, the primary form of
20  * parallelism in Rust-Lightning is in calls to [`read_event`]. Note, however, that calls to any
21  * [`PeerManager`] functions related to the same connection must occur only in serial, making new
22  * calls only after previous ones have returned.
23  * 
24  * Rather than using a plain [`PeerManager`], it is preferable to use either a [`SimpleArcPeerManager`]
25  * a [`SimpleRefPeerManager`], for conciseness. See their documentation for more details, but
26  * essentially you should default to using a [`SimpleRefPeerManager`], and use a
27  * [`SimpleArcPeerManager`] when you require a `PeerManager` with a static lifetime, such as when
28  * you're using lightning-net-tokio.
29  * 
30  * [`read_event`]: PeerManager::read_event
31  */
32 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
33 public class PeerManager extends CommonBase {
34         PeerManager(Object _dummy, long ptr) { super(ptr); }
35         @Override @SuppressWarnings("deprecation")
36         protected void finalize() throws Throwable {
37                 super.finalize();
38                 if (ptr != 0) { bindings.PeerManager_free(ptr); }
39         }
40
41         /**
42          * Constructs a new `PeerManager` with the given message handlers.
43          * 
44          * `ephemeral_random_data` is used to derive per-connection ephemeral keys and must be
45          * cryptographically secure random bytes.
46          * 
47          * `current_time` is used as an always-increasing counter that survives across restarts and is
48          * incremented irregularly internally. In general it is best to simply use the current UNIX
49          * timestamp, however if it is not available a persistent counter that increases once per
50          * minute should suffice.
51          */
52         public static PeerManager of(ChannelMessageHandler message_handler_chan_handler_arg, RoutingMessageHandler message_handler_route_handler_arg, OnionMessageHandler message_handler_onion_message_handler_arg, CustomMessageHandler message_handler_custom_message_handler_arg, int current_time, byte[] ephemeral_random_data, org.ldk.structs.Logger logger, org.ldk.structs.NodeSigner node_signer) {
53                 long ret = bindings.PeerManager_new(bindings.MessageHandler_new(message_handler_chan_handler_arg.ptr, message_handler_route_handler_arg.ptr, message_handler_onion_message_handler_arg.ptr, message_handler_custom_message_handler_arg.ptr), current_time, InternalUtils.check_arr_len(ephemeral_random_data, 32), logger.ptr, node_signer.ptr);
54                 Reference.reachabilityFence(message_handler_chan_handler_arg);
55                 Reference.reachabilityFence(message_handler_route_handler_arg);
56                 Reference.reachabilityFence(message_handler_onion_message_handler_arg);
57                 Reference.reachabilityFence(message_handler_custom_message_handler_arg);
58                 Reference.reachabilityFence(current_time);
59                 Reference.reachabilityFence(ephemeral_random_data);
60                 Reference.reachabilityFence(logger);
61                 Reference.reachabilityFence(node_signer);
62                 if (ret >= 0 && ret <= 4096) { return null; }
63                 org.ldk.structs.PeerManager ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.PeerManager(null, ret); }
64                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(ret_hu_conv); };
65                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(message_handler_chan_handler_arg); };
66                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(message_handler_route_handler_arg); };
67                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(message_handler_onion_message_handler_arg); };
68                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(message_handler_custom_message_handler_arg); };
69                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(logger); };
70                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(node_signer); };
71                 return ret_hu_conv;
72         }
73
74         /**
75          * Returns a list of [`PeerDetails`] for connected peers that have completed the initial
76          * handshake.
77          */
78         public PeerDetails[] list_peers() {
79                 long[] ret = bindings.PeerManager_list_peers(this.ptr);
80                 Reference.reachabilityFence(this);
81                 int ret_conv_13_len = ret.length;
82                 PeerDetails[] ret_conv_13_arr = new PeerDetails[ret_conv_13_len];
83                 for (int n = 0; n < ret_conv_13_len; n++) {
84                         long ret_conv_13 = ret[n];
85                         org.ldk.structs.PeerDetails ret_conv_13_hu_conv = null; if (ret_conv_13 < 0 || ret_conv_13 > 4096) { ret_conv_13_hu_conv = new org.ldk.structs.PeerDetails(null, ret_conv_13); }
86                         if (ret_conv_13_hu_conv != null) { ret_conv_13_hu_conv.ptrs_to.add(this); };
87                         ret_conv_13_arr[n] = ret_conv_13_hu_conv;
88                 }
89                 return ret_conv_13_arr;
90         }
91
92         /**
93          * Returns the [`PeerDetails`] of a connected peer that has completed the initial handshake.
94          * 
95          * Will return `None` if the peer is unknown or it hasn't completed the initial handshake.
96          * 
97          * Note that the return value (or a relevant inner pointer) may be NULL or all-0s to represent None
98          */
99         @Nullable
100         public PeerDetails peer_by_node_id(byte[] their_node_id) {
101                 long ret = bindings.PeerManager_peer_by_node_id(this.ptr, InternalUtils.check_arr_len(their_node_id, 33));
102                 Reference.reachabilityFence(this);
103                 Reference.reachabilityFence(their_node_id);
104                 if (ret >= 0 && ret <= 4096) { return null; }
105                 org.ldk.structs.PeerDetails ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.PeerDetails(null, ret); }
106                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };
107                 return ret_hu_conv;
108         }
109
110         /**
111          * Indicates a new outbound connection has been established to a node with the given `node_id`
112          * and an optional remote network address.
113          * 
114          * The remote network address adds the option to report a remote IP address back to a connecting
115          * peer using the init message.
116          * The user should pass the remote network address of the host they are connected to.
117          * 
118          * If an `Err` is returned here you must disconnect the connection immediately.
119          * 
120          * Returns a small number of bytes to send to the remote node (currently always 50).
121          * 
122          * Panics if descriptor is duplicative with some other descriptor which has not yet been
123          * [`socket_disconnected`].
124          * 
125          * [`socket_disconnected`]: PeerManager::socket_disconnected
126          */
127         public Result_CVec_u8ZPeerHandleErrorZ new_outbound_connection(byte[] their_node_id, org.ldk.structs.SocketDescriptor descriptor, org.ldk.structs.Option_SocketAddressZ remote_network_address) {
128                 long ret = bindings.PeerManager_new_outbound_connection(this.ptr, InternalUtils.check_arr_len(their_node_id, 33), descriptor.ptr, remote_network_address.ptr);
129                 Reference.reachabilityFence(this);
130                 Reference.reachabilityFence(their_node_id);
131                 Reference.reachabilityFence(descriptor);
132                 Reference.reachabilityFence(remote_network_address);
133                 if (ret >= 0 && ret <= 4096) { return null; }
134                 Result_CVec_u8ZPeerHandleErrorZ ret_hu_conv = Result_CVec_u8ZPeerHandleErrorZ.constr_from_ptr(ret);
135                 if (this != null) { this.ptrs_to.add(descriptor); };
136                 if (this != null) { this.ptrs_to.add(remote_network_address); };
137                 return ret_hu_conv;
138         }
139
140         /**
141          * Indicates a new inbound connection has been established to a node with an optional remote
142          * network address.
143          * 
144          * The remote network address adds the option to report a remote IP address back to a connecting
145          * peer using the init message.
146          * The user should pass the remote network address of the host they are connected to.
147          * 
148          * May refuse the connection by returning an Err, but will never write bytes to the remote end
149          * (outbound connector always speaks first). If an `Err` is returned here you must disconnect
150          * the connection immediately.
151          * 
152          * Panics if descriptor is duplicative with some other descriptor which has not yet been
153          * [`socket_disconnected`].
154          * 
155          * [`socket_disconnected`]: PeerManager::socket_disconnected
156          */
157         public Result_NonePeerHandleErrorZ new_inbound_connection(org.ldk.structs.SocketDescriptor descriptor, org.ldk.structs.Option_SocketAddressZ remote_network_address) {
158                 long ret = bindings.PeerManager_new_inbound_connection(this.ptr, descriptor.ptr, remote_network_address.ptr);
159                 Reference.reachabilityFence(this);
160                 Reference.reachabilityFence(descriptor);
161                 Reference.reachabilityFence(remote_network_address);
162                 if (ret >= 0 && ret <= 4096) { return null; }
163                 Result_NonePeerHandleErrorZ ret_hu_conv = Result_NonePeerHandleErrorZ.constr_from_ptr(ret);
164                 if (this != null) { this.ptrs_to.add(descriptor); };
165                 if (this != null) { this.ptrs_to.add(remote_network_address); };
166                 return ret_hu_conv;
167         }
168
169         /**
170          * Indicates that there is room to write data to the given socket descriptor.
171          * 
172          * May return an Err to indicate that the connection should be closed.
173          * 
174          * May call [`send_data`] on the descriptor passed in (or an equal descriptor) before
175          * returning. Thus, be very careful with reentrancy issues! The invariants around calling
176          * [`write_buffer_space_avail`] in case a write did not fully complete must still hold - be
177          * ready to call [`write_buffer_space_avail`] again if a write call generated here isn't
178          * sufficient!
179          * 
180          * [`send_data`]: SocketDescriptor::send_data
181          * [`write_buffer_space_avail`]: PeerManager::write_buffer_space_avail
182          */
183         public Result_NonePeerHandleErrorZ write_buffer_space_avail(org.ldk.structs.SocketDescriptor descriptor) {
184                 long ret = bindings.PeerManager_write_buffer_space_avail(this.ptr, descriptor.ptr);
185                 Reference.reachabilityFence(this);
186                 Reference.reachabilityFence(descriptor);
187                 if (ret >= 0 && ret <= 4096) { return null; }
188                 Result_NonePeerHandleErrorZ ret_hu_conv = Result_NonePeerHandleErrorZ.constr_from_ptr(ret);
189                 return ret_hu_conv;
190         }
191
192         /**
193          * Indicates that data was read from the given socket descriptor.
194          * 
195          * May return an Err to indicate that the connection should be closed.
196          * 
197          * Will *not* call back into [`send_data`] on any descriptors to avoid reentrancy complexity.
198          * Thus, however, you should call [`process_events`] after any `read_event` to generate
199          * [`send_data`] calls to handle responses.
200          * 
201          * If `Ok(true)` is returned, further read_events should not be triggered until a
202          * [`send_data`] call on this descriptor has `resume_read` set (preventing DoS issues in the
203          * send buffer).
204          * 
205          * In order to avoid processing too many messages at once per peer, `data` should be on the
206          * order of 4KiB.
207          * 
208          * [`send_data`]: SocketDescriptor::send_data
209          * [`process_events`]: PeerManager::process_events
210          */
211         public Result_boolPeerHandleErrorZ read_event(org.ldk.structs.SocketDescriptor peer_descriptor, byte[] data) {
212                 long ret = bindings.PeerManager_read_event(this.ptr, peer_descriptor.ptr, data);
213                 Reference.reachabilityFence(this);
214                 Reference.reachabilityFence(peer_descriptor);
215                 Reference.reachabilityFence(data);
216                 if (ret >= 0 && ret <= 4096) { return null; }
217                 Result_boolPeerHandleErrorZ ret_hu_conv = Result_boolPeerHandleErrorZ.constr_from_ptr(ret);
218                 return ret_hu_conv;
219         }
220
221         /**
222          * Checks for any events generated by our handlers and processes them. Includes sending most
223          * response messages as well as messages generated by calls to handler functions directly (eg
224          * functions like [`ChannelManager::process_pending_htlc_forwards`] or [`send_payment`]).
225          * 
226          * May call [`send_data`] on [`SocketDescriptor`]s. Thus, be very careful with reentrancy
227          * issues!
228          * 
229          * You don't have to call this function explicitly if you are using [`lightning-net-tokio`]
230          * or one of the other clients provided in our language bindings.
231          * 
232          * Note that if there are any other calls to this function waiting on lock(s) this may return
233          * without doing any work. All available events that need handling will be handled before the
234          * other calls return.
235          * 
236          * [`send_payment`]: crate::ln::channelmanager::ChannelManager::send_payment
237          * [`ChannelManager::process_pending_htlc_forwards`]: crate::ln::channelmanager::ChannelManager::process_pending_htlc_forwards
238          * [`send_data`]: SocketDescriptor::send_data
239          */
240         public void process_events() {
241                 bindings.PeerManager_process_events(this.ptr);
242                 Reference.reachabilityFence(this);
243         }
244
245         /**
246          * Indicates that the given socket descriptor's connection is now closed.
247          */
248         public void socket_disconnected(org.ldk.structs.SocketDescriptor descriptor) {
249                 bindings.PeerManager_socket_disconnected(this.ptr, descriptor.ptr);
250                 Reference.reachabilityFence(this);
251                 Reference.reachabilityFence(descriptor);
252         }
253
254         /**
255          * Disconnect a peer given its node id.
256          * 
257          * If a peer is connected, this will call [`disconnect_socket`] on the descriptor for the
258          * peer. Thus, be very careful about reentrancy issues.
259          * 
260          * [`disconnect_socket`]: SocketDescriptor::disconnect_socket
261          */
262         public void disconnect_by_node_id(byte[] node_id) {
263                 bindings.PeerManager_disconnect_by_node_id(this.ptr, InternalUtils.check_arr_len(node_id, 33));
264                 Reference.reachabilityFence(this);
265                 Reference.reachabilityFence(node_id);
266         }
267
268         /**
269          * Disconnects all currently-connected peers. This is useful on platforms where there may be
270          * an indication that TCP sockets have stalled even if we weren't around to time them out
271          * using regular ping/pongs.
272          */
273         public void disconnect_all_peers() {
274                 bindings.PeerManager_disconnect_all_peers(this.ptr);
275                 Reference.reachabilityFence(this);
276         }
277
278         /**
279          * Send pings to each peer and disconnect those which did not respond to the last round of
280          * pings.
281          * 
282          * This may be called on any timescale you want, however, roughly once every ten seconds is
283          * preferred. The call rate determines both how often we send a ping to our peers and how much
284          * time they have to respond before we disconnect them.
285          * 
286          * May call [`send_data`] on all [`SocketDescriptor`]s. Thus, be very careful with reentrancy
287          * issues!
288          * 
289          * [`send_data`]: SocketDescriptor::send_data
290          */
291         public void timer_tick_occurred() {
292                 bindings.PeerManager_timer_tick_occurred(this.ptr);
293                 Reference.reachabilityFence(this);
294         }
295
296         /**
297          * Generates a signed node_announcement from the given arguments, sending it to all connected
298          * peers. Note that peers will likely ignore this message unless we have at least one public
299          * channel which has at least six confirmations on-chain.
300          * 
301          * `rgb` is a node \"color\" and `alias` is a printable human-readable string to describe this
302          * node to humans. They carry no in-protocol meaning.
303          * 
304          * `addresses` represent the set (possibly empty) of socket addresses on which this node
305          * accepts incoming connections. These will be included in the node_announcement, publicly
306          * tying these addresses together and to this node. If you wish to preserve user privacy,
307          * addresses should likely contain only Tor Onion addresses.
308          * 
309          * Panics if `addresses` is absurdly large (more than 100).
310          * 
311          * [`get_and_clear_pending_msg_events`]: MessageSendEventsProvider::get_and_clear_pending_msg_events
312          */
313         public void broadcast_node_announcement(byte[] rgb, byte[] alias, SocketAddress[] addresses) {
314                 bindings.PeerManager_broadcast_node_announcement(this.ptr, InternalUtils.check_arr_len(rgb, 3), InternalUtils.check_arr_len(alias, 32), addresses != null ? Arrays.stream(addresses).mapToLong(addresses_conv_15 -> addresses_conv_15.ptr).toArray() : null);
315                 Reference.reachabilityFence(this);
316                 Reference.reachabilityFence(rgb);
317                 Reference.reachabilityFence(alias);
318                 Reference.reachabilityFence(addresses);
319                 for (SocketAddress addresses_conv_15: addresses) { if (this != null) { this.ptrs_to.add(addresses_conv_15); }; };
320         }
321
322 }