new communication structure

This commit is contained in:
2026-01-26 21:10:26 +01:00
parent 2b2f8622bf
commit 09c314463b
2 changed files with 74 additions and 33 deletions

View File

@@ -21,8 +21,11 @@ pub(crate) struct ClientHandler {
impl GDriverClient for ClientHandler {
async fn report_task_result(self, _: Context, id: TaskId, result: TaskResult) {
println!("Received task result for task {}: {:?}", id.0, result);
let mut tasks = self.pending_tasks.lock().unwrap();
if let Some(sender) = tasks.remove(&id.0) {
let sender = {
let mut tasks = self.pending_tasks.lock().unwrap();
tasks.remove(&id.0)
};
if let Some(sender) = sender {
let _ = sender.send(result).await;
}
}
@@ -52,6 +55,8 @@ pub(crate) async fn start_with_client(
}
}
let start = time::SystemTime::now();
// Use AsyncResponse for RunLong as well
let hello = client
.do_something2(tarpc::context::current(), BackendActionRequest::RunLong)
.await;
@@ -61,9 +66,32 @@ pub(crate) async fn start_with_client(
.as_secs();
match hello {
Ok(hello) => println!("Run Long returned after {} seconds: {:?}", seconds, hello),
Err(e) => println!(":( {:?}", (e)),
Ok(Ok(AsyncResponse::Pending(task_id))) => {
println!("Run Long returned pending task: {:?}", task_id);
let (tx, mut rx) = mpsc::channel(1);
handler
.pending_tasks
.lock()
.unwrap()
.insert(task_id.0.clone(), tx);
if let Some(result) = rx.recv().await {
println!(
"Received async result after {} seconds: {:?}",
(time::SystemTime::now().duration_since(start))
.unwrap()
.as_secs(),
result
);
}
}
Ok(Ok(AsyncResponse::Immediate(res))) => {
println!("Run Long returned immediate: {:?}", res)
}
Ok(Err(e)) => println!("Backend Error: {:?}", e),
Err(e) => println!("RPC Error: {:?}", e),
}
let start = time::SystemTime::now();
let hello = client
.do_something2(tarpc::context::current(), BackendActionRequest::StartLong)
@@ -111,9 +139,13 @@ pub async fn create_client(
e
})?;
let (client_channel, server_channel) = spawn_twoway(transport);
// spawn_twoway returns (server_channel, client_channel)
// server_channel: UnboundedChannel<ClientMessage<Req1>, Response<Resp1>> -> For Server
// client_channel: UnboundedChannel<Response<Resp2>, ClientMessage<Req2>> -> For Client
let (server_channel, client_channel) = spawn_twoway(transport);
let client = GDriverServiceClient::new(client::Config::default(), server_channel).spawn();
// GDriverServiceClient needs a channel to send requests (Req2) and receive responses (Resp2)
let client = GDriverServiceClient::new(client::Config::default(), client_channel).spawn();
let handler = ClientHandler {
pending_tasks: Arc::new(Mutex::new(HashMap::new())),
@@ -121,9 +153,12 @@ pub async fn create_client(
let handler_clone = handler.clone();
tokio::spawn(async move {
BaseChannel::with_defaults(client_channel)
// BaseChannel needs a channel to receive requests (Req1) and send responses (Resp1)
BaseChannel::with_defaults(server_channel)
.execute(handler_clone.serve())
.for_each(|_| async {})
.for_each(|f| async move {
tokio::spawn(f);
})
.await;
});