From 5ddb43351b67173a8a76ae51babc041e31c49c72 Mon Sep 17 00:00:00 2001 From: Florian RICHER Date: Mon, 30 Jan 2023 22:04:46 +0100 Subject: [PATCH] Refactor server --- src/daemon/main.rs | 55 ++++---------------------------------------- src/daemon/server.rs | 52 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 50 deletions(-) create mode 100644 src/daemon/server.rs diff --git a/src/daemon/main.rs b/src/daemon/main.rs index 30a6f67..15b93f3 100644 --- a/src/daemon/main.rs +++ b/src/daemon/main.rs @@ -1,67 +1,22 @@ #![cfg_attr(not(unix), allow(unused_imports))] +mod server; + use std::path::Path; #[cfg(unix)] use tokio::net::UnixListener; #[cfg(unix)] use tokio_stream::wrappers::UnixListenerStream; -#[cfg(unix)] -use tonic::transport::server::UdsConnectInfo; -use tonic::{transport::Server, Request, Response, Status}; -use libcommand::internal::{ - unix_server::{Unix, UnixServer}, - AuthorizeRequest, AuthorizeResponse, TerminateRequest, TerminateResponse -}; - -#[derive(Default)] -pub struct DaemonServer {} - -#[tonic::async_trait] -impl Unix for DaemonServer { - async fn authorize( - &self, - request: Request, - ) -> Result, Status> { - #[cfg(unix)] - { - let conn_info = request.extensions().get::().unwrap(); - println!("Got a request {:?} with info {:?}", request, conn_info); - } - - let reply = libcommand::internal::AuthorizeResponse { - status: libcommand::internal::AuthorizationStatus::Authorized.into(), - error_message: "".into(), - log_file: "".into(), - session_uuid: "".into() - }; - Ok(Response::new(reply)) - } - - async fn terminate( - &self, - request: Request, - ) -> Result, Status> { - #[cfg(unix)] - { - let conn_info = request.extensions().get::().unwrap(); - println!("Got a request {:?} with info {:?}", request, conn_info); - } - - let reply = libcommand::internal::TerminateResponse { - status: libcommand::internal::TerminateStatus::Ok.into(), - error_message: "".into(), - }; - Ok(Response::new(reply)) - } -} +use tonic::transport::Server; +use libcommand::internal::unix_server::UnixServer; #[cfg(unix)] #[tokio::main] async fn main() -> Result<(), Box> { std::fs::create_dir_all(Path::new(libcommand::SOCK_FILE).parent().unwrap())?; - let server = DaemonServer::default(); + let server = server::DaemonServer::default(); let uds = UnixListener::bind(libcommand::SOCK_FILE)?; let uds_stream = UnixListenerStream::new(uds); diff --git a/src/daemon/server.rs b/src/daemon/server.rs new file mode 100644 index 0000000..8f44975 --- /dev/null +++ b/src/daemon/server.rs @@ -0,0 +1,52 @@ +#![cfg_attr(not(unix), allow(unused_imports))] + +#[cfg(unix)] +use tonic::transport::server::UdsConnectInfo; +use tonic::{transport::Server, Request, Response, Status}; + +use libcommand::internal::{ + unix_server::{Unix, UnixServer}, + AuthorizeRequest, AuthorizeResponse, TerminateRequest, TerminateResponse +}; + +#[derive(Default)] +pub struct DaemonServer {} + +#[tonic::async_trait] +impl Unix for DaemonServer { + async fn authorize( + &self, + request: Request, + ) -> Result, Status> { + #[cfg(unix)] + { + let conn_info = request.extensions().get::().unwrap(); + println!("Got a request {:?} with info {:?}", request, conn_info); + } + + let reply = libcommand::internal::AuthorizeResponse { + status: libcommand::internal::AuthorizationStatus::Authorized.into(), + error_message: "".into(), + log_file: "".into(), + session_uuid: "".into() + }; + Ok(Response::new(reply)) + } + + async fn terminate( + &self, + request: Request, + ) -> Result, Status> { + #[cfg(unix)] + { + let conn_info = request.extensions().get::().unwrap(); + println!("Got a request {:?} with info {:?}", request, conn_info); + } + + let reply = libcommand::internal::TerminateResponse { + status: libcommand::internal::TerminateStatus::Ok.into(), + error_message: "".into(), + }; + Ok(Response::new(reply)) + } +} \ No newline at end of file