use std::{ops::ControlFlow, time::Duration}; use ump_ng_server::{AsyncHandler, ThreadedHandler}; #[derive(Debug)] #[allow(dead_code)] pub enum Put { Signal, Sleep(Duration), Croak, CroakSleepCount } #[derive(Debug)] #[allow(dead_code)] pub enum Request { Add(u32, u32), GetSignalState, Croak } #[derive(Debug, PartialEq, Eq)] #[allow(dead_code)] pub enum Reply { Sum(u64), SignalState(bool), OkIWillCroak } #[derive(Default)] pub struct ThreadedServer { sleep_count: usize, did_signal: bool } impl ThreadedHandler for ThreadedServer { fn post(&mut self, msg: Put) -> ControlFlow { match msg { Put::Signal => { self.did_signal = true; ControlFlow::Continue(()) } Put::Sleep(dur) => { self.sleep_count += 1; std::thread::sleep(dur); ControlFlow::Continue(()) } Put::Croak => ControlFlow::Break(42), Put::CroakSleepCount => { ControlFlow::Break(u32::try_from(self.sleep_count).unwrap()) } } } fn req( &mut self, msg: Request, rctx: ump_ng_server::ReplyContext ) -> ControlFlow { match msg { Request::Add(a, b) => { rctx.reply(Reply::Sum(u64::from(a + b))).unwrap(); ControlFlow::Continue(()) } Request::GetSignalState => { rctx.reply(Reply::SignalState(self.did_signal)).unwrap(); ControlFlow::Continue(()) } Request::Croak => { rctx.reply(Reply::OkIWillCroak).unwrap(); ControlFlow::Break(42) } } } } #[derive(Default)] pub struct AsyncServer { sleep_count: usize, did_signal: bool } #[ump_ng_server::async_trait] impl AsyncHandler for AsyncServer { async fn post(&mut self, msg: Put) -> ControlFlow { match msg { Put::Signal => { self.did_signal = true; ControlFlow::Continue(()) } Put::Sleep(dur) => { self.sleep_count += 1; std::thread::sleep(dur); ControlFlow::Continue(()) } Put::Croak => ControlFlow::Break(42), Put::CroakSleepCount => { ControlFlow::Break(u32::try_from(self.sleep_count).unwrap()) } } } async fn req( &mut self, msg: Request, rctx: ump_ng_server::ReplyContext ) -> ControlFlow { match msg { Request::Add(a, b) => { rctx.reply(Reply::Sum(u64::from(a + b))).unwrap(); ControlFlow::Continue(()) } Request::GetSignalState => { rctx.reply(Reply::SignalState(self.did_signal)).unwrap(); ControlFlow::Continue(()) } Request::Croak => { rctx.reply(Reply::OkIWillCroak).unwrap(); ControlFlow::Break(42) } } } } // vim: set ft=rust et sw=2 ts=2 sts=2 cinoptions=2 tw=79 :