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 * Gets the node feature flags which this handler itself supports. All available handlers are
62 * queried similarly and their feature flags are OR'd together to form the [`NodeFeatures`]
63 * which are broadcasted in our [`NodeAnnouncement`] message.
65 * [`NodeAnnouncement`]: crate::ln::msgs::NodeAnnouncement
67 NodeFeatures provided_node_features();
69 * Gets the init feature flags which should be sent to the given peer. All available handlers
70 * are queried similarly and their feature flags are OR'd together to form the [`InitFeatures`]
71 * which are sent in our [`Init`] message.
73 * [`Init`]: crate::ln::msgs::Init
75 InitFeatures provided_init_features(byte[] their_node_id);
77 private static class LDKCustomMessageHandlerHolder { CustomMessageHandler held; }
78 public static CustomMessageHandler new_impl(CustomMessageHandlerInterface arg, CustomMessageReader.CustomMessageReaderInterface CustomMessageReader_impl) {
79 final LDKCustomMessageHandlerHolder impl_holder = new LDKCustomMessageHandlerHolder();
80 impl_holder.held = new CustomMessageHandler(new bindings.LDKCustomMessageHandler() {
81 @Override public long handle_custom_message(long msg, byte[] sender_node_id) {
82 Type ret_hu_conv = new Type(null, msg);
83 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };
84 Result_NoneLightningErrorZ ret = arg.handle_custom_message(ret_hu_conv, sender_node_id);
85 Reference.reachabilityFence(arg);
86 long result = ret == null ? 0 : ret.clone_ptr();
89 @Override public long[] get_and_clear_pending_msg() {
90 TwoTuple_PublicKeyTypeZ[] ret = arg.get_and_clear_pending_msg();
91 Reference.reachabilityFence(arg);
92 long[] result = ret != null ? Arrays.stream(ret).mapToLong(ret_conv_25 -> ret_conv_25 == null ? 0 : ret_conv_25.clone_ptr()).toArray() : null;
95 @Override public long provided_node_features() {
96 NodeFeatures ret = arg.provided_node_features();
97 Reference.reachabilityFence(arg);
98 long result = ret == null ? 0 : ret.clone_ptr();
101 @Override public long provided_init_features(byte[] their_node_id) {
102 InitFeatures ret = arg.provided_init_features(their_node_id);
103 Reference.reachabilityFence(arg);
104 long result = ret == null ? 0 : ret.clone_ptr();
107 }, CustomMessageReader.new_impl(CustomMessageReader_impl).bindings_instance);
108 return impl_holder.held;
112 * Gets the underlying CustomMessageReader.
114 public CustomMessageReader get_custom_message_reader() {
115 CustomMessageReader res = new CustomMessageReader(null, bindings.LDKCustomMessageHandler_get_CustomMessageReader(this.ptr));
116 res.ptrs_to.add(this);
121 * Handles the given message sent from `sender_node_id`, possibly producing messages for
122 * [`CustomMessageHandler::get_and_clear_pending_msg`] to return and thus for [`PeerManager`]
125 public Result_NoneLightningErrorZ handle_custom_message(org.ldk.structs.Type msg, byte[] sender_node_id) {
126 long ret = bindings.CustomMessageHandler_handle_custom_message(this.ptr, msg.ptr, InternalUtils.check_arr_len(sender_node_id, 33));
127 Reference.reachabilityFence(this);
128 Reference.reachabilityFence(msg);
129 Reference.reachabilityFence(sender_node_id);
130 if (ret >= 0 && ret <= 4096) { return null; }
131 Result_NoneLightningErrorZ ret_hu_conv = Result_NoneLightningErrorZ.constr_from_ptr(ret);
132 if (this != null) { this.ptrs_to.add(msg); };
137 * Returns the list of pending messages that were generated by the handler, clearing the list
138 * in the process. Each message is paired with the node id of the intended recipient. If no
139 * connection to the node exists, then the message is simply not sent.
141 public TwoTuple_PublicKeyTypeZ[] get_and_clear_pending_msg() {
142 long[] ret = bindings.CustomMessageHandler_get_and_clear_pending_msg(this.ptr);
143 Reference.reachabilityFence(this);
144 int ret_conv_25_len = ret.length;
145 TwoTuple_PublicKeyTypeZ[] ret_conv_25_arr = new TwoTuple_PublicKeyTypeZ[ret_conv_25_len];
146 for (int z = 0; z < ret_conv_25_len; z++) {
147 long ret_conv_25 = ret[z];
148 TwoTuple_PublicKeyTypeZ ret_conv_25_hu_conv = new TwoTuple_PublicKeyTypeZ(null, ret_conv_25);
149 if (ret_conv_25_hu_conv != null) { ret_conv_25_hu_conv.ptrs_to.add(this); };
150 ret_conv_25_arr[z] = ret_conv_25_hu_conv;
152 return ret_conv_25_arr;
156 * Gets the node feature flags which this handler itself supports. All available handlers are
157 * queried similarly and their feature flags are OR'd together to form the [`NodeFeatures`]
158 * which are broadcasted in our [`NodeAnnouncement`] message.
160 * [`NodeAnnouncement`]: crate::ln::msgs::NodeAnnouncement
162 public NodeFeatures provided_node_features() {
163 long ret = bindings.CustomMessageHandler_provided_node_features(this.ptr);
164 Reference.reachabilityFence(this);
165 if (ret >= 0 && ret <= 4096) { return null; }
166 org.ldk.structs.NodeFeatures ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.NodeFeatures(null, ret); }
167 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };
172 * Gets the init feature flags which should be sent to the given peer. All available handlers
173 * are queried similarly and their feature flags are OR'd together to form the [`InitFeatures`]
174 * which are sent in our [`Init`] message.
176 * [`Init`]: crate::ln::msgs::Init
178 public InitFeatures provided_init_features(byte[] their_node_id) {
179 long ret = bindings.CustomMessageHandler_provided_init_features(this.ptr, InternalUtils.check_arr_len(their_node_id, 33));
180 Reference.reachabilityFence(this);
181 Reference.reachabilityFence(their_node_id);
182 if (ret >= 0 && ret <= 4096) { return null; }
183 org.ldk.structs.InitFeatures ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.InitFeatures(null, ret); }
184 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };