f4f22f3933eeacb55f7d063b04a14d2def807a35
[ldk-java] / src / main / java / org / ldk / structs / EventsProvider.java
1 package org.ldk.structs;
2
3 import org.ldk.impl.bindings;
4 import org.ldk.enums.*;
5 import org.ldk.util.*;
6 import java.util.Arrays;
7 import java.lang.ref.Reference;
8 import javax.annotation.Nullable;
9
10 /**
11  * A trait indicating an object may generate events.
12  * 
13  * Events are processed by passing an [`EventHandler`] to [`process_pending_events`].
14  * 
15  * # Requirements
16  * 
17  * When using this trait, [`process_pending_events`] will call [`handle_event`] for each pending
18  * event since the last invocation.
19  * 
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.
24  * 
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.
28  * 
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`]).
33  * 
34  * (C-not implementable) As there is likely no reason for a user to implement this trait on their
35  * own type(s).
36  * 
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
41  */
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;
50         }
51         @Override @SuppressWarnings("deprecation")
52         protected void finalize() throws Throwable {
53                 if (ptr != 0) { bindings.EventsProvider_free(ptr); } super.finalize();
54         }
55
56         public static interface EventsProviderInterface {
57                 /**
58                  * Processes any events generated since the last call using the given event handler.
59                  * 
60                  * See the trait-level documentation for requirements.
61                  */
62                 void process_pending_events(EventHandler handler);
63         }
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);
73                         }
74                 });
75                 return impl_holder.held;
76         }
77         /**
78          * Processes any events generated since the last call using the given event handler.
79          * 
80          * See the trait-level documentation for requirements.
81          */
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); };
87         }
88
89 }