use std::collections::HashMap;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, Mutex};
use serde_json::{Value, json};
use tokio::process::ChildStdout;
use tokio::sync::{mpsc, oneshot};
use super::Writer;
pub enum Incoming {
Notification {
method: String,
params: Value,
},
Request {
id: Value,
method: String,
params: Value,
},
Reply {
id: u64,
result: Result<Value, RpcError>,
},
}
#[derive(Debug)]
pub struct RpcError {
pub code: i64,
pub message: String,
}
impl std::fmt::Display for RpcError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{} ({})", self.message, self.code)
}
}
impl std::error::Error for RpcError {}
enum Slot {
Waiter(oneshot::Sender<Result<Value, RpcError>>),
InOrder,
}
type Pending = Arc<Mutex<HashMap<u64, Slot>>>;
#[derive(Clone)]
pub struct Rpc {
writer: Writer,
next_id: Arc<AtomicU64>,
pending: Pending,
}
impl Rpc {
pub fn start(stdout: ChildStdout, writer: Writer) -> (Rpc, mpsc::UnboundedReceiver<Incoming>) {
let pending: Pending = Arc::default();
let (tx, rx) = mpsc::unbounded_channel();
tokio::spawn(read_loop(stdout, pending.clone(), tx, writer.trace.clone()));
(
Rpc {
writer,
next_id: Arc::new(AtomicU64::new(1)),
pending,
},
rx,
)
}
pub async fn request(
&self,
method: &str,
params: Value,
) -> std::io::Result<oneshot::Receiver<Result<Value, RpcError>>> {
let (tx, rx) = oneshot::channel();
self.send_request(self.reserve(), Slot::Waiter(tx), method, params)
.await?;
Ok(rx)
}
pub async fn request_in_order(&self, method: &str, params: Value) -> std::io::Result<()> {
self.request_in_order_as(self.reserve(), method, params)
.await
}
pub fn reserve(&self) -> u64 {
self.next_id.fetch_add(1, Ordering::Relaxed)
}
pub async fn request_in_order_as(
&self,
id: u64,
method: &str,
params: Value,
) -> std::io::Result<()> {
self.send_request(id, Slot::InOrder, method, params).await
}
async fn send_request(
&self,
id: u64,
slot: Slot,
method: &str,
params: Value,
) -> std::io::Result<()> {
self.pending.lock().unwrap().insert(id, slot);
self.writer
.send(&json!({"jsonrpc": "2.0", "id": id, "method": method, "params": params}))
.await
}
pub async fn call(&self, method: &str, params: Value) -> anyhow::Result<Value> {
let rx = self.request(method, params).await?;
match rx.await {
Ok(Ok(v)) => Ok(v),
Ok(Err(e)) => Err(anyhow::Error::new(e).context(format!("{method} failed"))),
Err(_) => anyhow::bail!("{method}: agent exited before replying"),
}
}
pub async fn notify(&self, method: &str, params: Value) -> std::io::Result<()> {
self.writer
.send(&json!({"jsonrpc": "2.0", "method": method, "params": params}))
.await
}
pub async fn respond(&self, id: Value, result: Value) -> std::io::Result<()> {
self.writer
.send(&json!({"jsonrpc": "2.0", "id": id, "result": result}))
.await
}
pub async fn respond_error(&self, id: Value, message: &str) -> std::io::Result<()> {
self.writer
.send(
&json!({"jsonrpc": "2.0", "id": id, "error": {"code": -32601, "message": message}}),
)
.await
}
}
async fn read_loop(
stdout: ChildStdout,
pending: Pending,
tx: mpsc::UnboundedSender<Incoming>,
trace: super::Trace,
) {
let mut lines = super::lines(stdout);
while let Ok(Some(line)) = lines.next_line().await {
trace.line("recv", &line);
let Ok(msg) = serde_json::from_str::<Value>(&line) else {
continue;
};
let method = msg
.get("method")
.and_then(Value::as_str)
.map(str::to_string);
let params = msg.get("params").cloned().unwrap_or(Value::Null);
match (msg.get("id"), method) {
(Some(id), Some(method)) => {
let _ = tx.send(Incoming::Request {
id: id.clone(),
method,
params,
});
}
(None, Some(method)) => {
let _ = tx.send(Incoming::Notification { method, params });
}
(Some(id), None) => {
let Some(id) = id.as_u64() else { continue };
let Some(slot) = pending.lock().unwrap().remove(&id) else {
continue;
};
let result = match msg.get("error") {
Some(e) => Err(RpcError {
code: e.get("code").and_then(Value::as_i64).unwrap_or(0),
message: e
.get("message")
.and_then(Value::as_str)
.unwrap_or("")
.to_string(),
}),
None => Ok(msg.get("result").cloned().unwrap_or(Value::Null)),
};
let _ = match slot {
Slot::Waiter(tx) => tx.send(result).ok(),
Slot::InOrder => tx.send(Incoming::Reply { id, result }).ok(),
};
}
(None, None) => {}
}
}
pending.lock().unwrap().clear();
}
#[cfg(test)]
impl Rpc {
pub fn closed() -> Rpc {
Rpc {
writer: Writer {
stdin: Arc::default(),
trace: super::Trace::default(),
},
next_id: Arc::new(AtomicU64::new(1)),
pending: Arc::default(),
}
}
}