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 [`OnionMessage`].
13 * [`OnionMessage`]: msgs::OnionMessage
15 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
16 public class MessageRouter extends CommonBase {
17 final bindings.LDKMessageRouter bindings_instance;
18 MessageRouter(Object _dummy, long ptr) { super(ptr); bindings_instance = null; }
19 private MessageRouter(bindings.LDKMessageRouter arg) {
20 super(bindings.LDKMessageRouter_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.MessageRouter_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.MessageRouter_free(ptr); }
41 public static interface MessageRouterInterface {
43 * Returns a route for sending an [`OnionMessage`] to the given [`Destination`].
45 * [`OnionMessage`]: msgs::OnionMessage
47 Result_OnionMessagePathNoneZ find_path(byte[] sender, byte[][] peers, Destination destination);
49 private static class LDKMessageRouterHolder { MessageRouter held; }
50 public static MessageRouter new_impl(MessageRouterInterface arg) {
51 final LDKMessageRouterHolder impl_holder = new LDKMessageRouterHolder();
52 impl_holder.held = new MessageRouter(new bindings.LDKMessageRouter() {
53 @Override public long find_path(byte[] sender, byte[][] peers, long destination) {
54 org.ldk.structs.Destination destination_hu_conv = org.ldk.structs.Destination.constr_from_ptr(destination);
55 if (destination_hu_conv != null) { destination_hu_conv.ptrs_to.add(this); };
56 Result_OnionMessagePathNoneZ ret = arg.find_path(sender, peers, destination_hu_conv);
57 Reference.reachabilityFence(arg);
58 long result = ret == null ? 0 : ret.clone_ptr();
62 return impl_holder.held;
65 * Returns a route for sending an [`OnionMessage`] to the given [`Destination`].
67 * [`OnionMessage`]: msgs::OnionMessage
69 public Result_OnionMessagePathNoneZ find_path(byte[] sender, byte[][] peers, org.ldk.structs.Destination destination) {
70 long ret = bindings.MessageRouter_find_path(this.ptr, InternalUtils.check_arr_len(sender, 33), peers != null ? Arrays.stream(peers).map(peers_conv_8 -> InternalUtils.check_arr_len(peers_conv_8, 33)).toArray(byte[][]::new) : null, destination.ptr);
71 Reference.reachabilityFence(this);
72 Reference.reachabilityFence(sender);
73 Reference.reachabilityFence(peers);
74 Reference.reachabilityFence(destination);
75 if (ret >= 0 && ret <= 4096) { return null; }
76 Result_OnionMessagePathNoneZ ret_hu_conv = Result_OnionMessagePathNoneZ.constr_from_ptr(ret);
77 if (this != null) { this.ptrs_to.add(destination); };