]> git.bitcoin.ninja Git - ldk-java/blob - src/main/java/org/ldk/structs/OffersMessageHandler.java
Merge pull request #161 from TheBlueMatt/main
[ldk-java] / src / main / java / org / ldk / structs / OffersMessageHandler.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 for an [`OnionMessage`] containing a BOLT 12 Offers message as its payload.
12  * 
13  * [`OnionMessage`]: crate::ln::msgs::OnionMessage
14  */
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;
23         }
24         @Override @SuppressWarnings("deprecation")
25         protected void finalize() throws Throwable {
26                 if (ptr != 0) { bindings.OffersMessageHandler_free(ptr); } super.finalize();
27         }
28         /**
29          * Destroys the object, freeing associated resources. After this call, any access
30          * to this object may result in a SEGFAULT or worse.
31          *
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
35          * until the GC runs.
36          */
37         public void destroy() {
38                 if (ptr != 0) { bindings.OffersMessageHandler_free(ptr); }
39                 ptr = 0;
40         }
41         public static interface OffersMessageHandlerInterface {
42                 /**
43                  * Handles the given message by either responding with an [`Bolt12Invoice`], sending a payment,
44                  * or replying with an error.
45                  * 
46                  * The returned [`OffersMessage`], if any, is enqueued to be sent by [`OnionMessenger`].
47                  * 
48                  * [`OnionMessenger`]: crate::onion_message::messenger::OnionMessenger
49                  */
50                 Option_OffersMessageZ handle_message(OffersMessage message);
51                 /**
52                  * Releases any [`OffersMessage`]s that need to be sent.
53                  * 
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`].
56                  */
57                 ThreeTuple_OffersMessageDestinationBlindedPathZ[] release_pending_messages();
58         }
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); };
70                                 return result;
71                         }
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;
76                                 return result;
77                         }
78                 });
79                 return impl_holder.held;
80         }
81         /**
82          * Handles the given message by either responding with an [`Bolt12Invoice`], sending a payment,
83          * or replying with an error.
84          * 
85          * The returned [`OffersMessage`], if any, is enqueued to be sent by [`OnionMessenger`].
86          * 
87          * [`OnionMessenger`]: crate::onion_message::messenger::OnionMessenger
88          */
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); };
97                 return ret_hu_conv;
98         }
99
100         /**
101          * Releases any [`OffersMessage`]s that need to be sent.
102          * 
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`].
105          */
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;
116                 }
117                 return ret_conv_49_arr;
118         }
119
120 }