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 = 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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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 = 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 = 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 = 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 = 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(their_features);
222 this.ptrs_to.add(msg);
226 * Handle an incoming accept_channel message from the given peer.
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);
235 * Handle an incoming funding_created message from the given peer.
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);
243 * Handle an incoming funding_signed message from the given peer.
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);
251 * Handle an incoming funding_locked message from the given peer.
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);
259 * Handle an incoming shutdown message from the given peer.
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);
268 * Handle an incoming closing_signed message from the given peer.
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);
276 * Handle an incoming update_add_htlc message from the given peer.
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);
284 * Handle an incoming update_fulfill_htlc message from the given peer.
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);
292 * Handle an incoming update_fail_htlc message from the given peer.
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);
300 * Handle an incoming update_fail_malformed_htlc message from the given peer.
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);
308 * Handle an incoming commitment_signed message from the given peer.
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);
316 * Handle an incoming revoke_and_ack message from the given peer.
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);
324 * Handle an incoming update_fee message from the given peer.
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);
332 * Handle an incoming announcement_signatures message from the given peer.
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);
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.
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);
350 * Handle a peer reconnecting, possibly generating channel_reestablish message(s).
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);
358 * Handle an incoming channel_reestablish message from the given peer.
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);
366 * Handle an incoming channel update from the given peer.
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);
374 * Handle an incoming error message from the given peer.
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);