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 BOLT1-compliant messages.
13 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
14 public class CustomMessageHandler extends CommonBase {
15 final bindings.LDKCustomMessageHandler bindings_instance;
16 CustomMessageHandler(Object _dummy, long ptr) { super(ptr); bindings_instance = null; }
17 private CustomMessageHandler(bindings.LDKCustomMessageHandler arg, bindings.LDKCustomMessageReader CustomMessageReader) {
18 super(bindings.LDKCustomMessageHandler_new(arg, CustomMessageReader));
19 this.ptrs_to.add(arg);
20 this.ptrs_to.add(CustomMessageReader);
21 this.bindings_instance = arg;
23 @Override @SuppressWarnings("deprecation")
24 protected void finalize() throws Throwable {
25 if (ptr != 0) { bindings.CustomMessageHandler_free(ptr); } super.finalize();
28 public static interface CustomMessageHandlerInterface {
30 * Called with the message type that was received and the buffer to be read.
31 * Can return a `MessageHandlingError` if the message could not be handled.
33 Result_NoneLightningErrorZ handle_custom_message(Type msg, byte[] sender_node_id);
35 * Gets the list of pending messages which were generated by the custom message
36 * handler, clearing the list in the process. The first tuple element must
37 * correspond to the intended recipients node ids. If no connection to one of the
38 * specified node does not exist, the message is simply not sent to it.
40 TwoTuple_PublicKeyTypeZ[] get_and_clear_pending_msg();
42 private static class LDKCustomMessageHandlerHolder { CustomMessageHandler held; }
43 public static CustomMessageHandler new_impl(CustomMessageHandlerInterface arg, CustomMessageReader.CustomMessageReaderInterface CustomMessageReader_impl) {
44 final LDKCustomMessageHandlerHolder impl_holder = new LDKCustomMessageHandlerHolder();
45 impl_holder.held = new CustomMessageHandler(new bindings.LDKCustomMessageHandler() {
46 @Override public long handle_custom_message(long msg, byte[] sender_node_id) {
47 Type ret_hu_conv = new Type(null, msg);
48 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };
49 Result_NoneLightningErrorZ ret = arg.handle_custom_message(ret_hu_conv, sender_node_id);
50 Reference.reachabilityFence(arg);
51 long result = ret == null ? 0 : ret.clone_ptr();
54 @Override public long[] get_and_clear_pending_msg() {
55 TwoTuple_PublicKeyTypeZ[] ret = arg.get_and_clear_pending_msg();
56 Reference.reachabilityFence(arg);
57 long[] result = ret != null ? Arrays.stream(ret).mapToLong(ret_conv_25 -> ret_conv_25 == null ? 0 : ret_conv_25.clone_ptr()).toArray() : null;
60 }, CustomMessageReader.new_impl(CustomMessageReader_impl).bindings_instance);
61 return impl_holder.held;
65 * Gets the underlying CustomMessageReader.
67 public CustomMessageReader get_custom_message_reader() {
68 CustomMessageReader res = new CustomMessageReader(null, bindings.LDKCustomMessageHandler_get_CustomMessageReader(this.ptr));
69 this.ptrs_to.add(res);
74 * Called with the message type that was received and the buffer to be read.
75 * Can return a `MessageHandlingError` if the message could not be handled.
77 public Result_NoneLightningErrorZ handle_custom_message(Type msg, byte[] sender_node_id) {
78 long ret = bindings.CustomMessageHandler_handle_custom_message(this.ptr, msg == null ? 0 : msg.ptr, InternalUtils.check_arr_len(sender_node_id, 33));
79 Reference.reachabilityFence(this);
80 Reference.reachabilityFence(msg);
81 Reference.reachabilityFence(sender_node_id);
82 if (ret >= 0 && ret <= 4096) { return null; }
83 Result_NoneLightningErrorZ ret_hu_conv = Result_NoneLightningErrorZ.constr_from_ptr(ret);
84 if (this != null) { this.ptrs_to.add(msg); };
89 * Gets the list of pending messages which were generated by the custom message
90 * handler, clearing the list in the process. The first tuple element must
91 * correspond to the intended recipients node ids. If no connection to one of the
92 * specified node does not exist, the message is simply not sent to it.
94 public TwoTuple_PublicKeyTypeZ[] get_and_clear_pending_msg() {
95 long[] ret = bindings.CustomMessageHandler_get_and_clear_pending_msg(this.ptr);
96 Reference.reachabilityFence(this);
97 int ret_conv_25_len = ret.length;
98 TwoTuple_PublicKeyTypeZ[] ret_conv_25_arr = new TwoTuple_PublicKeyTypeZ[ret_conv_25_len];
99 for (int z = 0; z < ret_conv_25_len; z++) {
100 long ret_conv_25 = ret[z];
101 TwoTuple_PublicKeyTypeZ ret_conv_25_hu_conv = new TwoTuple_PublicKeyTypeZ(null, ret_conv_25);
102 if (ret_conv_25_hu_conv != null) { ret_conv_25_hu_conv.ptrs_to.add(this); };
103 ret_conv_25_arr[z] = ret_conv_25_hu_conv;
105 return ret_conv_25_arr;