chore: test client call server
This commit is contained in:
@@ -69,6 +69,21 @@ impl Client {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn call(&self, method: &str, args: Vec<String>) -> Result<RpcResult> {
|
||||
let (id, rx) = self.rpc.register();
|
||||
if let Some(conn) = self.conn.lock().await.as_ref() {
|
||||
conn.send(&ControlPacket::RpcRequest {
|
||||
id,
|
||||
method: method.to_string(),
|
||||
args,
|
||||
})
|
||||
.await?;
|
||||
Ok(rx.await?)
|
||||
} else {
|
||||
Err(anyhow::anyhow!("Not connected"))
|
||||
}
|
||||
}
|
||||
|
||||
async fn handle_packet(
|
||||
&self,
|
||||
packet: ControlPacket,
|
||||
|
||||
@@ -136,23 +136,30 @@ impl Server {
|
||||
})
|
||||
.await?;
|
||||
let audio = self.audio.clone();
|
||||
let target_addr = session.audio_addr;
|
||||
let filename = format!("temp/recorded_{}.wav", session.info.mac.replace(":", ""));
|
||||
tokio::spawn(async move {
|
||||
let mut writer =
|
||||
WavWriter::create("temp/recorded.wav", config.sample_rate, config.channels)
|
||||
.unwrap();
|
||||
WavWriter::create(&filename, config.sample_rate, config.channels).unwrap();
|
||||
let mut codec = OpusCodec::new(&config).unwrap();
|
||||
let mut pcm = vec![0i16; config.frame_size];
|
||||
let mut buf = vec![0u8; 4096];
|
||||
println!(
|
||||
"Recording started for {}, saving to {}",
|
||||
target_addr, filename
|
||||
);
|
||||
for _ in 0..500 {
|
||||
// Record ~10s
|
||||
if let Ok((packet, _)) = audio.recv(&mut buf).await {
|
||||
if let Ok(n) = codec.decode(&packet.data, &mut pcm) {
|
||||
writer.write_samples(&pcm[..n]).unwrap();
|
||||
if let Ok((packet, src_addr)) = audio.recv(&mut buf).await {
|
||||
if src_addr == target_addr {
|
||||
if let Ok(n) = codec.decode(&packet.data, &mut pcm) {
|
||||
writer.write_samples(&pcm[..n]).unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
writer.finalize().unwrap();
|
||||
println!("Recording saved to temp/recorded.wav");
|
||||
println!("Recording saved to {}", filename);
|
||||
});
|
||||
Ok(())
|
||||
}
|
||||
@@ -216,9 +223,30 @@ impl Server {
|
||||
|
||||
async fn handle_packet(session: Arc<Session>, packet: ControlPacket) -> Result<()> {
|
||||
match packet {
|
||||
ControlPacket::RpcRequest { id, method, args } => {
|
||||
let result = handle_rpc(&session, &method, args).await;
|
||||
session
|
||||
.conn
|
||||
.send(&ControlPacket::RpcResponse { id, result })
|
||||
.await?;
|
||||
}
|
||||
ControlPacket::RpcResponse { id, result } => session.rpc.resolve(id, result),
|
||||
ControlPacket::Ping => session.conn.send(&ControlPacket::Pong).await?,
|
||||
_ => {}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn handle_rpc(_session: &Arc<Session>, method: &str, args: Vec<String>) -> RpcResult {
|
||||
match method {
|
||||
"hello" => RpcResult {
|
||||
stdout: format!("Hello from server! Args: {:?}", args),
|
||||
..Default::default()
|
||||
},
|
||||
_ => RpcResult {
|
||||
stderr: format!("Unknown method: {}", method),
|
||||
code: -1,
|
||||
..Default::default()
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,22 @@ use std::sync::Arc;
|
||||
#[tokio::main]
|
||||
async fn main() -> anyhow::Result<()> {
|
||||
let client = Arc::new(Client::new());
|
||||
client.run().await?;
|
||||
let c = client.clone();
|
||||
tokio::spawn(async move {
|
||||
if let Err(e) = c.run().await {
|
||||
eprintln!("Client error: {}", e);
|
||||
}
|
||||
});
|
||||
|
||||
// Wait for connection
|
||||
tokio::time::sleep(std::time::Duration::from_secs(3)).await;
|
||||
|
||||
println!("Testing RPC call to server...");
|
||||
match client.call("hello", vec!["world".to_string()]).await {
|
||||
Ok(res) => println!("Server RPC response: {}", res.stdout),
|
||||
Err(e) => eprintln!("Server RPC call failed: {}", e),
|
||||
}
|
||||
|
||||
tokio::signal::ctrl_c().await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user