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_PublicKeyTypeZ[] 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_PublicKeyTypeZ[] ret = arg.get_and_clear_pending_msg();
54 long[] result = ret != null ? Arrays.stream(ret).mapToLong(ret_conv_25 -> ret_conv_25 != null ? ret_conv_25.ptr : 0).toArray() : null;
57 }, CustomMessageReader.new_impl(CustomMessageReader_impl).bindings_instance);
58 return impl_holder.held;
62 * Gets the underlying CustomMessageReader.
64 public CustomMessageReader get_custom_message_reader() {
65 CustomMessageReader res = new CustomMessageReader(null, bindings.LDKCustomMessageHandler_get_CustomMessageReader(this.ptr));
66 this.ptrs_to.add(res);
71 * Called with the message type that was received and the buffer to be read.
72 * Can return a `MessageHandlingError` if the message could not be handled.
74 public Result_NoneLightningErrorZ handle_custom_message(Type msg, byte[] sender_node_id) {
75 long ret = bindings.CustomMessageHandler_handle_custom_message(this.ptr, msg == null ? 0 : msg.ptr, sender_node_id);
76 if (ret < 1024) { return null; }
77 Result_NoneLightningErrorZ ret_hu_conv = Result_NoneLightningErrorZ.constr_from_ptr(ret);
78 this.ptrs_to.add(msg);
83 * Gets the list of pending messages which were generated by the custom message
84 * handler, clearing the list in the process. The first tuple element must
85 * correspond to the intended recipients node ids. If no connection to one of the
86 * specified node does not exist, the message is simply not sent to it.
88 public TwoTuple_PublicKeyTypeZ[] get_and_clear_pending_msg() {
89 long[] ret = bindings.CustomMessageHandler_get_and_clear_pending_msg(this.ptr);
90 TwoTuple_PublicKeyTypeZ[] ret_conv_25_arr = new TwoTuple_PublicKeyTypeZ[ret.length];
91 for (int z = 0; z < ret.length; z++) {
92 long ret_conv_25 = ret[z];
93 TwoTuple_PublicKeyTypeZ ret_conv_25_hu_conv = new TwoTuple_PublicKeyTypeZ(null, ret_conv_25);
94 ret_conv_25_hu_conv.ptrs_to.add(this);
95 ret_conv_25_arr[z] = ret_conv_25_hu_conv;
97 return ret_conv_25_arr;