1 package org.ldk.structs;
3 import org.ldk.impl.bindings;
4 import org.ldk.enums.*;
6 import java.util.Arrays;
7 import javax.annotation.Nullable;
10 * Handler for BOLT1-compliant messages.
12 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
13 public class CustomMessageHandler extends CommonBase {
14 final bindings.LDKCustomMessageHandler bindings_instance;
15 CustomMessageHandler(Object _dummy, long ptr) { super(ptr); bindings_instance = null; }
16 private CustomMessageHandler(bindings.LDKCustomMessageHandler arg, bindings.LDKCustomMessageReader CustomMessageReader) {
17 super(bindings.LDKCustomMessageHandler_new(arg, CustomMessageReader));
18 this.ptrs_to.add(arg);
19 this.ptrs_to.add(CustomMessageReader);
20 this.bindings_instance = arg;
22 @Override @SuppressWarnings("deprecation")
23 protected void finalize() throws Throwable {
24 if (ptr != 0) { bindings.CustomMessageHandler_free(ptr); } super.finalize();
27 public static interface CustomMessageHandlerInterface {
29 * Called with the message type that was received and the buffer to be read.
30 * Can return a `MessageHandlingError` if the message could not be handled.
32 Result_NoneLightningErrorZ handle_custom_message(Type msg, byte[] sender_node_id);
34 * Gets the list of pending messages which were generated by the custom message
35 * handler, clearing the list in the process. The first tuple element must
36 * correspond to the intended recipients node ids. If no connection to one of the
37 * specified node does not exist, the message is simply not sent to it.
39 TwoTuple<byte[], Type>[] get_and_clear_pending_msg();
41 private static class LDKCustomMessageHandlerHolder { CustomMessageHandler held; }
42 public static CustomMessageHandler new_impl(CustomMessageHandlerInterface arg, CustomMessageReader.CustomMessageReaderInterface CustomMessageReader_impl) {
43 final LDKCustomMessageHandlerHolder impl_holder = new LDKCustomMessageHandlerHolder();
44 impl_holder.held = new CustomMessageHandler(new bindings.LDKCustomMessageHandler() {
45 @Override public long handle_custom_message(long msg, byte[] sender_node_id) {
46 Type ret_hu_conv = new Type(null, msg);
47 ret_hu_conv.ptrs_to.add(this);
48 Result_NoneLightningErrorZ ret = arg.handle_custom_message(ret_hu_conv, sender_node_id);
49 long result = ret != null ? ret.ptr : 0;
52 @Override public long[] get_and_clear_pending_msg() {
53 TwoTuple<byte[], Type>[] ret = arg.get_and_clear_pending_msg();
54 long[] result = ret != null ? Arrays.stream(ret).mapToLong(ret_conv_24 -> bindings.C2Tuple_PublicKeyTypeZ_new(ret_conv_24.a, ret_conv_24.b == null ? 0 : ret_conv_24.b.ptr)).toArray() : null;
55 for (TwoTuple<byte[], Type> ret_conv_24: ret) { impl_holder.held.ptrs_to.add(ret_conv_24.b); };
58 }, CustomMessageReader.new_impl(CustomMessageReader_impl).bindings_instance);
59 return impl_holder.held;
63 * Gets the underlying CustomMessageReader.
65 public CustomMessageReader get_custom_message_reader() {
66 CustomMessageReader res = new CustomMessageReader(null, bindings.LDKCustomMessageHandler_get_CustomMessageReader(this.ptr));
67 this.ptrs_to.add(res);
72 * Called with the message type that was received and the buffer to be read.
73 * Can return a `MessageHandlingError` if the message could not be handled.
75 public Result_NoneLightningErrorZ handle_custom_message(Type msg, byte[] sender_node_id) {
76 long ret = bindings.CustomMessageHandler_handle_custom_message(this.ptr, msg == null ? 0 : msg.ptr, sender_node_id);
77 if (ret < 1024) { return null; }
78 Result_NoneLightningErrorZ ret_hu_conv = Result_NoneLightningErrorZ.constr_from_ptr(ret);
79 this.ptrs_to.add(msg);
84 * Gets the list of pending messages which were generated by the custom message
85 * handler, clearing the list in the process. The first tuple element must
86 * correspond to the intended recipients node ids. If no connection to one of the
87 * specified node does not exist, the message is simply not sent to it.
89 public TwoTuple<byte[], Type>[] get_and_clear_pending_msg() {
90 long[] ret = bindings.CustomMessageHandler_get_and_clear_pending_msg(this.ptr);
91 TwoTuple<byte[], Type>[] ret_conv_24_arr = new TwoTuple[ret.length];
92 for (int y = 0; y < ret.length; y++) {
93 long ret_conv_24 = ret[y];
94 byte[] ret_conv_24_a = bindings.LDKC2Tuple_PublicKeyTypeZ_get_a(ret_conv_24);
95 long ret_conv_24_b = bindings.LDKC2Tuple_PublicKeyTypeZ_get_b(ret_conv_24);
96 Type ret_hu_conv = new Type(null, ret_conv_24_b);
97 ret_hu_conv.ptrs_to.add(this);;
98 TwoTuple<byte[], Type> ret_conv_24_conv = new TwoTuple<byte[], Type>(ret_conv_24_a, ret_hu_conv, () -> {
99 bindings.C2Tuple_PublicKeyTypeZ_free(ret_conv_24);
101 ret_hu_conv.ptrs_to.add(ret_conv_24_conv);
102 ret_conv_24_arr[y] = ret_conv_24_conv;
104 return ret_conv_24_arr;