ffc488763574e4693297e2c62159aadce7487c10
[ldk-java] / src / main / java / org / ldk / structs / CustomMessageHandler.java
1 package org.ldk.structs;
2
3 import org.ldk.impl.bindings;
4 import org.ldk.enums.*;
5 import org.ldk.util.*;
6 import java.util.Arrays;
7 import java.lang.ref.Reference;
8 import javax.annotation.Nullable;
9
10 /**
11  * Handler for BOLT1-compliant messages.
12  */
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;
22         }
23         @Override @SuppressWarnings("deprecation")
24         protected void finalize() throws Throwable {
25                 if (ptr != 0) { bindings.CustomMessageHandler_free(ptr); } super.finalize();
26         }
27
28         public static interface CustomMessageHandlerInterface {
29                 /**
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.
32                  */
33                 Result_NoneLightningErrorZ handle_custom_message(Type msg, byte[] sender_node_id);
34                 /**
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.
39                  */
40                 TwoTuple_PublicKeyTypeZ[] get_and_clear_pending_msg();
41         }
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();
51                                 return result;
52                         }
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;
56                                 return result;
57                         }
58                 }, CustomMessageReader.new_impl(CustomMessageReader_impl).bindings_instance);
59                 return impl_holder.held;
60         }
61
62         /**
63          * Gets the underlying CustomMessageReader.
64          */
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);
68                 return res;
69         }
70
71         /**
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.
74          */
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);
83                 return ret_hu_conv;
84         }
85
86         /**
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.
91          */
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                 int ret_conv_25_len = ret.length;
96                 TwoTuple_PublicKeyTypeZ[] ret_conv_25_arr = new TwoTuple_PublicKeyTypeZ[ret_conv_25_len];
97                 for (int z = 0; z < ret_conv_25_len; z++) {
98                         long ret_conv_25 = ret[z];
99                         TwoTuple_PublicKeyTypeZ ret_conv_25_hu_conv = new TwoTuple_PublicKeyTypeZ(null, ret_conv_25);
100                         ret_conv_25_hu_conv.ptrs_to.add(this);
101                         ret_conv_25_arr[z] = ret_conv_25_hu_conv;
102                 }
103                 return ret_conv_25_arr;
104         }
105
106 }