Skip to content

Commit

Permalink
chore: begin of the new paylaod (need rework)
Browse files Browse the repository at this point in the history
Signed-off-by: GridexX <[email protected]>
  • Loading branch information
GridexX authored and WoodenMaiden committed Mar 28, 2023
1 parent 69a741c commit 4db2988
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 22 deletions.
113 changes: 113 additions & 0 deletions agent/lib/src/external_api/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,116 @@ pub struct CodeEntry {
pub files: Vec<FileModel>,
pub script: Vec<String>, // 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<ResponseStep>
}


impl ResponseData {
pub fn new(id: String, steps: Vec<ResponseStep>) -> 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<FileModel>,
pub steps: Vec<RequestStep>
}

#[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
}
}
}
24 changes: 18 additions & 6 deletions agent/lib/src/external_api/service.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -16,7 +16,7 @@ impl ExternalApi {
}
}

pub fn read_from_serial(&mut self) -> Result<CodeEntry> {
pub fn read_from_serial(&mut self) -> Result<RequestMessage> {
info!("Reading from serial port: {}", self.serial_path);

// Open the serial port
Expand Down Expand Up @@ -81,14 +81,26 @@ impl ExternalApi {
}
}

pub fn parse_json_payload(&mut self, data: &[u8]) -> Result<CodeEntry> {
pub fn parse_json_payload(&mut self, data: &[u8]) -> Result<RequestMessage> {
// 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<ResponseMessage>) -> Result<()> {
Ok(())
}

pub fn write_to_serial(&mut self, data: &str) -> Result<()> {
Expand Down
27 changes: 14 additions & 13 deletions agent/lib/src/internal_api/service.rs
Original file line number Diff line number Diff line change
@@ -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<()> {
Expand All @@ -23,7 +23,7 @@ impl InternalApi {
let mut file_models: Vec<FileModel> = 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();

Expand Down Expand Up @@ -98,16 +98,14 @@ impl InternalApi {
"Hello".to_string()
}

pub fn run(&mut self) -> Result<CodeReturn, InternalError> {
pub fn run(&mut self) -> Result<ResponseMessage, InternalError> {
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()
Expand All @@ -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))
}

}
Expand All @@ -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 {
Expand Down Expand Up @@ -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")],
};
Expand Down
8 changes: 5 additions & 3 deletions agent/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down

0 comments on commit 4db2988

Please sign in to comment.