[Java] Update auto-generated bindings
[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 and node_id secret key
43          * ephemeral_random_data is used to derive per-connection ephemeral keys and must be
44          * cryptographically secure random bytes.
45          */
46         public static PeerManager of(ChannelMessageHandler message_handler_chan_handler_arg, RoutingMessageHandler message_handler_route_handler_arg, byte[] our_node_secret, byte[] ephemeral_random_data, Logger logger, CustomMessageHandler custom_message_handler) {
47                 long ret = bindings.PeerManager_new(bindings.MessageHandler_new(message_handler_chan_handler_arg == null ? 0 : message_handler_chan_handler_arg.ptr, message_handler_route_handler_arg == null ? 0 : message_handler_route_handler_arg.ptr), InternalUtils.check_arr_len(our_node_secret, 32), InternalUtils.check_arr_len(ephemeral_random_data, 32), logger == null ? 0 : logger.ptr, custom_message_handler == null ? 0 : custom_message_handler.ptr);
48                 Reference.reachabilityFence(message_handler_chan_handler_arg);
49                 Reference.reachabilityFence(message_handler_route_handler_arg);
50                 Reference.reachabilityFence(our_node_secret);
51                 Reference.reachabilityFence(ephemeral_random_data);
52                 Reference.reachabilityFence(logger);
53                 Reference.reachabilityFence(custom_message_handler);
54                 if (ret >= 0 && ret <= 4096) { return null; }
55                 PeerManager ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new PeerManager(null, ret); }
56                 ret_hu_conv.ptrs_to.add(ret_hu_conv);
57                 ret_hu_conv.ptrs_to.add(message_handler_chan_handler_arg);
58                 ret_hu_conv.ptrs_to.add(message_handler_route_handler_arg);
59                 ret_hu_conv.ptrs_to.add(logger);
60                 ret_hu_conv.ptrs_to.add(custom_message_handler);
61                 return ret_hu_conv;
62         }
63
64         /**
65          * Get the list of node ids for peers which have completed the initial handshake.
66          * 
67          * For outbound connections, this will be the same as the their_node_id parameter passed in to
68          * new_outbound_connection, however entries will only appear once the initial handshake has
69          * completed and we are sure the remote peer has the private key for the given node_id.
70          */
71         public byte[][] get_peer_node_ids() {
72                 byte[][] ret = bindings.PeerManager_get_peer_node_ids(this.ptr);
73                 Reference.reachabilityFence(this);
74                 return ret;
75         }
76
77         /**
78          * Indicates a new outbound connection has been established to a node with the given node_id
79          * and an optional remote network address.
80          * 
81          * The remote network address adds the option to report a remote IP address back to a connecting
82          * peer using the init message.
83          * The user should pass the remote network address of the host they are connected to.
84          * 
85          * Note that if an Err is returned here you MUST NOT call socket_disconnected for the new
86          * descriptor but must disconnect the connection immediately.
87          * 
88          * Returns a small number of bytes to send to the remote node (currently always 50).
89          * 
90          * Panics if descriptor is duplicative with some other descriptor which has not yet been
91          * [`socket_disconnected()`].
92          * 
93          * [`socket_disconnected()`]: PeerManager::socket_disconnected
94          */
95         public Result_CVec_u8ZPeerHandleErrorZ new_outbound_connection(byte[] their_node_id, SocketDescriptor descriptor, Option_NetAddressZ remote_network_address) {
96                 long ret = bindings.PeerManager_new_outbound_connection(this.ptr, InternalUtils.check_arr_len(their_node_id, 33), descriptor == null ? 0 : descriptor.ptr, remote_network_address.ptr);
97                 Reference.reachabilityFence(this);
98                 Reference.reachabilityFence(their_node_id);
99                 Reference.reachabilityFence(descriptor);
100                 Reference.reachabilityFence(remote_network_address);
101                 if (ret >= 0 && ret <= 4096) { return null; }
102                 Result_CVec_u8ZPeerHandleErrorZ ret_hu_conv = Result_CVec_u8ZPeerHandleErrorZ.constr_from_ptr(ret);
103                 this.ptrs_to.add(descriptor);
104                 return ret_hu_conv;
105         }
106
107         /**
108          * Indicates a new inbound connection has been established to a node with an optional remote
109          * network address.
110          * 
111          * The remote network address adds the option to report a remote IP address back to a connecting
112          * peer using the init message.
113          * The user should pass the remote network address of the host they are connected to.
114          * 
115          * May refuse the connection by returning an Err, but will never write bytes to the remote end
116          * (outbound connector always speaks first). Note that if an Err is returned here you MUST NOT
117          * call socket_disconnected for the new descriptor but must disconnect the connection
118          * immediately.
119          * 
120          * Panics if descriptor is duplicative with some other descriptor which has not yet been
121          * [`socket_disconnected()`].
122          * 
123          * [`socket_disconnected()`]: PeerManager::socket_disconnected
124          */
125         public Result_NonePeerHandleErrorZ new_inbound_connection(SocketDescriptor descriptor, Option_NetAddressZ remote_network_address) {
126                 long ret = bindings.PeerManager_new_inbound_connection(this.ptr, descriptor == null ? 0 : descriptor.ptr, remote_network_address.ptr);
127                 Reference.reachabilityFence(this);
128                 Reference.reachabilityFence(descriptor);
129                 Reference.reachabilityFence(remote_network_address);
130                 if (ret >= 0 && ret <= 4096) { return null; }
131                 Result_NonePeerHandleErrorZ ret_hu_conv = Result_NonePeerHandleErrorZ.constr_from_ptr(ret);
132                 this.ptrs_to.add(descriptor);
133                 return ret_hu_conv;
134         }
135
136         /**
137          * Indicates that there is room to write data to the given socket descriptor.
138          * 
139          * May return an Err to indicate that the connection should be closed.
140          * 
141          * May call [`send_data`] on the descriptor passed in (or an equal descriptor) before
142          * returning. Thus, be very careful with reentrancy issues! The invariants around calling
143          * [`write_buffer_space_avail`] in case a write did not fully complete must still hold - be
144          * ready to call `[write_buffer_space_avail`] again if a write call generated here isn't
145          * sufficient!
146          * 
147          * [`send_data`]: SocketDescriptor::send_data
148          * [`write_buffer_space_avail`]: PeerManager::write_buffer_space_avail
149          */
150         public Result_NonePeerHandleErrorZ write_buffer_space_avail(SocketDescriptor descriptor) {
151                 long ret = bindings.PeerManager_write_buffer_space_avail(this.ptr, descriptor == null ? 0 : descriptor.ptr);
152                 Reference.reachabilityFence(this);
153                 Reference.reachabilityFence(descriptor);
154                 if (ret >= 0 && ret <= 4096) { return null; }
155                 Result_NonePeerHandleErrorZ ret_hu_conv = Result_NonePeerHandleErrorZ.constr_from_ptr(ret);
156                 return ret_hu_conv;
157         }
158
159         /**
160          * Indicates that data was read from the given socket descriptor.
161          * 
162          * May return an Err to indicate that the connection should be closed.
163          * 
164          * Will *not* call back into [`send_data`] on any descriptors to avoid reentrancy complexity.
165          * Thus, however, you should call [`process_events`] after any `read_event` to generate
166          * [`send_data`] calls to handle responses.
167          * 
168          * If `Ok(true)` is returned, further read_events should not be triggered until a
169          * [`send_data`] call on this descriptor has `resume_read` set (preventing DoS issues in the
170          * send buffer).
171          * 
172          * [`send_data`]: SocketDescriptor::send_data
173          * [`process_events`]: PeerManager::process_events
174          */
175         public Result_boolPeerHandleErrorZ read_event(SocketDescriptor peer_descriptor, byte[] data) {
176                 long ret = bindings.PeerManager_read_event(this.ptr, peer_descriptor == null ? 0 : peer_descriptor.ptr, data);
177                 Reference.reachabilityFence(this);
178                 Reference.reachabilityFence(peer_descriptor);
179                 Reference.reachabilityFence(data);
180                 if (ret >= 0 && ret <= 4096) { return null; }
181                 Result_boolPeerHandleErrorZ ret_hu_conv = Result_boolPeerHandleErrorZ.constr_from_ptr(ret);
182                 return ret_hu_conv;
183         }
184
185         /**
186          * Checks for any events generated by our handlers and processes them. Includes sending most
187          * response messages as well as messages generated by calls to handler functions directly (eg
188          * functions like [`ChannelManager::process_pending_htlc_forwards`] or [`send_payment`]).
189          * 
190          * May call [`send_data`] on [`SocketDescriptor`]s. Thus, be very careful with reentrancy
191          * issues!
192          * 
193          * You don't have to call this function explicitly if you are using [`lightning-net-tokio`]
194          * or one of the other clients provided in our language bindings.
195          * 
196          * [`send_payment`]: crate::ln::channelmanager::ChannelManager::send_payment
197          * [`ChannelManager::process_pending_htlc_forwards`]: crate::ln::channelmanager::ChannelManager::process_pending_htlc_forwards
198          * [`send_data`]: SocketDescriptor::send_data
199          */
200         public void process_events() {
201                 bindings.PeerManager_process_events(this.ptr);
202                 Reference.reachabilityFence(this);
203         }
204
205         /**
206          * Indicates that the given socket descriptor's connection is now closed.
207          */
208         public void socket_disconnected(SocketDescriptor descriptor) {
209                 bindings.PeerManager_socket_disconnected(this.ptr, descriptor == null ? 0 : descriptor.ptr);
210                 Reference.reachabilityFence(this);
211                 Reference.reachabilityFence(descriptor);
212         }
213
214         /**
215          * Disconnect a peer given its node id.
216          * 
217          * Set `no_connection_possible` to true to prevent any further connection with this peer,
218          * force-closing any channels we have with it.
219          * 
220          * If a peer is connected, this will call [`disconnect_socket`] on the descriptor for the
221          * peer. Thus, be very careful about reentrancy issues.
222          * 
223          * [`disconnect_socket`]: SocketDescriptor::disconnect_socket
224          */
225         public void disconnect_by_node_id(byte[] node_id, boolean no_connection_possible) {
226                 bindings.PeerManager_disconnect_by_node_id(this.ptr, InternalUtils.check_arr_len(node_id, 33), no_connection_possible);
227                 Reference.reachabilityFence(this);
228                 Reference.reachabilityFence(node_id);
229                 Reference.reachabilityFence(no_connection_possible);
230         }
231
232         /**
233          * Disconnects all currently-connected peers. This is useful on platforms where there may be
234          * an indication that TCP sockets have stalled even if we weren't around to time them out
235          * using regular ping/pongs.
236          */
237         public void disconnect_all_peers() {
238                 bindings.PeerManager_disconnect_all_peers(this.ptr);
239                 Reference.reachabilityFence(this);
240         }
241
242         /**
243          * Send pings to each peer and disconnect those which did not respond to the last round of
244          * pings.
245          * 
246          * This may be called on any timescale you want, however, roughly once every ten seconds is
247          * preferred. The call rate determines both how often we send a ping to our peers and how much
248          * time they have to respond before we disconnect them.
249          * 
250          * May call [`send_data`] on all [`SocketDescriptor`]s. Thus, be very careful with reentrancy
251          * issues!
252          * 
253          * [`send_data`]: SocketDescriptor::send_data
254          */
255         public void timer_tick_occurred() {
256                 bindings.PeerManager_timer_tick_occurred(this.ptr);
257                 Reference.reachabilityFence(this);
258         }
259
260 }