82be8420739e8d1710bc461cdfe6ffd43a6c7420
[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  * [`OnionMessage`]: msgs::OnionMessage
14  */
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;
23         }
24         @Override @SuppressWarnings("deprecation")
25         protected void finalize() throws Throwable {
26                 if (ptr != 0) { bindings.MessageRouter_free(ptr); } super.finalize();
27         }
28         /**
29          * Destroys the object, freeing associated resources. After this call, any access
30          * to this object may result in a SEGFAULT or worse.
31          *
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
35          * until the GC runs.
36          */
37         public void destroy() {
38                 if (ptr != 0) { bindings.MessageRouter_free(ptr); }
39                 ptr = 0;
40         }
41         public static interface MessageRouterInterface {
42                 /**
43                  * Returns a route for sending an [`OnionMessage`] to the given [`Destination`].
44                  * 
45                  * [`OnionMessage`]: msgs::OnionMessage
46                  */
47                 Result_OnionMessagePathNoneZ find_path(byte[] sender, byte[][] peers, Destination destination);
48         }
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();
59                                 return result;
60                         }
61                 });
62                 return impl_holder.held;
63         }
64         /**
65          * Returns a route for sending an [`OnionMessage`] to the given [`Destination`].
66          * 
67          * [`OnionMessage`]: msgs::OnionMessage
68          */
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); };
78                 return ret_hu_conv;
79         }
80
81 }