1 package org.ldk.structs;
3 import org.ldk.impl.bindings;
4 import org.ldk.enums.*;
6 import java.util.Arrays;
7 import java.lang.ref.Reference;
8 import javax.annotation.Nullable;
11 * A handler for received [`OnionMessage`]s and for providing generated ones to send.
13 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
14 public class OnionMessageHandler extends CommonBase {
15 final bindings.LDKOnionMessageHandler bindings_instance;
16 OnionMessageHandler(Object _dummy, long ptr) { super(ptr); bindings_instance = null; }
17 private OnionMessageHandler(bindings.LDKOnionMessageHandler arg) {
18 super(bindings.LDKOnionMessageHandler_new(arg));
19 this.ptrs_to.add(arg);
20 this.bindings_instance = arg;
22 @Override @SuppressWarnings("deprecation")
23 protected void finalize() throws Throwable {
24 if (ptr != 0) { bindings.OnionMessageHandler_free(ptr); } super.finalize();
27 * Destroys the object, freeing associated resources. After this call, any access
28 * to this object may result in a SEGFAULT or worse.
30 * You should generally NEVER call this method. You should let the garbage collector
31 * do this for you when it finalizes objects. However, it may be useful for types
32 * which represent locks and should be closed immediately to avoid holding locks
35 public void destroy() {
36 if (ptr != 0) { bindings.OnionMessageHandler_free(ptr); }
39 public static interface OnionMessageHandlerInterface {
41 * Because much of the lightning network does not yet support forwarding onion messages, we
42 * may need to directly connect to a node which will forward a message for us. In such a case,
43 * this method will return the set of nodes which need connection by node_id and the
44 * corresponding socket addresses where they may accept incoming connections.
46 * Thus, this method should be polled regularly to detect messages await such a direct
49 TwoTuple_PublicKeyCVec_SocketAddressZZ[] get_and_clear_connections_needed();
51 * Handle an incoming `onion_message` message from the given peer.
53 void handle_onion_message(byte[] peer_node_id, OnionMessage msg);
55 * Returns the next pending onion message for the peer with the given node id.
57 * Note that the return value (or a relevant inner pointer) may be NULL or all-0s to represent None
59 OnionMessage next_onion_message_for_peer(byte[] peer_node_id);
61 * Called when a connection is established with a peer. Can be used to track which peers
62 * advertise onion message support and are online.
64 * May return an `Err(())` if the features the peer supports are not sufficient to communicate
65 * with us. Implementors should be somewhat conservative about doing so, however, as other
66 * message handlers may still wish to communicate with this peer.
68 Result_NoneNoneZ peer_connected(byte[] their_node_id, Init init, boolean inbound);
70 * Indicates a connection to the peer failed/an existing connection was lost. Allows handlers to
71 * drop and refuse to forward onion messages to this peer.
73 void peer_disconnected(byte[] their_node_id);
75 * Performs actions that should happen roughly every ten seconds after startup. Allows handlers
76 * to drop any buffered onion messages intended for prospective peers.
78 void timer_tick_occurred();
80 * Gets the node feature flags which this handler itself supports. All available handlers are
81 * queried similarly and their feature flags are OR'd together to form the [`NodeFeatures`]
82 * which are broadcasted in our [`NodeAnnouncement`] message.
84 NodeFeatures provided_node_features();
86 * Gets the init feature flags which should be sent to the given peer. All available handlers
87 * are queried similarly and their feature flags are OR'd together to form the [`InitFeatures`]
88 * which are sent in our [`Init`] message.
90 * Note that this method is called before [`Self::peer_connected`].
92 InitFeatures provided_init_features(byte[] their_node_id);
94 private static class LDKOnionMessageHandlerHolder { OnionMessageHandler held; }
95 public static OnionMessageHandler new_impl(OnionMessageHandlerInterface arg) {
96 final LDKOnionMessageHandlerHolder impl_holder = new LDKOnionMessageHandlerHolder();
97 impl_holder.held = new OnionMessageHandler(new bindings.LDKOnionMessageHandler() {
98 @Override public long[] get_and_clear_connections_needed() {
99 TwoTuple_PublicKeyCVec_SocketAddressZZ[] ret = arg.get_and_clear_connections_needed();
100 Reference.reachabilityFence(arg);
101 long[] result = ret != null ? Arrays.stream(ret).mapToLong(ret_conv_40 -> ret_conv_40 == null ? 0 : ret_conv_40.clone_ptr()).toArray() : null;
104 @Override public void handle_onion_message(byte[] peer_node_id, long msg) {
105 org.ldk.structs.OnionMessage msg_hu_conv = null; if (msg < 0 || msg > 4096) { msg_hu_conv = new org.ldk.structs.OnionMessage(null, msg); }
106 arg.handle_onion_message(peer_node_id, msg_hu_conv);
107 Reference.reachabilityFence(arg);
109 @Override public long next_onion_message_for_peer(byte[] peer_node_id) {
110 OnionMessage ret = arg.next_onion_message_for_peer(peer_node_id);
111 Reference.reachabilityFence(arg);
112 long result = ret == null ? 0 : ret.clone_ptr();
115 @Override public long peer_connected(byte[] their_node_id, long init, boolean inbound) {
116 org.ldk.structs.Init init_hu_conv = null; if (init < 0 || init > 4096) { init_hu_conv = new org.ldk.structs.Init(null, init); }
117 Result_NoneNoneZ ret = arg.peer_connected(their_node_id, init_hu_conv, inbound);
118 Reference.reachabilityFence(arg);
119 long result = ret == null ? 0 : ret.clone_ptr();
122 @Override public void peer_disconnected(byte[] their_node_id) {
123 arg.peer_disconnected(their_node_id);
124 Reference.reachabilityFence(arg);
126 @Override public void timer_tick_occurred() {
127 arg.timer_tick_occurred();
128 Reference.reachabilityFence(arg);
130 @Override public long provided_node_features() {
131 NodeFeatures ret = arg.provided_node_features();
132 Reference.reachabilityFence(arg);
133 long result = ret == null ? 0 : ret.clone_ptr();
136 @Override public long provided_init_features(byte[] their_node_id) {
137 InitFeatures ret = arg.provided_init_features(their_node_id);
138 Reference.reachabilityFence(arg);
139 long result = ret == null ? 0 : ret.clone_ptr();
143 return impl_holder.held;
146 * Because much of the lightning network does not yet support forwarding onion messages, we
147 * may need to directly connect to a node which will forward a message for us. In such a case,
148 * this method will return the set of nodes which need connection by node_id and the
149 * corresponding socket addresses where they may accept incoming connections.
151 * Thus, this method should be polled regularly to detect messages await such a direct
154 public TwoTuple_PublicKeyCVec_SocketAddressZZ[] get_and_clear_connections_needed() {
155 long[] ret = bindings.OnionMessageHandler_get_and_clear_connections_needed(this.ptr);
156 Reference.reachabilityFence(this);
157 int ret_conv_40_len = ret.length;
158 TwoTuple_PublicKeyCVec_SocketAddressZZ[] ret_conv_40_arr = new TwoTuple_PublicKeyCVec_SocketAddressZZ[ret_conv_40_len];
159 for (int o = 0; o < ret_conv_40_len; o++) {
160 long ret_conv_40 = ret[o];
161 TwoTuple_PublicKeyCVec_SocketAddressZZ ret_conv_40_hu_conv = new TwoTuple_PublicKeyCVec_SocketAddressZZ(null, ret_conv_40);
162 if (ret_conv_40_hu_conv != null) { ret_conv_40_hu_conv.ptrs_to.add(this); };
163 ret_conv_40_arr[o] = ret_conv_40_hu_conv;
165 return ret_conv_40_arr;
169 * Handle an incoming `onion_message` message from the given peer.
171 public void handle_onion_message(byte[] peer_node_id, org.ldk.structs.OnionMessage msg) {
172 bindings.OnionMessageHandler_handle_onion_message(this.ptr, InternalUtils.check_arr_len(peer_node_id, 33), msg == null ? 0 : msg.ptr);
173 Reference.reachabilityFence(this);
174 Reference.reachabilityFence(peer_node_id);
175 Reference.reachabilityFence(msg);
176 if (this != null) { this.ptrs_to.add(msg); };
180 * Returns the next pending onion message for the peer with the given node id.
182 * Note that the return value (or a relevant inner pointer) may be NULL or all-0s to represent None
185 public OnionMessage next_onion_message_for_peer(byte[] peer_node_id) {
186 long ret = bindings.OnionMessageHandler_next_onion_message_for_peer(this.ptr, InternalUtils.check_arr_len(peer_node_id, 33));
187 Reference.reachabilityFence(this);
188 Reference.reachabilityFence(peer_node_id);
189 if (ret >= 0 && ret <= 4096) { return null; }
190 org.ldk.structs.OnionMessage ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.OnionMessage(null, ret); }
191 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };
196 * Called when a connection is established with a peer. Can be used to track which peers
197 * advertise onion message support and are online.
199 * May return an `Err(())` if the features the peer supports are not sufficient to communicate
200 * with us. Implementors should be somewhat conservative about doing so, however, as other
201 * message handlers may still wish to communicate with this peer.
203 public Result_NoneNoneZ peer_connected(byte[] their_node_id, org.ldk.structs.Init init, boolean inbound) {
204 long ret = bindings.OnionMessageHandler_peer_connected(this.ptr, InternalUtils.check_arr_len(their_node_id, 33), init == null ? 0 : init.ptr, inbound);
205 Reference.reachabilityFence(this);
206 Reference.reachabilityFence(their_node_id);
207 Reference.reachabilityFence(init);
208 Reference.reachabilityFence(inbound);
209 if (ret >= 0 && ret <= 4096) { return null; }
210 Result_NoneNoneZ ret_hu_conv = Result_NoneNoneZ.constr_from_ptr(ret);
211 if (this != null) { this.ptrs_to.add(init); };
216 * Indicates a connection to the peer failed/an existing connection was lost. Allows handlers to
217 * drop and refuse to forward onion messages to this peer.
219 public void peer_disconnected(byte[] their_node_id) {
220 bindings.OnionMessageHandler_peer_disconnected(this.ptr, InternalUtils.check_arr_len(their_node_id, 33));
221 Reference.reachabilityFence(this);
222 Reference.reachabilityFence(their_node_id);
226 * Performs actions that should happen roughly every ten seconds after startup. Allows handlers
227 * to drop any buffered onion messages intended for prospective peers.
229 public void timer_tick_occurred() {
230 bindings.OnionMessageHandler_timer_tick_occurred(this.ptr);
231 Reference.reachabilityFence(this);
235 * Gets the node feature flags which this handler itself supports. All available handlers are
236 * queried similarly and their feature flags are OR'd together to form the [`NodeFeatures`]
237 * which are broadcasted in our [`NodeAnnouncement`] message.
239 public NodeFeatures provided_node_features() {
240 long ret = bindings.OnionMessageHandler_provided_node_features(this.ptr);
241 Reference.reachabilityFence(this);
242 if (ret >= 0 && ret <= 4096) { return null; }
243 org.ldk.structs.NodeFeatures ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.NodeFeatures(null, ret); }
244 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };
249 * Gets the init feature flags which should be sent to the given peer. All available handlers
250 * are queried similarly and their feature flags are OR'd together to form the [`InitFeatures`]
251 * which are sent in our [`Init`] message.
253 * Note that this method is called before [`Self::peer_connected`].
255 public InitFeatures provided_init_features(byte[] their_node_id) {
256 long ret = bindings.OnionMessageHandler_provided_init_features(this.ptr, InternalUtils.check_arr_len(their_node_id, 33));
257 Reference.reachabilityFence(this);
258 Reference.reachabilityFence(their_node_id);
259 if (ret >= 0 && ret <= 4096) { return null; }
260 org.ldk.structs.InitFeatures ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.InitFeatures(null, ret); }
261 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };