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 ret_hu_conv.ptrs_to.add(this);
49 Result_NoneLightningErrorZ ret = arg.handle_custom_message(ret_hu_conv, sender_node_id);
50 long result = ret == null ? 0 : ret.clone_ptr();
53 @Override public long[] get_and_clear_pending_msg() {
54 TwoTuple_PublicKeyTypeZ[] ret = arg.get_and_clear_pending_msg();
55 long[] result = ret != null ? Arrays.stream(ret).mapToLong(ret_conv_25 -> ret_conv_25 == null ? 0 : ret_conv_25.clone_ptr()).toArray() : null;
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, InternalUtils.check_arr_len(sender_node_id, 33));
77 Reference.reachabilityFence(this);
78 Reference.reachabilityFence(msg);
79 Reference.reachabilityFence(sender_node_id);
80 if (ret >= 0 && ret <= 4096) { return null; }
81 Result_NoneLightningErrorZ ret_hu_conv = Result_NoneLightningErrorZ.constr_from_ptr(ret);
82 this.ptrs_to.add(msg);
87 * Gets the list of pending messages which were generated by the custom message
88 * handler, clearing the list in the process. The first tuple element must
89 * correspond to the intended recipients node ids. If no connection to one of the
90 * specified node does not exist, the message is simply not sent to it.
92 public TwoTuple_PublicKeyTypeZ[] get_and_clear_pending_msg() {
93 long[] ret = bindings.CustomMessageHandler_get_and_clear_pending_msg(this.ptr);
94 Reference.reachabilityFence(this);
95 TwoTuple_PublicKeyTypeZ[] ret_conv_25_arr = new TwoTuple_PublicKeyTypeZ[ret.length];
96 for (int z = 0; z < ret.length; z++) {
97 long ret_conv_25 = ret[z];
98 TwoTuple_PublicKeyTypeZ ret_conv_25_hu_conv = new TwoTuple_PublicKeyTypeZ(null, ret_conv_25);
99 ret_conv_25_hu_conv.ptrs_to.add(this);
100 ret_conv_25_arr[z] = ret_conv_25_hu_conv;
102 return ret_conv_25_arr;