Merge pull request #119 from TheBlueMatt/main
[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 an [`Invoice`] 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         public static interface RouterInterface {
28                 /**
29                  * Finds a [`Route`] between `payer` and `payee` for a payment with the given values.
30                  * 
31                  * Note that first_hops (or a relevant inner pointer) may be NULL or all-0s to represent None
32                  */
33                 Result_RouteLightningErrorZ find_route(byte[] payer, RouteParameters route_params, byte[] payment_hash, ChannelDetails[] first_hops, InFlightHtlcs inflight_htlcs);
34                 /**
35                  * Lets the router know that payment through a specific path has failed.
36                  */
37                 void notify_payment_path_failed(RouteHop[] path, long short_channel_id);
38                 /**
39                  * Lets the router know that payment through a specific path was successful.
40                  */
41                 void notify_payment_path_successful(RouteHop[] path);
42                 /**
43                  * Lets the router know that a payment probe was successful.
44                  */
45                 void notify_payment_probe_successful(RouteHop[] path);
46                 /**
47                  * Lets the router know that a payment probe failed.
48                  */
49                 void notify_payment_probe_failed(RouteHop[] path, long short_channel_id);
50         }
51         private static class LDKRouterHolder { Router held; }
52         public static Router new_impl(RouterInterface arg) {
53                 final LDKRouterHolder impl_holder = new LDKRouterHolder();
54                 impl_holder.held = new Router(new bindings.LDKRouter() {
55                         @Override public long find_route(byte[] payer, long route_params, byte[] payment_hash, long[] first_hops, long inflight_htlcs) {
56                                 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); }
57                                 int first_hops_conv_16_len = first_hops.length;
58                                 ChannelDetails[] first_hops_conv_16_arr = new ChannelDetails[first_hops_conv_16_len];
59                                 for (int q = 0; q < first_hops_conv_16_len; q++) {
60                                         long first_hops_conv_16 = first_hops[q];
61                                         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); }
62                                         if (first_hops_conv_16_hu_conv != null) { first_hops_conv_16_hu_conv.ptrs_to.add(this); };
63                                         first_hops_conv_16_arr[q] = first_hops_conv_16_hu_conv;
64                                 }
65                                 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); }
66                                 if (inflight_htlcs_hu_conv != null) { inflight_htlcs_hu_conv.ptrs_to.add(this); };
67                                 Result_RouteLightningErrorZ ret = arg.find_route(payer, route_params_hu_conv, payment_hash, first_hops_conv_16_arr, inflight_htlcs_hu_conv);
68                                 Reference.reachabilityFence(arg);
69                                 long result = ret == null ? 0 : ret.clone_ptr();
70                                 return result;
71                         }
72                         @Override public void notify_payment_path_failed(long[] path, long short_channel_id) {
73                                 int path_conv_10_len = path.length;
74                                 RouteHop[] path_conv_10_arr = new RouteHop[path_conv_10_len];
75                                 for (int k = 0; k < path_conv_10_len; k++) {
76                                         long path_conv_10 = path[k];
77                                         org.ldk.structs.RouteHop path_conv_10_hu_conv = null; if (path_conv_10 < 0 || path_conv_10 > 4096) { path_conv_10_hu_conv = new org.ldk.structs.RouteHop(null, path_conv_10); }
78                                         if (path_conv_10_hu_conv != null) { path_conv_10_hu_conv.ptrs_to.add(this); };
79                                         path_conv_10_arr[k] = path_conv_10_hu_conv;
80                                 }
81                                 arg.notify_payment_path_failed(path_conv_10_arr, short_channel_id);
82                                 Reference.reachabilityFence(arg);
83                         }
84                         @Override public void notify_payment_path_successful(long[] path) {
85                                 int path_conv_10_len = path.length;
86                                 RouteHop[] path_conv_10_arr = new RouteHop[path_conv_10_len];
87                                 for (int k = 0; k < path_conv_10_len; k++) {
88                                         long path_conv_10 = path[k];
89                                         org.ldk.structs.RouteHop path_conv_10_hu_conv = null; if (path_conv_10 < 0 || path_conv_10 > 4096) { path_conv_10_hu_conv = new org.ldk.structs.RouteHop(null, path_conv_10); }
90                                         if (path_conv_10_hu_conv != null) { path_conv_10_hu_conv.ptrs_to.add(this); };
91                                         path_conv_10_arr[k] = path_conv_10_hu_conv;
92                                 }
93                                 arg.notify_payment_path_successful(path_conv_10_arr);
94                                 Reference.reachabilityFence(arg);
95                         }
96                         @Override public void notify_payment_probe_successful(long[] path) {
97                                 int path_conv_10_len = path.length;
98                                 RouteHop[] path_conv_10_arr = new RouteHop[path_conv_10_len];
99                                 for (int k = 0; k < path_conv_10_len; k++) {
100                                         long path_conv_10 = path[k];
101                                         org.ldk.structs.RouteHop path_conv_10_hu_conv = null; if (path_conv_10 < 0 || path_conv_10 > 4096) { path_conv_10_hu_conv = new org.ldk.structs.RouteHop(null, path_conv_10); }
102                                         if (path_conv_10_hu_conv != null) { path_conv_10_hu_conv.ptrs_to.add(this); };
103                                         path_conv_10_arr[k] = path_conv_10_hu_conv;
104                                 }
105                                 arg.notify_payment_probe_successful(path_conv_10_arr);
106                                 Reference.reachabilityFence(arg);
107                         }
108                         @Override public void notify_payment_probe_failed(long[] path, long short_channel_id) {
109                                 int path_conv_10_len = path.length;
110                                 RouteHop[] path_conv_10_arr = new RouteHop[path_conv_10_len];
111                                 for (int k = 0; k < path_conv_10_len; k++) {
112                                         long path_conv_10 = path[k];
113                                         org.ldk.structs.RouteHop path_conv_10_hu_conv = null; if (path_conv_10 < 0 || path_conv_10 > 4096) { path_conv_10_hu_conv = new org.ldk.structs.RouteHop(null, path_conv_10); }
114                                         if (path_conv_10_hu_conv != null) { path_conv_10_hu_conv.ptrs_to.add(this); };
115                                         path_conv_10_arr[k] = path_conv_10_hu_conv;
116                                 }
117                                 arg.notify_payment_probe_failed(path_conv_10_arr, short_channel_id);
118                                 Reference.reachabilityFence(arg);
119                         }
120                 });
121                 return impl_holder.held;
122         }
123         /**
124          * Finds a [`Route`] between `payer` and `payee` for a payment with the given values.
125          * 
126          * Note that first_hops (or a relevant inner pointer) may be NULL or all-0s to represent None
127          */
128         public Result_RouteLightningErrorZ find_route(byte[] payer, RouteParameters route_params, byte[] payment_hash, @Nullable ChannelDetails[] first_hops, InFlightHtlcs inflight_htlcs) {
129                 long ret = bindings.Router_find_route(this.ptr, InternalUtils.check_arr_len(payer, 33), route_params == null ? 0 : route_params.ptr, InternalUtils.check_arr_len(payment_hash, 32), 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);
130                 Reference.reachabilityFence(this);
131                 Reference.reachabilityFence(payer);
132                 Reference.reachabilityFence(route_params);
133                 Reference.reachabilityFence(payment_hash);
134                 Reference.reachabilityFence(first_hops);
135                 Reference.reachabilityFence(inflight_htlcs);
136                 if (ret >= 0 && ret <= 4096) { return null; }
137                 Result_RouteLightningErrorZ ret_hu_conv = Result_RouteLightningErrorZ.constr_from_ptr(ret);
138                 if (this != null) { this.ptrs_to.add(route_params); };
139                 for (ChannelDetails first_hops_conv_16: first_hops) { if (this != null) { this.ptrs_to.add(first_hops_conv_16); }; };
140                 if (this != null) { this.ptrs_to.add(inflight_htlcs); };
141                 // Due to rust's strict-ownership memory model, in some cases we need to "move"
142                 // an object to pass exclusive ownership to the function being called.
143                 // In most cases, we avoid this being visible in GC'd languages by cloning the object
144                 // at the FFI layer, creating a new object which Rust can claim ownership of
145                 // However, in some cases (eg here), there is no way to clone an object, and thus
146                 // we actually have to pass full ownership to Rust.
147                 // Thus, after this call, inflight_htlcs is reset to null and is now a dummy object.
148                 inflight_htlcs.ptr = 0;;
149                 return ret_hu_conv;
150         }
151
152         /**
153          * Lets the router know that payment through a specific path has failed.
154          */
155         public void notify_payment_path_failed(RouteHop[] path, long short_channel_id) {
156                 bindings.Router_notify_payment_path_failed(this.ptr, path != null ? Arrays.stream(path).mapToLong(path_conv_10 -> path_conv_10 == null ? 0 : path_conv_10.ptr).toArray() : null, short_channel_id);
157                 Reference.reachabilityFence(this);
158                 Reference.reachabilityFence(path);
159                 Reference.reachabilityFence(short_channel_id);
160                 for (RouteHop path_conv_10: path) { if (this != null) { this.ptrs_to.add(path_conv_10); }; };
161         }
162
163         /**
164          * Lets the router know that payment through a specific path was successful.
165          */
166         public void notify_payment_path_successful(RouteHop[] path) {
167                 bindings.Router_notify_payment_path_successful(this.ptr, path != null ? Arrays.stream(path).mapToLong(path_conv_10 -> path_conv_10 == null ? 0 : path_conv_10.ptr).toArray() : null);
168                 Reference.reachabilityFence(this);
169                 Reference.reachabilityFence(path);
170                 for (RouteHop path_conv_10: path) { if (this != null) { this.ptrs_to.add(path_conv_10); }; };
171         }
172
173         /**
174          * Lets the router know that a payment probe was successful.
175          */
176         public void notify_payment_probe_successful(RouteHop[] path) {
177                 bindings.Router_notify_payment_probe_successful(this.ptr, path != null ? Arrays.stream(path).mapToLong(path_conv_10 -> path_conv_10 == null ? 0 : path_conv_10.ptr).toArray() : null);
178                 Reference.reachabilityFence(this);
179                 Reference.reachabilityFence(path);
180                 for (RouteHop path_conv_10: path) { if (this != null) { this.ptrs_to.add(path_conv_10); }; };
181         }
182
183         /**
184          * Lets the router know that a payment probe failed.
185          */
186         public void notify_payment_probe_failed(RouteHop[] path, long short_channel_id) {
187                 bindings.Router_notify_payment_probe_failed(this.ptr, path != null ? Arrays.stream(path).mapToLong(path_conv_10 -> path_conv_10 == null ? 0 : path_conv_10.ptr).toArray() : null, short_channel_id);
188                 Reference.reachabilityFence(this);
189                 Reference.reachabilityFence(path);
190                 Reference.reachabilityFence(short_channel_id);
191                 for (RouteHop path_conv_10: path) { if (this != null) { this.ptrs_to.add(path_conv_10); }; };
192         }
193
194 }