us update 2
parent
026a502044
commit
27e79ce731
|
|
@ -16,13 +16,13 @@ pub async fn try_send(cli: Cli) -> Result<()> {
|
||||||
let msg = serde_json::to_vec(&cli)
|
let msg = serde_json::to_vec(&cli)
|
||||||
.map_err(|_| NoxisCliError::ToStringCliParsingParsing)?;
|
.map_err(|_| NoxisCliError::ToStringCliParsingParsing)?;
|
||||||
|
|
||||||
stream.write_all(&msg).await
|
stream.try_write(&msg)
|
||||||
.map_err(|_| NoxisCliError::CliPromptCanNotBeSent)?;
|
.map_err(|_| NoxisCliError::CliPromptCanNotBeSent)?;
|
||||||
|
|
||||||
stream.shutdown().await?;
|
stream.shutdown().await?;
|
||||||
|
|
||||||
let mut response = Vec::new();
|
let mut response = Vec::new();
|
||||||
stream.read_to_end(&mut response).await
|
stream.read(&mut response).await
|
||||||
.map_err(|_| NoxisCliError::CliResponseReadError)?;
|
.map_err(|_| NoxisCliError::CliResponseReadError)?;
|
||||||
|
|
||||||
println!("Received response: {}", String::from_utf8_lossy(&response));
|
println!("Received response: {}", String::from_utf8_lossy(&response));
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,6 @@ use tokio::net::{ UnixStream, UnixListener };
|
||||||
use anyhow::Result as DynResult;
|
use anyhow::Result as DynResult;
|
||||||
use tokio::time::{sleep, Duration};
|
use tokio::time::{sleep, Duration};
|
||||||
use std::fs;
|
use std::fs;
|
||||||
// use std::io::BufReader;
|
|
||||||
use tokio::io::{ AsyncWriteExt, AsyncReadExt};
|
use tokio::io::{ AsyncWriteExt, AsyncReadExt};
|
||||||
use noxis_cli::Cli;
|
use noxis_cli::Cli;
|
||||||
|
|
||||||
|
|
@ -21,17 +20,20 @@ use noxis_cli::Cli;
|
||||||
/// *depends on* : -
|
/// *depends on* : -
|
||||||
///
|
///
|
||||||
pub async fn init_cli_pipeline() -> DynResult<()> {
|
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) => {
|
Ok(list) => {
|
||||||
// TODO: remove `unwrap`s
|
// TODO: remove `unwrap`s
|
||||||
info!("Listening on {}", &list.local_addr()?.as_pathname().unwrap().display());
|
info!("Listening on {}", &list.local_addr()?.as_pathname().unwrap().display());
|
||||||
loop {
|
loop {
|
||||||
|
match list.accept().await {
|
||||||
if let Ok((socket, a)) = list.accept().await {
|
Ok((socket, addr)) => {
|
||||||
info!("CLI connection from {}", a.as_pathname().unwrap().display());
|
info!("CLI connection from {}", addr.as_pathname().unwrap().display());
|
||||||
process_connection(socket).await;
|
process_connection(socket).await;
|
||||||
} else {
|
},
|
||||||
error!("Cannot poll connection to CLI");
|
Err(er) => error!("Cannot poll connection to CLI due to {}", er),
|
||||||
}
|
}
|
||||||
dbg!(1);
|
dbg!(1);
|
||||||
sleep(Duration::from_millis(300)).await;
|
sleep(Duration::from_millis(300)).await;
|
||||||
|
|
@ -41,40 +43,11 @@ pub async fn init_cli_pipeline() -> DynResult<()> {
|
||||||
},
|
},
|
||||||
Err(er) => {
|
Err(er) => {
|
||||||
error!("Failed to open UnixListener for CLI");
|
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`
|
/// # Fn `process_connection`
|
||||||
/// ## for processing input CLI requests
|
/// ## for processing input CLI requests
|
||||||
///
|
///
|
||||||
|
|
@ -89,15 +62,12 @@ async fn init_listener() -> anyhow::Result<UnixListener> {
|
||||||
/// *depends on* : `tokio::net::TcpStream`
|
/// *depends on* : `tokio::net::TcpStream`
|
||||||
///
|
///
|
||||||
async fn process_connection(mut stream: UnixStream) {
|
async fn process_connection(mut stream: UnixStream) {
|
||||||
info!("Processing new connection");
|
|
||||||
|
|
||||||
let mut buf = Vec::new();
|
let mut buf = Vec::new();
|
||||||
match stream.read_to_end(&mut buf).await {
|
match stream.read(&mut buf).await {
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
match serde_json::from_slice::<Cli>(&buf) {
|
match serde_json::from_slice::<Cli>(&buf) {
|
||||||
Ok(cli) => {
|
Ok(cli) => {
|
||||||
info!("Received CLI request: {:?}", cli);
|
info!("Received CLI request: {:?}", cli);
|
||||||
// Обработка запроса
|
|
||||||
let response = "OK";
|
let response = "OK";
|
||||||
if let Err(e) = stream.write_all(response.as_bytes()).await {
|
if let Err(e) = stream.write_all(response.as_bytes()).await {
|
||||||
error!("Failed to send response: {}", e);
|
error!("Failed to send response: {}", e);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue