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 * A handler provided to [`PeerManager`] for reading and handling custom messages.
13 * [BOLT 1] specifies a custom message type range for use with experimental or application-specific
14 * messages. `CustomMessageHandler` allows for user-defined handling of such types. See the
15 * [`lightning_custom_message`] crate for tools useful in composing more than one custom handler.
17 * [BOLT 1]: https://github.com/lightning/bolts/blob/master/01-messaging.md
18 * [`lightning_custom_message`]: https://docs.rs/lightning_custom_message/latest/lightning_custom_message
20 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
21 public class CustomMessageHandler extends CommonBase {
22 final bindings.LDKCustomMessageHandler bindings_instance;
23 CustomMessageHandler(Object _dummy, long ptr) { super(ptr); bindings_instance = null; }
24 private CustomMessageHandler(bindings.LDKCustomMessageHandler arg, bindings.LDKCustomMessageReader CustomMessageReader) {
25 super(bindings.LDKCustomMessageHandler_new(arg, CustomMessageReader));
26 this.ptrs_to.add(arg);
27 this.ptrs_to.add(CustomMessageReader);
28 this.bindings_instance = arg;
30 @Override @SuppressWarnings("deprecation")
31 protected void finalize() throws Throwable {
32 if (ptr != 0) { bindings.CustomMessageHandler_free(ptr); } super.finalize();
35 * Destroys the object, freeing associated resources. After this call, any access
36 * to this object may result in a SEGFAULT or worse.
38 * You should generally NEVER call this method. You should let the garbage collector
39 * do this for you when it finalizes objects. However, it may be useful for types
40 * which represent locks and should be closed immediately to avoid holding locks
43 public void destroy() {
44 if (ptr != 0) { bindings.CustomMessageHandler_free(ptr); }
47 public static interface CustomMessageHandlerInterface {
49 * Handles the given message sent from `sender_node_id`, possibly producing messages for
50 * [`CustomMessageHandler::get_and_clear_pending_msg`] to return and thus for [`PeerManager`]
53 Result_NoneLightningErrorZ handle_custom_message(Type msg, byte[] sender_node_id);
55 * Returns the list of pending messages that were generated by the handler, clearing the list
56 * in the process. Each message is paired with the node id of the intended recipient. If no
57 * connection to the node exists, then the message is simply not sent.
59 TwoTuple_PublicKeyTypeZ[] get_and_clear_pending_msg();
61 private static class LDKCustomMessageHandlerHolder { CustomMessageHandler held; }
62 public static CustomMessageHandler new_impl(CustomMessageHandlerInterface arg, CustomMessageReader.CustomMessageReaderInterface CustomMessageReader_impl) {
63 final LDKCustomMessageHandlerHolder impl_holder = new LDKCustomMessageHandlerHolder();
64 impl_holder.held = new CustomMessageHandler(new bindings.LDKCustomMessageHandler() {
65 @Override public long handle_custom_message(long msg, byte[] sender_node_id) {
66 Type ret_hu_conv = new Type(null, msg);
67 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };
68 Result_NoneLightningErrorZ ret = arg.handle_custom_message(ret_hu_conv, sender_node_id);
69 Reference.reachabilityFence(arg);
70 long result = ret == null ? 0 : ret.clone_ptr();
73 @Override public long[] get_and_clear_pending_msg() {
74 TwoTuple_PublicKeyTypeZ[] ret = arg.get_and_clear_pending_msg();
75 Reference.reachabilityFence(arg);
76 long[] result = ret != null ? Arrays.stream(ret).mapToLong(ret_conv_25 -> ret_conv_25 == null ? 0 : ret_conv_25.clone_ptr()).toArray() : null;
79 }, CustomMessageReader.new_impl(CustomMessageReader_impl).bindings_instance);
80 return impl_holder.held;
84 * Gets the underlying CustomMessageReader.
86 public CustomMessageReader get_custom_message_reader() {
87 CustomMessageReader res = new CustomMessageReader(null, bindings.LDKCustomMessageHandler_get_CustomMessageReader(this.ptr));
88 res.ptrs_to.add(this);
93 * Handles the given message sent from `sender_node_id`, possibly producing messages for
94 * [`CustomMessageHandler::get_and_clear_pending_msg`] to return and thus for [`PeerManager`]
97 public Result_NoneLightningErrorZ handle_custom_message(org.ldk.structs.Type msg, byte[] sender_node_id) {
98 long ret = bindings.CustomMessageHandler_handle_custom_message(this.ptr, msg == null ? 0 : msg.ptr, InternalUtils.check_arr_len(sender_node_id, 33));
99 Reference.reachabilityFence(this);
100 Reference.reachabilityFence(msg);
101 Reference.reachabilityFence(sender_node_id);
102 if (ret >= 0 && ret <= 4096) { return null; }
103 Result_NoneLightningErrorZ ret_hu_conv = Result_NoneLightningErrorZ.constr_from_ptr(ret);
104 if (this != null) { this.ptrs_to.add(msg); };
109 * Returns the list of pending messages that were generated by the handler, clearing the list
110 * in the process. Each message is paired with the node id of the intended recipient. If no
111 * connection to the node exists, then the message is simply not sent.
113 public TwoTuple_PublicKeyTypeZ[] get_and_clear_pending_msg() {
114 long[] ret = bindings.CustomMessageHandler_get_and_clear_pending_msg(this.ptr);
115 Reference.reachabilityFence(this);
116 int ret_conv_25_len = ret.length;
117 TwoTuple_PublicKeyTypeZ[] ret_conv_25_arr = new TwoTuple_PublicKeyTypeZ[ret_conv_25_len];
118 for (int z = 0; z < ret_conv_25_len; z++) {
119 long ret_conv_25 = ret[z];
120 TwoTuple_PublicKeyTypeZ ret_conv_25_hu_conv = new TwoTuple_PublicKeyTypeZ(null, ret_conv_25);
121 if (ret_conv_25_hu_conv != null) { ret_conv_25_hu_conv.ptrs_to.add(this); };
122 ret_conv_25_arr[z] = ret_conv_25_hu_conv;
124 return ret_conv_25_arr;