better pong response

This commit is contained in:
Ilya Zlobintsev
2022-11-24 16:34:19 +02:00
parent 5cd55b563a
commit d411cf425c
5 changed files with 35 additions and 19 deletions

View File

@@ -5,10 +5,7 @@ mod vulkan;
use self::handler::Handler;
use crate::{config::Config, socket};
use lact_schema::{
request::Request,
response::{Pong, Response},
};
use lact_schema::{Pong, Request, Response};
use serde::Serialize;
use tokio::{
io::{AsyncBufReadExt, AsyncWriteExt, BufReader},
@@ -79,7 +76,7 @@ async fn handle_stream(stream: UnixStream, handler: Handler) -> anyhow::Result<(
#[instrument(level = "debug", skip(handler))]
async fn handle_request<'a>(request: Request<'a>, handler: &'a Handler) -> anyhow::Result<Vec<u8>> {
match request {
Request::Ping => ok_response(Pong),
Request::Ping => ok_response(ping()),
Request::ListDevices => ok_response(handler.list_devices()),
Request::DeviceInfo { id } => ok_response(handler.get_device_info(id)?),
Request::DeviceStats { id } => ok_response(handler.get_gpu_stats(id)?),
@@ -93,3 +90,14 @@ async fn handle_request<'a>(request: Request<'a>, handler: &'a Handler) -> anyho
fn ok_response<T: Serialize>(data: T) -> anyhow::Result<Vec<u8>> {
Ok(serde_json::to_vec(&Response::Ok(data))?)
}
fn ping() -> Pong<'static> {
let version = env!("CARGO_PKG_VERSION");
let profile = if cfg!(debug_assertions) {
"debug"
} else {
"release"
};
Pong { version, profile }
}

View File

@@ -1,7 +1,5 @@
use anyhow::{anyhow, Context};
use lact_schema::{
request::Request, response::Response, DeviceInfo, DeviceListEntry, DeviceStats, FanCurveMap,
};
use lact_schema::{DeviceInfo, DeviceListEntry, DeviceStats, FanCurveMap, Request, Response};
use nix::unistd::getuid;
use serde::Deserialize;
use std::{

View File

@@ -1,8 +1,12 @@
pub mod request;
pub mod response;
mod request;
mod response;
#[cfg(test)]
mod tests;
pub use request::Request;
pub use response::Response;
pub use amdgpu_sysfs::{
gpu_handle::{
overdrive::{ClocksTable, ClocksTableGen},
@@ -20,6 +24,12 @@ use std::{
pub type FanCurveMap = BTreeMap<i32, f32>;
#[derive(Serialize, Deserialize, Debug)]
pub struct Pong<'a> {
pub version: &'a str,
pub profile: &'a str,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct DeviceListEntry<'a> {
pub id: &'a str,

View File

@@ -6,6 +6,3 @@ pub enum Response<T> {
Ok(T),
Error(String),
}
#[derive(Serialize, Deserialize, Debug)]
pub struct Pong;

View File

@@ -1,7 +1,4 @@
use crate::{
request::Request,
response::{Pong, Response},
};
use crate::{Pong, Request, Response};
use serde_json::json;
#[test]
@@ -18,9 +15,15 @@ fn ping_requset() {
fn pong_response() {
let expected_response = json!({
"status": "ok",
"data": null
"data": {
"version": "0.0.1",
"profile": "debug"
}
});
let response = Response::Ok(Pong {
version: "0.0.1",
profile: "debug",
});
let response = Response::Ok(Pong);
assert_eq!(serde_json::to_value(&response).unwrap(), expected_response);
}