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`].
17 * When using this trait, [`process_pending_events`] will call [`handle_event`] for each pending
18 * event since the last invocation.
20 * In order to ensure no [`Event`]s are lost, implementors of this trait will persist [`Event`]s
21 * and replay any unhandled events on startup. An [`Event`] is considered handled when
22 * [`process_pending_events`] returns, thus handlers MUST fully handle [`Event`]s and persist any
23 * relevant changes to disk *before* returning.
25 * Further, because an application may crash between an [`Event`] being handled and the
26 * implementor of this trait being re-serialized, [`Event`] handling must be idempotent - in
27 * effect, [`Event`]s may be replayed.
29 * Note, handlers may call back into the provider and thus deadlocking must be avoided. Be sure to
30 * consult the provider's documentation on the implication of processing events and how a handler
31 * may safely use the provider (e.g., see [`ChannelManager::process_pending_events`] and
32 * [`ChainMonitor::process_pending_events`]).
34 * (C-not implementable) As there is likely no reason for a user to implement this trait on their
37 * [`process_pending_events`]: Self::process_pending_events
38 * [`handle_event`]: EventHandler::handle_event
39 * [`ChannelManager::process_pending_events`]: crate::ln::channelmanager::ChannelManager#method.process_pending_events
40 * [`ChainMonitor::process_pending_events`]: crate::chain::chainmonitor::ChainMonitor#method.process_pending_events
42 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
43 public class EventsProvider extends CommonBase {
44 final bindings.LDKEventsProvider bindings_instance;
45 EventsProvider(Object _dummy, long ptr) { super(ptr); bindings_instance = null; }
46 private EventsProvider(bindings.LDKEventsProvider arg) {
47 super(bindings.LDKEventsProvider_new(arg));
48 this.ptrs_to.add(arg);
49 this.bindings_instance = arg;
51 @Override @SuppressWarnings("deprecation")
52 protected void finalize() throws Throwable {
53 if (ptr != 0) { bindings.EventsProvider_free(ptr); } super.finalize();
56 public static interface EventsProviderInterface {
58 * Processes any events generated since the last call using the given event handler.
60 * See the trait-level documentation for requirements.
62 void process_pending_events(EventHandler handler);
64 private static class LDKEventsProviderHolder { EventsProvider held; }
65 public static EventsProvider new_impl(EventsProviderInterface arg) {
66 final LDKEventsProviderHolder impl_holder = new LDKEventsProviderHolder();
67 impl_holder.held = new EventsProvider(new bindings.LDKEventsProvider() {
68 @Override public void process_pending_events(long handler) {
69 EventHandler ret_hu_conv = new EventHandler(null, handler);
70 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };
71 arg.process_pending_events(ret_hu_conv);
72 Reference.reachabilityFence(arg);
75 return impl_holder.held;
78 * Processes any events generated since the last call using the given event handler.
80 * See the trait-level documentation for requirements.
82 public void process_pending_events(EventHandler handler) {
83 bindings.EventsProvider_process_pending_events(this.ptr, handler == null ? 0 : handler.ptr);
84 Reference.reachabilityFence(this);
85 Reference.reachabilityFence(handler);
86 if (this != null) { this.ptrs_to.add(handler); };