summaryrefslogtreecommitdiff
path: root/common/src
diff options
context:
space:
mode:
Diffstat (limited to 'common/src')
-rw-r--r--common/src/lib.rs25
1 files changed, 15 insertions, 10 deletions
diff --git a/common/src/lib.rs b/common/src/lib.rs
index 5317b3e..d58d10b 100644
--- a/common/src/lib.rs
+++ b/common/src/lib.rs
@@ -2,6 +2,7 @@
use std::{
future::Future,
io::{self, Read, Write},
+ sync::Arc,
thread,
};
@@ -11,19 +12,21 @@ use serde::{Deserialize, Serialize};
use serde_json::Deserializer;
use smol::{
channel::{self, Receiver, Sender},
- future,
stream::StreamExt,
- Executor,
};
pub mod msg;
pub mod msg_id;
-pub trait Handler {
+pub trait Handler: Send + Sync + 'static {
type Body: Serialize + for<'a> Deserialize<'a> + Send + Clone;
fn init(node_id: String, node_ids: Vec<String>, output: Output<Self::Body>) -> Self;
- fn handle(&self, header: MessageHeader, body: Self::Body) -> impl Future<Output = ()> + Send;
+ fn handle(
+ &self,
+ header: MessageHeader,
+ body: Self::Body,
+ ) -> impl Future<Output = ()> + Send + '_;
}
pub fn run_server<H: Handler>() {
@@ -40,14 +43,16 @@ pub fn run_server<H: Handler>() {
s.spawn(|| send_loop(io::stdout(), out_recv));
// As we receive messages, spawn a future for each
- let executor = Executor::new();
- future::block_on(executor.run(async {
+ let handler = Arc::new(handler);
+ smol::block_on(async move {
while let Some(msg) = inp_recv.next().await {
- executor
- .spawn(handler.handle(msg.header, msg.body))
- .detach();
+ let handler = handler.clone();
+ smol::spawn(async move {
+ handler.handle(msg.header, msg.body).await;
+ })
+ .detach();
}
- }));
+ });
});
}