From bc0fac1bf2a02c9a76c8c4b9eccc9235de4e86b2 Mon Sep 17 00:00:00 2001 From: yuzu Date: Wed, 9 Jul 2025 20:24:36 +0000 Subject: working network communication git-svn-id: svn+ssh://diminuette.aengel.lesbianunix.dev/salaryman/trunk@13 b9215c17-b818-4693-b096-d1e41a411fef --- src/service.rs | 40 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 3 deletions(-) (limited to 'src/service.rs') diff --git a/src/service.rs b/src/service.rs index b42daba..dee6e59 100644 --- a/src/service.rs +++ b/src/service.rs @@ -1,3 +1,4 @@ +use schemars::JsonSchema; use serde::{Deserialize, Serialize}; use tokio::{ io::{AsyncBufReadExt, AsyncWriteExt, BufReader}, @@ -8,11 +9,13 @@ use tokio::{ }, task::spawn, }; +use uuid::Uuid; use std::{path::PathBuf, process::Stdio, sync::Arc}; -#[derive(Serialize, Deserialize, Clone, Debug)] +#[derive(Serialize, Deserialize, JsonSchema, Clone, Debug)] pub struct ServiceConf { + pub uuid: Uuid, pub name: String, pub command: String, pub args: Option, @@ -30,6 +33,7 @@ impl ServiceConf { */ pub fn new() -> Self { Self { + uuid: Uuid::new_v4(), name: String::new(), command: String::new(), args: None, @@ -41,6 +45,7 @@ impl ServiceConf { * Returns a new `ServiceConf` from parts. */ pub fn from_parts( + uuid: Uuid, name: String, command: String, args: Option, @@ -48,6 +53,26 @@ impl ServiceConf { autostart: bool, ) -> Self { Self { + uuid, + name, + command, + args, + directory, + autostart, + } + } + /** + * Returns a new `ServiceConf` from parts with new uuid. + */ + pub fn new_from_parts( + name: String, + command: String, + args: Option, + directory: Option, + autostart: bool, + ) -> Self { + Self { + uuid: Uuid::new_v4(), name, command, args, @@ -174,10 +199,11 @@ impl Service { drop(lock); let (tx, rx) = channel(1024); let sname = self.conf.name.clone(); + let suuid = self.conf.uuid.clone(); spawn(async move { let mut br = BufReader::new(stdout).lines(); while let Ok(Some(line)) = br.next_line().await { - println!("{} :: {}", &sname, &line); + println!("{} ({}) :: {}", &suuid, &sname, &line); if let Err(_) = tx.send(line).await { return; }; @@ -210,10 +236,11 @@ impl Service { drop(lock); let (tx, rx) = channel(1024); let sname = self.conf.name.clone(); + let suuid = self.conf.uuid.clone(); spawn(async move { let mut br = BufReader::new(stderr).lines(); while let Ok(Some(line)) = br.next_line().await { - eprintln!("{} :: {}", &sname, &line); + eprintln!("{} ({}) >< {}", &suuid, &sname, &line); if let Err(_) = tx.send(line).await { return; }; @@ -252,4 +279,11 @@ impl Service { ))) } } + /** + * Writes a line to the service process' stdin, if it exists. + */ + #[inline] + pub async fn writeln_stdin(&mut self, buf: String) -> Result<(), Box> { + self.write_stdin(format!("{}\n", buf)).await + } } -- cgit 1.4.1-2-gfad0