Skip to content

Commit

Permalink
Remove ResultReady event after 5 successful sends
Browse files Browse the repository at this point in the history
  • Loading branch information
Florian Guggi committed Jul 10, 2024
1 parent 995ea8f commit cf28ded
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 33 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions src/command/execute_program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use subprocess::Popen;

use crate::{
command::{
check_length, terminate_student_program, truncate_to_size, Event, ProgramStatus, ResultId,
check_length, terminate_student_program, truncate_to_size, Event, ProgramStatus, ResultId, RetryEvent,
},
communication::{CEPPacket, CommunicationHandle},
};
Expand Down Expand Up @@ -51,8 +51,8 @@ pub fn execute_program(
build_result_archive(rid).unwrap(); // create the tar file with result and log

let mut context = wd_context.lock().unwrap();
context.event_vec.push(Event::Status(sid)).unwrap();
context.event_vec.push(Event::Result(rid)).unwrap();
context.event_vec.push(RetryEvent::new(Event::Status(sid))).unwrap();
context.event_vec.push(RetryEvent::new(Event::Result(rid))).unwrap();
context.running_flag = false;
context.update_pin.set_high();
drop(context);
Expand Down
21 changes: 17 additions & 4 deletions src/command/execution_context.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use filevec::FileVec;
use std::{
str::FromStr,
sync::{Arc, Mutex},
thread,
};

use filevec::FileVec;
const EVENT_SEND_TRIES: u32 = 5;

/// This type makes the ExecutionContext thread-safe
pub type SyncExecutionContext = Arc<Mutex<ExecutionContext>>;
Expand All @@ -20,7 +21,7 @@ pub struct ExecutionContext {
/// This integer is the pin number of the EDU_Update pin
pub update_pin: UpdatePin,
/// Vector containing events that should be sent to the COBC
pub event_vec: FileVec<Event>,
pub event_vec: FileVec<RetryEvent<Event>>,
}

impl ExecutionContext {
Expand All @@ -35,13 +36,13 @@ impl ExecutionContext {
event_vec: FileVec::open(event_file_path).unwrap(),
};

ec.check_update_pin();
ec.configure_update_pin();

Ok(Arc::new(Mutex::new(ec)))
}

/// Checks and sets/resets the update pin accordingly
pub fn check_update_pin(&mut self) {
pub fn configure_update_pin(&mut self) {
if self.has_data_ready() {
self.update_pin.set_high();
} else {
Expand Down Expand Up @@ -123,6 +124,18 @@ pub enum Event {
DisableDosimeter,
}

#[derive(serde::Serialize, serde::Deserialize, Clone, Copy, PartialEq, Eq, Debug)]
pub struct RetryEvent<T> {
pub retries: u32,
pub event: T,
}

impl<T> RetryEvent<T> {
pub fn new(event: T) -> Self {
Self { retries: EVENT_SEND_TRIES, event }
}
}

impl From<Event> for Vec<u8> {
fn from(value: Event) -> Self {
let mut v = Vec::new();
Expand Down
26 changes: 11 additions & 15 deletions src/command/get_status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,18 @@ pub fn get_status(
return Ok(());
}

if let Some(index) =
l_exec.event_vec.as_ref().iter().position(|x| matches!(x, Event::Status(_)))
{
let event = l_exec.event_vec[index];
com.send_packet(&CEPPacket::Data(event.into()))?;
l_exec.event_vec.remove(index)?;
} else {
let event = *l_exec.event_vec.as_ref().first().unwrap(); // Safe, because we know it is not empty
com.send_packet(&CEPPacket::Data(event.into()))?;
let result = {
let mut events = l_exec.event_vec.as_mut();
let index = events.iter().position(|x| matches!(x.event, Event::Status(_))).unwrap_or(0);

if !matches!(event, Event::Result(_)) {
// Results are removed when deleted
l_exec.event_vec.remove(0)?;
events[index].retries -= 1;
let result = com.send_packet(&CEPPacket::Data(events[index].event.into()));
if !matches!(events[index].event, Event::Result(_)) || events[index].retries == 0 {
events.remove(index);
}
}
result
};

l_exec.check_update_pin();
Ok(())
l_exec.configure_update_pin();
Ok(result?)
}
13 changes: 9 additions & 4 deletions src/command/return_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,16 @@ pub fn return_result(
delete_result(result_id)?;

let mut l_exec = exec.lock().unwrap();
let event_index =
l_exec.event_vec.as_ref().iter().position(|x| x == &Event::Result(result_id)).unwrap();
l_exec.event_vec.remove(event_index)?;
l_exec.check_update_pin();
if let Some(event_index) =
l_exec.event_vec.as_ref().iter().position(|x| x.event == Event::Result(result_id))
{
l_exec.event_vec.remove(event_index)?;
}
else {
log::error!("Could not find event entry for existing result file {program_id}:{timestamp}");
}

l_exec.configure_update_pin();
Ok(())
}

Expand Down
6 changes: 3 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#![allow(non_snake_case)]
use command::ExecutionContext;
use command::{ExecutionContext, RetryEvent};
use communication::socket::UnixSocketParser;
use core::time;
use rppal::gpio::Gpio;
Expand Down Expand Up @@ -96,8 +96,8 @@ fn event_socket_loop(context: Arc<Mutex<ExecutionContext>>, mut socket: UnixSock

log::info!("Received on socket: {event:?}");
let mut context = context.lock().unwrap();
context.event_vec.push(event).unwrap();
context.check_update_pin();
context.event_vec.push(RetryEvent::new(event)).unwrap();
context.configure_update_pin();
}
}

Expand Down
17 changes: 17 additions & 0 deletions tests/simulation/command_execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,20 @@ fn simulate_archive_is_stored_correctly() -> Result<(), std::io::Error> {

Ok(())
}

#[test]
fn return_result_is_retried_n_times() {
let (mut com, _socat) = SimulationComHandle::with_socat_proc("return_result_retries");
let _sched = start_scheduler("return_result_retries").unwrap();

simulate_test_store_archive(&mut com, 8).unwrap();
simulate_execute_program(&mut com, 8, 0, 5).unwrap();
std::thread::sleep(std::time::Duration::from_millis(400));

assert_eq!(get_status_program_finished(8, 0, 0), simulate_get_status(&mut com).unwrap());
for i in 0..5 {
assert_eq!(get_status_result_ready(8, 0), simulate_get_status(&mut com).unwrap());
dbg!(i);
}
assert_eq!([0u8], *simulate_get_status(&mut com).unwrap());
}
15 changes: 15 additions & 0 deletions tests/simulation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,18 @@ pub fn return_result(program_id: u16, timestamp: u32) -> Vec<u8> {
vec.extend(timestamp.to_le_bytes());
vec
}

pub fn get_status_program_finished(program_id: u16, timestamp: u32, exit_code: u8) -> Vec<u8> {
let mut vec = vec![1];
vec.extend(program_id.to_le_bytes());
vec.extend(timestamp.to_le_bytes());
vec.push(exit_code);
vec
}

pub fn get_status_result_ready(program_id: u16, timestamp: u32) -> Vec<u8> {
let mut vec = vec![2];
vec.extend(program_id.to_le_bytes());
vec.extend(timestamp.to_le_bytes());
vec
}

0 comments on commit cf28ded

Please sign in to comment.