summaryrefslogtreecommitdiff
path: root/common/src/msg.rs
diff options
context:
space:
mode:
authorAria <me@aria.rip>2023-10-13 01:33:38 +0100
committerAria <me@aria.rip>2023-10-13 01:33:38 +0100
commit1b6c1b425f78f4ec3eb275f21a792776e50cbf93 (patch)
tree9adb3c9fc11ee379078b60243f1705e991f7bf5d /common/src/msg.rs
parent186087b2010f7f2b9631a28b80527d99b751b882 (diff)
start using async
Diffstat (limited to 'common/src/msg.rs')
-rw-r--r--common/src/msg.rs50
1 files changed, 34 insertions, 16 deletions
diff --git a/common/src/msg.rs b/common/src/msg.rs
index 23db171..7e9863f 100644
--- a/common/src/msg.rs
+++ b/common/src/msg.rs
@@ -1,10 +1,13 @@
use serde::{Deserialize, Serialize};
+use smol::channel::Sender;
+
+use crate::msg_id::MessageID;
#[derive(Debug, Serialize, Deserialize)]
pub struct Message<B> {
#[serde(flatten)]
pub header: MessageHeader,
- pub body: MaelstromBodyOr<B>,
+ pub body: B,
}
#[derive(Debug, Serialize, Deserialize)]
@@ -24,27 +27,42 @@ impl MessageHeader {
}
#[derive(Debug, Serialize, Deserialize)]
-#[serde(untagged)]
-pub enum MaelstromBodyOr<B> {
- MaelstromBody {
- #[serde(flatten)]
- inner: MaelstromBody,
- },
- Other {
- #[serde(flatten)]
- inner: B,
- },
-}
-
-#[derive(Debug, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum MaelstromBody {
#[serde(rename = "init")]
Init {
node_id: String,
node_ids: Vec<String>,
- msg_id: usize,
+ msg_id: MessageID,
},
#[serde(rename = "init_ok")]
- InitOk { msg_id: usize, in_reply_to: usize },
+ InitOk {
+ msg_id: MessageID,
+ in_reply_to: MessageID,
+ },
+}
+
+pub struct Output<B> {
+ node_id: String,
+ channel: Sender<Message<B>>,
+}
+
+impl<B: Serialize + Clone> Output<B> {
+ pub fn new(node_id: String, channel: Sender<Message<B>>) -> Self {
+ Self { node_id, channel }
+ }
+
+ pub async fn send(&self, dst: &str, body: &B) {
+ self.send_raw(Message {
+ header: MessageHeader {
+ src: self.node_id.clone(),
+ dst: dst.to_string(),
+ },
+ body: body.clone(),
+ })
+ .await;
+ }
+ pub async fn send_raw(&self, msg: Message<B>) {
+ self.channel.send(msg).await.unwrap();
+ }
}