Expand description
ump-ng dispatch server running on a thread.
§Example
use std::ops::ControlFlow;
use ump_ng_server::{
async_trait,
task::{Handler, spawn},
ump_ng::{ReplyContext, WeakClient}
};
enum Post {
ShoutIntoVoid
}
enum Request {
Add(usize, usize)
}
enum Reply {
Sum(usize)
}
#[derive(Debug)]
enum MyError { }
struct MyHandler {
wclnt: WeakClient<Post, Request, Reply, MyError>
}
#[async_trait]
impl Handler<Post, Request, Reply, MyError, ()> for MyHandler {
async fn post(&mut self, msg: Post) -> ControlFlow<(), ()> {
match msg {
Post::ShoutIntoVoid => {
// No reply .. but keep on trudging on
ControlFlow::Continue(())
}
}
}
async fn req(
&mut self,
msg: Request,
rctx: ReplyContext<Reply, MyError>
) -> ControlFlow<(), ()> {
match msg {
Request::Add(a, b) => {
rctx.reply(Reply::Sum(a+b)).unwrap();
ControlFlow::Continue(())
}
}
}
}
let (clnt, jh) = spawn(|clnt| {
// Store a weak client in the handler so it doesn't keep the dispatch
// loop alive when the Client returned to the application is dropped.
Ok(MyHandler {
wclnt: clnt.weak()
})
}).unwrap();
clnt.post(Post::ShoutIntoVoid).unwrap();
let Ok(Reply::Sum(sum)) = clnt.areq(Request::Add(3, 7)).await else {
panic!("Unexpected reply");
};
assert_eq!(sum, 10);
// drop client to force dispatch loop to terminate
drop(clnt);
jh.await;
Traits§
- Handler
- Message processing trait for an async handler.
Functions§
- spawn
- Launch a task that will process incoming messages from an ump-ng server end-point.