6b40b272d7e05eed79d3602479bd2369a8f261fb
[ldk-java] / src / main / java / org / ldk / structs / Router.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 trait defining behavior for routing a payment.
12  */
13 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
14 public class Router extends CommonBase {
15         final bindings.LDKRouter bindings_instance;
16         Router(Object _dummy, long ptr) { super(ptr); bindings_instance = null; }
17         private Router(bindings.LDKRouter arg) {
18                 super(bindings.LDKRouter_new(arg));
19                 this.ptrs_to.add(arg);
20                 this.bindings_instance = arg;
21         }
22         @Override @SuppressWarnings("deprecation")
23         protected void finalize() throws Throwable {
24                 if (ptr != 0) { bindings.Router_free(ptr); } super.finalize();
25         }
26         /**
27          * Destroys the object, freeing associated resources. After this call, any access
28          * to this object may result in a SEGFAULT or worse.
29          *
30          * You should generally NEVER call this method. You should let the garbage collector
31          * do this for you when it finalizes objects. However, it may be useful for types
32          * which represent locks and should be closed immediately to avoid holding locks
33          * until the GC runs.
34          */
35         public void destroy() {
36                 if (ptr != 0) { bindings.Router_free(ptr); }
37                 ptr = 0;
38         }
39         public static interface RouterInterface {
40                 /**
41                  * Finds a [`Route`] for a payment between the given `payer` and a payee.
42                  * 
43                  * The `payee` and the payment's value are given in [`RouteParameters::payment_params`]
44                  * and [`RouteParameters::final_value_msat`], respectively.
45                  * 
46                  * Note that first_hops (or a relevant inner pointer) may be NULL or all-0s to represent None
47                  */
48                 Result_RouteLightningErrorZ find_route(byte[] payer, RouteParameters route_params, ChannelDetails[] first_hops, InFlightHtlcs inflight_htlcs);
49                 /**
50                  * Finds a [`Route`] for a payment between the given `payer` and a payee.
51                  * 
52                  * The `payee` and the payment's value are given in [`RouteParameters::payment_params`]
53                  * and [`RouteParameters::final_value_msat`], respectively.
54                  * 
55                  * Includes a [`PaymentHash`] and a [`PaymentId`] to be able to correlate the request with a specific
56                  * payment.
57                  * 
58                  * Note that first_hops (or a relevant inner pointer) may be NULL or all-0s to represent None
59                  */
60                 Result_RouteLightningErrorZ find_route_with_id(byte[] payer, RouteParameters route_params, ChannelDetails[] first_hops, InFlightHtlcs inflight_htlcs, byte[] _payment_hash, byte[] _payment_id);
61         }
62         private static class LDKRouterHolder { Router held; }
63         public static Router new_impl(RouterInterface arg) {
64                 final LDKRouterHolder impl_holder = new LDKRouterHolder();
65                 impl_holder.held = new Router(new bindings.LDKRouter() {
66                         @Override public long find_route(byte[] payer, long route_params, long[] first_hops, long inflight_htlcs) {
67                                 org.ldk.structs.RouteParameters route_params_hu_conv = null; if (route_params < 0 || route_params > 4096) { route_params_hu_conv = new org.ldk.structs.RouteParameters(null, route_params); }
68                                 int first_hops_conv_16_len = first_hops.length;
69                                 ChannelDetails[] first_hops_conv_16_arr = new ChannelDetails[first_hops_conv_16_len];
70                                 if (first_hops != null) {
71                                         for (int q = 0; q < first_hops_conv_16_len; q++) {
72                                                 long first_hops_conv_16 = first_hops[q];
73                                                 org.ldk.structs.ChannelDetails first_hops_conv_16_hu_conv = null; if (first_hops_conv_16 < 0 || first_hops_conv_16 > 4096) { first_hops_conv_16_hu_conv = new org.ldk.structs.ChannelDetails(null, first_hops_conv_16); }
74                                                 if (first_hops_conv_16_hu_conv != null) { first_hops_conv_16_hu_conv.ptrs_to.add(this); };
75                                                 first_hops_conv_16_arr[q] = first_hops_conv_16_hu_conv;
76                                         }
77                                 }
78                                 org.ldk.structs.InFlightHtlcs inflight_htlcs_hu_conv = null; if (inflight_htlcs < 0 || inflight_htlcs > 4096) { inflight_htlcs_hu_conv = new org.ldk.structs.InFlightHtlcs(null, inflight_htlcs); }
79                                 if (inflight_htlcs_hu_conv != null) { inflight_htlcs_hu_conv.ptrs_to.add(this); };
80                                 Result_RouteLightningErrorZ ret = arg.find_route(payer, route_params_hu_conv, first_hops_conv_16_arr, inflight_htlcs_hu_conv);
81                                 Reference.reachabilityFence(arg);
82                                 long result = ret == null ? 0 : ret.clone_ptr();
83                                 return result;
84                         }
85                         @Override public long find_route_with_id(byte[] payer, long route_params, long[] first_hops, long inflight_htlcs, byte[] _payment_hash, byte[] _payment_id) {
86                                 org.ldk.structs.RouteParameters route_params_hu_conv = null; if (route_params < 0 || route_params > 4096) { route_params_hu_conv = new org.ldk.structs.RouteParameters(null, route_params); }
87                                 int first_hops_conv_16_len = first_hops.length;
88                                 ChannelDetails[] first_hops_conv_16_arr = new ChannelDetails[first_hops_conv_16_len];
89                                 if (first_hops != null) {
90                                         for (int q = 0; q < first_hops_conv_16_len; q++) {
91                                                 long first_hops_conv_16 = first_hops[q];
92                                                 org.ldk.structs.ChannelDetails first_hops_conv_16_hu_conv = null; if (first_hops_conv_16 < 0 || first_hops_conv_16 > 4096) { first_hops_conv_16_hu_conv = new org.ldk.structs.ChannelDetails(null, first_hops_conv_16); }
93                                                 if (first_hops_conv_16_hu_conv != null) { first_hops_conv_16_hu_conv.ptrs_to.add(this); };
94                                                 first_hops_conv_16_arr[q] = first_hops_conv_16_hu_conv;
95                                         }
96                                 }
97                                 org.ldk.structs.InFlightHtlcs inflight_htlcs_hu_conv = null; if (inflight_htlcs < 0 || inflight_htlcs > 4096) { inflight_htlcs_hu_conv = new org.ldk.structs.InFlightHtlcs(null, inflight_htlcs); }
98                                 if (inflight_htlcs_hu_conv != null) { inflight_htlcs_hu_conv.ptrs_to.add(this); };
99                                 Result_RouteLightningErrorZ ret = arg.find_route_with_id(payer, route_params_hu_conv, first_hops_conv_16_arr, inflight_htlcs_hu_conv, _payment_hash, _payment_id);
100                                 Reference.reachabilityFence(arg);
101                                 long result = ret == null ? 0 : ret.clone_ptr();
102                                 return result;
103                         }
104                 });
105                 return impl_holder.held;
106         }
107         /**
108          * Finds a [`Route`] for a payment between the given `payer` and a payee.
109          * 
110          * The `payee` and the payment's value are given in [`RouteParameters::payment_params`]
111          * and [`RouteParameters::final_value_msat`], respectively.
112          * 
113          * Note that first_hops (or a relevant inner pointer) may be NULL or all-0s to represent None
114          */
115         public Result_RouteLightningErrorZ find_route(byte[] payer, org.ldk.structs.RouteParameters route_params, @Nullable ChannelDetails[] first_hops, org.ldk.structs.InFlightHtlcs inflight_htlcs) {
116                 long ret = bindings.Router_find_route(this.ptr, InternalUtils.check_arr_len(payer, 33), route_params == null ? 0 : route_params.ptr, first_hops != null ? Arrays.stream(first_hops).mapToLong(first_hops_conv_16 -> first_hops_conv_16 == null ? 0 : first_hops_conv_16.ptr).toArray() : null, inflight_htlcs == null ? 0 : inflight_htlcs.ptr);
117                 Reference.reachabilityFence(this);
118                 Reference.reachabilityFence(payer);
119                 Reference.reachabilityFence(route_params);
120                 Reference.reachabilityFence(first_hops);
121                 Reference.reachabilityFence(inflight_htlcs);
122                 if (ret >= 0 && ret <= 4096) { return null; }
123                 Result_RouteLightningErrorZ ret_hu_conv = Result_RouteLightningErrorZ.constr_from_ptr(ret);
124                 if (this != null) { this.ptrs_to.add(route_params); };
125                 if (first_hops != null) { for (ChannelDetails first_hops_conv_16: first_hops) { if (this != null) { this.ptrs_to.add(first_hops_conv_16); }; } };
126                 if (this != null) { this.ptrs_to.add(inflight_htlcs); };
127                 return ret_hu_conv;
128         }
129
130         /**
131          * Finds a [`Route`] for a payment between the given `payer` and a payee.
132          * 
133          * The `payee` and the payment's value are given in [`RouteParameters::payment_params`]
134          * and [`RouteParameters::final_value_msat`], respectively.
135          * 
136          * Includes a [`PaymentHash`] and a [`PaymentId`] to be able to correlate the request with a specific
137          * payment.
138          * 
139          * Note that first_hops (or a relevant inner pointer) may be NULL or all-0s to represent None
140          */
141         public Result_RouteLightningErrorZ find_route_with_id(byte[] payer, org.ldk.structs.RouteParameters route_params, @Nullable ChannelDetails[] first_hops, org.ldk.structs.InFlightHtlcs inflight_htlcs, byte[] _payment_hash, byte[] _payment_id) {
142                 long ret = bindings.Router_find_route_with_id(this.ptr, InternalUtils.check_arr_len(payer, 33), route_params == null ? 0 : route_params.ptr, first_hops != null ? Arrays.stream(first_hops).mapToLong(first_hops_conv_16 -> first_hops_conv_16 == null ? 0 : first_hops_conv_16.ptr).toArray() : null, inflight_htlcs == null ? 0 : inflight_htlcs.ptr, InternalUtils.check_arr_len(_payment_hash, 32), InternalUtils.check_arr_len(_payment_id, 32));
143                 Reference.reachabilityFence(this);
144                 Reference.reachabilityFence(payer);
145                 Reference.reachabilityFence(route_params);
146                 Reference.reachabilityFence(first_hops);
147                 Reference.reachabilityFence(inflight_htlcs);
148                 Reference.reachabilityFence(_payment_hash);
149                 Reference.reachabilityFence(_payment_id);
150                 if (ret >= 0 && ret <= 4096) { return null; }
151                 Result_RouteLightningErrorZ ret_hu_conv = Result_RouteLightningErrorZ.constr_from_ptr(ret);
152                 if (this != null) { this.ptrs_to.add(route_params); };
153                 if (first_hops != null) { for (ChannelDetails first_hops_conv_16: first_hops) { if (this != null) { this.ptrs_to.add(first_hops_conv_16); }; } };
154                 if (this != null) { this.ptrs_to.add(inflight_htlcs); };
155                 return ret_hu_conv;
156         }
157
158 }