-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
218 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Copyright (C) 2018, 2019, 2020 O.S. Systems Sofware LTDA | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use std::{future::Future, pin::Pin}; | ||
use updatehub_sdk::{listener, Result}; | ||
|
||
fn download_callback( | ||
mut handler: listener::ListenerHandler, | ||
) -> Pin<Box<dyn Future<Output = Result<()>>>> { | ||
Box::pin(async move { | ||
println!("CALLBACK: download_callback"); | ||
println!("Canceling the command..."); | ||
println!("Done"); | ||
handler.cancel().await | ||
}) | ||
} | ||
|
||
fn error_callback(handler: listener::ListenerHandler) -> Pin<Box<dyn Future<Output = Result<()>>>> { | ||
Box::pin(async move { | ||
println!("ERROR"); | ||
println!("Done"); | ||
handler.proceed().await | ||
}) | ||
} | ||
|
||
fn rebooting_callback( | ||
handler: listener::ListenerHandler, | ||
) -> Pin<Box<dyn Future<Output = Result<()>>>> { | ||
Box::pin(async move { | ||
println!("CALLBACK: rebooting_callback"); | ||
println!("Stopping listener..."); | ||
handler.stop().await | ||
}) | ||
} | ||
|
||
#[async_std::main] | ||
async fn main() -> Result<()> { | ||
let mut listener = listener::StateChangeListener::default(); | ||
listener.on_state_change(listener::State::Download, Box::new(download_callback)).await; | ||
listener.on_state_change(listener::State::Reboot, Box::new(rebooting_callback)).await; | ||
listener.on_error(Box::new(error_callback)).await; | ||
|
||
listener.listen().await | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
pub mod api; | ||
mod client; | ||
mod error; | ||
pub mod listener; | ||
mod serde_helpers; | ||
|
||
pub use client::Client; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
// Copyright (C) 2018, 2019, 2020 O.S. Systems Sofware LTDA | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use crate::{Error, Result}; | ||
use async_std::{ | ||
io::BufReader, | ||
os::unix::net::{UnixListener, UnixStream}, | ||
prelude::*, | ||
}; | ||
use log::warn; | ||
use std::{env, fs, path::Path, pin::Pin, process}; | ||
|
||
const SDK_TRIGGER_FILENAME: &str = | ||
"/usr/share/updatehub/state-change-callbacks.d/10-updatehub-sdk-statechange-trigger"; | ||
const SOCKET_PATH: &str = "/run/updatehub-statechange.sock"; | ||
|
||
type Callback = | ||
Box<dyn Fn(ListenerHandler) -> Pin<Box<dyn std::future::Future<Output = Result<()>>>>>; | ||
|
||
#[derive(Default)] | ||
pub struct StateChangeListener { | ||
download_state_callbacks: Vec<Callback>, | ||
install_state_callbacks: Vec<Callback>, | ||
reboot_state_callbacks: Vec<Callback>, | ||
error_state_callbacks: Vec<Callback>, | ||
} | ||
|
||
pub enum State { | ||
Download, | ||
Install, | ||
Reboot, | ||
Error, | ||
} | ||
|
||
pub struct ListenerHandler { | ||
stream: UnixStream, | ||
} | ||
|
||
impl ListenerHandler { | ||
// Cancels the current action on the agent. | ||
pub async fn cancel(&mut self) -> Result<()> { | ||
self.stream.write_all(b"cancel").await.map_err(Error::Io) | ||
} | ||
|
||
// Tell the agent to proceed with the transition. | ||
pub async fn proceed(&self) -> Result<()> { | ||
// No message need to be sent to the connection in order to the | ||
// agent to proceed handling the current state. | ||
Ok(()) | ||
} | ||
|
||
// Tell the listener to stop the execution. | ||
pub async fn stop(&self) -> Result<()> { | ||
process::exit(1); | ||
} | ||
} | ||
|
||
impl StateChangeListener { | ||
pub async fn on_state_change(&mut self, state: State, f: Callback) { | ||
match state { | ||
State::Download => self.download_state_callbacks.push(f), | ||
State::Install => self.install_state_callbacks.push(f), | ||
State::Reboot => self.reboot_state_callbacks.push(f), | ||
State::Error => self.error_state_callbacks.push(f), | ||
} | ||
} | ||
|
||
pub async fn on_error(&mut self, f: Callback) { | ||
self.error_state_callbacks.push(f); | ||
} | ||
|
||
pub async fn listen(&self) -> Result<()> { | ||
if !Path::new(&SDK_TRIGGER_FILENAME).exists() { | ||
warn!( | ||
"WARNING: updatehub-sdk-statechange-trigger not found on {}", | ||
SDK_TRIGGER_FILENAME | ||
); | ||
} | ||
let socket_path = env::var("UH_LISTENER_TEST").unwrap_or_else(|_| SOCKET_PATH.to_string()); | ||
if Path::new(&socket_path).exists() { | ||
fs::remove_file(&socket_path)?; | ||
} | ||
let listener = UnixListener::bind(socket_path).await?; | ||
|
||
loop { | ||
let (socket, ..) = listener.accept().await?; | ||
self.handle_connection(socket).await?; | ||
} | ||
} | ||
|
||
async fn handle_connection(&self, stream: UnixStream) -> Result<()> { | ||
let mut reader = BufReader::new(&stream); | ||
let mut line = String::new(); | ||
|
||
reader.read_line(&mut line).await?; | ||
|
||
self.emit(stream, &line.trim()).await | ||
} | ||
|
||
async fn emit(&self, stream: UnixStream, input: &str) -> Result<()> { | ||
let input: Vec<&str> = input.split_whitespace().collect(); | ||
match input[0] { | ||
"entry_point" => {} | ||
|
||
"download" => { | ||
for f in self.download_state_callbacks.iter() { | ||
let stream = stream.clone(); | ||
f(ListenerHandler { stream }).await? | ||
} | ||
} | ||
"install" => { | ||
for f in self.install_state_callbacks.iter() { | ||
let stream = stream.clone(); | ||
f(ListenerHandler { stream }).await?; | ||
} | ||
} | ||
"reboot" => { | ||
for f in self.reboot_state_callbacks.iter() { | ||
let stream = stream.clone(); | ||
f(ListenerHandler { stream }).await?; | ||
} | ||
} | ||
"error" => { | ||
for f in self.error_state_callbacks.iter() { | ||
let stream = stream.clone(); | ||
f(ListenerHandler { stream }).await?; | ||
} | ||
} | ||
_ => unreachable!("the input is not valid"), | ||
} | ||
Ok(()) | ||
} | ||
} |