Module daemon

Module daemon 

Source
Expand description

Daemon infrastructure for plugin development

This module provides utilities for creating daemon-based plugins with IPC communication. It offers three levels of abstraction:

  1. Low-level utilities (socket, ipc) - Direct control over sockets and IPC
  2. Mid-level client helper (client) - Automatic retry-with-autospawn pattern
  3. High-level traits (traits) - Structured daemon framework

§Example: Using the client helper

use taiga_plugin_api::daemon::{
    client::{DaemonClientConfig, send_command_with_autospawn},
    ipc::DaemonSpawnConfig,
};
use serde::{Serialize, Deserialize};

#[derive(Serialize)]
struct MyCommand { action: String }

#[derive(Deserialize)]
struct MyResponse { result: String }

async fn send_command() -> Result<MyResponse, PluginError> {
    let config = DaemonClientConfig::new(
        "/tmp/my-plugin.sock",
        DaemonSpawnConfig::new("my-plugin", "daemon"),
    );

    let cmd = MyCommand { action: "test".into() };
    send_command_with_autospawn(&config, &cmd).await
}

§Example: Using the daemon framework

use taiga_plugin_api::daemon::traits::{DaemonHandler, DaemonConfig, HandleResult, run_daemon_loop};
use async_trait::async_trait;

struct MyDaemon { counter: u32 }

#[async_trait]
impl DaemonHandler for MyDaemon {
    type Command = MyCommand;
    type Response = MyResponse;

    async fn handle_command(&mut self, cmd: Self::Command) -> HandleResult<Self::Response> {
        HandleResult::response(MyResponse::Ok)
    }
}

// Run the daemon:
run_daemon_loop(DaemonConfig::new("/tmp/my.sock"), MyDaemon { counter: 0 }).await?;

Re-exports§

pub use socket::create_listener;
pub use socket::connect;
pub use socket::cleanup_socket;
pub use ipc::send_message;
pub use ipc::receive_message;
pub use ipc::spawn_daemon_process;
pub use ipc::DaemonSpawnConfig;
pub use client::send_command_with_autospawn;
pub use client::DaemonClientConfig;
pub use traits::DaemonHandler;
pub use traits::DaemonConfig;
pub use traits::HandleResult;
pub use traits::run_daemon_loop;

Modules§

client
Generic client helper for daemon-based plugins
ipc
IPC message utilities for daemon-based plugins
socket
Cross-platform socket utilities for daemon-based plugins
traits
Generic daemon framework traits and runner