us update 2

feature/configv2
prplV 2025-03-28 07:35:07 -04:00
parent 026a502044
commit 27e79ce731
2 changed files with 15 additions and 45 deletions

View File

@ -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));

View File

@ -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());
match list.accept().await {
Ok((socket, addr)) => {
info!("CLI connection from {}", addr.as_pathname().unwrap().display());
process_connection(socket).await;
} else {
error!("Cannot poll connection to CLI");
},
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<TcpListener>` 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<UnixListener> {
// 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
///
@ -89,15 +62,12 @@ async fn init_listener() -> anyhow::Result<UnixListener> {
/// *depends on* : `tokio::net::TcpStream`
///
async fn process_connection(mut stream: UnixStream) {
info!("Processing new connection");
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::<Cli>(&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);