1 package org.ldk.structs;
3 import org.ldk.impl.bindings;
4 import org.ldk.enums.*;
6 import java.util.Arrays;
7 import javax.annotation.Nullable;
11 * BackgroundProcessor takes care of tasks that (1) need to happen periodically to keep
12 * Rust-Lightning running properly, and (2) either can or should be run in the background. Its
13 * responsibilities are:
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
18 * `PeerManager::timer_tick_occurred()` every minute (can be done in the
21 * Note that if ChannelManager persistence fails and the persisted manager becomes out-of-date,
22 * then there is a risk of channels force-closing on startup when the manager realizes it's
23 * outdated. However, as long as `ChannelMonitor` backups are sound, no funds besides those used
24 * for unilateral chain closure fees are at risk.
26 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
27 public class BackgroundProcessor extends CommonBase {
28 BackgroundProcessor(Object _dummy, long ptr) { super(ptr); }
29 @Override @SuppressWarnings("deprecation")
30 protected void finalize() throws Throwable {
32 if (ptr != 0) { bindings.BackgroundProcessor_free(ptr); }
36 * Start a background thread that takes care of responsibilities enumerated in the top-level
39 * If `persist_manager` returns an error, then this thread will return said error (and
40 * `start()` will need to be called again to restart the `BackgroundProcessor`). Users should
41 * wait on [`thread_handle`]'s `join()` method to be able to tell if and when an error is
42 * returned, or implement `persist_manager` such that an error is never returned to the
43 * `BackgroundProcessor`
45 * `persist_manager` is responsible for writing out the [`ChannelManager`] to disk, and/or
46 * uploading to one or more backup services. See [`ChannelManager::write`] for writing out a
47 * [`ChannelManager`]. See [`FilesystemPersister::persist_manager`] for Rust-Lightning's
48 * provided implementation.
50 * [`thread_handle`]: BackgroundProcessor::thread_handle
51 * [`ChannelManager`]: lightning::ln::channelmanager::ChannelManager
52 * [`ChannelManager::write`]: lightning::ln::channelmanager::ChannelManager#impl-Writeable
53 * [`FilesystemPersister::persist_manager`]: lightning_persister::FilesystemPersister::persist_manager
55 public static BackgroundProcessor start(ChannelManagerPersister persister, EventHandler event_handler, ChainMonitor chain_monitor, ChannelManager channel_manager, PeerManager peer_manager, Logger logger) {
56 long ret = bindings.BackgroundProcessor_start(persister == null ? 0 : persister.ptr, event_handler == null ? 0 : event_handler.ptr, chain_monitor == null ? 0 : chain_monitor.ptr & ~1, channel_manager == null ? 0 : channel_manager.ptr & ~1, peer_manager == null ? 0 : peer_manager.ptr & ~1, logger == null ? 0 : logger.ptr);
57 if (ret < 1024) { return null; }
58 BackgroundProcessor ret_hu_conv = new BackgroundProcessor(null, ret);
59 ret_hu_conv.ptrs_to.add(ret_hu_conv);
60 ret_hu_conv.ptrs_to.add(persister);
61 ret_hu_conv.ptrs_to.add(event_handler);
62 ret_hu_conv.ptrs_to.add(chain_monitor);
63 ret_hu_conv.ptrs_to.add(channel_manager);
64 ret_hu_conv.ptrs_to.add(peer_manager);
65 ret_hu_conv.ptrs_to.add(logger);
70 * Stop `BackgroundProcessor`'s thread.
72 public Result_NoneErrorZ stop() {
73 long ret = bindings.BackgroundProcessor_stop(this.ptr);
74 if (ret < 1024) { return null; }
75 Result_NoneErrorZ ret_hu_conv = Result_NoneErrorZ.constr_from_ptr(ret);
76 this.ptrs_to.add(this);
77 // Due to rust's strict-ownership memory model, in some cases we need to "move"
78 // an object to pass exclusive ownership to the function being called.
79 // In most cases, we avoid this being visible in GC'd languages by cloning the object
80 // at the FFI layer, creating a new object which Rust can claim ownership of
81 // However, in some cases (eg here), there is no way to clone an object, and thus
82 // we actually have to pass full ownership to Rust.
83 // Thus, after this call, this is reset to null and is now a dummy object.