From 4db29881cc771a1795514aebbdae861de82da36f Mon Sep 17 00:00:00 2001 From: GridexX Date: Tue, 21 Mar 2023 11:38:12 +0100 Subject: [PATCH] chore: begin of the new paylaod (need rework) Signed-off-by: GridexX --- agent/lib/src/external_api/model.rs | 113 ++++++++++++++++++++++++++ agent/lib/src/external_api/service.rs | 24 ++++-- agent/lib/src/internal_api/service.rs | 27 +++--- agent/src/main.rs | 8 +- 4 files changed, 150 insertions(+), 22 deletions(-) diff --git a/agent/lib/src/external_api/model.rs b/agent/lib/src/external_api/model.rs index 5120c36..b62bea5 100644 --- a/agent/lib/src/external_api/model.rs +++ b/agent/lib/src/external_api/model.rs @@ -11,3 +11,116 @@ pub struct CodeEntry { pub files: Vec, pub script: Vec, // All commands to execute at startup } + +#[derive(Deserialize, Serialize, Debug)] +pub enum Type { + Status, + Request, + Response, +} + +#[derive(Deserialize, Serialize, Debug)] +pub enum Code { + Run, + Ok, +} + +#[derive(Deserialize, Serialize, Debug)] +pub struct StatusMessage { + pub r#type: Type, + pub code: Code, +} + +impl StatusMessage { + pub fn new(code: String) -> StatusMessage { + StatusMessage { + r#type: Type::Status, + code: Code::Run + } + } +} + +#[derive(Deserialize, Serialize, Debug)] +pub struct ResponseStep { + pub command: String, + pub result: i32, + pub stdout: String, + pub stderr: String, + pub enable_output: bool +} + +impl ResponseStep { + pub fn new(command: String, result: i32, stdout: String, stderr: String, enable_output: bool) -> ResponseStep { + ResponseStep { + command, + result, + stdout, + stderr, + enable_output + } + } +} + +#[derive(Deserialize, Serialize, Debug)] +pub struct ResponseData { + pub id: String, + pub steps: Vec +} + + +impl ResponseData { + pub fn new(id: String, steps: Vec) -> ResponseData { + ResponseData { + id, + steps + } + } +} + +#[derive(Deserialize, Serialize, Debug)] +pub struct ResponseMessage { + pub r#type: Type, + pub code: Code, + pub data: ResponseData +} + +impl ResponseMessage { + pub fn new(data: ResponseData) -> ResponseMessage { + ResponseMessage { + r#type: Type::Response, + code: Code::Run, + data + } + } +} + +#[derive(Deserialize, Serialize, Debug)] +pub struct RequestStep { + pub command: String, + pub enable_output: bool +} + + +#[derive(Deserialize, Serialize, Debug)] +pub struct RequestData { + pub id: String, + pub files: Vec, + pub steps: Vec +} + +#[derive(Deserialize, Serialize, Debug)] +pub struct RequestMessage { + pub r#type: Type, + pub code: Code, + pub data: RequestData +} + +impl RequestMessage { + pub fn new(data: RequestData) -> RequestMessage { + RequestMessage { + r#type: Type::Request, + code: Code::Run, + data + } + } +} \ No newline at end of file diff --git a/agent/lib/src/external_api/service.rs b/agent/lib/src/external_api/service.rs index 36c11e9..c25916c 100644 --- a/agent/lib/src/external_api/service.rs +++ b/agent/lib/src/external_api/service.rs @@ -1,7 +1,7 @@ use anyhow::{anyhow, Result}; use log::{error, info}; -use super::model::CodeEntry; +use super::model::{StatusMessage, ResponseMessage, RequestMessage}; pub struct ExternalApi { serial_path: String, @@ -16,7 +16,7 @@ impl ExternalApi { } } - pub fn read_from_serial(&mut self) -> Result { + pub fn read_from_serial(&mut self) -> Result { info!("Reading from serial port: {}", self.serial_path); // Open the serial port @@ -81,14 +81,26 @@ impl ExternalApi { } } - pub fn parse_json_payload(&mut self, data: &[u8]) -> Result { + pub fn parse_json_payload(&mut self, data: &[u8]) -> Result { // Convert the data vector to a codeEntry struct - let code_entry: CodeEntry = serde_json::from_slice(data) + let request_message: RequestMessage = serde_json::from_slice(data) .map_err(|e| anyhow!("Failed to parse JSON payload: {}", e))?; - info!("Code entry: {:?}", code_entry); + info!("Code request message: {:?}", request_message); - Ok(code_entry) + Ok(request_message) + } + + pub fn send_status_message(& mut self) -> Result<()> { + let status_message: StatusMessage = StatusMessage::new("ok".to_string()); + let status_message_json = serde_json::to_string(&status_message) + .map_err(|e| anyhow!("Failed to serialize status message: {}", e))?; + self.write_to_serial(&status_message_json)?; + Ok(()) + } + + pub fn send_response_message(&mut self, id: String, steps: Vec) -> Result<()> { + Ok(()) } pub fn write_to_serial(&mut self, data: &str) -> Result<()> { diff --git a/agent/lib/src/internal_api/service.rs b/agent/lib/src/internal_api/service.rs index 81df8b6..7886a61 100644 --- a/agent/lib/src/internal_api/service.rs +++ b/agent/lib/src/internal_api/service.rs @@ -1,19 +1,19 @@ use std::{process::Command, fs::File, path::{PathBuf, Path}}; use anyhow::{anyhow, Result}; use log::{error, info}; -use crate::{external_api::model::CodeEntry, internal_api::model::FileModel}; +use crate::{external_api::model::{RequestMessage, ResponseMessage, ResponseData, ResponseStep}, internal_api::model::FileModel}; use std::io::Write; use super::model::{CodeReturn, InternalError}; const WORKSPACE_PATH: &str = "/tmp"; pub struct InternalApi { - pub code_entry: CodeEntry, + pub request_message: RequestMessage, } impl InternalApi { - pub fn new(code_entry: CodeEntry) -> Self { - Self { code_entry } + pub fn new(request_message: RequestMessage) -> Self { + Self { request_message } } pub fn create_workspace(&mut self) -> Result<()> { @@ -23,7 +23,7 @@ impl InternalApi { let mut file_models: Vec = Vec::new(); let root_path = PathBuf::from(WORKSPACE_PATH); - self.code_entry.files.iter().for_each(|file| { + self.request_message.data.files.iter().for_each(|file| { let mut file_path = PathBuf::from(&file.filename); file_path.pop(); @@ -98,16 +98,14 @@ impl InternalApi { "Hello".to_string() } - pub fn run(&mut self) -> Result { + pub fn run(&mut self) -> Result { info!("Running code"); // Running the latest command in vector for now let child_process = Command::new("/bin/sh") .args(["-c", - self.code_entry.script.last().ok_or( - InternalError::CmdSpawn - )?.as_str() + self.request_message.data.steps.last().ok_or(InternalError::CmdSpawn)?.command.as_str() ]) .current_dir(WORKSPACE_PATH) .output() @@ -124,8 +122,11 @@ impl InternalApi { let stderr = String::from_utf8(child_process.stderr).map_err( |_| InternalError::StderrRead )?; - - Ok(CodeReturn::new(stdout, stderr, exit_code)) + let step = ResponseStep::new(self.request_message.data.steps.last().ok_or(InternalError::CmdSpawn)?.command.clone(), exit_code, stdout.clone(), stderr.clone(), false); + let steps = vec![step]; + let data: ResponseData = ResponseData::new(stdout.clone(), steps); + // let response_message = ResponseMessage::new( + // Ok(CodeReturn::new(stdout, stderr, exit_code)) } } @@ -134,7 +135,7 @@ impl InternalApi { mod tests { use std::fs::File; use std::io::Read; - use crate::external_api::model::FileModel; + use crate::external_api::model::{FileModel, CodeEntry}; use super::*; fn random_usize(max: usize) -> usize { @@ -163,7 +164,7 @@ mod tests { #[test] fn workload_runs_correctly() { - let entry = CodeEntry { + let entry = CodeEntry { files: vec![], script: vec![String::from("echo 'This is stdout' && echo 'This is stderr' >&2")], }; diff --git a/agent/src/main.rs b/agent/src/main.rs index 0963188..50d5c73 100644 --- a/agent/src/main.rs +++ b/agent/src/main.rs @@ -28,13 +28,15 @@ fn main() -> Result<()> { trace!( "config file loaded successfully with content: {:#?}", - config + config ); let mut external_api = ExternalApi::new(config.serial.path, config.serial.baud_rate); - let code_entry = external_api.read_from_serial()?; - let mut internal_api = InternalApi::new(code_entry); + external_api.send_status_message()?; + + let request_message = external_api.read_from_serial()?; + let mut internal_api = InternalApi::new(request_message); internal_api.create_workspace()?; let res = internal_api.run().map_err(|e| anyhow!("{:?}", e));