95bd437934fcd5c955e7e302e9f2db8c8a51f04c
[ldk-java] / src / main / java / org / ldk / structs / MessageRouter.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 [`OnionMessage`].
12  */
13 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
14 public class MessageRouter extends CommonBase {
15         final bindings.LDKMessageRouter bindings_instance;
16         MessageRouter(Object _dummy, long ptr) { super(ptr); bindings_instance = null; }
17         private MessageRouter(bindings.LDKMessageRouter arg) {
18                 super(bindings.LDKMessageRouter_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.MessageRouter_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.MessageRouter_free(ptr); }
37                 ptr = 0;
38         }
39         public static interface MessageRouterInterface {
40                 /**
41                  * Returns a route for sending an [`OnionMessage`] to the given [`Destination`].
42                  */
43                 Result_OnionMessagePathNoneZ find_path(byte[] sender, byte[][] peers, Destination destination);
44         }
45         private static class LDKMessageRouterHolder { MessageRouter held; }
46         public static MessageRouter new_impl(MessageRouterInterface arg) {
47                 final LDKMessageRouterHolder impl_holder = new LDKMessageRouterHolder();
48                 impl_holder.held = new MessageRouter(new bindings.LDKMessageRouter() {
49                         @Override public long find_path(byte[] sender, byte[][] peers, long destination) {
50                                 org.ldk.structs.Destination destination_hu_conv = org.ldk.structs.Destination.constr_from_ptr(destination);
51                                 if (destination_hu_conv != null) { destination_hu_conv.ptrs_to.add(this); };
52                                 Result_OnionMessagePathNoneZ ret = arg.find_path(sender, peers, destination_hu_conv);
53                                 Reference.reachabilityFence(arg);
54                                 long result = ret == null ? 0 : ret.clone_ptr();
55                                 return result;
56                         }
57                 });
58                 return impl_holder.held;
59         }
60         /**
61          * Returns a route for sending an [`OnionMessage`] to the given [`Destination`].
62          */
63         public Result_OnionMessagePathNoneZ find_path(byte[] sender, byte[][] peers, org.ldk.structs.Destination destination) {
64                 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);
65                 Reference.reachabilityFence(this);
66                 Reference.reachabilityFence(sender);
67                 Reference.reachabilityFence(peers);
68                 Reference.reachabilityFence(destination);
69                 if (ret >= 0 && ret <= 4096) { return null; }
70                 Result_OnionMessagePathNoneZ ret_hu_conv = Result_OnionMessagePathNoneZ.constr_from_ptr(ret);
71                 if (this != null) { this.ptrs_to.add(destination); };
72                 return ret_hu_conv;
73         }
74
75 }