1 package org.ldk.structs;
3 import org.ldk.impl.bindings;
4 import org.ldk.enums.*;
6 import java.util.Arrays;
7 import java.lang.ref.Reference;
8 import javax.annotation.Nullable;
11 * A trait indicating an object may generate events.
13 * Events are processed by passing an [`EventHandler`] to [`process_pending_events`].
15 * Implementations of this trait may also feature an async version of event handling, as shown with
16 * [`ChannelManager::process_pending_events_async`] and
17 * [`ChainMonitor::process_pending_events_async`].
21 * When using this trait, [`process_pending_events`] will call [`handle_event`] for each pending
22 * event since the last invocation.
24 * In order to ensure no [`Event`]s are lost, implementors of this trait will persist [`Event`]s
25 * and replay any unhandled events on startup. An [`Event`] is considered handled when
26 * [`process_pending_events`] returns, thus handlers MUST fully handle [`Event`]s and persist any
27 * relevant changes to disk *before* returning.
29 * Further, because an application may crash between an [`Event`] being handled and the
30 * implementor of this trait being re-serialized, [`Event`] handling must be idempotent - in
31 * effect, [`Event`]s may be replayed.
33 * Note, handlers may call back into the provider and thus deadlocking must be avoided. Be sure to
34 * consult the provider's documentation on the implication of processing events and how a handler
35 * may safely use the provider (e.g., see [`ChannelManager::process_pending_events`] and
36 * [`ChainMonitor::process_pending_events`]).
38 * (C-not implementable) As there is likely no reason for a user to implement this trait on their
41 * [`process_pending_events`]: Self::process_pending_events
42 * [`handle_event`]: EventHandler::handle_event
43 * [`ChannelManager::process_pending_events`]: crate::ln::channelmanager::ChannelManager#method.process_pending_events
44 * [`ChainMonitor::process_pending_events`]: crate::chain::chainmonitor::ChainMonitor#method.process_pending_events
45 * [`ChannelManager::process_pending_events_async`]: crate::ln::channelmanager::ChannelManager::process_pending_events_async
46 * [`ChainMonitor::process_pending_events_async`]: crate::chain::chainmonitor::ChainMonitor::process_pending_events_async
48 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
49 public class EventsProvider extends CommonBase {
50 final bindings.LDKEventsProvider bindings_instance;
51 EventsProvider(Object _dummy, long ptr) { super(ptr); bindings_instance = null; }
52 private EventsProvider(bindings.LDKEventsProvider arg) {
53 super(bindings.LDKEventsProvider_new(arg));
54 this.ptrs_to.add(arg);
55 this.bindings_instance = arg;
57 @Override @SuppressWarnings("deprecation")
58 protected void finalize() throws Throwable {
59 if (ptr != 0) { bindings.EventsProvider_free(ptr); } super.finalize();
62 public static interface EventsProviderInterface {
64 * Processes any events generated since the last call using the given event handler.
66 * See the trait-level documentation for requirements.
68 void process_pending_events(EventHandler handler);
70 private static class LDKEventsProviderHolder { EventsProvider held; }
71 public static EventsProvider new_impl(EventsProviderInterface arg) {
72 final LDKEventsProviderHolder impl_holder = new LDKEventsProviderHolder();
73 impl_holder.held = new EventsProvider(new bindings.LDKEventsProvider() {
74 @Override public void process_pending_events(long handler) {
75 EventHandler ret_hu_conv = new EventHandler(null, handler);
76 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };
77 arg.process_pending_events(ret_hu_conv);
78 Reference.reachabilityFence(arg);
81 return impl_holder.held;
84 * Processes any events generated since the last call using the given event handler.
86 * See the trait-level documentation for requirements.
88 public void process_pending_events(org.ldk.structs.EventHandler handler) {
89 bindings.EventsProvider_process_pending_events(this.ptr, handler == null ? 0 : handler.ptr);
90 Reference.reachabilityFence(this);
91 Reference.reachabilityFence(handler);
92 if (this != null) { this.ptrs_to.add(handler); };