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 * Destroys the object, freeing associated resources. After this call, any access
63 * to this object may result in a SEGFAULT or worse.
65 * You should generally NEVER call this method. You should let the garbage collector
66 * do this for you when it finalizes objects. However, it may be useful for types
67 * which represent locks and should be closed immediately to avoid holding locks
70 public void destroy() {
71 if (ptr != 0) { bindings.EventsProvider_free(ptr); }
74 public static interface EventsProviderInterface {
76 * Processes any events generated since the last call using the given event handler.
78 * See the trait-level documentation for requirements.
80 void process_pending_events(EventHandler handler);
82 private static class LDKEventsProviderHolder { EventsProvider held; }
83 public static EventsProvider new_impl(EventsProviderInterface arg) {
84 final LDKEventsProviderHolder impl_holder = new LDKEventsProviderHolder();
85 impl_holder.held = new EventsProvider(new bindings.LDKEventsProvider() {
86 @Override public void process_pending_events(long handler) {
87 EventHandler ret_hu_conv = new EventHandler(null, handler);
88 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };
89 arg.process_pending_events(ret_hu_conv);
90 Reference.reachabilityFence(arg);
93 return impl_holder.held;
96 * Processes any events generated since the last call using the given event handler.
98 * See the trait-level documentation for requirements.
100 public void process_pending_events(org.ldk.structs.EventHandler handler) {
101 bindings.EventsProvider_process_pending_events(this.ptr, handler.ptr);
102 Reference.reachabilityFence(this);
103 Reference.reachabilityFence(handler);
104 if (this != null) { this.ptrs_to.add(handler); };