2024-02-23 03:25:06 +00:00
|
|
|
use std::{
|
2024-03-06 12:05:01 +00:00
|
|
|
io::stdout,
|
2024-02-23 03:25:06 +00:00
|
|
|
os::fd::{AsRawFd, FromRawFd},
|
|
|
|
};
|
|
|
|
|
|
|
|
use anyhow::Result;
|
2024-03-06 12:05:01 +00:00
|
|
|
use async_stream::stream;
|
|
|
|
use krata::control::{ConsoleDataReply, ConsoleDataRequest};
|
2024-02-23 05:26:32 +00:00
|
|
|
use log::debug;
|
2024-02-23 03:25:06 +00:00
|
|
|
use termion::raw::IntoRawMode;
|
|
|
|
use tokio::{
|
|
|
|
fs::File,
|
2024-03-06 12:05:01 +00:00
|
|
|
io::{stdin, AsyncReadExt, AsyncWriteExt},
|
2024-02-23 03:25:06 +00:00
|
|
|
};
|
2024-03-06 12:05:01 +00:00
|
|
|
use tokio_stream::{Stream, StreamExt};
|
|
|
|
use tonic::Streaming;
|
2024-02-23 03:25:06 +00:00
|
|
|
|
2024-03-06 12:05:01 +00:00
|
|
|
pub struct StdioConsoleStream;
|
2024-02-23 03:25:06 +00:00
|
|
|
|
2024-03-06 12:05:01 +00:00
|
|
|
impl StdioConsoleStream {
|
|
|
|
pub async fn stdin_stream(guest: String) -> impl Stream<Item = ConsoleDataRequest> {
|
|
|
|
let mut stdin = stdin();
|
|
|
|
stream! {
|
2024-03-06 15:57:56 +00:00
|
|
|
yield ConsoleDataRequest { guest_id: guest, data: vec![] };
|
2024-03-05 11:35:25 +00:00
|
|
|
|
2024-03-06 12:05:01 +00:00
|
|
|
let mut buffer = vec![0u8; 60];
|
|
|
|
loop {
|
|
|
|
let size = match stdin.read(&mut buffer).await {
|
|
|
|
Ok(size) => size,
|
2024-03-05 11:35:25 +00:00
|
|
|
Err(error) => {
|
2024-03-06 12:05:01 +00:00
|
|
|
debug!("failed to read stdin: {}", error);
|
|
|
|
break;
|
2024-03-05 11:35:25 +00:00
|
|
|
}
|
2024-03-06 12:05:01 +00:00
|
|
|
};
|
|
|
|
let data = buffer[0..size].to_vec();
|
|
|
|
if size == 1 && buffer[0] == 0x1d {
|
|
|
|
break;
|
2024-03-05 11:35:25 +00:00
|
|
|
}
|
2024-03-06 15:57:56 +00:00
|
|
|
yield ConsoleDataRequest { guest_id: String::default(), data };
|
2024-03-06 12:05:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn stdout(mut stream: Streaming<ConsoleDataReply>) -> Result<()> {
|
|
|
|
let terminal = stdout().into_raw_mode()?;
|
|
|
|
let mut stdout = unsafe { File::from_raw_fd(terminal.as_raw_fd()) };
|
|
|
|
while let Some(reply) = stream.next().await {
|
|
|
|
let reply = reply?;
|
|
|
|
if reply.data.is_empty() {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
stdout.write_all(&reply.data).await?;
|
|
|
|
stdout.flush().await?;
|
2024-02-23 03:25:06 +00:00
|
|
|
}
|
2024-03-05 11:35:25 +00:00
|
|
|
Ok(())
|
2024-02-23 03:25:06 +00:00
|
|
|
}
|
|
|
|
}
|