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 trait defining behavior for routing an [`Invoice`] payment.
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;
22 @Override @SuppressWarnings("deprecation")
23 protected void finalize() throws Throwable {
24 if (ptr != 0) { bindings.Router_free(ptr); } super.finalize();
27 public static interface RouterInterface {
29 * Finds a [`Route`] between `payer` and `payee` for a payment with the given values.
31 * Note that first_hops (or a relevant inner pointer) may be NULL or all-0s to represent None
33 Result_RouteLightningErrorZ find_route(byte[] payer, RouteParameters route_params, byte[] payment_hash, ChannelDetails[] first_hops, InFlightHtlcs inflight_htlcs);
35 * Lets the router know that payment through a specific path has failed.
37 void notify_payment_path_failed(RouteHop[] path, long short_channel_id);
39 * Lets the router know that payment through a specific path was successful.
41 void notify_payment_path_successful(RouteHop[] path);
43 * Lets the router know that a payment probe was successful.
45 void notify_payment_probe_successful(RouteHop[] path);
47 * Lets the router know that a payment probe failed.
49 void notify_payment_probe_failed(RouteHop[] path, long short_channel_id);
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 if (first_hops != null) {
60 for (int q = 0; q < first_hops_conv_16_len; q++) {
61 long first_hops_conv_16 = first_hops[q];
62 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); }
63 if (first_hops_conv_16_hu_conv != null) { first_hops_conv_16_hu_conv.ptrs_to.add(this); };
64 first_hops_conv_16_arr[q] = first_hops_conv_16_hu_conv;
67 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); }
68 if (inflight_htlcs_hu_conv != null) { inflight_htlcs_hu_conv.ptrs_to.add(this); };
69 Result_RouteLightningErrorZ ret = arg.find_route(payer, route_params_hu_conv, payment_hash, first_hops_conv_16_arr, inflight_htlcs_hu_conv);
70 Reference.reachabilityFence(arg);
71 long result = ret == null ? 0 : ret.clone_ptr();
74 @Override public void notify_payment_path_failed(long[] path, long short_channel_id) {
75 int path_conv_10_len = path.length;
76 RouteHop[] path_conv_10_arr = new RouteHop[path_conv_10_len];
77 for (int k = 0; k < path_conv_10_len; k++) {
78 long path_conv_10 = path[k];
79 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); }
80 if (path_conv_10_hu_conv != null) { path_conv_10_hu_conv.ptrs_to.add(this); };
81 path_conv_10_arr[k] = path_conv_10_hu_conv;
83 arg.notify_payment_path_failed(path_conv_10_arr, short_channel_id);
84 Reference.reachabilityFence(arg);
86 @Override public void notify_payment_path_successful(long[] path) {
87 int path_conv_10_len = path.length;
88 RouteHop[] path_conv_10_arr = new RouteHop[path_conv_10_len];
89 for (int k = 0; k < path_conv_10_len; k++) {
90 long path_conv_10 = path[k];
91 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); }
92 if (path_conv_10_hu_conv != null) { path_conv_10_hu_conv.ptrs_to.add(this); };
93 path_conv_10_arr[k] = path_conv_10_hu_conv;
95 arg.notify_payment_path_successful(path_conv_10_arr);
96 Reference.reachabilityFence(arg);
98 @Override public void notify_payment_probe_successful(long[] path) {
99 int path_conv_10_len = path.length;
100 RouteHop[] path_conv_10_arr = new RouteHop[path_conv_10_len];
101 for (int k = 0; k < path_conv_10_len; k++) {
102 long path_conv_10 = path[k];
103 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); }
104 if (path_conv_10_hu_conv != null) { path_conv_10_hu_conv.ptrs_to.add(this); };
105 path_conv_10_arr[k] = path_conv_10_hu_conv;
107 arg.notify_payment_probe_successful(path_conv_10_arr);
108 Reference.reachabilityFence(arg);
110 @Override public void notify_payment_probe_failed(long[] path, long short_channel_id) {
111 int path_conv_10_len = path.length;
112 RouteHop[] path_conv_10_arr = new RouteHop[path_conv_10_len];
113 for (int k = 0; k < path_conv_10_len; k++) {
114 long path_conv_10 = path[k];
115 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); }
116 if (path_conv_10_hu_conv != null) { path_conv_10_hu_conv.ptrs_to.add(this); };
117 path_conv_10_arr[k] = path_conv_10_hu_conv;
119 arg.notify_payment_probe_failed(path_conv_10_arr, short_channel_id);
120 Reference.reachabilityFence(arg);
123 return impl_holder.held;
126 * Finds a [`Route`] between `payer` and `payee` for a payment with the given values.
128 * Note that first_hops (or a relevant inner pointer) may be NULL or all-0s to represent None
130 public Result_RouteLightningErrorZ find_route(byte[] payer, RouteParameters route_params, byte[] payment_hash, @Nullable ChannelDetails[] first_hops, InFlightHtlcs inflight_htlcs) {
131 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);
132 Reference.reachabilityFence(this);
133 Reference.reachabilityFence(payer);
134 Reference.reachabilityFence(route_params);
135 Reference.reachabilityFence(payment_hash);
136 Reference.reachabilityFence(first_hops);
137 Reference.reachabilityFence(inflight_htlcs);
138 if (ret >= 0 && ret <= 4096) { return null; }
139 Result_RouteLightningErrorZ ret_hu_conv = Result_RouteLightningErrorZ.constr_from_ptr(ret);
140 if (this != null) { this.ptrs_to.add(route_params); };
141 if (first_hops != null) { for (ChannelDetails first_hops_conv_16: first_hops) { if (this != null) { this.ptrs_to.add(first_hops_conv_16); }; } };
142 if (this != null) { this.ptrs_to.add(inflight_htlcs); };
143 // Due to rust's strict-ownership memory model, in some cases we need to "move"
144 // an object to pass exclusive ownership to the function being called.
145 // In most cases, we avoid this being visible in GC'd languages by cloning the object
146 // at the FFI layer, creating a new object which Rust can claim ownership of
147 // However, in some cases (eg here), there is no way to clone an object, and thus
148 // we actually have to pass full ownership to Rust.
149 // Thus, after this call, inflight_htlcs is reset to null and is now a dummy object.
150 inflight_htlcs.ptr = 0;;
155 * Lets the router know that payment through a specific path has failed.
157 public void notify_payment_path_failed(RouteHop[] path, long short_channel_id) {
158 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);
159 Reference.reachabilityFence(this);
160 Reference.reachabilityFence(path);
161 Reference.reachabilityFence(short_channel_id);
162 for (RouteHop path_conv_10: path) { if (this != null) { this.ptrs_to.add(path_conv_10); }; };
166 * Lets the router know that payment through a specific path was successful.
168 public void notify_payment_path_successful(RouteHop[] path) {
169 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);
170 Reference.reachabilityFence(this);
171 Reference.reachabilityFence(path);
172 for (RouteHop path_conv_10: path) { if (this != null) { this.ptrs_to.add(path_conv_10); }; };
176 * Lets the router know that a payment probe was successful.
178 public void notify_payment_probe_successful(RouteHop[] path) {
179 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);
180 Reference.reachabilityFence(this);
181 Reference.reachabilityFence(path);
182 for (RouteHop path_conv_10: path) { if (this != null) { this.ptrs_to.add(path_conv_10); }; };
186 * Lets the router know that a payment probe failed.
188 public void notify_payment_probe_failed(RouteHop[] path, long short_channel_id) {
189 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);
190 Reference.reachabilityFence(this);
191 Reference.reachabilityFence(path);
192 Reference.reachabilityFence(short_channel_id);
193 for (RouteHop path_conv_10: path) { if (this != null) { this.ptrs_to.add(path_conv_10); }; };