Merge pull request #33 from TheBlueMatt/main
[ldk-java] / src / main / java / org / ldk / structs / ChannelMessageHandler.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 javax.annotation.Nullable;
8
9 /**
10  * A trait to describe an object which can receive channel messages.
11  * 
12  * Messages MAY be called in parallel when they originate from different their_node_ids, however
13  * they MUST NOT be called in parallel when the two calls have the same their_node_id.
14  */
15 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
16 public class ChannelMessageHandler extends CommonBase {
17         final bindings.LDKChannelMessageHandler bindings_instance;
18         ChannelMessageHandler(Object _dummy, long ptr) { super(ptr); bindings_instance = null; }
19         private ChannelMessageHandler(bindings.LDKChannelMessageHandler arg, bindings.LDKMessageSendEventsProvider MessageSendEventsProvider) {
20                 super(bindings.LDKChannelMessageHandler_new(arg, MessageSendEventsProvider));
21                 this.ptrs_to.add(arg);
22                 this.ptrs_to.add(MessageSendEventsProvider);
23                 this.bindings_instance = arg;
24         }
25         @Override @SuppressWarnings("deprecation")
26         protected void finalize() throws Throwable {
27                 if (ptr != 0) { bindings.ChannelMessageHandler_free(ptr); } super.finalize();
28         }
29
30         public static interface ChannelMessageHandlerInterface {
31                 /**
32                  * Handle an incoming open_channel message from the given peer.
33                  */
34                 void handle_open_channel(byte[] their_node_id, InitFeatures their_features, OpenChannel msg);
35                 /**
36                  * Handle an incoming accept_channel message from the given peer.
37                  */
38                 void handle_accept_channel(byte[] their_node_id, InitFeatures their_features, AcceptChannel msg);
39                 /**
40                  * Handle an incoming funding_created message from the given peer.
41                  */
42                 void handle_funding_created(byte[] their_node_id, FundingCreated msg);
43                 /**
44                  * Handle an incoming funding_signed message from the given peer.
45                  */
46                 void handle_funding_signed(byte[] their_node_id, FundingSigned msg);
47                 /**
48                  * Handle an incoming funding_locked message from the given peer.
49                  */
50                 void handle_funding_locked(byte[] their_node_id, FundingLocked msg);
51                 /**
52                  * Handle an incoming shutdown message from the given peer.
53                  */
54                 void handle_shutdown(byte[] their_node_id, InitFeatures their_features, Shutdown msg);
55                 /**
56                  * Handle an incoming closing_signed message from the given peer.
57                  */
58                 void handle_closing_signed(byte[] their_node_id, ClosingSigned msg);
59                 /**
60                  * Handle an incoming update_add_htlc message from the given peer.
61                  */
62                 void handle_update_add_htlc(byte[] their_node_id, UpdateAddHTLC msg);
63                 /**
64                  * Handle an incoming update_fulfill_htlc message from the given peer.
65                  */
66                 void handle_update_fulfill_htlc(byte[] their_node_id, UpdateFulfillHTLC msg);
67                 /**
68                  * Handle an incoming update_fail_htlc message from the given peer.
69                  */
70                 void handle_update_fail_htlc(byte[] their_node_id, UpdateFailHTLC msg);
71                 /**
72                  * Handle an incoming update_fail_malformed_htlc message from the given peer.
73                  */
74                 void handle_update_fail_malformed_htlc(byte[] their_node_id, UpdateFailMalformedHTLC msg);
75                 /**
76                  * Handle an incoming commitment_signed message from the given peer.
77                  */
78                 void handle_commitment_signed(byte[] their_node_id, CommitmentSigned msg);
79                 /**
80                  * Handle an incoming revoke_and_ack message from the given peer.
81                  */
82                 void handle_revoke_and_ack(byte[] their_node_id, RevokeAndACK msg);
83                 /**
84                  * Handle an incoming update_fee message from the given peer.
85                  */
86                 void handle_update_fee(byte[] their_node_id, UpdateFee msg);
87                 /**
88                  * Handle an incoming announcement_signatures message from the given peer.
89                  */
90                 void handle_announcement_signatures(byte[] their_node_id, AnnouncementSignatures msg);
91                 /**
92                  * Indicates a connection to the peer failed/an existing connection was lost. If no connection
93                  * is believed to be possible in the future (eg they're sending us messages we don't
94                  * understand or indicate they require unknown feature bits), no_connection_possible is set
95                  * and any outstanding channels should be failed.
96                  */
97                 void peer_disconnected(byte[] their_node_id, boolean no_connection_possible);
98                 /**
99                  * Handle a peer reconnecting, possibly generating channel_reestablish message(s).
100                  */
101                 void peer_connected(byte[] their_node_id, Init msg);
102                 /**
103                  * Handle an incoming channel_reestablish message from the given peer.
104                  */
105                 void handle_channel_reestablish(byte[] their_node_id, ChannelReestablish msg);
106                 /**
107                  * Handle an incoming channel update from the given peer.
108                  */
109                 void handle_channel_update(byte[] their_node_id, ChannelUpdate msg);
110                 /**
111                  * Handle an incoming error message from the given peer.
112                  */
113                 void handle_error(byte[] their_node_id, ErrorMessage msg);
114         }
115         private static class LDKChannelMessageHandlerHolder { ChannelMessageHandler held; }
116         public static ChannelMessageHandler new_impl(ChannelMessageHandlerInterface arg, MessageSendEventsProvider.MessageSendEventsProviderInterface MessageSendEventsProvider_impl) {
117                 final LDKChannelMessageHandlerHolder impl_holder = new LDKChannelMessageHandlerHolder();
118                 impl_holder.held = new ChannelMessageHandler(new bindings.LDKChannelMessageHandler() {
119                         @Override public void handle_open_channel(byte[] their_node_id, long their_features, long msg) {
120                                 InitFeatures their_features_hu_conv = new InitFeatures(null, their_features);
121                                 their_features_hu_conv.ptrs_to.add(this);
122                                 OpenChannel msg_hu_conv = new OpenChannel(null, msg);
123                                 arg.handle_open_channel(their_node_id, their_features_hu_conv, msg_hu_conv);
124                         }
125                         @Override public void handle_accept_channel(byte[] their_node_id, long their_features, long msg) {
126                                 InitFeatures their_features_hu_conv = new InitFeatures(null, their_features);
127                                 their_features_hu_conv.ptrs_to.add(this);
128                                 AcceptChannel msg_hu_conv = new AcceptChannel(null, msg);
129                                 arg.handle_accept_channel(their_node_id, their_features_hu_conv, msg_hu_conv);
130                         }
131                         @Override public void handle_funding_created(byte[] their_node_id, long msg) {
132                                 FundingCreated msg_hu_conv = new FundingCreated(null, msg);
133                                 arg.handle_funding_created(their_node_id, msg_hu_conv);
134                         }
135                         @Override public void handle_funding_signed(byte[] their_node_id, long msg) {
136                                 FundingSigned msg_hu_conv = new FundingSigned(null, msg);
137                                 arg.handle_funding_signed(their_node_id, msg_hu_conv);
138                         }
139                         @Override public void handle_funding_locked(byte[] their_node_id, long msg) {
140                                 FundingLocked msg_hu_conv = new FundingLocked(null, msg);
141                                 arg.handle_funding_locked(their_node_id, msg_hu_conv);
142                         }
143                         @Override public void handle_shutdown(byte[] their_node_id, long their_features, long msg) {
144                                 InitFeatures their_features_hu_conv = new InitFeatures(null, their_features);
145                                 Shutdown msg_hu_conv = new Shutdown(null, msg);
146                                 arg.handle_shutdown(their_node_id, their_features_hu_conv, msg_hu_conv);
147                         }
148                         @Override public void handle_closing_signed(byte[] their_node_id, long msg) {
149                                 ClosingSigned msg_hu_conv = new ClosingSigned(null, msg);
150                                 arg.handle_closing_signed(their_node_id, msg_hu_conv);
151                         }
152                         @Override public void handle_update_add_htlc(byte[] their_node_id, long msg) {
153                                 UpdateAddHTLC msg_hu_conv = new UpdateAddHTLC(null, msg);
154                                 arg.handle_update_add_htlc(their_node_id, msg_hu_conv);
155                         }
156                         @Override public void handle_update_fulfill_htlc(byte[] their_node_id, long msg) {
157                                 UpdateFulfillHTLC msg_hu_conv = new UpdateFulfillHTLC(null, msg);
158                                 arg.handle_update_fulfill_htlc(their_node_id, msg_hu_conv);
159                         }
160                         @Override public void handle_update_fail_htlc(byte[] their_node_id, long msg) {
161                                 UpdateFailHTLC msg_hu_conv = new UpdateFailHTLC(null, msg);
162                                 arg.handle_update_fail_htlc(their_node_id, msg_hu_conv);
163                         }
164                         @Override public void handle_update_fail_malformed_htlc(byte[] their_node_id, long msg) {
165                                 UpdateFailMalformedHTLC msg_hu_conv = new UpdateFailMalformedHTLC(null, msg);
166                                 arg.handle_update_fail_malformed_htlc(their_node_id, msg_hu_conv);
167                         }
168                         @Override public void handle_commitment_signed(byte[] their_node_id, long msg) {
169                                 CommitmentSigned msg_hu_conv = new CommitmentSigned(null, msg);
170                                 arg.handle_commitment_signed(their_node_id, msg_hu_conv);
171                         }
172                         @Override public void handle_revoke_and_ack(byte[] their_node_id, long msg) {
173                                 RevokeAndACK msg_hu_conv = new RevokeAndACK(null, msg);
174                                 arg.handle_revoke_and_ack(their_node_id, msg_hu_conv);
175                         }
176                         @Override public void handle_update_fee(byte[] their_node_id, long msg) {
177                                 UpdateFee msg_hu_conv = new UpdateFee(null, msg);
178                                 arg.handle_update_fee(their_node_id, msg_hu_conv);
179                         }
180                         @Override public void handle_announcement_signatures(byte[] their_node_id, long msg) {
181                                 AnnouncementSignatures msg_hu_conv = new AnnouncementSignatures(null, msg);
182                                 arg.handle_announcement_signatures(their_node_id, msg_hu_conv);
183                         }
184                         @Override public void peer_disconnected(byte[] their_node_id, boolean no_connection_possible) {
185                                 arg.peer_disconnected(their_node_id, no_connection_possible);
186                         }
187                         @Override public void peer_connected(byte[] their_node_id, long msg) {
188                                 Init msg_hu_conv = new Init(null, msg);
189                                 arg.peer_connected(their_node_id, msg_hu_conv);
190                         }
191                         @Override public void handle_channel_reestablish(byte[] their_node_id, long msg) {
192                                 ChannelReestablish msg_hu_conv = new ChannelReestablish(null, msg);
193                                 arg.handle_channel_reestablish(their_node_id, msg_hu_conv);
194                         }
195                         @Override public void handle_channel_update(byte[] their_node_id, long msg) {
196                                 ChannelUpdate msg_hu_conv = new ChannelUpdate(null, msg);
197                                 arg.handle_channel_update(their_node_id, msg_hu_conv);
198                         }
199                         @Override public void handle_error(byte[] their_node_id, long msg) {
200                                 ErrorMessage msg_hu_conv = new ErrorMessage(null, msg);
201                                 arg.handle_error(their_node_id, msg_hu_conv);
202                         }
203                 }, MessageSendEventsProvider.new_impl(MessageSendEventsProvider_impl).bindings_instance);
204                 return impl_holder.held;
205         }
206
207         /**
208          * Gets the underlying MessageSendEventsProvider.
209          */
210         public MessageSendEventsProvider get_message_send_events_provider() {
211                 MessageSendEventsProvider res = new MessageSendEventsProvider(null, bindings.LDKChannelMessageHandler_get_MessageSendEventsProvider(this.ptr));
212                 this.ptrs_to.add(res);
213                 return res;
214         }
215
216         /**
217          * Handle an incoming open_channel message from the given peer.
218          */
219         public void handle_open_channel(byte[] their_node_id, InitFeatures their_features, OpenChannel msg) {
220                 bindings.ChannelMessageHandler_handle_open_channel(this.ptr, their_node_id, their_features == null ? 0 : their_features.ptr & ~1, msg == null ? 0 : msg.ptr & ~1);
221                 this.ptrs_to.add(their_features);
222                 this.ptrs_to.add(msg);
223         }
224
225         /**
226          * Handle an incoming accept_channel message from the given peer.
227          */
228         public void handle_accept_channel(byte[] their_node_id, InitFeatures their_features, AcceptChannel msg) {
229                 bindings.ChannelMessageHandler_handle_accept_channel(this.ptr, their_node_id, their_features == null ? 0 : their_features.ptr & ~1, msg == null ? 0 : msg.ptr & ~1);
230                 this.ptrs_to.add(their_features);
231                 this.ptrs_to.add(msg);
232         }
233
234         /**
235          * Handle an incoming funding_created message from the given peer.
236          */
237         public void handle_funding_created(byte[] their_node_id, FundingCreated msg) {
238                 bindings.ChannelMessageHandler_handle_funding_created(this.ptr, their_node_id, msg == null ? 0 : msg.ptr & ~1);
239                 this.ptrs_to.add(msg);
240         }
241
242         /**
243          * Handle an incoming funding_signed message from the given peer.
244          */
245         public void handle_funding_signed(byte[] their_node_id, FundingSigned msg) {
246                 bindings.ChannelMessageHandler_handle_funding_signed(this.ptr, their_node_id, msg == null ? 0 : msg.ptr & ~1);
247                 this.ptrs_to.add(msg);
248         }
249
250         /**
251          * Handle an incoming funding_locked message from the given peer.
252          */
253         public void handle_funding_locked(byte[] their_node_id, FundingLocked msg) {
254                 bindings.ChannelMessageHandler_handle_funding_locked(this.ptr, their_node_id, msg == null ? 0 : msg.ptr & ~1);
255                 this.ptrs_to.add(msg);
256         }
257
258         /**
259          * Handle an incoming shutdown message from the given peer.
260          */
261         public void handle_shutdown(byte[] their_node_id, InitFeatures their_features, Shutdown msg) {
262                 bindings.ChannelMessageHandler_handle_shutdown(this.ptr, their_node_id, their_features == null ? 0 : their_features.ptr & ~1, msg == null ? 0 : msg.ptr & ~1);
263                 this.ptrs_to.add(their_features);
264                 this.ptrs_to.add(msg);
265         }
266
267         /**
268          * Handle an incoming closing_signed message from the given peer.
269          */
270         public void handle_closing_signed(byte[] their_node_id, ClosingSigned msg) {
271                 bindings.ChannelMessageHandler_handle_closing_signed(this.ptr, their_node_id, msg == null ? 0 : msg.ptr & ~1);
272                 this.ptrs_to.add(msg);
273         }
274
275         /**
276          * Handle an incoming update_add_htlc message from the given peer.
277          */
278         public void handle_update_add_htlc(byte[] their_node_id, UpdateAddHTLC msg) {
279                 bindings.ChannelMessageHandler_handle_update_add_htlc(this.ptr, their_node_id, msg == null ? 0 : msg.ptr & ~1);
280                 this.ptrs_to.add(msg);
281         }
282
283         /**
284          * Handle an incoming update_fulfill_htlc message from the given peer.
285          */
286         public void handle_update_fulfill_htlc(byte[] their_node_id, UpdateFulfillHTLC msg) {
287                 bindings.ChannelMessageHandler_handle_update_fulfill_htlc(this.ptr, their_node_id, msg == null ? 0 : msg.ptr & ~1);
288                 this.ptrs_to.add(msg);
289         }
290
291         /**
292          * Handle an incoming update_fail_htlc message from the given peer.
293          */
294         public void handle_update_fail_htlc(byte[] their_node_id, UpdateFailHTLC msg) {
295                 bindings.ChannelMessageHandler_handle_update_fail_htlc(this.ptr, their_node_id, msg == null ? 0 : msg.ptr & ~1);
296                 this.ptrs_to.add(msg);
297         }
298
299         /**
300          * Handle an incoming update_fail_malformed_htlc message from the given peer.
301          */
302         public void handle_update_fail_malformed_htlc(byte[] their_node_id, UpdateFailMalformedHTLC msg) {
303                 bindings.ChannelMessageHandler_handle_update_fail_malformed_htlc(this.ptr, their_node_id, msg == null ? 0 : msg.ptr & ~1);
304                 this.ptrs_to.add(msg);
305         }
306
307         /**
308          * Handle an incoming commitment_signed message from the given peer.
309          */
310         public void handle_commitment_signed(byte[] their_node_id, CommitmentSigned msg) {
311                 bindings.ChannelMessageHandler_handle_commitment_signed(this.ptr, their_node_id, msg == null ? 0 : msg.ptr & ~1);
312                 this.ptrs_to.add(msg);
313         }
314
315         /**
316          * Handle an incoming revoke_and_ack message from the given peer.
317          */
318         public void handle_revoke_and_ack(byte[] their_node_id, RevokeAndACK msg) {
319                 bindings.ChannelMessageHandler_handle_revoke_and_ack(this.ptr, their_node_id, msg == null ? 0 : msg.ptr & ~1);
320                 this.ptrs_to.add(msg);
321         }
322
323         /**
324          * Handle an incoming update_fee message from the given peer.
325          */
326         public void handle_update_fee(byte[] their_node_id, UpdateFee msg) {
327                 bindings.ChannelMessageHandler_handle_update_fee(this.ptr, their_node_id, msg == null ? 0 : msg.ptr & ~1);
328                 this.ptrs_to.add(msg);
329         }
330
331         /**
332          * Handle an incoming announcement_signatures message from the given peer.
333          */
334         public void handle_announcement_signatures(byte[] their_node_id, AnnouncementSignatures msg) {
335                 bindings.ChannelMessageHandler_handle_announcement_signatures(this.ptr, their_node_id, msg == null ? 0 : msg.ptr & ~1);
336                 this.ptrs_to.add(msg);
337         }
338
339         /**
340          * Indicates a connection to the peer failed/an existing connection was lost. If no connection
341          * is believed to be possible in the future (eg they're sending us messages we don't
342          * understand or indicate they require unknown feature bits), no_connection_possible is set
343          * and any outstanding channels should be failed.
344          */
345         public void peer_disconnected(byte[] their_node_id, boolean no_connection_possible) {
346                 bindings.ChannelMessageHandler_peer_disconnected(this.ptr, their_node_id, no_connection_possible);
347         }
348
349         /**
350          * Handle a peer reconnecting, possibly generating channel_reestablish message(s).
351          */
352         public void peer_connected(byte[] their_node_id, Init msg) {
353                 bindings.ChannelMessageHandler_peer_connected(this.ptr, their_node_id, msg == null ? 0 : msg.ptr & ~1);
354                 this.ptrs_to.add(msg);
355         }
356
357         /**
358          * Handle an incoming channel_reestablish message from the given peer.
359          */
360         public void handle_channel_reestablish(byte[] their_node_id, ChannelReestablish msg) {
361                 bindings.ChannelMessageHandler_handle_channel_reestablish(this.ptr, their_node_id, msg == null ? 0 : msg.ptr & ~1);
362                 this.ptrs_to.add(msg);
363         }
364
365         /**
366          * Handle an incoming channel update from the given peer.
367          */
368         public void handle_channel_update(byte[] their_node_id, ChannelUpdate msg) {
369                 bindings.ChannelMessageHandler_handle_channel_update(this.ptr, their_node_id, msg == null ? 0 : msg.ptr & ~1);
370                 this.ptrs_to.add(msg);
371         }
372
373         /**
374          * Handle an incoming error message from the given peer.
375          */
376         public void handle_error(byte[] their_node_id, ErrorMessage msg) {
377                 bindings.ChannelMessageHandler_handle_error(this.ptr, their_node_id, msg == null ? 0 : msg.ptr & ~1);
378                 this.ptrs_to.add(msg);
379         }
380
381 }