Skip to content

Commit

Permalink
Merge branch 'main' into barebones
Browse files Browse the repository at this point in the history
  • Loading branch information
CaspianA1 committed Sep 17, 2024
2 parents a75a7a6 + 94df6b4 commit 06d81bd
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 18 deletions.
14 changes: 6 additions & 8 deletions src/dashboard_defs/surprise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,18 +191,16 @@ pub fn make_surprise_window(

const SURPRISE_STREAM_PATH_BUFFER_INITIAL_SIZE: usize = 64;

let options = ListenerOptions::new().name(artificial_triggering_socket_path.to_fs_name::<GenericFilePath>()?);
let socket_path_fs_name = artificial_triggering_socket_path.to_fs_name::<GenericFilePath>()?;
let make_listener = || ListenerOptions::new().name(socket_path_fs_name.clone()).create_sync();

let surprise_stream_listener = match options.create_sync() {
let surprise_stream_listener = match make_listener() {
Ok(listener) => listener,

Err(err) => {
return error_msg!(
"Could not create a surprise stream listener. \
Perhaps the socket at '{artificial_triggering_socket_path}' is already in use, or \
maybe it was still around from a crash? \
Official error: '{err}'."
);
log::warn!("A previous surprise stream socket path was still around after a previous crash; removing it and making a new one.");
std::fs::remove_file(artificial_triggering_socket_path)?;
make_listener().unwrap_or_else(|_| panic!("Could not create a surprise stream listener: '{err}'."))
}
};

Expand Down
2 changes: 1 addition & 1 deletion src/dashboard_defs/twilio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
generic_result::*,
update_rate::UpdateRate,
dynamic_optional::DynamicOptional,
thread_task::{ContinuallyUpdated, Updatable}
continually_updated::{ContinuallyUpdated, Updatable}
},

dashboard_defs::shared_window_state::SharedWindowState,
Expand Down
2 changes: 1 addition & 1 deletion src/dashboard_defs/weather.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
generic_result::*,
dynamic_optional::DynamicOptional,
update_rate::{UpdateRateCreator, Seconds},
thread_task::{ContinuallyUpdated, Updatable}
continually_updated::{ContinuallyUpdated, Updatable}
},

window_tree::{
Expand Down
2 changes: 1 addition & 1 deletion src/spinitron/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{

utility_types::{
generic_result::*,
thread_task::{Updatable, ContinuallyUpdated}
continually_updated::{Updatable, ContinuallyUpdated}
},

spinitron::model::{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use std::thread;
use std::sync::mpsc;

use crate::utility_types::generic_result::*;

const STACK_SIZE: usize = 65536;

//////////

/* TODO:
Expand All @@ -11,8 +12,6 @@ in short bursts when you call `update` (or work with coroutines somehow)?
- I can maybe use the `park_timeout`, `sleep`, `yield_now`, or `sleep_until` functions for that...
- Allow for thread joining (or just finishing one iteration, rather;) (would be useful for Twilio) (but this is assuming that I don't do the thread-always-alive idea)
- Give the thread the name string, rather than keeping the string around
- Rename this file to `continually_updated.rs`
*/

pub trait Updatable: Clone + Send {
Expand All @@ -27,14 +26,16 @@ pub struct ContinuallyUpdated<T: Updatable> {
name: &'static str
}

//////////

impl<T: Updatable + 'static> ContinuallyUpdated<T> {
pub fn new(data: &T, initial_param: &T::Param, name: &'static str) -> Self {
let (data_sender, data_receiver) = mpsc::sync_channel(1); // This can be async if needed
let (param_sender, param_receiver) = mpsc::sync_channel(1);

let mut cloned_data = data.clone();

thread::spawn(move || {
thread::Builder::new().name(name.to_string()).stack_size(STACK_SIZE).spawn(move || {
loop {
fn handle_channel_error<Error: std::fmt::Display>(err: Error, name: &str, transfer_description: &str) {
log::warn!("Problem from {name} with {transfer_description} main thread (probably harmless, at program shutdown): {err}");
Expand All @@ -61,7 +62,7 @@ impl<T: Updatable + 'static> ContinuallyUpdated<T> {
return;
}
}
});
}).expect("Failed to spawn thread");

let continually_updated = Self {
curr_data: data.clone(), param_sender,
Expand Down
2 changes: 1 addition & 1 deletion src/utility_types/generic_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub trait ToGenericError<T, E> {
impl<T, E> ToGenericError<T, E> for Result<T, E>
where E: std::fmt::Debug + std::fmt::Display + Send + Sync + 'static {

fn to_generic(self) -> anyhow::Result<T> {
fn to_generic(self) -> GenericResult<T> {
self.map_err(anyhow::Error::msg)
}
}
2 changes: 1 addition & 1 deletion src/utility_types/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pub mod vec2f;
pub mod json_utils;
pub mod update_rate;
pub mod thread_task;
pub mod generic_result;
pub mod dynamic_optional;
pub mod continually_updated;

0 comments on commit 06d81bd

Please sign in to comment.