use std::io::{BufRead, Write};
use tokio::sync::mpsc;
use tokio::task::JoinSet;
use crate::mcp::{self, Host};
pub async fn serve(host: Host) {
let (lines, mut incoming) = mpsc::unbounded_channel::<String>();
let reader = tokio::task::spawn_blocking(move || {
for line in std::io::stdin().lock().lines() {
let Ok(line) = line else { return };
if lines.send(line).is_err() {
return;
}
}
});
let mut answering = JoinSet::new();
while let Some(line) = incoming.recv().await {
let host = host.clone();
answering.spawn(async move { mcp::handle_line(&line, &host).await });
while let Some(done) = answering.try_join_next() {
write(done.ok().flatten());
}
}
while let Some(done) = answering.join_next().await {
write(done.ok().flatten());
}
let _ = reader.await;
}
fn write(reply: Option<String>) {
let Some(reply) = reply else { return };
let mut out = std::io::stdout().lock();
let _ = writeln!(out, "{reply}");
let _ = out.flush();
}