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 javax.annotation.Nullable;
8
9
10 /**
11  * A PeerManager manages a set of peers, described by their [`SocketDescriptor`] and marshalls
12  * socket events into messages which it passes on to its [`MessageHandler`].
13  * 
14  * Locks are taken internally, so you must never assume that reentrancy from a
15  * [`SocketDescriptor`] call back into [`PeerManager`] methods will not deadlock.
16  * 
17  * Calls to [`read_event`] will decode relevant messages and pass them to the
18  * [`ChannelMessageHandler`], likely doing message processing in-line. Thus, the primary form of
19  * parallelism in Rust-Lightning is in calls to [`read_event`]. Note, however, that calls to any
20  * [`PeerManager`] functions related to the same connection must occur only in serial, making new
21  * calls only after previous ones have returned.
22  * 
23  * Rather than using a plain PeerManager, it is preferable to use either a SimpleArcPeerManager
24  * a SimpleRefPeerManager, for conciseness. See their documentation for more details, but
25  * essentially you should default to using a SimpleRefPeerManager, and use a
26  * SimpleArcPeerManager when you require a PeerManager with a static lifetime, such as when
27  * you're using lightning-net-tokio.
28  * 
29  * [`read_event`]: PeerManager::read_event
30  */
31 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
32 public class PeerManager extends CommonBase {
33         PeerManager(Object _dummy, long ptr) { super(ptr); }
34         @Override @SuppressWarnings("deprecation")
35         protected void finalize() throws Throwable {
36                 super.finalize();
37                 if (ptr != 0) { bindings.PeerManager_free(ptr); }
38         }
39
40         /**
41          * Constructs a new PeerManager with the given message handlers and node_id secret key
42          * ephemeral_random_data is used to derive per-connection ephemeral keys and must be
43          * cryptographically secure random bytes.
44          */
45         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) {
46                 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), our_node_secret, ephemeral_random_data, logger == null ? 0 : logger.ptr, custom_message_handler == null ? 0 : custom_message_handler.ptr);
47                 if (ret >= 0 && ret < 1024) { return null; }
48                 PeerManager ret_hu_conv = new PeerManager(null, ret);
49                 ret_hu_conv.ptrs_to.add(ret_hu_conv);
50                 ret_hu_conv.ptrs_to.add(message_handler_chan_handler_arg);
51                 ret_hu_conv.ptrs_to.add(message_handler_route_handler_arg);
52                 ret_hu_conv.ptrs_to.add(logger);
53                 ret_hu_conv.ptrs_to.add(custom_message_handler);
54                 return ret_hu_conv;
55         }
56
57         /**
58          * Get the list of node ids for peers which have completed the initial handshake.
59          * 
60          * For outbound connections, this will be the same as the their_node_id parameter passed in to
61          * new_outbound_connection, however entries will only appear once the initial handshake has
62          * completed and we are sure the remote peer has the private key for the given node_id.
63          */
64         public byte[][] get_peer_node_ids() {
65                 byte[][] ret = bindings.PeerManager_get_peer_node_ids(this.ptr);
66                 return ret;
67         }
68
69         /**
70          * Indicates a new outbound connection has been established to a node with the given node_id.
71          * Note that if an Err is returned here you MUST NOT call socket_disconnected for the new
72          * descriptor but must disconnect the connection immediately.
73          * 
74          * Returns a small number of bytes to send to the remote node (currently always 50).
75          * 
76          * Panics if descriptor is duplicative with some other descriptor which has not yet been
77          * [`socket_disconnected()`].
78          * 
79          * [`socket_disconnected()`]: PeerManager::socket_disconnected
80          */
81         public Result_CVec_u8ZPeerHandleErrorZ new_outbound_connection(byte[] their_node_id, SocketDescriptor descriptor) {
82                 long ret = bindings.PeerManager_new_outbound_connection(this.ptr, their_node_id, descriptor == null ? 0 : descriptor.ptr);
83                 if (ret >= 0 && ret < 1024) { return null; }
84                 Result_CVec_u8ZPeerHandleErrorZ ret_hu_conv = Result_CVec_u8ZPeerHandleErrorZ.constr_from_ptr(ret);
85                 this.ptrs_to.add(descriptor);
86                 return ret_hu_conv;
87         }
88
89         /**
90          * Indicates a new inbound connection has been established.
91          * 
92          * May refuse the connection by returning an Err, but will never write bytes to the remote end
93          * (outbound connector always speaks first). Note that if an Err is returned here you MUST NOT
94          * call socket_disconnected for the new descriptor but must disconnect the connection
95          * immediately.
96          * 
97          * Panics if descriptor is duplicative with some other descriptor which has not yet been
98          * [`socket_disconnected()`].
99          * 
100          * [`socket_disconnected()`]: PeerManager::socket_disconnected
101          */
102         public Result_NonePeerHandleErrorZ new_inbound_connection(SocketDescriptor descriptor) {
103                 long ret = bindings.PeerManager_new_inbound_connection(this.ptr, descriptor == null ? 0 : descriptor.ptr);
104                 if (ret >= 0 && ret < 1024) { return null; }
105                 Result_NonePeerHandleErrorZ ret_hu_conv = Result_NonePeerHandleErrorZ.constr_from_ptr(ret);
106                 this.ptrs_to.add(descriptor);
107                 return ret_hu_conv;
108         }
109
110         /**
111          * Indicates that there is room to write data to the given socket descriptor.
112          * 
113          * May return an Err to indicate that the connection should be closed.
114          * 
115          * May call [`send_data`] on the descriptor passed in (or an equal descriptor) before
116          * returning. Thus, be very careful with reentrancy issues! The invariants around calling
117          * [`write_buffer_space_avail`] in case a write did not fully complete must still hold - be
118          * ready to call `[write_buffer_space_avail`] again if a write call generated here isn't
119          * sufficient!
120          * 
121          * [`send_data`]: SocketDescriptor::send_data
122          * [`write_buffer_space_avail`]: PeerManager::write_buffer_space_avail
123          */
124         public Result_NonePeerHandleErrorZ write_buffer_space_avail(SocketDescriptor descriptor) {
125                 long ret = bindings.PeerManager_write_buffer_space_avail(this.ptr, descriptor == null ? 0 : descriptor.ptr);
126                 if (ret >= 0 && ret < 1024) { return null; }
127                 Result_NonePeerHandleErrorZ ret_hu_conv = Result_NonePeerHandleErrorZ.constr_from_ptr(ret);
128                 return ret_hu_conv;
129         }
130
131         /**
132          * Indicates that data was read from the given socket descriptor.
133          * 
134          * May return an Err to indicate that the connection should be closed.
135          * 
136          * Will *not* call back into [`send_data`] on any descriptors to avoid reentrancy complexity.
137          * Thus, however, you should call [`process_events`] after any `read_event` to generate
138          * [`send_data`] calls to handle responses.
139          * 
140          * If `Ok(true)` is returned, further read_events should not be triggered until a
141          * [`send_data`] call on this descriptor has `resume_read` set (preventing DoS issues in the
142          * send buffer).
143          * 
144          * [`send_data`]: SocketDescriptor::send_data
145          * [`process_events`]: PeerManager::process_events
146          */
147         public Result_boolPeerHandleErrorZ read_event(SocketDescriptor peer_descriptor, byte[] data) {
148                 long ret = bindings.PeerManager_read_event(this.ptr, peer_descriptor == null ? 0 : peer_descriptor.ptr, data);
149                 if (ret >= 0 && ret < 1024) { return null; }
150                 Result_boolPeerHandleErrorZ ret_hu_conv = Result_boolPeerHandleErrorZ.constr_from_ptr(ret);
151                 return ret_hu_conv;
152         }
153
154         /**
155          * Checks for any events generated by our handlers and processes them. Includes sending most
156          * response messages as well as messages generated by calls to handler functions directly (eg
157          * functions like [`ChannelManager::process_pending_htlc_forwards`] or [`send_payment`]).
158          * 
159          * May call [`send_data`] on [`SocketDescriptor`]s. Thus, be very careful with reentrancy
160          * issues!
161          * 
162          * You don't have to call this function explicitly if you are using [`lightning-net-tokio`]
163          * or one of the other clients provided in our language bindings.
164          * 
165          * [`send_payment`]: crate::ln::channelmanager::ChannelManager::send_payment
166          * [`ChannelManager::process_pending_htlc_forwards`]: crate::ln::channelmanager::ChannelManager::process_pending_htlc_forwards
167          * [`send_data`]: SocketDescriptor::send_data
168          */
169         public void process_events() {
170                 bindings.PeerManager_process_events(this.ptr);
171         }
172
173         /**
174          * Indicates that the given socket descriptor's connection is now closed.
175          */
176         public void socket_disconnected(SocketDescriptor descriptor) {
177                 bindings.PeerManager_socket_disconnected(this.ptr, descriptor == null ? 0 : descriptor.ptr);
178         }
179
180         /**
181          * Disconnect a peer given its node id.
182          * 
183          * Set `no_connection_possible` to true to prevent any further connection with this peer,
184          * force-closing any channels we have with it.
185          * 
186          * If a peer is connected, this will call [`disconnect_socket`] on the descriptor for the
187          * peer. Thus, be very careful about reentrancy issues.
188          * 
189          * [`disconnect_socket`]: SocketDescriptor::disconnect_socket
190          */
191         public void disconnect_by_node_id(byte[] node_id, boolean no_connection_possible) {
192                 bindings.PeerManager_disconnect_by_node_id(this.ptr, node_id, no_connection_possible);
193         }
194
195         /**
196          * Send pings to each peer and disconnect those which did not respond to the last round of
197          * pings.
198          * 
199          * This may be called on any timescale you want, however, roughly once every five to ten
200          * seconds is preferred. The call rate determines both how often we send a ping to our peers
201          * and how much time they have to respond before we disconnect them.
202          * 
203          * May call [`send_data`] on all [`SocketDescriptor`]s. Thus, be very careful with reentrancy
204          * issues!
205          * 
206          * [`send_data`]: SocketDescriptor::send_data
207          */
208         public void timer_tick_occurred() {
209                 bindings.PeerManager_timer_tick_occurred(this.ptr);
210         }
211
212 }