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