Skip to content

Commit

Permalink
Merge e6d68be into d218dd6
Browse files Browse the repository at this point in the history
  • Loading branch information
otavio committed Oct 19, 2020
2 parents d218dd6 + e6d68be commit b8d5647
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 4 deletions.
4 changes: 4 additions & 0 deletions updatehub-sdk/src/listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type CallbackFn = dyn Fn(Handler) -> Pin<Box<dyn Future<Output = Result<()>>>>;

#[derive(Default)]
pub struct StateChangeListener {
probe_callbacks: Vec<Box<CallbackFn>>,
download_callbacks: Vec<Box<CallbackFn>>,
install_callbacks: Vec<Box<CallbackFn>>,
reboot_callbacks: Vec<Box<CallbackFn>>,
Expand All @@ -27,6 +28,7 @@ pub struct StateChangeListener {

#[derive(Debug)]
pub enum State {
Probe,
Download,
Install,
Reboot,
Expand Down Expand Up @@ -63,6 +65,7 @@ impl StateChangeListener {
Fut: Future<Output = Result<()>> + 'static,
{
match state {
State::Probe => self.probe_callbacks.push(Box::new(move |d| Box::pin(f(d)))),
State::Download => self.download_callbacks.push(Box::new(move |d| Box::pin(f(d)))),
State::Install => self.install_callbacks.push(Box::new(move |d| Box::pin(f(d)))),
State::Reboot => self.reboot_callbacks.push(Box::new(move |d| Box::pin(f(d)))),
Expand Down Expand Up @@ -100,6 +103,7 @@ impl StateChangeListener {

async fn emit(&self, stream: UnixStream, input: &str) -> Result<()> {
let callbacks = match input {
"probe" => &self.probe_callbacks,
"download" => &self.download_callbacks,
"install" => &self.install_callbacks,
"reboot" => &self.reboot_callbacks,
Expand Down
2 changes: 1 addition & 1 deletion updatehub/src/states/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ impl State {
State::Park(s) => s.handle(context).await,
State::EntryPoint(s) => s.handle(context).await,
State::Poll(s) => s.handle(context).await,
State::Probe(s) => s.handle(context).await,
State::Probe(s) => s.handle_with_callback(context).await,
State::Validation(s) => s.handle(context).await,
State::DirectDownload(s) => s.handle(context).await,
State::PrepareLocalInstall(s) => s.handle(context).await,
Expand Down
4 changes: 3 additions & 1 deletion updatehub/src/states/probe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use super::{
machine::{self, Context},
EntryPoint, Result, State, StateChangeImpl, Validation,
CallbackReporter, EntryPoint, Result, State, StateChangeImpl, Validation,
};
use chrono::{Duration, Utc};
use cloud::api::ProbeResponse;
Expand All @@ -13,6 +13,8 @@ use slog_scope::{error, info};
#[derive(Debug)]
pub(super) struct Probe;

impl CallbackReporter for Probe {}

/// Implements the state change for State<Probe>.
#[async_trait::async_trait]
impl StateChangeImpl for Probe {
Expand Down
4 changes: 2 additions & 2 deletions updatehub/tests/successful_integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ fn correct_config_update_polling() {
#[test]
fn correct_config_statechange_callback() {
let state_change_script = r#"#! /bin/bash
echo "cancel"
[ "$1" = "download" ] && echo "cancel" || echo
"#;

let (mut session, setup) = Settings::default()
Expand Down Expand Up @@ -391,7 +391,7 @@ echo "cancel"
#[test]
fn correct_config_error_state_callback() {
let state_change_script = r#"#! /bin/bash
echo "cancel"
[ "$1" = "error" ] && echo "cancel" || echo
"#;

let (mut session, setup) = Settings::default()
Expand Down

0 comments on commit b8d5647

Please sign in to comment.