46e5da4f354b5d82927782eef3bcb7ed4447a0a8
[ldk-java] / src / main / java / org / ldk / structs / OnionMessageHandler.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 to describe an object that can receive onion messages.
12  */
13 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
14 public class OnionMessageHandler extends CommonBase {
15         final bindings.LDKOnionMessageHandler bindings_instance;
16         OnionMessageHandler(Object _dummy, long ptr) { super(ptr); bindings_instance = null; }
17         private OnionMessageHandler(bindings.LDKOnionMessageHandler arg, bindings.LDKOnionMessageProvider OnionMessageProvider) {
18                 super(bindings.LDKOnionMessageHandler_new(arg, OnionMessageProvider));
19                 this.ptrs_to.add(arg);
20                 this.ptrs_to.add(OnionMessageProvider);
21                 this.bindings_instance = arg;
22         }
23         @Override @SuppressWarnings("deprecation")
24         protected void finalize() throws Throwable {
25                 if (ptr != 0) { bindings.OnionMessageHandler_free(ptr); } super.finalize();
26         }
27         /**
28          * Destroys the object, freeing associated resources. After this call, any access
29          * to this object may result in a SEGFAULT or worse.
30          *
31          * You should generally NEVER call this method. You should let the garbage collector
32          * do this for you when it finalizes objects. However, it may be useful for types
33          * which represent locks and should be closed immediately to avoid holding locks
34          * until the GC runs.
35          */
36         public void destroy() {
37                 if (ptr != 0) { bindings.OnionMessageHandler_free(ptr); }
38                 ptr = 0;
39         }
40         public static interface OnionMessageHandlerInterface {
41                 /**
42                  * Handle an incoming `onion_message` message from the given peer.
43                  */
44                 void handle_onion_message(byte[] peer_node_id, OnionMessage msg);
45                 /**
46                  * Called when a connection is established with a peer. Can be used to track which peers
47                  * advertise onion message support and are online.
48                  * 
49                  * May return an `Err(())` if the features the peer supports are not sufficient to communicate
50                  * with us. Implementors should be somewhat conservative about doing so, however, as other
51                  * message handlers may still wish to communicate with this peer.
52                  */
53                 Result_NoneNoneZ peer_connected(byte[] their_node_id, Init init, boolean inbound);
54                 /**
55                  * Indicates a connection to the peer failed/an existing connection was lost. Allows handlers to
56                  * drop and refuse to forward onion messages to this peer.
57                  */
58                 void peer_disconnected(byte[] their_node_id);
59                 /**
60                  * Gets the node feature flags which this handler itself supports. All available handlers are
61                  * queried similarly and their feature flags are OR'd together to form the [`NodeFeatures`]
62                  * which are broadcasted in our [`NodeAnnouncement`] message.
63                  */
64                 NodeFeatures provided_node_features();
65                 /**
66                  * Gets the init feature flags which should be sent to the given peer. All available handlers
67                  * are queried similarly and their feature flags are OR'd together to form the [`InitFeatures`]
68                  * which are sent in our [`Init`] message.
69                  * 
70                  * Note that this method is called before [`Self::peer_connected`].
71                  */
72                 InitFeatures provided_init_features(byte[] their_node_id);
73         }
74         private static class LDKOnionMessageHandlerHolder { OnionMessageHandler held; }
75         public static OnionMessageHandler new_impl(OnionMessageHandlerInterface arg, OnionMessageProvider.OnionMessageProviderInterface OnionMessageProvider_impl) {
76                 final LDKOnionMessageHandlerHolder impl_holder = new LDKOnionMessageHandlerHolder();
77                 impl_holder.held = new OnionMessageHandler(new bindings.LDKOnionMessageHandler() {
78                         @Override public void handle_onion_message(byte[] peer_node_id, long msg) {
79                                 org.ldk.structs.OnionMessage msg_hu_conv = null; if (msg < 0 || msg > 4096) { msg_hu_conv = new org.ldk.structs.OnionMessage(null, msg); }
80                                 arg.handle_onion_message(peer_node_id, msg_hu_conv);
81                                 Reference.reachabilityFence(arg);
82                         }
83                         @Override public long peer_connected(byte[] their_node_id, long init, boolean inbound) {
84                                 org.ldk.structs.Init init_hu_conv = null; if (init < 0 || init > 4096) { init_hu_conv = new org.ldk.structs.Init(null, init); }
85                                 Result_NoneNoneZ ret = arg.peer_connected(their_node_id, init_hu_conv, inbound);
86                                 Reference.reachabilityFence(arg);
87                                 long result = ret == null ? 0 : ret.clone_ptr();
88                                 return result;
89                         }
90                         @Override public void peer_disconnected(byte[] their_node_id) {
91                                 arg.peer_disconnected(their_node_id);
92                                 Reference.reachabilityFence(arg);
93                         }
94                         @Override public long provided_node_features() {
95                                 NodeFeatures ret = arg.provided_node_features();
96                                 Reference.reachabilityFence(arg);
97                                 long result = ret == null ? 0 : ret.clone_ptr();
98                                 return result;
99                         }
100                         @Override public long provided_init_features(byte[] their_node_id) {
101                                 InitFeatures ret = arg.provided_init_features(their_node_id);
102                                 Reference.reachabilityFence(arg);
103                                 long result = ret == null ? 0 : ret.clone_ptr();
104                                 return result;
105                         }
106                 }, OnionMessageProvider.new_impl(OnionMessageProvider_impl).bindings_instance);
107                 return impl_holder.held;
108         }
109
110         /**
111          * Gets the underlying OnionMessageProvider.
112          */
113         public OnionMessageProvider get_onion_message_provider() {
114                 OnionMessageProvider res = new OnionMessageProvider(null, bindings.LDKOnionMessageHandler_get_OnionMessageProvider(this.ptr));
115                 res.ptrs_to.add(this);
116                 return res;
117         }
118
119         /**
120          * Handle an incoming `onion_message` message from the given peer.
121          */
122         public void handle_onion_message(byte[] peer_node_id, org.ldk.structs.OnionMessage msg) {
123                 bindings.OnionMessageHandler_handle_onion_message(this.ptr, InternalUtils.check_arr_len(peer_node_id, 33), msg == null ? 0 : msg.ptr);
124                 Reference.reachabilityFence(this);
125                 Reference.reachabilityFence(peer_node_id);
126                 Reference.reachabilityFence(msg);
127                 if (this != null) { this.ptrs_to.add(msg); };
128         }
129
130         /**
131          * Called when a connection is established with a peer. Can be used to track which peers
132          * advertise onion message support and are online.
133          * 
134          * May return an `Err(())` if the features the peer supports are not sufficient to communicate
135          * with us. Implementors should be somewhat conservative about doing so, however, as other
136          * message handlers may still wish to communicate with this peer.
137          */
138         public Result_NoneNoneZ peer_connected(byte[] their_node_id, org.ldk.structs.Init init, boolean inbound) {
139                 long ret = bindings.OnionMessageHandler_peer_connected(this.ptr, InternalUtils.check_arr_len(their_node_id, 33), init == null ? 0 : init.ptr, inbound);
140                 Reference.reachabilityFence(this);
141                 Reference.reachabilityFence(their_node_id);
142                 Reference.reachabilityFence(init);
143                 Reference.reachabilityFence(inbound);
144                 if (ret >= 0 && ret <= 4096) { return null; }
145                 Result_NoneNoneZ ret_hu_conv = Result_NoneNoneZ.constr_from_ptr(ret);
146                 if (this != null) { this.ptrs_to.add(init); };
147                 return ret_hu_conv;
148         }
149
150         /**
151          * Indicates a connection to the peer failed/an existing connection was lost. Allows handlers to
152          * drop and refuse to forward onion messages to this peer.
153          */
154         public void peer_disconnected(byte[] their_node_id) {
155                 bindings.OnionMessageHandler_peer_disconnected(this.ptr, InternalUtils.check_arr_len(their_node_id, 33));
156                 Reference.reachabilityFence(this);
157                 Reference.reachabilityFence(their_node_id);
158         }
159
160         /**
161          * Gets the node feature flags which this handler itself supports. All available handlers are
162          * queried similarly and their feature flags are OR'd together to form the [`NodeFeatures`]
163          * which are broadcasted in our [`NodeAnnouncement`] message.
164          */
165         public NodeFeatures provided_node_features() {
166                 long ret = bindings.OnionMessageHandler_provided_node_features(this.ptr);
167                 Reference.reachabilityFence(this);
168                 if (ret >= 0 && ret <= 4096) { return null; }
169                 org.ldk.structs.NodeFeatures ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.NodeFeatures(null, ret); }
170                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };
171                 return ret_hu_conv;
172         }
173
174         /**
175          * Gets the init feature flags which should be sent to the given peer. All available handlers
176          * are queried similarly and their feature flags are OR'd together to form the [`InitFeatures`]
177          * which are sent in our [`Init`] message.
178          * 
179          * Note that this method is called before [`Self::peer_connected`].
180          */
181         public InitFeatures provided_init_features(byte[] their_node_id) {
182                 long ret = bindings.OnionMessageHandler_provided_init_features(this.ptr, InternalUtils.check_arr_len(their_node_id, 33));
183                 Reference.reachabilityFence(this);
184                 Reference.reachabilityFence(their_node_id);
185                 if (ret >= 0 && ret <= 4096) { return null; }
186                 org.ldk.structs.InitFeatures ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.InitFeatures(null, ret); }
187                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };
188                 return ret_hu_conv;
189         }
190
191 }