[C#] Run tests against release library in determinism CI run
[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  * 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`].
18  * 
19  * # Requirements
20  * 
21  * When using this trait, [`process_pending_events`] will call [`handle_event`] for each pending
22  * event since the last invocation.
23  * 
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.
28  * 
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.
32  * 
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`]).
37  * 
38  * (C-not implementable) As there is likely no reason for a user to implement this trait on their
39  * own type(s).
40  * 
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
47  */
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;
56         }
57         @Override @SuppressWarnings("deprecation")
58         protected void finalize() throws Throwable {
59                 if (ptr != 0) { bindings.EventsProvider_free(ptr); } super.finalize();
60         }
61         /**
62          * Destroys the object, freeing associated resources. After this call, any access
63          * to this object may result in a SEGFAULT or worse.
64          *
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
68          * until the GC runs.
69          */
70         public void destroy() {
71                 if (ptr != 0) { bindings.EventsProvider_free(ptr); }
72                 ptr = 0;
73         }
74         public static interface EventsProviderInterface {
75                 /**
76                  * Processes any events generated since the last call using the given event handler.
77                  * 
78                  * See the trait-level documentation for requirements.
79                  */
80                 void process_pending_events(EventHandler handler);
81         }
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);
91                         }
92                 });
93                 return impl_holder.held;
94         }
95         /**
96          * Processes any events generated since the last call using the given event handler.
97          * 
98          * See the trait-level documentation for requirements.
99          */
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); };
105         }
106
107 }