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 for an [`OnionMessage`] containing a BOLT 12 Offers message as its payload.
13 * [`OnionMessage`]: crate::ln::msgs::OnionMessage
15 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
16 public class OffersMessageHandler extends CommonBase {
17 final bindings.LDKOffersMessageHandler bindings_instance;
18 OffersMessageHandler(Object _dummy, long ptr) { super(ptr); bindings_instance = null; }
19 private OffersMessageHandler(bindings.LDKOffersMessageHandler arg) {
20 super(bindings.LDKOffersMessageHandler_new(arg));
21 this.ptrs_to.add(arg);
22 this.bindings_instance = arg;
24 @Override @SuppressWarnings("deprecation")
25 protected void finalize() throws Throwable {
26 if (ptr != 0) { bindings.OffersMessageHandler_free(ptr); } super.finalize();
29 * Destroys the object, freeing associated resources. After this call, any access
30 * to this object may result in a SEGFAULT or worse.
32 * You should generally NEVER call this method. You should let the garbage collector
33 * do this for you when it finalizes objects. However, it may be useful for types
34 * which represent locks and should be closed immediately to avoid holding locks
37 public void destroy() {
38 if (ptr != 0) { bindings.OffersMessageHandler_free(ptr); }
41 public static interface OffersMessageHandlerInterface {
43 * Handles the given message by either responding with an [`Bolt12Invoice`], sending a payment,
44 * or replying with an error.
46 * The returned [`OffersMessage`], if any, is enqueued to be sent by [`OnionMessenger`].
48 * [`OnionMessenger`]: crate::onion_message::messenger::OnionMessenger
50 Option_OffersMessageZ handle_message(OffersMessage message);
52 * Releases any [`OffersMessage`]s that need to be sent.
54 * Typically, this is used for messages initiating a payment flow rather than in response to
55 * another message. The latter should use the return value of [`Self::handle_message`].
57 ThreeTuple_OffersMessageDestinationBlindedPathZ[] release_pending_messages();
59 private static class LDKOffersMessageHandlerHolder { OffersMessageHandler held; }
60 public static OffersMessageHandler new_impl(OffersMessageHandlerInterface arg) {
61 final LDKOffersMessageHandlerHolder impl_holder = new LDKOffersMessageHandlerHolder();
62 impl_holder.held = new OffersMessageHandler(new bindings.LDKOffersMessageHandler() {
63 @Override public long handle_message(long message) {
64 org.ldk.structs.OffersMessage message_hu_conv = org.ldk.structs.OffersMessage.constr_from_ptr(message);
65 if (message_hu_conv != null) { message_hu_conv.ptrs_to.add(this); };
66 Option_OffersMessageZ ret = arg.handle_message(message_hu_conv);
67 Reference.reachabilityFence(arg);
68 long result = ret.clone_ptr();
69 if (impl_holder.held != null) { impl_holder.held.ptrs_to.add(ret); };
72 @Override public long[] release_pending_messages() {
73 ThreeTuple_OffersMessageDestinationBlindedPathZ[] ret = arg.release_pending_messages();
74 Reference.reachabilityFence(arg);
75 long[] result = ret != null ? Arrays.stream(ret).mapToLong(ret_conv_49 -> ret_conv_49.clone_ptr()).toArray() : null;
79 return impl_holder.held;
82 * Handles the given message by either responding with an [`Bolt12Invoice`], sending a payment,
83 * or replying with an error.
85 * The returned [`OffersMessage`], if any, is enqueued to be sent by [`OnionMessenger`].
87 * [`OnionMessenger`]: crate::onion_message::messenger::OnionMessenger
89 public Option_OffersMessageZ handle_message(org.ldk.structs.OffersMessage message) {
90 long ret = bindings.OffersMessageHandler_handle_message(this.ptr, message.ptr);
91 Reference.reachabilityFence(this);
92 Reference.reachabilityFence(message);
93 if (ret >= 0 && ret <= 4096) { return null; }
94 org.ldk.structs.Option_OffersMessageZ ret_hu_conv = org.ldk.structs.Option_OffersMessageZ.constr_from_ptr(ret);
95 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };
96 if (this != null) { this.ptrs_to.add(message); };
101 * Releases any [`OffersMessage`]s that need to be sent.
103 * Typically, this is used for messages initiating a payment flow rather than in response to
104 * another message. The latter should use the return value of [`Self::handle_message`].
106 public ThreeTuple_OffersMessageDestinationBlindedPathZ[] release_pending_messages() {
107 long[] ret = bindings.OffersMessageHandler_release_pending_messages(this.ptr);
108 Reference.reachabilityFence(this);
109 int ret_conv_49_len = ret.length;
110 ThreeTuple_OffersMessageDestinationBlindedPathZ[] ret_conv_49_arr = new ThreeTuple_OffersMessageDestinationBlindedPathZ[ret_conv_49_len];
111 for (int x = 0; x < ret_conv_49_len; x++) {
112 long ret_conv_49 = ret[x];
113 ThreeTuple_OffersMessageDestinationBlindedPathZ ret_conv_49_hu_conv = new ThreeTuple_OffersMessageDestinationBlindedPathZ(null, ret_conv_49);
114 if (ret_conv_49_hu_conv != null) { ret_conv_49_hu_conv.ptrs_to.add(this); };
115 ret_conv_49_arr[x] = ret_conv_49_hu_conv;
117 return ret_conv_49_arr;