ipc_broker/
rpc.rs

1use std::fmt;
2
3use serde::{Deserialize, Serialize};
4use uuid::Uuid;
5
6pub const BUF_SIZE: usize = (u16::MAX as usize) + 1;
7pub const TCP_ADDR: &str = "0.0.0.0:5123";
8#[cfg(unix)]
9pub const UNIX_PATH: &str = "/tmp/ipc_broker.sock";
10#[cfg(windows)]
11pub const PIPE_PATH: &str = r"\\.\pipe\ipc_broker";
12
13#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
14pub struct ClientId(String);
15
16impl From<Uuid> for ClientId {
17    fn from(uuid: Uuid) -> Self {
18        ClientId(uuid.to_string())
19    }
20}
21
22impl fmt::Display for ClientId {
23    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
24        // Adjust this to however ClientId should be displayed, for example if it wraps a Uuid:
25        write!(f, "{}", self.0)
26    }
27}
28
29#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
30pub struct CallId(String);
31
32impl From<Uuid> for CallId {
33    fn from(uuid: Uuid) -> Self {
34        CallId(uuid.to_string())
35    }
36}
37
38#[derive(Serialize, Deserialize, Debug, PartialEq)]
39pub enum RpcRequest {
40    RegisterObject {
41        object_name: String,
42    },
43    RegisterService {
44        object_name: String,
45        service_name: String,
46    },
47    Call {
48        call_id: CallId,
49        object_name: String,
50        method: String,
51        args: serde_json::Value,
52    },
53    Subscribe {
54        object_name: String,
55        topic: String,
56    },
57    Publish {
58        object_name: String,
59        topic: String,
60        args: serde_json::Value,
61    },
62    HasObject {
63        object_name: String,
64    },
65}
66
67#[derive(Serialize, Deserialize, Debug, PartialEq)]
68pub enum RpcResponse {
69    Registered {
70        object_name: String,
71    },
72    Result {
73        call_id: CallId,
74        object_name: String,
75        value: serde_json::Value,
76    },
77    Error {
78        call_id: Option<CallId>,
79        object_name: String,
80        message: String,
81    },
82    Event {
83        object_name: String,
84        topic: String,
85        args: serde_json::Value,
86    },
87    Subscribed {
88        object_name: String,
89        topic: String,
90    },
91    HasObjectResult {
92        object_name: String,
93        exists: bool,
94    },
95}