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 * Handle an incoming `onion_message` message from the given peer.
43 void handle_onion_message(byte[] peer_node_id, OnionMessage msg);
45 * Returns the next pending onion message for the peer with the given node id.
47 * Note that the return value (or a relevant inner pointer) may be NULL or all-0s to represent None
49 OnionMessage next_onion_message_for_peer(byte[] peer_node_id);
51 * Called when a connection is established with a peer. Can be used to track which peers
52 * advertise onion message support and are online.
54 * May return an `Err(())` if the features the peer supports are not sufficient to communicate
55 * with us. Implementors should be somewhat conservative about doing so, however, as other
56 * message handlers may still wish to communicate with this peer.
58 Result_NoneNoneZ peer_connected(byte[] their_node_id, Init init, boolean inbound);
60 * Indicates a connection to the peer failed/an existing connection was lost. Allows handlers to
61 * drop and refuse to forward onion messages to this peer.
63 void peer_disconnected(byte[] their_node_id);
65 * Gets the node feature flags which this handler itself supports. All available handlers are
66 * queried similarly and their feature flags are OR'd together to form the [`NodeFeatures`]
67 * which are broadcasted in our [`NodeAnnouncement`] message.
69 NodeFeatures provided_node_features();
71 * Gets the init feature flags which should be sent to the given peer. All available handlers
72 * are queried similarly and their feature flags are OR'd together to form the [`InitFeatures`]
73 * which are sent in our [`Init`] message.
75 * Note that this method is called before [`Self::peer_connected`].
77 InitFeatures provided_init_features(byte[] their_node_id);
79 private static class LDKOnionMessageHandlerHolder { OnionMessageHandler held; }
80 public static OnionMessageHandler new_impl(OnionMessageHandlerInterface arg) {
81 final LDKOnionMessageHandlerHolder impl_holder = new LDKOnionMessageHandlerHolder();
82 impl_holder.held = new OnionMessageHandler(new bindings.LDKOnionMessageHandler() {
83 @Override public void handle_onion_message(byte[] peer_node_id, long msg) {
84 org.ldk.structs.OnionMessage msg_hu_conv = null; if (msg < 0 || msg > 4096) { msg_hu_conv = new org.ldk.structs.OnionMessage(null, msg); }
85 arg.handle_onion_message(peer_node_id, msg_hu_conv);
86 Reference.reachabilityFence(arg);
88 @Override public long next_onion_message_for_peer(byte[] peer_node_id) {
89 OnionMessage ret = arg.next_onion_message_for_peer(peer_node_id);
90 Reference.reachabilityFence(arg);
91 long result = ret == null ? 0 : ret.clone_ptr();
94 @Override public long peer_connected(byte[] their_node_id, long init, boolean inbound) {
95 org.ldk.structs.Init init_hu_conv = null; if (init < 0 || init > 4096) { init_hu_conv = new org.ldk.structs.Init(null, init); }
96 Result_NoneNoneZ ret = arg.peer_connected(their_node_id, init_hu_conv, inbound);
97 Reference.reachabilityFence(arg);
98 long result = ret == null ? 0 : ret.clone_ptr();
101 @Override public void peer_disconnected(byte[] their_node_id) {
102 arg.peer_disconnected(their_node_id);
103 Reference.reachabilityFence(arg);
105 @Override public long provided_node_features() {
106 NodeFeatures ret = arg.provided_node_features();
107 Reference.reachabilityFence(arg);
108 long result = ret == null ? 0 : ret.clone_ptr();
111 @Override public long provided_init_features(byte[] their_node_id) {
112 InitFeatures ret = arg.provided_init_features(their_node_id);
113 Reference.reachabilityFence(arg);
114 long result = ret == null ? 0 : ret.clone_ptr();
118 return impl_holder.held;
121 * Handle an incoming `onion_message` message from the given peer.
123 public void handle_onion_message(byte[] peer_node_id, org.ldk.structs.OnionMessage msg) {
124 bindings.OnionMessageHandler_handle_onion_message(this.ptr, InternalUtils.check_arr_len(peer_node_id, 33), msg == null ? 0 : msg.ptr);
125 Reference.reachabilityFence(this);
126 Reference.reachabilityFence(peer_node_id);
127 Reference.reachabilityFence(msg);
128 if (this != null) { this.ptrs_to.add(msg); };
132 * Returns the next pending onion message for the peer with the given node id.
134 * Note that the return value (or a relevant inner pointer) may be NULL or all-0s to represent None
137 public OnionMessage next_onion_message_for_peer(byte[] peer_node_id) {
138 long ret = bindings.OnionMessageHandler_next_onion_message_for_peer(this.ptr, InternalUtils.check_arr_len(peer_node_id, 33));
139 Reference.reachabilityFence(this);
140 Reference.reachabilityFence(peer_node_id);
141 if (ret >= 0 && ret <= 4096) { return null; }
142 org.ldk.structs.OnionMessage ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.OnionMessage(null, ret); }
143 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };
148 * Called when a connection is established with a peer. Can be used to track which peers
149 * advertise onion message support and are online.
151 * May return an `Err(())` if the features the peer supports are not sufficient to communicate
152 * with us. Implementors should be somewhat conservative about doing so, however, as other
153 * message handlers may still wish to communicate with this peer.
155 public Result_NoneNoneZ peer_connected(byte[] their_node_id, org.ldk.structs.Init init, boolean inbound) {
156 long ret = bindings.OnionMessageHandler_peer_connected(this.ptr, InternalUtils.check_arr_len(their_node_id, 33), init == null ? 0 : init.ptr, inbound);
157 Reference.reachabilityFence(this);
158 Reference.reachabilityFence(their_node_id);
159 Reference.reachabilityFence(init);
160 Reference.reachabilityFence(inbound);
161 if (ret >= 0 && ret <= 4096) { return null; }
162 Result_NoneNoneZ ret_hu_conv = Result_NoneNoneZ.constr_from_ptr(ret);
163 if (this != null) { this.ptrs_to.add(init); };
168 * Indicates a connection to the peer failed/an existing connection was lost. Allows handlers to
169 * drop and refuse to forward onion messages to this peer.
171 public void peer_disconnected(byte[] their_node_id) {
172 bindings.OnionMessageHandler_peer_disconnected(this.ptr, InternalUtils.check_arr_len(their_node_id, 33));
173 Reference.reachabilityFence(this);
174 Reference.reachabilityFence(their_node_id);
178 * Gets the node feature flags which this handler itself supports. All available handlers are
179 * queried similarly and their feature flags are OR'd together to form the [`NodeFeatures`]
180 * which are broadcasted in our [`NodeAnnouncement`] message.
182 public NodeFeatures provided_node_features() {
183 long ret = bindings.OnionMessageHandler_provided_node_features(this.ptr);
184 Reference.reachabilityFence(this);
185 if (ret >= 0 && ret <= 4096) { return null; }
186 org.ldk.structs.NodeFeatures ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.NodeFeatures(null, ret); }
187 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };
192 * Gets the init feature flags which should be sent to the given peer. All available handlers
193 * are queried similarly and their feature flags are OR'd together to form the [`InitFeatures`]
194 * which are sent in our [`Init`] message.
196 * Note that this method is called before [`Self::peer_connected`].
198 public InitFeatures provided_init_features(byte[] their_node_id) {
199 long ret = bindings.OnionMessageHandler_provided_init_features(this.ptr, InternalUtils.check_arr_len(their_node_id, 33));
200 Reference.reachabilityFence(this);
201 Reference.reachabilityFence(their_node_id);
202 if (ret >= 0 && ret <= 4096) { return null; }
203 org.ldk.structs.InitFeatures ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.InitFeatures(null, ret); }
204 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };