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 * Handler for custom onion messages. If you are using [`SimpleArcOnionMessenger`],
12 * [`SimpleRefOnionMessenger`], or prefer to ignore inbound custom onion messages,
13 * [`IgnoringMessageHandler`] must be provided to [`OnionMessenger::new`]. Otherwise, a custom
14 * implementation of this trait must be provided, with [`CustomMessage`] specifying the supported
17 * See [`OnionMessenger`] for example usage.
19 * [`IgnoringMessageHandler`]: crate::ln::peer_handler::IgnoringMessageHandler
20 * [`CustomMessage`]: Self::CustomMessage
22 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
23 public class CustomOnionMessageHandler extends CommonBase {
24 final bindings.LDKCustomOnionMessageHandler bindings_instance;
25 CustomOnionMessageHandler(Object _dummy, long ptr) { super(ptr); bindings_instance = null; }
26 private CustomOnionMessageHandler(bindings.LDKCustomOnionMessageHandler arg) {
27 super(bindings.LDKCustomOnionMessageHandler_new(arg));
28 this.ptrs_to.add(arg);
29 this.bindings_instance = arg;
31 @Override @SuppressWarnings("deprecation")
32 protected void finalize() throws Throwable {
33 if (ptr != 0) { bindings.CustomOnionMessageHandler_free(ptr); } super.finalize();
36 * Destroys the object, freeing associated resources. After this call, any access
37 * to this object may result in a SEGFAULT or worse.
39 * You should generally NEVER call this method. You should let the garbage collector
40 * do this for you when it finalizes objects. However, it may be useful for types
41 * which represent locks and should be closed immediately to avoid holding locks
44 public void destroy() {
45 if (ptr != 0) { bindings.CustomOnionMessageHandler_free(ptr); }
48 public static interface CustomOnionMessageHandlerInterface {
50 * Called with the custom message that was received, returning a response to send, if any.
52 Option_CustomOnionMessageContentsZ handle_custom_message(CustomOnionMessageContents msg);
54 * Read a custom message of type `message_type` from `buffer`, returning `Ok(None)` if the
55 * message type is unknown.
57 Result_COption_CustomOnionMessageContentsZDecodeErrorZ read_custom_message(long message_type, byte[] buffer);
59 private static class LDKCustomOnionMessageHandlerHolder { CustomOnionMessageHandler held; }
60 public static CustomOnionMessageHandler new_impl(CustomOnionMessageHandlerInterface arg) {
61 final LDKCustomOnionMessageHandlerHolder impl_holder = new LDKCustomOnionMessageHandlerHolder();
62 impl_holder.held = new CustomOnionMessageHandler(new bindings.LDKCustomOnionMessageHandler() {
63 @Override public long handle_custom_message(long msg) {
64 CustomOnionMessageContents ret_hu_conv = new CustomOnionMessageContents(null, msg);
65 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };
66 Option_CustomOnionMessageContentsZ ret = arg.handle_custom_message(ret_hu_conv);
67 Reference.reachabilityFence(arg);
68 long result = ret == null ? 0 : ret.clone_ptr();
69 if (impl_holder.held != null) { impl_holder.held.ptrs_to.add(ret); };
72 @Override public long read_custom_message(long message_type, byte[] buffer) {
73 Result_COption_CustomOnionMessageContentsZDecodeErrorZ ret = arg.read_custom_message(message_type, buffer);
74 Reference.reachabilityFence(arg);
75 long result = ret == null ? 0 : ret.clone_ptr();
79 return impl_holder.held;
82 * Called with the custom message that was received, returning a response to send, if any.
84 public Option_CustomOnionMessageContentsZ handle_custom_message(org.ldk.structs.CustomOnionMessageContents msg) {
85 long ret = bindings.CustomOnionMessageHandler_handle_custom_message(this.ptr, msg.ptr);
86 Reference.reachabilityFence(this);
87 Reference.reachabilityFence(msg);
88 if (ret >= 0 && ret <= 4096) { return null; }
89 org.ldk.structs.Option_CustomOnionMessageContentsZ ret_hu_conv = org.ldk.structs.Option_CustomOnionMessageContentsZ.constr_from_ptr(ret);
90 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };
91 if (this != null) { this.ptrs_to.add(msg); };
96 * Read a custom message of type `message_type` from `buffer`, returning `Ok(None)` if the
97 * message type is unknown.
99 public Result_COption_CustomOnionMessageContentsZDecodeErrorZ read_custom_message(long message_type, byte[] buffer) {
100 long ret = bindings.CustomOnionMessageHandler_read_custom_message(this.ptr, message_type, buffer);
101 Reference.reachabilityFence(this);
102 Reference.reachabilityFence(message_type);
103 Reference.reachabilityFence(buffer);
104 if (ret >= 0 && ret <= 4096) { return null; }
105 Result_COption_CustomOnionMessageContentsZDecodeErrorZ ret_hu_conv = Result_COption_CustomOnionMessageContentsZDecodeErrorZ.constr_from_ptr(ret);