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 trait to describe an object that can receive onion messages.
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, bindings.LDKOnionMessageProvider OnionMessageProvider) {
18 super(bindings.LDKOnionMessageHandler_new(arg, OnionMessageProvider));
19 this.ptrs_to.add(arg);
20 this.ptrs_to.add(OnionMessageProvider);
21 this.bindings_instance = arg;
23 @Override @SuppressWarnings("deprecation")
24 protected void finalize() throws Throwable {
25 if (ptr != 0) { bindings.OnionMessageHandler_free(ptr); } super.finalize();
28 * Destroys the object, freeing associated resources. After this call, any access
29 * to this object may result in a SEGFAULT or worse.
31 * You should generally NEVER call this method. You should let the garbage collector
32 * do this for you when it finalizes objects. However, it may be useful for types
33 * which represent locks and should be closed immediately to avoid holding locks
36 public void destroy() {
37 if (ptr != 0) { bindings.OnionMessageHandler_free(ptr); }
40 public static interface OnionMessageHandlerInterface {
42 * Handle an incoming `onion_message` message from the given peer.
44 void handle_onion_message(byte[] peer_node_id, OnionMessage msg);
46 * Called when a connection is established with a peer. Can be used to track which peers
47 * advertise onion message support and are online.
49 * May return an `Err(())` if the features the peer supports are not sufficient to communicate
50 * with us. Implementors should be somewhat conservative about doing so, however, as other
51 * message handlers may still wish to communicate with this peer.
53 Result_NoneNoneZ peer_connected(byte[] their_node_id, Init init, boolean inbound);
55 * Indicates a connection to the peer failed/an existing connection was lost. Allows handlers to
56 * drop and refuse to forward onion messages to this peer.
58 void peer_disconnected(byte[] their_node_id);
60 * Gets the node feature flags which this handler itself supports. All available handlers are
61 * queried similarly and their feature flags are OR'd together to form the [`NodeFeatures`]
62 * which are broadcasted in our [`NodeAnnouncement`] message.
64 NodeFeatures provided_node_features();
66 * Gets the init feature flags which should be sent to the given peer. All available handlers
67 * are queried similarly and their feature flags are OR'd together to form the [`InitFeatures`]
68 * which are sent in our [`Init`] message.
70 * Note that this method is called before [`Self::peer_connected`].
72 InitFeatures provided_init_features(byte[] their_node_id);
74 private static class LDKOnionMessageHandlerHolder { OnionMessageHandler held; }
75 public static OnionMessageHandler new_impl(OnionMessageHandlerInterface arg, OnionMessageProvider.OnionMessageProviderInterface OnionMessageProvider_impl) {
76 final LDKOnionMessageHandlerHolder impl_holder = new LDKOnionMessageHandlerHolder();
77 impl_holder.held = new OnionMessageHandler(new bindings.LDKOnionMessageHandler() {
78 @Override public void handle_onion_message(byte[] peer_node_id, long msg) {
79 org.ldk.structs.OnionMessage msg_hu_conv = null; if (msg < 0 || msg > 4096) { msg_hu_conv = new org.ldk.structs.OnionMessage(null, msg); }
80 arg.handle_onion_message(peer_node_id, msg_hu_conv);
81 Reference.reachabilityFence(arg);
83 @Override public long peer_connected(byte[] their_node_id, long init, boolean inbound) {
84 org.ldk.structs.Init init_hu_conv = null; if (init < 0 || init > 4096) { init_hu_conv = new org.ldk.structs.Init(null, init); }
85 Result_NoneNoneZ ret = arg.peer_connected(their_node_id, init_hu_conv, inbound);
86 Reference.reachabilityFence(arg);
87 long result = ret == null ? 0 : ret.clone_ptr();
90 @Override public void peer_disconnected(byte[] their_node_id) {
91 arg.peer_disconnected(their_node_id);
92 Reference.reachabilityFence(arg);
94 @Override public long provided_node_features() {
95 NodeFeatures ret = arg.provided_node_features();
96 Reference.reachabilityFence(arg);
97 long result = ret == null ? 0 : ret.clone_ptr();
100 @Override public long provided_init_features(byte[] their_node_id) {
101 InitFeatures ret = arg.provided_init_features(their_node_id);
102 Reference.reachabilityFence(arg);
103 long result = ret == null ? 0 : ret.clone_ptr();
106 }, OnionMessageProvider.new_impl(OnionMessageProvider_impl).bindings_instance);
107 return impl_holder.held;
111 * Gets the underlying OnionMessageProvider.
113 public OnionMessageProvider get_onion_message_provider() {
114 OnionMessageProvider res = new OnionMessageProvider(null, bindings.LDKOnionMessageHandler_get_OnionMessageProvider(this.ptr));
115 res.ptrs_to.add(this);
120 * Handle an incoming `onion_message` message from the given peer.
122 public void handle_onion_message(byte[] peer_node_id, org.ldk.structs.OnionMessage msg) {
123 bindings.OnionMessageHandler_handle_onion_message(this.ptr, InternalUtils.check_arr_len(peer_node_id, 33), msg == null ? 0 : msg.ptr);
124 Reference.reachabilityFence(this);
125 Reference.reachabilityFence(peer_node_id);
126 Reference.reachabilityFence(msg);
127 if (this != null) { this.ptrs_to.add(msg); };
131 * Called when a connection is established with a peer. Can be used to track which peers
132 * advertise onion message support and are online.
134 * May return an `Err(())` if the features the peer supports are not sufficient to communicate
135 * with us. Implementors should be somewhat conservative about doing so, however, as other
136 * message handlers may still wish to communicate with this peer.
138 public Result_NoneNoneZ peer_connected(byte[] their_node_id, org.ldk.structs.Init init, boolean inbound) {
139 long ret = bindings.OnionMessageHandler_peer_connected(this.ptr, InternalUtils.check_arr_len(their_node_id, 33), init == null ? 0 : init.ptr, inbound);
140 Reference.reachabilityFence(this);
141 Reference.reachabilityFence(their_node_id);
142 Reference.reachabilityFence(init);
143 Reference.reachabilityFence(inbound);
144 if (ret >= 0 && ret <= 4096) { return null; }
145 Result_NoneNoneZ ret_hu_conv = Result_NoneNoneZ.constr_from_ptr(ret);
146 if (this != null) { this.ptrs_to.add(init); };
151 * Indicates a connection to the peer failed/an existing connection was lost. Allows handlers to
152 * drop and refuse to forward onion messages to this peer.
154 public void peer_disconnected(byte[] their_node_id) {
155 bindings.OnionMessageHandler_peer_disconnected(this.ptr, InternalUtils.check_arr_len(their_node_id, 33));
156 Reference.reachabilityFence(this);
157 Reference.reachabilityFence(their_node_id);
161 * Gets the node feature flags which this handler itself supports. All available handlers are
162 * queried similarly and their feature flags are OR'd together to form the [`NodeFeatures`]
163 * which are broadcasted in our [`NodeAnnouncement`] message.
165 public NodeFeatures provided_node_features() {
166 long ret = bindings.OnionMessageHandler_provided_node_features(this.ptr);
167 Reference.reachabilityFence(this);
168 if (ret >= 0 && ret <= 4096) { return null; }
169 org.ldk.structs.NodeFeatures ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.NodeFeatures(null, ret); }
170 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };
175 * Gets the init feature flags which should be sent to the given peer. All available handlers
176 * are queried similarly and their feature flags are OR'd together to form the [`InitFeatures`]
177 * which are sent in our [`Init`] message.
179 * Note that this method is called before [`Self::peer_connected`].
181 public InitFeatures provided_init_features(byte[] their_node_id) {
182 long ret = bindings.OnionMessageHandler_provided_init_features(this.ptr, InternalUtils.check_arr_len(their_node_id, 33));
183 Reference.reachabilityFence(this);
184 Reference.reachabilityFence(their_node_id);
185 if (ret >= 0 && ret <= 4096) { return null; }
186 org.ldk.structs.InitFeatures ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.InitFeatures(null, ret); }
187 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };