From 1b6c1b425f78f4ec3eb275f21a792776e50cbf93 Mon Sep 17 00:00:00 2001 From: Aria Date: Fri, 13 Oct 2023 01:33:38 +0100 Subject: start using async --- common/src/msg.rs | 50 ++++++++++++++++++++++++++++++++++---------------- 1 file changed, 34 insertions(+), 16 deletions(-) (limited to 'common/src/msg.rs') 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 { #[serde(flatten)] pub header: MessageHeader, - pub body: MaelstromBodyOr, + pub body: B, } #[derive(Debug, Serialize, Deserialize)] @@ -23,19 +26,6 @@ impl MessageHeader { } } -#[derive(Debug, Serialize, Deserialize)] -#[serde(untagged)] -pub enum MaelstromBodyOr { - MaelstromBody { - #[serde(flatten)] - inner: MaelstromBody, - }, - Other { - #[serde(flatten)] - inner: B, - }, -} - #[derive(Debug, Serialize, Deserialize)] #[serde(tag = "type")] pub enum MaelstromBody { @@ -43,8 +33,36 @@ pub enum MaelstromBody { Init { node_id: String, node_ids: Vec, - 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 { + node_id: String, + channel: Sender>, +} + +impl Output { + pub fn new(node_id: String, channel: Sender>) -> 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) { + self.channel.send(msg).await.unwrap(); + } } -- cgit v1.2.3