[C#] Check in initial auto-generated C# bindings
[ldk-java] / c_sharp / src / org / ldk / structs / BackgroundProcessor.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 /**
10  * `BackgroundProcessor` takes care of tasks that (1) need to happen periodically to keep
11  * Rust-Lightning running properly, and (2) either can or should be run in the background. Its
12  * responsibilities are:
13  * Processing [`Event`]s with a user-provided [`EventHandler`].
14  * Monitoring whether the [`ChannelManager`] needs to be re-persisted to disk, and if so,
15  * writing it to disk/backups by invoking the callback given to it at startup.
16  * [`ChannelManager`] persistence should be done in the background.
17  * Calling [`ChannelManager::timer_tick_occurred`] and [`PeerManager::timer_tick_occurred`]
18  * at the appropriate intervals.
19  * Calling [`NetworkGraph::remove_stale_channels_and_tracking`] (if a [`GossipSync`] with a
20  * [`NetworkGraph`] is provided to [`BackgroundProcessor::start`]).
21  * 
22  * It will also call [`PeerManager::process_events`] periodically though this shouldn't be relied
23  * upon as doing so may result in high latency.
24  * 
25  * # Note
26  * 
27  * If [`ChannelManager`] persistence fails and the persisted manager becomes out-of-date, then
28  * there is a risk of channels force-closing on startup when the manager realizes it's outdated.
29  * However, as long as [`ChannelMonitor`] backups are sound, no funds besides those used for
30  * unilateral chain closure fees are at risk.
31  * 
32  * [`ChannelMonitor`]: lightning::chain::channelmonitor::ChannelMonitor
33  * [`Event`]: lightning::util::events::Event
34  * BackgroundProcessor will immediately stop on drop. It should be stored until shutdown.
35  */
36 public class BackgroundProcessor : CommonBase {
37         internal BackgroundProcessor(object _dummy, long ptr) : base(ptr) { }
38         ~BackgroundProcessor() {
39                 if (ptr != 0) { bindings.BackgroundProcessor_free(ptr); }
40         }
41
42         /**
43          * Start a background thread that takes care of responsibilities enumerated in the [top-level
44          * documentation].
45          * 
46          * The thread runs indefinitely unless the object is dropped, [`stop`] is called, or
47          * [`Persister::persist_manager`] returns an error. In case of an error, the error is retrieved by calling
48          * either [`join`] or [`stop`].
49          * 
50          * # Data Persistence
51          * 
52          * [`Persister::persist_manager`] is responsible for writing out the [`ChannelManager`] to disk, and/or
53          * uploading to one or more backup services. See [`ChannelManager::write`] for writing out a
54          * [`ChannelManager`]. See the `lightning-persister` crate for LDK's
55          * provided implementation.
56          * 
57          * [`Persister::persist_graph`] is responsible for writing out the [`NetworkGraph`] to disk, if
58          * [`GossipSync`] is supplied. See [`NetworkGraph::write`] for writing out a [`NetworkGraph`].
59          * See the `lightning-persister` crate for LDK's provided implementation.
60          * 
61          * Typically, users should either implement [`Persister::persist_manager`] to never return an
62          * error or call [`join`] and handle any error that may arise. For the latter case,
63          * `BackgroundProcessor` must be restarted by calling `start` again after handling the error.
64          * 
65          * # Event Handling
66          * 
67          * `event_handler` is responsible for handling events that users should be notified of (e.g.,
68          * payment failed). [`BackgroundProcessor`] may decorate the given [`EventHandler`] with common
69          * functionality implemented by other handlers.
70          * [`P2PGossipSync`] if given will update the [`NetworkGraph`] based on payment failures.
71          * 
72          * # Rapid Gossip Sync
73          * 
74          * If rapid gossip sync is meant to run at startup, pass [`RapidGossipSync`] via `gossip_sync`
75          * to indicate that the [`BackgroundProcessor`] should not prune the [`NetworkGraph`] instance
76          * until the [`RapidGossipSync`] instance completes its first sync.
77          * 
78          * [top-level documentation]: BackgroundProcessor
79          * [`join`]: Self::join
80          * [`stop`]: Self::stop
81          * [`ChannelManager`]: lightning::ln::channelmanager::ChannelManager
82          * [`ChannelManager::write`]: lightning::ln::channelmanager::ChannelManager#impl-Writeable
83          * [`Persister::persist_manager`]: lightning::util::persist::Persister::persist_manager
84          * [`Persister::persist_graph`]: lightning::util::persist::Persister::persist_graph
85          * [`NetworkGraph`]: lightning::routing::gossip::NetworkGraph
86          * [`NetworkGraph::write`]: lightning::routing::gossip::NetworkGraph#impl-Writeable
87          */
88         public static BackgroundProcessor start(org.ldk.structs.Persister persister, org.ldk.structs.EventHandler event_handler, org.ldk.structs.ChainMonitor chain_monitor, org.ldk.structs.ChannelManager channel_manager, org.ldk.structs.GossipSync gossip_sync, org.ldk.structs.PeerManager peer_manager, org.ldk.structs.Logger logger, org.ldk.structs.Option_WriteableScoreZ scorer) {
89                 long ret = bindings.BackgroundProcessor_start(persister == null ? 0 : persister.ptr, event_handler == null ? 0 : event_handler.ptr, chain_monitor == null ? 0 : chain_monitor.ptr, channel_manager == null ? 0 : channel_manager.ptr, gossip_sync.ptr, peer_manager == null ? 0 : peer_manager.ptr, logger == null ? 0 : logger.ptr, scorer.ptr);
90                 GC.KeepAlive(persister);
91                 GC.KeepAlive(event_handler);
92                 GC.KeepAlive(chain_monitor);
93                 GC.KeepAlive(channel_manager);
94                 GC.KeepAlive(gossip_sync);
95                 GC.KeepAlive(peer_manager);
96                 GC.KeepAlive(logger);
97                 GC.KeepAlive(scorer);
98                 if (ret >= 0 && ret <= 4096) { return null; }
99                 org.ldk.structs.BackgroundProcessor ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.BackgroundProcessor(null, ret); }
100                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(ret_hu_conv); };
101                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(persister); };
102                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(event_handler); };
103                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(chain_monitor); };
104                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(channel_manager); };
105                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(peer_manager); };
106                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(logger); };
107                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(scorer); };
108                 return ret_hu_conv;
109         }
110
111         /**
112          * Join `BackgroundProcessor`'s thread, returning any error that occurred while persisting
113          * [`ChannelManager`].
114          * 
115          * # Panics
116          * 
117          * This function panics if the background thread has panicked such as while persisting or
118          * handling events.
119          * 
120          * [`ChannelManager`]: lightning::ln::channelmanager::ChannelManager
121          */
122         public Result_NoneErrorZ join() {
123                 long ret = bindings.BackgroundProcessor_join(this.ptr);
124                 GC.KeepAlive(this);
125                 if (ret >= 0 && ret <= 4096) { return null; }
126                 Result_NoneErrorZ ret_hu_conv = Result_NoneErrorZ.constr_from_ptr(ret);
127                 if (this != null) { this.ptrs_to.AddLast(this); };
128                 // Due to rust's strict-ownership memory model, in some cases we need to "move"
129                 // an object to pass exclusive ownership to the function being called.
130                 // In most cases, we avoid this being visible in GC'd languages by cloning the object
131                 // at the FFI layer, creating a new object which Rust can claim ownership of
132                 // However, in some cases (eg here), there is no way to clone an object, and thus
133                 // we actually have to pass full ownership to Rust.
134                 // Thus, after this call, this is reset to null and is now a dummy object.
135                 this.ptr = 0;;
136                 return ret_hu_conv;
137         }
138
139         /**
140          * Stop `BackgroundProcessor`'s thread, returning any error that occurred while persisting
141          * [`ChannelManager`].
142          * 
143          * # Panics
144          * 
145          * This function panics if the background thread has panicked such as while persisting or
146          * handling events.
147          * 
148          * [`ChannelManager`]: lightning::ln::channelmanager::ChannelManager
149          */
150         public Result_NoneErrorZ stop() {
151                 long ret = bindings.BackgroundProcessor_stop(this.ptr);
152                 GC.KeepAlive(this);
153                 if (ret >= 0 && ret <= 4096) { return null; }
154                 Result_NoneErrorZ ret_hu_conv = Result_NoneErrorZ.constr_from_ptr(ret);
155                 if (this != null) { this.ptrs_to.AddLast(this); };
156                 // Due to rust's strict-ownership memory model, in some cases we need to "move"
157                 // an object to pass exclusive ownership to the function being called.
158                 // In most cases, we avoid this being visible in GC'd languages by cloning the object
159                 // at the FFI layer, creating a new object which Rust can claim ownership of
160                 // However, in some cases (eg here), there is no way to clone an object, and thus
161                 // we actually have to pass full ownership to Rust.
162                 // Thus, after this call, this is reset to null and is now a dummy object.
163                 this.ptr = 0;;
164                 return ret_hu_conv;
165         }
166
167 }
168 } } }