Naut/include/naut/swarm.h
ookami125 b633b7d216 nautd/webui: scripting, labels, settings, set-location, pause fix
Session checkpoint on webui-plugin:
- engine dump (nautctl dump) + engine endgame integration
- per-file move locations persistence; torrent-level "Set location"
  with reset/keep-relative/leave-separate handling + residual prune
- Lua: naut.get_labels, define_settings/get_setting (script_host struct)
- daemon-owned labels (category+tags) + taxonomy persistence; webui write-through
- fix: pausing a completed/seeding torrent now sticks (stop wins over result)
- automation tab responsive layout; anime_sort label gating + settings

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-21 23:19:41 -04:00

124 lines
4.1 KiB
C

/* swarm.h - reusable multi-peer download driver.
*
* naut_swarm is a small CLI wrapper around this API. Long-running applications
* such as nautd own the worker thread and use callbacks for progress, control
* commands, cancellation, and event delivery.
*/
#ifndef NAUT_SWARM_H
#define NAUT_SWARM_H
#include "naut/common.h"
#include "naut/event.h"
#include "naut/storage.h"
#define NAUT_SWARM_MAX_PEER_STATS 64
#define NAUT_SWARM_MAX_PIECE_STATS 4000
#define NAUT_SWARM_MAX_TRACKER_STATS 32
#define NAUT_SWARM_MAX_FILE_STATS 1024
typedef struct {
char ip[46];
uint16_t port;
char client[64];
char connection[16];
char flags[16];
double progress;
double relevance;
double dlspeed;
double upspeed;
uint64_t downloaded;
uint64_t uploaded;
} naut_swarm_peer_stats;
typedef struct {
char url[256];
int32_t tier;
char status[32];
int32_t seeds;
int32_t peers;
int32_t leeches;
int32_t downloaded;
char message[128];
} naut_swarm_tracker_stats;
typedef struct {
char path[512];
uint64_t size;
double progress;
int32_t priority;
double availability;
} naut_swarm_file_stats;
typedef struct {
uint64_t total_bytes;
uint64_t bytes_done;
uint32_t total_pieces;
uint32_t pieces_done;
uint32_t peers_total; /* discovered endpoints */
uint32_t peers_connecting;
uint32_t peers_active;
uint32_t peers_failed;
bool stalled;
double elapsed_seconds;
uint32_t peer_count;
naut_swarm_peer_stats peer_stats[NAUT_SWARM_MAX_PEER_STATS];
uint32_t tracker_count;
naut_swarm_tracker_stats tracker_stats[NAUT_SWARM_MAX_TRACKER_STATS];
uint32_t file_count;
naut_swarm_file_stats file_stats[NAUT_SWARM_MAX_FILE_STATS];
uint32_t piece_state_count;
uint8_t piece_states[NAUT_SWARM_MAX_PIECE_STATS];
} naut_swarm_stats;
typedef void (*naut_swarm_progress_cb)(void *context,
const naut_swarm_stats *stats);
/* Called on the swarm owner thread. The callback may safely operate on storage,
* including relocating completed files. */
typedef void (*naut_swarm_control_cb)(void *context, naut_storage *storage);
typedef bool (*naut_swarm_stop_cb)(void *context);
/* Optional: return the desired engine-wide download cap in bytes/sec (0 =
* unlimited). Polled on the swarm owner thread; the engine limit is updated
* whenever the returned value changes. */
typedef uint64_t (*naut_swarm_rate_cb)(void *context);
/* Optional diagnostics. should_dump is polled on the swarm owner thread; when it
* returns true the swarm renders a full engine + piece-assembly state dump and
* hands the text to on_dump (also on the owner thread, where engine and download
* state can be read safely). Used by `nautctl dump` to investigate why a few
* pieces never finish downloading. */
typedef bool (*naut_swarm_dump_cb)(void *context);
typedef void (*naut_swarm_dump_sink)(void *context, const char *text);
/* A file's last known on-disk location, from a relocate on a prior run. Passed
* back in so the file is reopened in place instead of re-downloaded. */
typedef struct {
uint32_t file_index;
const char *path;
} naut_swarm_file_location;
typedef struct {
const char *source; /* .torrent path or magnet URI */
const char *output_dir;
const char *const *peers; /* optional explicit ip:port endpoints */
size_t num_peers;
const naut_swarm_file_location *locations; /* optional moved-file locations */
size_t num_locations;
uint64_t torrent_id;
naut_event_bus *events; /* optional */
bool keep_alive; /* retain completed storage until stopped */
naut_swarm_progress_cb on_progress;
naut_swarm_control_cb on_control;
naut_swarm_stop_cb should_stop;
naut_swarm_rate_cb download_rate; /* optional download throttle provider */
naut_swarm_dump_cb should_dump; /* optional state-dump request poll */
naut_swarm_dump_sink on_dump; /* optional rendered-dump sink */
void *context;
} naut_swarm_config;
/* Blocks until the torrent completes, is cancelled, or fails. */
naut_err naut_swarm_run(const naut_swarm_config *config);
#endif /* NAUT_SWARM_H */