X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Forg%2Fldk%2Fstructs%2FEventsProvider.java;h=3ab3f1715ac736289ef6e955e09b996a90b7d0ac;hb=b6cc0960eaee5a5bbc7fde79a56152d6dcd649d8;hp=960ab4494333f475f11286740592d7870071f834;hpb=aa0e2aade37133339f4113a0cd7465b111f0a0d0;p=ldk-java diff --git a/src/main/java/org/ldk/structs/EventsProvider.java b/src/main/java/org/ldk/structs/EventsProvider.java index 960ab449..3ab3f171 100644 --- a/src/main/java/org/ldk/structs/EventsProvider.java +++ b/src/main/java/org/ldk/structs/EventsProvider.java @@ -5,6 +5,32 @@ import org.ldk.enums.*; import org.ldk.util.*; import java.util.Arrays; +/** + * A trait indicating an object may generate events. + * + * Events are processed by passing an [`EventHandler`] to [`process_pending_events`]. + * + * # Requirements + * + * See [`process_pending_events`] for requirements around event processing. + * + * When using this trait, [`process_pending_events`] will call [`handle_event`] for each pending + * event since the last invocation. The handler must either act upon the event immediately + * or preserve it for later handling. + * + * Note, handlers may call back into the provider and thus deadlocking must be avoided. Be sure to + * consult the provider's documentation on the implication of processing events and how a handler + * may safely use the provider (e.g., see [`ChannelManager::process_pending_events`] and + * [`ChainMonitor::process_pending_events`]). + * + * (C-not implementable) As there is likely no reason for a user to implement this trait on their + * own type(s). + * + * [`process_pending_events`]: Self::process_pending_events + * [`handle_event`]: EventHandler::handle_event + * [`ChannelManager::process_pending_events`]: crate::ln::channelmanager::ChannelManager#method.process_pending_events + * [`ChainMonitor::process_pending_events`]: crate::chain::chainmonitor::ChainMonitor#method.process_pending_events + */ @SuppressWarnings("unchecked") // We correctly assign various generic arrays public class EventsProvider extends CommonBase { final bindings.LDKEventsProvider bindings_instance; @@ -20,27 +46,37 @@ public class EventsProvider extends CommonBase { } public static interface EventsProviderInterface { - Event[] get_and_clear_pending_events(); + /** + * Processes any events generated since the last call using the given event handler. + * + * Subsequent calls must only process new events. However, handlers must be capable of handling + * duplicate events across process restarts. This may occur if the provider was recovered from + * an old state (i.e., it hadn't been successfully persisted after processing pending events). + */ + void process_pending_events(EventHandler handler); } - public EventsProvider(EventsProviderInterface arg) { - this(new bindings.LDKEventsProvider() { - @Override public long[] get_and_clear_pending_events() { - Event[] ret = arg.get_and_clear_pending_events(); - long[] result = Arrays.stream(ret).mapToLong(arr_conv_7 -> arr_conv_7.ptr).toArray(); - /* TODO 2 Event */ - return result; + private static class LDKEventsProviderHolder { EventsProvider held; } + public static EventsProvider new_impl(EventsProviderInterface arg) { + final LDKEventsProviderHolder impl_holder = new LDKEventsProviderHolder(); + impl_holder.held = new EventsProvider(new bindings.LDKEventsProvider() { + @Override public void process_pending_events(long handler) { + EventHandler ret_hu_conv = new EventHandler(null, handler); + ret_hu_conv.ptrs_to.add(this); + arg.process_pending_events(ret_hu_conv); } }); + return impl_holder.held; } - public Event[] get_and_clear_pending_events() { - long[] ret = bindings.EventsProvider_get_and_clear_pending_events(this.ptr); - Event[] arr_conv_7_arr = new Event[ret.length]; - for (int h = 0; h < ret.length; h++) { - long arr_conv_7 = ret[h]; - Event arr_conv_7_hu_conv = Event.constr_from_ptr(arr_conv_7); - arr_conv_7_arr[h] = arr_conv_7_hu_conv; - } - return arr_conv_7_arr; + /** + * Processes any events generated since the last call using the given event handler. + * + * Subsequent calls must only process new events. However, handlers must be capable of handling + * duplicate events across process restarts. This may occur if the provider was recovered from + * an old state (i.e., it hadn't been successfully persisted after processing pending events). + */ + public void process_pending_events(EventHandler handler) { + bindings.EventsProvider_process_pending_events(this.ptr, handler == null ? 0 : handler.ptr); + this.ptrs_to.add(handler); } }