From 27e79ce731c4030c527f69f78997a836a5a7cab6 Mon Sep 17 00:00:00 2001 From: prplV Date: Fri, 28 Mar 2025 07:35:07 -0400 Subject: [PATCH] us update 2 --- noxis-cli/src/cli_net.rs | 4 +- noxis-rs/src/options/cli_pipeline.rs | 56 +++++++--------------------- 2 files changed, 15 insertions(+), 45 deletions(-) diff --git a/noxis-cli/src/cli_net.rs b/noxis-cli/src/cli_net.rs index f148849..7bb0178 100644 --- a/noxis-cli/src/cli_net.rs +++ b/noxis-cli/src/cli_net.rs @@ -16,13 +16,13 @@ pub async fn try_send(cli: Cli) -> Result<()> { let msg = serde_json::to_vec(&cli) .map_err(|_| NoxisCliError::ToStringCliParsingParsing)?; - stream.write_all(&msg).await + stream.try_write(&msg) .map_err(|_| NoxisCliError::CliPromptCanNotBeSent)?; stream.shutdown().await?; let mut response = Vec::new(); - stream.read_to_end(&mut response).await + stream.read(&mut response).await .map_err(|_| NoxisCliError::CliResponseReadError)?; println!("Received response: {}", String::from_utf8_lossy(&response)); diff --git a/noxis-rs/src/options/cli_pipeline.rs b/noxis-rs/src/options/cli_pipeline.rs index 96233e9..1c85dae 100644 --- a/noxis-rs/src/options/cli_pipeline.rs +++ b/noxis-rs/src/options/cli_pipeline.rs @@ -3,7 +3,6 @@ use tokio::net::{ UnixStream, UnixListener }; use anyhow::Result as DynResult; use tokio::time::{sleep, Duration}; use std::fs; -// use std::io::BufReader; use tokio::io::{ AsyncWriteExt, AsyncReadExt}; use noxis_cli::Cli; @@ -21,17 +20,20 @@ use noxis_cli::Cli; /// *depends on* : - /// pub async fn init_cli_pipeline() -> DynResult<()> { - match init_listener().await { + let socket_path = "noxis-rs.sock"; + let _ = fs::remove_file(socket_path); + + match UnixListener::bind(socket_path) { Ok(list) => { // TODO: remove `unwrap`s info!("Listening on {}", &list.local_addr()?.as_pathname().unwrap().display()); loop { - - if let Ok((socket, a)) = list.accept().await { - info!("CLI connection from {}", a.as_pathname().unwrap().display()); - process_connection(socket).await; - } else { - error!("Cannot poll connection to CLI"); + match list.accept().await { + Ok((socket, addr)) => { + info!("CLI connection from {}", addr.as_pathname().unwrap().display()); + process_connection(socket).await; + }, + Err(er) => error!("Cannot poll connection to CLI due to {}", er), } dbg!(1); sleep(Duration::from_millis(300)).await; @@ -41,40 +43,11 @@ pub async fn init_cli_pipeline() -> DynResult<()> { }, Err(er) => { error!("Failed to open UnixListener for CLI"); - Err(er) + Err(er.into()) }, } } -/// # Fn `init_listener` -/// ## for creating TCP-listener for communicating with CLI -/// -/// *input* : - -/// -/// *output* : `Some` if port 7753 was opened | None if not -/// -/// *initiator* : fn `init_cli_pipeline` -/// -/// *managing* : `TcpListener` object to handle requests -/// -/// *depends on* : `tokio::net::TcpListener` -/// -async fn init_listener() -> anyhow::Result { - // match TcpListener::bind("127.0.0.1:7753").await { - // Ok(listener) => { - // info!("Runner is listening localhost:7753"); - // Some(listener) - // }, - // Err(_) => { - // error!("Cannot create TCP listener for CLI"); - // None - // } - // } - let socket_path = "noxis-rs.sock"; - let _ = fs::remove_file(socket_path); - Ok(UnixListener::bind(socket_path)?) -} - /// # Fn `process_connection` /// ## for processing input CLI requests /// @@ -88,16 +61,13 @@ async fn init_listener() -> anyhow::Result { /// /// *depends on* : `tokio::net::TcpStream` /// -async fn process_connection(mut stream: UnixStream) { - info!("Processing new connection"); - +async fn process_connection(mut stream: UnixStream) { let mut buf = Vec::new(); - match stream.read_to_end(&mut buf).await { + match stream.read(&mut buf).await { Ok(_) => { match serde_json::from_slice::(&buf) { Ok(cli) => { info!("Received CLI request: {:?}", cli); - // Обработка запроса let response = "OK"; if let Err(e) = stream.write_all(response.as_bytes()).await { error!("Failed to send response: {}", e);