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
pub mod req;
pub mod resp;
mod errors;
pub use self::{
errors::IpcError,
req::{AuthReq, IpcReq},
resp::{AuthGranted, IpcResp},
};
use serde::{Deserialize, Serialize};
use std::{collections::HashSet, net::SocketAddr, u32};
pub type BootstrapConfig = HashSet<SocketAddr>;
#[allow(clippy::large_enum_variant)]
#[derive(Serialize, Deserialize, Debug, PartialEq)]
pub enum IpcMsg {
Req(IpcReq),
Resp(IpcResp),
Err(IpcError),
}
impl IpcMsg {
pub fn new_auth_req(app_id: &str, app_name: &str, app_vendor: &str) -> Self {
use rand::Rng;
let req_id: u32 = rand::thread_rng().gen_range(0, u32::max_value()) + 1;
Self::Req(IpcReq::Auth(AuthReq {
req_id,
app_id: app_id.to_string(),
app_name: app_name.to_string(),
app_vendor: app_vendor.to_string(),
}))
}
pub fn new_unreg_req(user_data: &[u8]) -> Self {
Self::Req(IpcReq::Unregistered(user_data.to_vec()))
}
pub fn to_string(&self) -> Result<String, IpcError> {
serde_json::to_string(self).map_err(|_| IpcError::EncodeDecodeError)
}
pub fn from_string(msg_str: &str) -> Result<IpcMsg, IpcError> {
serde_json::from_str(msg_str).map_err(|_| IpcError::EncodeDecodeError)
}
}