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 public static interface OnionMessageHandlerInterface {
30 * Handle an incoming onion_message message from the given peer.
32 void handle_onion_message(byte[] peer_node_id, OnionMessage msg);
34 * Called when a connection is established with a peer. Can be used to track which peers
35 * advertise onion message support and are online.
37 * May return an `Err(())` if the features the peer supports are not sufficient to communicate
38 * with us. Implementors should be somewhat conservative about doing so, however, as other
39 * message handlers may still wish to communicate with this peer.
41 Result_NoneNoneZ peer_connected(byte[] their_node_id, Init init);
43 * Indicates a connection to the peer failed/an existing connection was lost. Allows handlers to
44 * drop and refuse to forward onion messages to this peer.
46 * Note that in some rare cases this may be called without a corresponding
47 * [`Self::peer_connected`].
49 void peer_disconnected(byte[] their_node_id, boolean no_connection_possible);
51 * Gets the node feature flags which this handler itself supports. All available handlers are
52 * queried similarly and their feature flags are OR'd together to form the [`NodeFeatures`]
53 * which are broadcasted in our [`NodeAnnouncement`] message.
55 NodeFeatures provided_node_features();
57 * Gets the init feature flags which should be sent to the given peer. All available handlers
58 * are queried similarly and their feature flags are OR'd together to form the [`InitFeatures`]
59 * which are sent in our [`Init`] message.
61 * Note that this method is called before [`Self::peer_connected`].
63 InitFeatures provided_init_features(byte[] their_node_id);
65 private static class LDKOnionMessageHandlerHolder { OnionMessageHandler held; }
66 public static OnionMessageHandler new_impl(OnionMessageHandlerInterface arg, OnionMessageProvider.OnionMessageProviderInterface OnionMessageProvider_impl) {
67 final LDKOnionMessageHandlerHolder impl_holder = new LDKOnionMessageHandlerHolder();
68 impl_holder.held = new OnionMessageHandler(new bindings.LDKOnionMessageHandler() {
69 @Override public void handle_onion_message(byte[] peer_node_id, long msg) {
70 org.ldk.structs.OnionMessage msg_hu_conv = null; if (msg < 0 || msg > 4096) { msg_hu_conv = new org.ldk.structs.OnionMessage(null, msg); }
71 arg.handle_onion_message(peer_node_id, msg_hu_conv);
72 Reference.reachabilityFence(arg);
74 @Override public long peer_connected(byte[] their_node_id, long init) {
75 org.ldk.structs.Init init_hu_conv = null; if (init < 0 || init > 4096) { init_hu_conv = new org.ldk.structs.Init(null, init); }
76 Result_NoneNoneZ ret = arg.peer_connected(their_node_id, init_hu_conv);
77 Reference.reachabilityFence(arg);
78 long result = ret == null ? 0 : ret.clone_ptr();
81 @Override public void peer_disconnected(byte[] their_node_id, boolean no_connection_possible) {
82 arg.peer_disconnected(their_node_id, no_connection_possible);
83 Reference.reachabilityFence(arg);
85 @Override public long provided_node_features() {
86 NodeFeatures ret = arg.provided_node_features();
87 Reference.reachabilityFence(arg);
88 long result = ret == null ? 0 : ret.clone_ptr();
91 @Override public long provided_init_features(byte[] their_node_id) {
92 InitFeatures ret = arg.provided_init_features(their_node_id);
93 Reference.reachabilityFence(arg);
94 long result = ret == null ? 0 : ret.clone_ptr();
97 }, OnionMessageProvider.new_impl(OnionMessageProvider_impl).bindings_instance);
98 return impl_holder.held;
102 * Gets the underlying OnionMessageProvider.
104 public OnionMessageProvider get_onion_message_provider() {
105 OnionMessageProvider res = new OnionMessageProvider(null, bindings.LDKOnionMessageHandler_get_OnionMessageProvider(this.ptr));
106 this.ptrs_to.add(res);
111 * Handle an incoming onion_message message from the given peer.
113 public void handle_onion_message(byte[] peer_node_id, OnionMessage msg) {
114 bindings.OnionMessageHandler_handle_onion_message(this.ptr, InternalUtils.check_arr_len(peer_node_id, 33), msg == null ? 0 : msg.ptr);
115 Reference.reachabilityFence(this);
116 Reference.reachabilityFence(peer_node_id);
117 Reference.reachabilityFence(msg);
118 if (this != null) { this.ptrs_to.add(msg); };
122 * Called when a connection is established with a peer. Can be used to track which peers
123 * advertise onion message support and are online.
125 * May return an `Err(())` if the features the peer supports are not sufficient to communicate
126 * with us. Implementors should be somewhat conservative about doing so, however, as other
127 * message handlers may still wish to communicate with this peer.
129 public Result_NoneNoneZ peer_connected(byte[] their_node_id, Init init) {
130 long ret = bindings.OnionMessageHandler_peer_connected(this.ptr, InternalUtils.check_arr_len(their_node_id, 33), init == null ? 0 : init.ptr);
131 Reference.reachabilityFence(this);
132 Reference.reachabilityFence(their_node_id);
133 Reference.reachabilityFence(init);
134 if (ret >= 0 && ret <= 4096) { return null; }
135 Result_NoneNoneZ ret_hu_conv = Result_NoneNoneZ.constr_from_ptr(ret);
136 if (this != null) { this.ptrs_to.add(init); };
141 * Indicates a connection to the peer failed/an existing connection was lost. Allows handlers to
142 * drop and refuse to forward onion messages to this peer.
144 * Note that in some rare cases this may be called without a corresponding
145 * [`Self::peer_connected`].
147 public void peer_disconnected(byte[] their_node_id, boolean no_connection_possible) {
148 bindings.OnionMessageHandler_peer_disconnected(this.ptr, InternalUtils.check_arr_len(their_node_id, 33), no_connection_possible);
149 Reference.reachabilityFence(this);
150 Reference.reachabilityFence(their_node_id);
151 Reference.reachabilityFence(no_connection_possible);
155 * Gets the node feature flags which this handler itself supports. All available handlers are
156 * queried similarly and their feature flags are OR'd together to form the [`NodeFeatures`]
157 * which are broadcasted in our [`NodeAnnouncement`] message.
159 public NodeFeatures provided_node_features() {
160 long ret = bindings.OnionMessageHandler_provided_node_features(this.ptr);
161 Reference.reachabilityFence(this);
162 if (ret >= 0 && ret <= 4096) { return null; }
163 org.ldk.structs.NodeFeatures ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.NodeFeatures(null, ret); }
164 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };
169 * Gets the init feature flags which should be sent to the given peer. All available handlers
170 * are queried similarly and their feature flags are OR'd together to form the [`InitFeatures`]
171 * which are sent in our [`Init`] message.
173 * Note that this method is called before [`Self::peer_connected`].
175 public InitFeatures provided_init_features(byte[] their_node_id) {
176 long ret = bindings.OnionMessageHandler_provided_init_features(this.ptr, InternalUtils.check_arr_len(their_node_id, 33));
177 Reference.reachabilityFence(this);
178 Reference.reachabilityFence(their_node_id);
179 if (ret >= 0 && ret <= 4096) { return null; }
180 org.ldk.structs.InitFeatures ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.InitFeatures(null, ret); }
181 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };