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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
use crate::{ComponentId, UserId};
use anyhow::Result;
use derive::FromValue;
use netidx_derive::Pack;
use serde::{Deserialize, Serialize};
use uuid::Uuid;

#[derive(
    Debug, Clone, Copy, PartialEq, Eq, Hash, Pack, FromValue, Serialize, Deserialize,
)]
pub enum Address {
    Component(ComponentId),
    Channel(UserId, u32),
}

impl From<ComponentId> for Address {
    #[inline(always)]
    fn from(id: ComponentId) -> Self {
        Self::Component(id)
    }
}

impl std::fmt::Display for Address {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Address::Component(id) => write!(f, "#{}", id),
            Address::Channel(user_id, channel) => write!(f, "{}:{}", user_id, channel),
        }
    }
}

impl Address {
    #[inline(always)]
    pub fn is_loopback(&self) -> bool {
        match self {
            Address::Component(id) => id.is_loopback(),
            Address::Channel(..) => false,
        }
    }

    #[inline(always)]
    pub fn component<T>(id: T) -> Result<Self>
    where
        T: TryInto<ComponentId>,
        <T as TryInto<ComponentId>>::Error: std::error::Error + Send + Sync + 'static,
    {
        Ok(Self::Component(id.try_into()?))
    }
}

/// Architect components communicate with each other by sending `Envelope`s.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Pack, FromValue, Serialize)]
pub struct Envelope<M: 'static> {
    pub src: Address,
    pub dst: Address,
    pub stamp: Stamp,
    pub msg: M,
}

impl<M> Envelope<M> {
    pub fn system_control(msg: M) -> Self {
        Self {
            src: Address::Component(ComponentId::none()),
            dst: Address::Component(ComponentId::none()),
            stamp: Stamp::default(),
            msg,
        }
    }
}

#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Pack, FromValue, Serialize)]
pub struct Stamp {
    pub user_id: Option<UserId>,
    pub sequence: Option<Sequence>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Pack, FromValue, Serialize)]
pub enum Sequence {
    Local(u64),
    Remote { core_id: Uuid, last_seqno: Option<u64>, seqno: u64 },
}