[C#] Run tests against release library in determinism CI run
[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  * A handler provided to [`PeerManager`] for reading and handling custom messages.
12  * 
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.
16  * 
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
19  */
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;
29         }
30         @Override @SuppressWarnings("deprecation")
31         protected void finalize() throws Throwable {
32                 if (ptr != 0) { bindings.CustomMessageHandler_free(ptr); } super.finalize();
33         }
34         /**
35          * Destroys the object, freeing associated resources. After this call, any access
36          * to this object may result in a SEGFAULT or worse.
37          *
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
41          * until the GC runs.
42          */
43         public void destroy() {
44                 if (ptr != 0) { bindings.CustomMessageHandler_free(ptr); }
45                 ptr = 0;
46         }
47         public static interface CustomMessageHandlerInterface {
48                 /**
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`]
51                  * to send.
52                  */
53                 Result_NoneLightningErrorZ handle_custom_message(Type msg, byte[] sender_node_id);
54                 /**
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.
58                  */
59                 TwoTuple_PublicKeyTypeZ[] get_and_clear_pending_msg();
60                 /**
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.
64                  * 
65                  * [`NodeAnnouncement`]: crate::ln::msgs::NodeAnnouncement
66                  */
67                 NodeFeatures provided_node_features();
68                 /**
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.
72                  * 
73                  * [`Init`]: crate::ln::msgs::Init
74                  */
75                 InitFeatures provided_init_features(byte[] their_node_id);
76         }
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.clone_ptr();
87                                 return result;
88                         }
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.clone_ptr()).toArray() : null;
93                                 return result;
94                         }
95                         @Override public long provided_node_features() {
96                                 NodeFeatures ret = arg.provided_node_features();
97                                 Reference.reachabilityFence(arg);
98                                 long result = ret.clone_ptr();
99                                 return result;
100                         }
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.clone_ptr();
105                                 return result;
106                         }
107                 }, CustomMessageReader.new_impl(CustomMessageReader_impl).bindings_instance);
108                 return impl_holder.held;
109         }
110
111         /**
112          * Gets the underlying CustomMessageReader.
113          */
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);
117                 return res;
118         }
119
120         /**
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`]
123          * to send.
124          */
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); };
133                 return ret_hu_conv;
134         }
135
136         /**
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.
140          */
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;
151                 }
152                 return ret_conv_25_arr;
153         }
154
155         /**
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.
159          * 
160          * [`NodeAnnouncement`]: crate::ln::msgs::NodeAnnouncement
161          */
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); };
168                 return ret_hu_conv;
169         }
170
171         /**
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.
175          * 
176          * [`Init`]: crate::ln::msgs::Init
177          */
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); };
185                 return ret_hu_conv;
186         }
187
188 }