Terminal client
This commit is contained in:
+24
-3
@@ -2,9 +2,30 @@ pub mod terminal;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> tokio::io::Result<()> {
|
||||
let mut terminal_server = terminal::TerminalServer::new();
|
||||
let terminal_server = terminal::TerminalServer::new();
|
||||
|
||||
terminal_server.run().await?;
|
||||
{
|
||||
let terminal_server = terminal_server.clone();
|
||||
|
||||
Ok(())
|
||||
tokio::spawn(async move {
|
||||
terminal_server
|
||||
.run()
|
||||
.await
|
||||
.expect("Failed to run terminal server");
|
||||
});
|
||||
}
|
||||
|
||||
loop {
|
||||
tokio::time::sleep(tokio::time::Duration::from_millis(1500)).await;
|
||||
terminal_server
|
||||
.broadcast(pulse_wire::terminal::TerminalServerMessage::AddLog(
|
||||
pulse_wire::terminal::EventLog {
|
||||
kind: pulse_wire::terminal::LogKind::Debug,
|
||||
name: "Engine".to_string(),
|
||||
message: "Hello, world!".to_string(),
|
||||
},
|
||||
))
|
||||
.await?;
|
||||
println!("Ok");
|
||||
}
|
||||
}
|
||||
|
||||
+14
-10
@@ -1,3 +1,5 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use pulse_wire::PulseWire;
|
||||
use tokio::{
|
||||
io::{AsyncReadExt, AsyncWriteExt},
|
||||
@@ -5,20 +7,22 @@ use tokio::{
|
||||
UnixListener,
|
||||
unix::{OwnedReadHalf, OwnedWriteHalf},
|
||||
},
|
||||
sync::Mutex,
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct TerminalServer {
|
||||
clients: Vec<OwnedWriteHalf>,
|
||||
clients: Arc<Mutex<Vec<OwnedWriteHalf>>>,
|
||||
}
|
||||
|
||||
impl TerminalServer {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
clients: Vec::new(),
|
||||
clients: Arc::new(Mutex::new(Vec::new())),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn run(&mut self) -> tokio::io::Result<()> {
|
||||
pub async fn run(&self) -> tokio::io::Result<()> {
|
||||
let path = pulse_wire::server_path();
|
||||
|
||||
if path.exists() {
|
||||
@@ -34,7 +38,7 @@ impl TerminalServer {
|
||||
|
||||
let (reader, writer) = stream.into_split();
|
||||
|
||||
self.clients.push(writer);
|
||||
self.clients.lock().await.push(writer);
|
||||
|
||||
tokio::spawn(async move {
|
||||
if let Err(err) = Self::handle_client(reader).await {
|
||||
@@ -67,18 +71,18 @@ impl TerminalServer {
|
||||
}
|
||||
|
||||
pub async fn broadcast(
|
||||
&mut self,
|
||||
&self,
|
||||
message: pulse_wire::terminal::TerminalServerMessage,
|
||||
) -> tokio::io::Result<()> {
|
||||
let mut res = Ok(());
|
||||
let msg = message.to_com();
|
||||
|
||||
for client in &mut self.clients {
|
||||
if let Err(e) = client.write(&msg).await {
|
||||
res = Err(e);
|
||||
for i in (0..self.clients.lock().await.len()).rev() {
|
||||
if let Err(e) = self.clients.lock().await[i].write(&msg).await {
|
||||
self.clients.lock().await.remove(i);
|
||||
println!("{e:?}");
|
||||
}
|
||||
}
|
||||
|
||||
res
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user