use std::io::{self, Write}; use common::{msg::*, run_with, Handler, MsgWriter}; use serde::{Deserialize, Serialize}; fn main() { run_with::(io::stdin(), io::stdout()); } #[derive(Debug, Serialize, Deserialize)] #[serde(tag = "type")] pub enum EchoBody { #[serde(rename = "echo")] Echo { msg_id: usize, echo: String }, #[serde(rename = "echo_ok")] EchoOk { msg_id: usize, in_reply_to: usize, echo: String, }, } pub struct EchoHandler { next_msg_id: usize, } impl Handler for EchoHandler { type Body = EchoBody; fn init(_node_id: String, _node_ids: Vec, _msg_id: usize) -> Self { EchoHandler { next_msg_id: 1 } } fn handle( &mut self, header: MessageHeader, body: Self::Body, writer: &mut MsgWriter, ) { match body { EchoBody::Echo { msg_id, echo } => { writer.write( header.src, &EchoBody::EchoOk { msg_id: self.next_msg_id, in_reply_to: msg_id, echo, }, ); self.next_msg_id += 1; } EchoBody::EchoOk { .. } => (), }; } }