c2b1fb974eaa1474b48bd2114c4ed07905518d7b
[ldk-java] / c_sharp / src / org / ldk / structs / MessageRouter.cs
1
2 using org.ldk.impl;
3 using org.ldk.enums;
4 using org.ldk.util;
5 using System;
6
7 namespace org { namespace ldk { namespace structs {
8
9
10
11 /** An implementation of MessageRouter */
12 public interface MessageRouterInterface {
13         /**Returns a route for sending an [`OnionMessage`] to the given [`Destination`].
14          */
15         Result_OnionMessagePathNoneZ find_path(byte[] sender, byte[][] peers, Destination destination);
16 }
17
18 /**
19  * A trait defining behavior for routing an [`OnionMessage`].
20  */
21 public class MessageRouter : CommonBase {
22         internal bindings.LDKMessageRouter bindings_instance;
23         internal long instance_idx;
24
25         internal MessageRouter(object _dummy, long ptr) : base(ptr) { bindings_instance = null; }
26         ~MessageRouter() {
27                 if (ptr != 0) { bindings.MessageRouter_free(ptr); }
28         }
29
30         private class LDKMessageRouterHolder { internal MessageRouter held; }
31         private class LDKMessageRouterImpl : bindings.LDKMessageRouter {
32                 internal LDKMessageRouterImpl(MessageRouterInterface arg, LDKMessageRouterHolder impl_holder) { this.arg = arg; this.impl_holder = impl_holder; }
33                 private MessageRouterInterface arg;
34                 private LDKMessageRouterHolder impl_holder;
35                 public long find_path(long _sender, long _peers, long _destination) {
36                         byte[] _sender_conv = InternalUtils.decodeUint8Array(_sender);
37                         int _peers_conv_8_len = InternalUtils.getArrayLength(_peers);
38                         byte[][] _peers_conv_8_arr = new byte[_peers_conv_8_len][];
39                         for (int i = 0; i < _peers_conv_8_len; i++) {
40                                 long _peers_conv_8 = InternalUtils.getU64ArrayElem(_peers, i);
41                                 byte[] _peers_conv_8_conv = InternalUtils.decodeUint8Array(_peers_conv_8);
42                                 _peers_conv_8_arr[i] = _peers_conv_8_conv;
43                         }
44                         bindings.free_buffer(_peers);
45                         org.ldk.structs.Destination _destination_hu_conv = org.ldk.structs.Destination.constr_from_ptr(_destination);
46                         if (_destination_hu_conv != null) { _destination_hu_conv.ptrs_to.AddLast(this); };
47                         Result_OnionMessagePathNoneZ ret = arg.find_path(_sender_conv, _peers_conv_8_arr, _destination_hu_conv);
48                                 GC.KeepAlive(arg);
49                         long result = ret == null ? 0 : ret.clone_ptr();
50                         return result;
51                 }
52         }
53
54         /** Creates a new instance of MessageRouter from a given implementation */
55         public static MessageRouter new_impl(MessageRouterInterface arg) {
56                 LDKMessageRouterHolder impl_holder = new LDKMessageRouterHolder();
57                 LDKMessageRouterImpl impl = new LDKMessageRouterImpl(arg, impl_holder);
58                 long[] ptr_idx = bindings.LDKMessageRouter_new(impl);
59
60                 impl_holder.held = new MessageRouter(null, ptr_idx[0]);
61                 impl_holder.held.instance_idx = ptr_idx[1];
62                 impl_holder.held.bindings_instance = impl;
63                 return impl_holder.held;
64         }
65
66         /**
67          * Returns a route for sending an [`OnionMessage`] to the given [`Destination`].
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.encodeUint8Array(InternalUtils.check_arr_len(sender, 33)), InternalUtils.encodeUint64Array(InternalUtils.mapArray(peers, peers_conv_8 => InternalUtils.encodeUint8Array(InternalUtils.check_arr_len(peers_conv_8, 33)))), destination.ptr);
71                 GC.KeepAlive(this);
72                 GC.KeepAlive(sender);
73                 GC.KeepAlive(peers);
74                 GC.KeepAlive(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.AddLast(destination); };
78                 return ret_hu_conv;
79         }
80
81 }
82 } } }