ff15c350e9139967667be222fb1869d83da8597e
[ldk-java] / c_sharp / src / org / ldk / structs / EventsProvider.cs
1 using org.ldk.impl;
2 using org.ldk.enums;
3 using org.ldk.util;
4 using System;
5
6 namespace org { namespace ldk { namespace structs {
7
8 /**
9  * A trait indicating an object may generate events.
10  * 
11  * Events are processed by passing an [`EventHandler`] to [`process_pending_events`].
12  * 
13  * Implementations of this trait may also feature an async version of event handling, as shown with
14  * [`ChannelManager::process_pending_events_async`] and
15  * [`ChainMonitor::process_pending_events_async`].
16  * 
17  * # Requirements
18  * 
19  * When using this trait, [`process_pending_events`] will call [`handle_event`] for each pending
20  * event since the last invocation.
21  * 
22  * In order to ensure no [`Event`]s are lost, implementors of this trait will persist [`Event`]s
23  * and replay any unhandled events on startup. An [`Event`] is considered handled when
24  * [`process_pending_events`] returns, thus handlers MUST fully handle [`Event`]s and persist any
25  * relevant changes to disk *before* returning.
26  * 
27  * Further, because an application may crash between an [`Event`] being handled and the
28  * implementor of this trait being re-serialized, [`Event`] handling must be idempotent - in
29  * effect, [`Event`]s may be replayed.
30  * 
31  * Note, handlers may call back into the provider and thus deadlocking must be avoided. Be sure to
32  * consult the provider's documentation on the implication of processing events and how a handler
33  * may safely use the provider (e.g., see [`ChannelManager::process_pending_events`] and
34  * [`ChainMonitor::process_pending_events`]).
35  * 
36  * (C-not implementable) As there is likely no reason for a user to implement this trait on their
37  * own type(s).
38  * 
39  * [`process_pending_events`]: Self::process_pending_events
40  * [`handle_event`]: EventHandler::handle_event
41  * [`ChannelManager::process_pending_events`]: crate::ln::channelmanager::ChannelManager#method.process_pending_events
42  * [`ChainMonitor::process_pending_events`]: crate::chain::chainmonitor::ChainMonitor#method.process_pending_events
43  * [`ChannelManager::process_pending_events_async`]: crate::ln::channelmanager::ChannelManager::process_pending_events_async
44  * [`ChainMonitor::process_pending_events_async`]: crate::chain::chainmonitor::ChainMonitor::process_pending_events_async
45  */
46 public class EventsProvider : CommonBase {
47         internal readonly bindings.LDKEventsProvider bindings_instance;
48         internal EventsProvider(object _dummy, long ptr) : base(ptr) { bindings_instance = null; }
49         private EventsProvider(bindings.LDKEventsProvider arg) : base(bindings.LDKEventsProvider_new(arg)) {
50                 this.ptrs_to.AddLast(arg);
51                 this.bindings_instance = arg;
52         }
53         ~EventsProvider() {
54                 if (ptr != 0) { bindings.EventsProvider_free(ptr); }
55         }
56
57         public interface EventsProviderInterface {
58                 /**
59                  * Processes any events generated since the last call using the given event handler.
60                  * 
61                  * See the trait-level documentation for requirements.
62                  */
63                 void process_pending_events(EventHandler _handler);
64         }
65         private class LDKEventsProviderHolder { internal EventsProvider held; }
66         private class LDKEventsProviderImpl : bindings.LDKEventsProvider {
67                 internal LDKEventsProviderImpl(EventsProviderInterface arg, LDKEventsProviderHolder impl_holder) { this.arg = arg; this.impl_holder = impl_holder; }
68                 private EventsProviderInterface arg;
69                 private LDKEventsProviderHolder impl_holder;
70                 public void process_pending_events(long _handler) {
71                         EventHandler ret_hu_conv = new EventHandler(null, _handler);
72                         if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(this); };
73                         arg.process_pending_events(ret_hu_conv);
74                                 GC.KeepAlive(arg);
75                 }
76         }
77         public static EventsProvider new_impl(EventsProviderInterface arg) {
78                 LDKEventsProviderHolder impl_holder = new LDKEventsProviderHolder();
79                 impl_holder.held = new EventsProvider(new LDKEventsProviderImpl(arg, impl_holder));
80                 return impl_holder.held;
81         }
82         /**
83          * Processes any events generated since the last call using the given event handler.
84          * 
85          * See the trait-level documentation for requirements.
86          */
87         public void process_pending_events(org.ldk.structs.EventHandler handler) {
88                 bindings.EventsProvider_process_pending_events(this.ptr, handler == null ? 0 : handler.ptr);
89                 GC.KeepAlive(this);
90                 GC.KeepAlive(handler);
91                 if (this != null) { this.ptrs_to.AddLast(handler); };
92         }
93
94 }
95 } } }