1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
pub mod control;
pub mod distributed;
pub mod quic;

use anyhow::Result;
use lunatic_process::{
    env::Environment,
    runtimes::wasmtime::{WasmtimeCompiledModule, WasmtimeRuntime},
    state::ProcessState,
};
use std::{net::SocketAddr, sync::Arc};

pub trait DistributedCtx: ProcessState + Sized {
    fn new_dist_state(
        environment: Environment,
        distributed: DistributedProcessState,
        runtime: WasmtimeRuntime,
        module: WasmtimeCompiledModule<Self>,
        config: Arc<Self::Config>,
    ) -> Result<Self>;
    fn distributed(&self) -> Result<&DistributedProcessState>;
    fn distributed_mut(&mut self) -> Result<&mut DistributedProcessState>;
    fn module_id(&self) -> u64;
    fn environment_id(&self) -> u64;
    fn can_spawn(&self) -> bool;
}

#[derive(Clone)]
pub struct DistributedProcessState {
    node_id: u64,
    pub control: control::Client,
    pub node_client: distributed::Client,
}

impl DistributedProcessState {
    pub async fn new(
        node_id: u64,
        control_client: control::Client,
        node_client: distributed::Client,
    ) -> Result<Self> {
        Ok(Self {
            node_id,
            control: control_client,
            node_client,
        })
    }

    pub fn node_id(&self) -> u64 {
        self.node_id
    }
}

#[derive(Debug, Clone)]
pub struct NodeInfo {
    pub id: u64,
    pub address: SocketAddr,
    pub name: String,
}