Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_shield/src/srv/msg/core.rs

3.0 KiB, 34 runs

created by r1870400018:4323, which is this file's identity for as long as the history lasts, whatever it is later renamed to

download · who wrote it · its history

1use oxedyne_fe2o3_core::{
2 prelude::*,
3 byte::Encoding,
4 mem::Extract,
5};
6use oxedyne_fe2o3_jdat::{
7 prelude::*,
8 id::NumIdDat,
9};
10use oxedyne_fe2o3_hash::pow::ZeroBits;
11use oxedyne_fe2o3_net::id;
12use oxedyne_fe2o3_syntax::{
13 SyntaxRef,
14 msg::Msg,
15};
16
17use std::{
18 clone::Clone,
19 fmt,
20};
21
22
23pub trait IdTypes<
24 const ML: usize,
25 const SL: usize,
26 const UL: usize,
27>:
28 Clone
29 + Default
30 + fmt::Debug
31{
32 type M: NumIdDat<ML>;
33 type S: NumIdDat<SL>;
34 type U: NumIdDat<UL>;
35}
36
37#[derive(Clone, Debug, Default)]
38pub struct DefaultIdTypes<
39 const ML: usize,
40 const SL: usize,
41 const UL: usize,
42>;
43
44impl<
45 const ML: usize,
46 const SL: usize,
47 const UL: usize,
48>
49 IdTypes<ML, SL, UL> for DefaultIdTypes<ML, SL, UL>
50 where
51 id::Mid: NumIdDat<ML>,
52 id::Sid: NumIdDat<SL>,
53 id::Uid: NumIdDat<UL>,
54{
55 type M = id::Mid;
56 type S = id::Sid;
57 type U = id::Uid;
58}
59
60pub type MsgType = u16;
61//pub const MSG_TYPE_BYTE_LEN: usize = 2;
62//pub const MSG_TYPE_USER_START: MsgType = 1_024;
63//pub type MsgId = u64;
64//pub const MSG_ID_BYTE_LEN: usize = 8;
65
66/// The MsgFmt captures the syntax protocol against which incoming and outgoing messages are
67/// validated, and the encoding for any outgoing messages.
68#[derive(Clone, Debug, Default)]
69pub struct MsgFmt {
70 pub syntax: SyntaxRef,
71 pub encoding: Encoding,
72}
73
74/// Capture the required (when receiving) and expected (when sending) Proof of Work parameters.
75#[derive(Clone, Debug, Default)]
76pub struct MsgPow {
77 pub zbits: ZeroBits,
78}
79
80impl MsgPow {
81 pub fn from_msg(msg: &mut Msg) -> Outcome<Self> {
82 let zbits = match msg.get_arg_vals_mut("-zb") {
83 Some(v) => try_extract_dat_as!(v[0].extract(), ZeroBits, U8, U16, U32),
84 None => return Err(err!(
85 "No proof of work zero bits specified in message arguments (-zb).";
86 Input, Missing)),
87 };
88 Ok(Self {
89 zbits,
90 })
91 }
92}
93
94/// Capture the user id and possibly the session id.
95#[derive(Clone, Debug, Default)]
96pub struct MsgIds<
97 const SIDL: usize,
98 const UIDL: usize,
99 SID: NumIdDat<SIDL>,
100 UID: NumIdDat<UIDL>,
101> {
102 pub sid_opt: Option<SID>,
103 pub uid: UID,
104}
105
106impl<
107 const SIDL: usize,
108 const UIDL: usize,
109 SID: NumIdDat<SIDL>,
110 UID: NumIdDat<UIDL>,
111>
112 MsgIds<SIDL, UIDL, SID, UID>
113{
114 pub fn from_msg(uid: UID, msg: &mut Msg) -> Outcome<Self> {
115 //let uid = match msg.get_arg_vals_mut("-u") {
116 // Some(v) => try_extract_dat_as!(v[0].extract(), IdDat, U128),
117 // None => return Err(err!(
118 // "No user id value in message arguments (-u).",
119 // ), Input, Missing)),
120 //};
121 let sid_opt = match msg.get_arg_vals_mut("-s") {
122 Some(v) => Some(res!(SID::from_dat(v[0].extract()))),
123 None => None, // not required
124 };
125 Ok(Self {
126 uid,
127 sid_opt,
128 })
129 }
130}
131
132pub trait IdentifiedMessage {
133 fn typ(&self) -> MsgType;
134 fn name(&self) -> &'static str;
135}