anachro_icd/arbitrator.rs
1//! # Arbitrator Messages
2//!
3//! These are messages that are sent FROM the central Arbitrator,
4//! to the peripheral Components/Clients.
5//!
6//! The [`Arbitrator` enum](enum.Arbitrator.html) is the top level
7//! message sent by the Arbitrator.
8
9use crate::{PubSubPath, Uuid};
10use serde::{Deserialize, Serialize};
11
12/// The primary Arbitrator mesage
13///
14/// These are all messages that are sent FROM the Arbitrator,
15/// TO the Components/Clients
16#[derive(Debug, Serialize, Deserialize, Eq, PartialEq)]
17pub enum Arbitrator<'a> {
18 /// Control messages
19 ///
20 /// Control messages are intended to be the primary
21 /// management channel between an Arbitrator and a
22 /// Component/Client
23 Control(Control),
24
25 /// Pub/Sub messages
26 ///
27 /// These are messages sent on the Publish/Subscribe
28 /// channel
29 #[serde(borrow)]
30 PubSub(Result<PubSubResponse<'a>, PubSubError>),
31
32 /// Object Store messages
33 ///
34 /// These are messages intended for the Object Store
35 /// channel for sending bulk messages.
36 ///
37 /// This functionality has not yet been implemented.
38 ObjStore,
39
40 /// Mailbox messages
41 ///
42 /// These are messages intended for the Mailbox layer,
43 /// including guaranteed delivery messages and bulk
44 /// message delivery
45 ///
46 /// This functionality has not yet been implemented.
47 Mailbox,
48}
49
50/// An Arbitrator Response to a Pub/Sub message
51///
52/// These are any Arbitrator -> Client relevant messages
53#[derive(Debug, Serialize, Deserialize, Eq, PartialEq)]
54pub enum PubSubResponse<'a> {
55 /// Subscription Acknowledgement
56 ///
57 /// Sent to acknowledge the reception of subscription
58 /// request from a client
59 SubAck {
60 #[serde(borrow)]
61 path: PubSubPath<'a>,
62 },
63
64 /// Subscription Message
65 ///
66 /// This is a "subscribed to" message, containing a
67 /// payload sent by another Client
68 SubMsg(SubMsg<'a>),
69}
70
71/// Subscription Message
72///
73/// This is a message that has been subscribed to by a
74/// client.
75#[derive(Debug, Serialize, Deserialize, Eq, PartialEq)]
76pub struct SubMsg<'a> {
77 /// The path that this message was sent to
78 ///
79 /// Note: If the client used wildcard subscribe(s), this
80 /// may not match the subscription text
81 #[serde(borrow)]
82 pub path: PubSubPath<'a>,
83
84 /// The payload sent along with the message
85 pub payload: &'a [u8],
86}
87
88/// Control Message
89///
90/// This is the 'control channel', used for establishing
91/// and managing connections between the Arbitrator and
92/// Client(s).
93#[derive(Debug, Serialize, Deserialize, Eq, PartialEq)]
94pub struct Control {
95 /// Sequence Number
96 ///
97 /// This number is provided by the client. The Arbitrator
98 /// will always respond with the same sequence number when
99 /// replying to a specific message
100 pub seq: u16,
101
102 /// Response
103 ///
104 /// The arbitrator response to the client request
105 pub response: Result<ControlResponse, ControlError>,
106}
107
108/// Control Response
109///
110/// A successful response to a Client's request
111#[derive(Debug, Serialize, Deserialize, Eq, PartialEq)]
112pub enum ControlResponse {
113 /// The client/component has registered
114 ComponentRegistration(Uuid),
115
116 /// The client has registered a Pub/Sub path shortcode
117 PubSubShortRegistration(u16),
118}
119
120/// Control Message Errors
121#[derive(Debug, Serialize, Deserialize, Eq, PartialEq)]
122pub enum ControlError {
123 NoWildcardsInShorts,
124 ResetConnection,
125}
126
127/// Publish/Subscribe Errors
128#[derive(Debug, Serialize, Deserialize, Eq, PartialEq)]
129pub enum PubSubError {}
130
131#[cfg(test)]
132mod test {
133 use super::*;
134 use postcard::{from_bytes, to_stdvec};
135
136 #[test]
137 fn ser_check() {
138 let uuid = Uuid::from_bytes([
139 0xd0, 0x36, 0xe7, 0x3b, 0x23, 0xec, 0x4f, 0x60, 0xac, 0xcb, 0x0e, 0xdd, 0xb6, 0x17,
140 0xf4, 0x71,
141 ]);
142 let msg = Arbitrator::Control(Control {
143 seq: 0x0405,
144 response: Ok(ControlResponse::ComponentRegistration(uuid)),
145 });
146
147 let ser_msg = to_stdvec(&msg).unwrap();
148 assert_eq!(
149 &ser_msg[..],
150 &[
151 0x00, // Arbitrator::Control
152 0x05, 0x04, // seq
153 0x00, // OK
154 0x00, // ControlResponse::ComponentRegistration
155 0xd0, 0x36, 0xe7, 0x3b, 0x23, 0xec, 0x4f, 0x60, 0xac, 0xcb, 0x0e, 0xdd, 0xb6, 0x17,
156 0xf4, 0x71,
157 ],
158 );
159
160 let deser_msg: Arbitrator = from_bytes(&ser_msg).unwrap();
161 assert_eq!(deser_msg, msg);
162 }
163}