1 package org.ldk.structs;
3 import org.ldk.impl.bindings;
4 import org.ldk.enums.*;
6 import java.util.Arrays;
7 import javax.annotation.Nullable;
10 * A trait to describe an object which can receive channel messages.
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.
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;
25 @Override @SuppressWarnings("deprecation")
26 protected void finalize() throws Throwable {
27 if (ptr != 0) { bindings.ChannelMessageHandler_free(ptr); } super.finalize();
30 public static interface ChannelMessageHandlerInterface {
32 * Handle an incoming open_channel message from the given peer.
34 void handle_open_channel(byte[] their_node_id, InitFeatures their_features, OpenChannel msg);
36 * Handle an incoming accept_channel message from the given peer.
38 void handle_accept_channel(byte[] their_node_id, InitFeatures their_features, AcceptChannel msg);
40 * Handle an incoming funding_created message from the given peer.
42 void handle_funding_created(byte[] their_node_id, FundingCreated msg);
44 * Handle an incoming funding_signed message from the given peer.
46 void handle_funding_signed(byte[] their_node_id, FundingSigned msg);
48 * Handle an incoming funding_locked message from the given peer.
50 void handle_funding_locked(byte[] their_node_id, FundingLocked msg);
52 * Handle an incoming shutdown message from the given peer.
54 void handle_shutdown(byte[] their_node_id, InitFeatures their_features, Shutdown msg);
56 * Handle an incoming closing_signed message from the given peer.
58 void handle_closing_signed(byte[] their_node_id, ClosingSigned msg);
60 * Handle an incoming update_add_htlc message from the given peer.
62 void handle_update_add_htlc(byte[] their_node_id, UpdateAddHTLC msg);
64 * Handle an incoming update_fulfill_htlc message from the given peer.
66 void handle_update_fulfill_htlc(byte[] their_node_id, UpdateFulfillHTLC msg);
68 * Handle an incoming update_fail_htlc message from the given peer.
70 void handle_update_fail_htlc(byte[] their_node_id, UpdateFailHTLC msg);
72 * Handle an incoming update_fail_malformed_htlc message from the given peer.
74 void handle_update_fail_malformed_htlc(byte[] their_node_id, UpdateFailMalformedHTLC msg);
76 * Handle an incoming commitment_signed message from the given peer.
78 void handle_commitment_signed(byte[] their_node_id, CommitmentSigned msg);
80 * Handle an incoming revoke_and_ack message from the given peer.
82 void handle_revoke_and_ack(byte[] their_node_id, RevokeAndACK msg);
84 * Handle an incoming update_fee message from the given peer.
86 void handle_update_fee(byte[] their_node_id, UpdateFee msg);
88 * Handle an incoming announcement_signatures message from the given peer.
90 void handle_announcement_signatures(byte[] their_node_id, AnnouncementSignatures msg);
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.
97 void peer_disconnected(byte[] their_node_id, boolean no_connection_possible);
99 * Handle a peer reconnecting, possibly generating channel_reestablish message(s).
101 void peer_connected(byte[] their_node_id, Init msg);
103 * Handle an incoming channel_reestablish message from the given peer.
105 void handle_channel_reestablish(byte[] their_node_id, ChannelReestablish msg);
107 * Handle an incoming channel update from the given peer.
109 void handle_channel_update(byte[] their_node_id, ChannelUpdate msg);
111 * Handle an incoming error message from the given peer.
113 void handle_error(byte[] their_node_id, ErrorMessage msg);
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 = null; if (their_features < 0 || their_features > 4096) { their_features_hu_conv = new InitFeatures(null, their_features); }
121 their_features_hu_conv.ptrs_to.add(this);
122 OpenChannel msg_hu_conv = null; if (msg < 0 || msg > 4096) { msg_hu_conv = new OpenChannel(null, msg); }
123 arg.handle_open_channel(their_node_id, their_features_hu_conv, msg_hu_conv);
125 @Override public void handle_accept_channel(byte[] their_node_id, long their_features, long msg) {
126 InitFeatures their_features_hu_conv = null; if (their_features < 0 || their_features > 4096) { their_features_hu_conv = new InitFeatures(null, their_features); }
127 their_features_hu_conv.ptrs_to.add(this);
128 AcceptChannel msg_hu_conv = null; if (msg < 0 || msg > 4096) { msg_hu_conv = new AcceptChannel(null, msg); }
129 arg.handle_accept_channel(their_node_id, their_features_hu_conv, msg_hu_conv);
131 @Override public void handle_funding_created(byte[] their_node_id, long msg) {
132 FundingCreated msg_hu_conv = null; if (msg < 0 || msg > 4096) { msg_hu_conv = new FundingCreated(null, msg); }
133 arg.handle_funding_created(their_node_id, msg_hu_conv);
135 @Override public void handle_funding_signed(byte[] their_node_id, long msg) {
136 FundingSigned msg_hu_conv = null; if (msg < 0 || msg > 4096) { msg_hu_conv = new FundingSigned(null, msg); }
137 arg.handle_funding_signed(their_node_id, msg_hu_conv);
139 @Override public void handle_funding_locked(byte[] their_node_id, long msg) {
140 FundingLocked msg_hu_conv = null; if (msg < 0 || msg > 4096) { msg_hu_conv = new FundingLocked(null, msg); }
141 arg.handle_funding_locked(their_node_id, msg_hu_conv);
143 @Override public void handle_shutdown(byte[] their_node_id, long their_features, long msg) {
144 InitFeatures their_features_hu_conv = null; if (their_features < 0 || their_features > 4096) { their_features_hu_conv = new InitFeatures(null, their_features); }
145 Shutdown msg_hu_conv = null; if (msg < 0 || msg > 4096) { msg_hu_conv = new Shutdown(null, msg); }
146 arg.handle_shutdown(their_node_id, their_features_hu_conv, msg_hu_conv);
148 @Override public void handle_closing_signed(byte[] their_node_id, long msg) {
149 ClosingSigned msg_hu_conv = null; if (msg < 0 || msg > 4096) { msg_hu_conv = new ClosingSigned(null, msg); }
150 arg.handle_closing_signed(their_node_id, msg_hu_conv);
152 @Override public void handle_update_add_htlc(byte[] their_node_id, long msg) {
153 UpdateAddHTLC msg_hu_conv = null; if (msg < 0 || msg > 4096) { msg_hu_conv = new UpdateAddHTLC(null, msg); }
154 arg.handle_update_add_htlc(their_node_id, msg_hu_conv);
156 @Override public void handle_update_fulfill_htlc(byte[] their_node_id, long msg) {
157 UpdateFulfillHTLC msg_hu_conv = null; if (msg < 0 || msg > 4096) { msg_hu_conv = new UpdateFulfillHTLC(null, msg); }
158 arg.handle_update_fulfill_htlc(their_node_id, msg_hu_conv);
160 @Override public void handle_update_fail_htlc(byte[] their_node_id, long msg) {
161 UpdateFailHTLC msg_hu_conv = null; if (msg < 0 || msg > 4096) { msg_hu_conv = new UpdateFailHTLC(null, msg); }
162 arg.handle_update_fail_htlc(their_node_id, msg_hu_conv);
164 @Override public void handle_update_fail_malformed_htlc(byte[] their_node_id, long msg) {
165 UpdateFailMalformedHTLC msg_hu_conv = null; if (msg < 0 || msg > 4096) { msg_hu_conv = new UpdateFailMalformedHTLC(null, msg); }
166 arg.handle_update_fail_malformed_htlc(their_node_id, msg_hu_conv);
168 @Override public void handle_commitment_signed(byte[] their_node_id, long msg) {
169 CommitmentSigned msg_hu_conv = null; if (msg < 0 || msg > 4096) { msg_hu_conv = new CommitmentSigned(null, msg); }
170 arg.handle_commitment_signed(their_node_id, msg_hu_conv);
172 @Override public void handle_revoke_and_ack(byte[] their_node_id, long msg) {
173 RevokeAndACK msg_hu_conv = null; if (msg < 0 || msg > 4096) { msg_hu_conv = new RevokeAndACK(null, msg); }
174 arg.handle_revoke_and_ack(their_node_id, msg_hu_conv);
176 @Override public void handle_update_fee(byte[] their_node_id, long msg) {
177 UpdateFee msg_hu_conv = null; if (msg < 0 || msg > 4096) { msg_hu_conv = new UpdateFee(null, msg); }
178 arg.handle_update_fee(their_node_id, msg_hu_conv);
180 @Override public void handle_announcement_signatures(byte[] their_node_id, long msg) {
181 AnnouncementSignatures msg_hu_conv = null; if (msg < 0 || msg > 4096) { msg_hu_conv = new AnnouncementSignatures(null, msg); }
182 arg.handle_announcement_signatures(their_node_id, msg_hu_conv);
184 @Override public void peer_disconnected(byte[] their_node_id, boolean no_connection_possible) {
185 arg.peer_disconnected(their_node_id, no_connection_possible);
187 @Override public void peer_connected(byte[] their_node_id, long msg) {
188 Init msg_hu_conv = null; if (msg < 0 || msg > 4096) { msg_hu_conv = new Init(null, msg); }
189 arg.peer_connected(their_node_id, msg_hu_conv);
191 @Override public void handle_channel_reestablish(byte[] their_node_id, long msg) {
192 ChannelReestablish msg_hu_conv = null; if (msg < 0 || msg > 4096) { msg_hu_conv = new ChannelReestablish(null, msg); }
193 arg.handle_channel_reestablish(their_node_id, msg_hu_conv);
195 @Override public void handle_channel_update(byte[] their_node_id, long msg) {
196 ChannelUpdate msg_hu_conv = null; if (msg < 0 || msg > 4096) { msg_hu_conv = new ChannelUpdate(null, msg); }
197 arg.handle_channel_update(their_node_id, msg_hu_conv);
199 @Override public void handle_error(byte[] their_node_id, long msg) {
200 ErrorMessage msg_hu_conv = null; if (msg < 0 || msg > 4096) { msg_hu_conv = new ErrorMessage(null, msg); }
201 arg.handle_error(their_node_id, msg_hu_conv);
203 }, MessageSendEventsProvider.new_impl(MessageSendEventsProvider_impl).bindings_instance);
204 return impl_holder.held;
208 * Gets the underlying MessageSendEventsProvider.
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);
217 * Handle an incoming open_channel message from the given peer.
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(msg);
225 * Handle an incoming accept_channel message from the given peer.
227 public void handle_accept_channel(byte[] their_node_id, InitFeatures their_features, AcceptChannel msg) {
228 bindings.ChannelMessageHandler_handle_accept_channel(this.ptr, their_node_id, their_features == null ? 0 : their_features.ptr & ~1, msg == null ? 0 : msg.ptr & ~1);
229 this.ptrs_to.add(msg);
233 * Handle an incoming funding_created message from the given peer.
235 public void handle_funding_created(byte[] their_node_id, FundingCreated msg) {
236 bindings.ChannelMessageHandler_handle_funding_created(this.ptr, their_node_id, msg == null ? 0 : msg.ptr & ~1);
237 this.ptrs_to.add(msg);
241 * Handle an incoming funding_signed message from the given peer.
243 public void handle_funding_signed(byte[] their_node_id, FundingSigned msg) {
244 bindings.ChannelMessageHandler_handle_funding_signed(this.ptr, their_node_id, msg == null ? 0 : msg.ptr & ~1);
245 this.ptrs_to.add(msg);
249 * Handle an incoming funding_locked message from the given peer.
251 public void handle_funding_locked(byte[] their_node_id, FundingLocked msg) {
252 bindings.ChannelMessageHandler_handle_funding_locked(this.ptr, their_node_id, msg == null ? 0 : msg.ptr & ~1);
253 this.ptrs_to.add(msg);
257 * Handle an incoming shutdown message from the given peer.
259 public void handle_shutdown(byte[] their_node_id, InitFeatures their_features, Shutdown msg) {
260 bindings.ChannelMessageHandler_handle_shutdown(this.ptr, their_node_id, their_features == null ? 0 : their_features.ptr & ~1, msg == null ? 0 : msg.ptr & ~1);
261 this.ptrs_to.add(their_features);
262 this.ptrs_to.add(msg);
266 * Handle an incoming closing_signed message from the given peer.
268 public void handle_closing_signed(byte[] their_node_id, ClosingSigned msg) {
269 bindings.ChannelMessageHandler_handle_closing_signed(this.ptr, their_node_id, msg == null ? 0 : msg.ptr & ~1);
270 this.ptrs_to.add(msg);
274 * Handle an incoming update_add_htlc message from the given peer.
276 public void handle_update_add_htlc(byte[] their_node_id, UpdateAddHTLC msg) {
277 bindings.ChannelMessageHandler_handle_update_add_htlc(this.ptr, their_node_id, msg == null ? 0 : msg.ptr & ~1);
278 this.ptrs_to.add(msg);
282 * Handle an incoming update_fulfill_htlc message from the given peer.
284 public void handle_update_fulfill_htlc(byte[] their_node_id, UpdateFulfillHTLC msg) {
285 bindings.ChannelMessageHandler_handle_update_fulfill_htlc(this.ptr, their_node_id, msg == null ? 0 : msg.ptr & ~1);
286 this.ptrs_to.add(msg);
290 * Handle an incoming update_fail_htlc message from the given peer.
292 public void handle_update_fail_htlc(byte[] their_node_id, UpdateFailHTLC msg) {
293 bindings.ChannelMessageHandler_handle_update_fail_htlc(this.ptr, their_node_id, msg == null ? 0 : msg.ptr & ~1);
294 this.ptrs_to.add(msg);
298 * Handle an incoming update_fail_malformed_htlc message from the given peer.
300 public void handle_update_fail_malformed_htlc(byte[] their_node_id, UpdateFailMalformedHTLC msg) {
301 bindings.ChannelMessageHandler_handle_update_fail_malformed_htlc(this.ptr, their_node_id, msg == null ? 0 : msg.ptr & ~1);
302 this.ptrs_to.add(msg);
306 * Handle an incoming commitment_signed message from the given peer.
308 public void handle_commitment_signed(byte[] their_node_id, CommitmentSigned msg) {
309 bindings.ChannelMessageHandler_handle_commitment_signed(this.ptr, their_node_id, msg == null ? 0 : msg.ptr & ~1);
310 this.ptrs_to.add(msg);
314 * Handle an incoming revoke_and_ack message from the given peer.
316 public void handle_revoke_and_ack(byte[] their_node_id, RevokeAndACK msg) {
317 bindings.ChannelMessageHandler_handle_revoke_and_ack(this.ptr, their_node_id, msg == null ? 0 : msg.ptr & ~1);
318 this.ptrs_to.add(msg);
322 * Handle an incoming update_fee message from the given peer.
324 public void handle_update_fee(byte[] their_node_id, UpdateFee msg) {
325 bindings.ChannelMessageHandler_handle_update_fee(this.ptr, their_node_id, msg == null ? 0 : msg.ptr & ~1);
326 this.ptrs_to.add(msg);
330 * Handle an incoming announcement_signatures message from the given peer.
332 public void handle_announcement_signatures(byte[] their_node_id, AnnouncementSignatures msg) {
333 bindings.ChannelMessageHandler_handle_announcement_signatures(this.ptr, their_node_id, msg == null ? 0 : msg.ptr & ~1);
334 this.ptrs_to.add(msg);
338 * Indicates a connection to the peer failed/an existing connection was lost. If no connection
339 * is believed to be possible in the future (eg they're sending us messages we don't
340 * understand or indicate they require unknown feature bits), no_connection_possible is set
341 * and any outstanding channels should be failed.
343 public void peer_disconnected(byte[] their_node_id, boolean no_connection_possible) {
344 bindings.ChannelMessageHandler_peer_disconnected(this.ptr, their_node_id, no_connection_possible);
348 * Handle a peer reconnecting, possibly generating channel_reestablish message(s).
350 public void peer_connected(byte[] their_node_id, Init msg) {
351 bindings.ChannelMessageHandler_peer_connected(this.ptr, their_node_id, msg == null ? 0 : msg.ptr & ~1);
352 this.ptrs_to.add(msg);
356 * Handle an incoming channel_reestablish message from the given peer.
358 public void handle_channel_reestablish(byte[] their_node_id, ChannelReestablish msg) {
359 bindings.ChannelMessageHandler_handle_channel_reestablish(this.ptr, their_node_id, msg == null ? 0 : msg.ptr & ~1);
360 this.ptrs_to.add(msg);
364 * Handle an incoming channel update from the given peer.
366 public void handle_channel_update(byte[] their_node_id, ChannelUpdate msg) {
367 bindings.ChannelMessageHandler_handle_channel_update(this.ptr, their_node_id, msg == null ? 0 : msg.ptr & ~1);
368 this.ptrs_to.add(msg);
372 * Handle an incoming error message from the given peer.
374 public void handle_error(byte[] their_node_id, ErrorMessage msg) {
375 bindings.ChannelMessageHandler_handle_error(this.ptr, their_node_id, msg == null ? 0 : msg.ptr & ~1);
376 this.ptrs_to.add(msg);