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