mm1_proto_network_management/
protocols.rs

1use std::any::TypeId;
2use std::sync::Arc;
3use std::time::Duration;
4
5use mm1_address::subnet::NetAddress;
6use mm1_common::errors::error_of::ErrorOf;
7use mm1_common::impl_error_kind;
8use mm1_proto::message;
9
10slotmap::new_key_type! {
11    pub struct LocalTypeKey;
12    pub struct ForeignTypeKey;
13}
14
15#[derive(Debug)]
16#[message(base_path = ::mm1_proto)]
17pub struct RegisterOpaqueMessageRequest {
18    pub name: crate::MessageName,
19}
20
21#[derive(Debug)]
22#[message(base_path = ::mm1_proto)]
23pub struct GetMessageNameRequest {
24    pub key: LocalTypeKey,
25}
26
27#[derive(Debug)]
28#[message(base_path = ::mm1_proto)]
29pub struct GetMessageNameResponse {
30    pub name: crate::MessageName,
31}
32
33#[derive(Debug)]
34#[message(base_path = ::mm1_proto)]
35pub struct RegisterOpaqueMessageResponse {
36    pub key: LocalTypeKey,
37}
38
39#[derive(Debug)]
40#[message(base_path = ::mm1_proto)]
41pub struct RegisterProtocolRequest<P> {
42    pub name:     crate::ProtocolName,
43    #[serde(skip)]
44    pub protocol: P,
45}
46
47pub type RegisterProtocolResponse = Result<(), ErrorOf<RegisterProtocolErrorKind>>;
48
49#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
50#[message(base_path = ::mm1_proto)]
51pub enum RegisterProtocolErrorKind {
52    DuplicateProtocolName,
53}
54
55impl_error_kind!(RegisterProtocolErrorKind);
56
57#[derive(Debug)]
58#[message(base_path = ::mm1_proto)]
59pub struct UnregisterProtocolRequest {
60    pub name: crate::ProtocolName,
61}
62
63pub type UnregisterProtocolResponse = Result<(), ErrorOf<UnregisterProtocolErrorKind>>;
64
65#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
66#[message(base_path = ::mm1_proto)]
67pub enum UnregisterProtocolErrorKind {
68    NoProtocol,
69    ProtocolInUse,
70}
71
72impl_error_kind!(UnregisterProtocolErrorKind);
73
74// TODO: move to routing
75#[derive(Debug)]
76#[message(base_path = ::mm1_proto)]
77pub struct RegisterLocalSubnetRequest {
78    pub net: NetAddress,
79}
80
81pub type RegisterLocalSubnetResponse = Result<(), ErrorOf<RegisterLocalSubnetErrorKind>>;
82
83#[derive(Debug)]
84#[message(base_path = ::mm1_proto)]
85pub struct GetLocalSubnetsRequest;
86
87pub type GetLocalSubnetsResponse = Vec<NetAddress>;
88
89#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
90#[message(base_path = ::mm1_proto)]
91pub enum RegisterLocalSubnetErrorKind {}
92
93impl_error_kind!(RegisterLocalSubnetErrorKind);
94
95#[derive(Debug)]
96#[message(base_path = ::mm1_proto)]
97pub struct GetProtocolByNameRequest {
98    pub name:    crate::ProtocolName,
99    pub timeout: Option<Duration>,
100}
101
102#[derive(Debug)]
103#[message(base_path = ::mm1_proto)]
104pub struct ProtocolResolved<P> {
105    #[serde(skip)]
106    pub protocol: Arc<P>,
107
108    pub outbound: Vec<(crate::MessageName, LocalTypeKey)>,
109    pub inbound:  Vec<(crate::MessageName, LocalTypeKey)>,
110}
111
112pub type GetProtocolByNameResponse<P> =
113    Result<ProtocolResolved<P>, ErrorOf<GetProtocolByNameErrorKind>>;
114
115#[message(base_path = ::mm1_proto)]
116pub struct ResolveTypeIdRequest {
117    #[serde(with = "no_serde")]
118    pub type_id: TypeId,
119}
120
121#[message(base_path = ::mm1_proto)]
122pub struct ResolveTypeIdResponse {
123    pub type_key_opt: Option<LocalTypeKey>,
124}
125
126#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
127#[message(base_path = ::mm1_proto)]
128pub enum GetProtocolByNameErrorKind {
129    NoProtocol,
130}
131
132impl_error_kind!(GetProtocolByNameErrorKind);
133
134mod no_serde {
135    use serde::de::Error as _;
136    use serde::ser::Error as _;
137    use serde::{Deserializer, Serializer};
138
139    pub(super) fn serialize<S, T>(_: T, _: S) -> Result<S::Ok, S::Error>
140    where
141        S: Serializer,
142    {
143        let reason = S::Error::custom("not supported");
144        Err(reason)
145    }
146    pub(super) fn deserialize<'de, D, T>(_: D) -> Result<T, D::Error>
147    where
148        D: Deserializer<'de>,
149    {
150        let reason = D::Error::custom("not supported");
151        Err(reason)
152    }
153}