hive-ag3nt: serve loop + send/recv CLI; template runs serve
This commit is contained in:
parent
d79b5a39a1
commit
61407f41c9
6 changed files with 139 additions and 4 deletions
27
hive-ag3nt/src/client.rs
Normal file
27
hive-ag3nt/src/client.rs
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
use std::path::Path;
|
||||
|
||||
use anyhow::{Context, Result, bail};
|
||||
use hive_sh4re::{AgentRequest, AgentResponse};
|
||||
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
|
||||
use tokio::net::UnixStream;
|
||||
|
||||
pub async fn request(socket: &Path, req: AgentRequest) -> Result<AgentResponse> {
|
||||
let stream = UnixStream::connect(socket)
|
||||
.await
|
||||
.with_context(|| format!("connect to {}", socket.display()))?;
|
||||
let (read, mut write) = stream.into_split();
|
||||
|
||||
let mut payload = serde_json::to_string(&req)?;
|
||||
payload.push('\n');
|
||||
write.write_all(payload.as_bytes()).await?;
|
||||
write.flush().await?;
|
||||
|
||||
let mut reader = BufReader::new(read);
|
||||
let mut line = String::new();
|
||||
reader.read_line(&mut line).await?;
|
||||
if line.is_empty() {
|
||||
bail!("server closed connection without responding");
|
||||
}
|
||||
let resp: AgentResponse = serde_json::from_str(line.trim())?;
|
||||
Ok(resp)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue