1
0
Fork 0

Refactor server

This commit is contained in:
Florian RICHER 2023-01-30 22:04:46 +01:00
parent 0243d23a03
commit 5ddb43351b
2 changed files with 57 additions and 50 deletions

View file

@ -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<AuthorizeRequest>,
) -> Result<Response<AuthorizeResponse>, Status> {
#[cfg(unix)]
{
let conn_info = request.extensions().get::<UdsConnectInfo>().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<TerminateRequest>,
) -> Result<Response<TerminateResponse>, Status> {
#[cfg(unix)]
{
let conn_info = request.extensions().get::<UdsConnectInfo>().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<dyn std::error::Error>> {
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);

52
src/daemon/server.rs Normal file
View file

@ -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<AuthorizeRequest>,
) -> Result<Response<AuthorizeResponse>, Status> {
#[cfg(unix)]
{
let conn_info = request.extensions().get::<UdsConnectInfo>().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<TerminateRequest>,
) -> Result<Response<TerminateResponse>, Status> {
#[cfg(unix)]
{
let conn_info = request.extensions().get::<UdsConnectInfo>().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))
}
}