2 modular socket

This commit is contained in:
Ward from fusion-voyager-3 2024-06-26 05:32:30 +03:00
parent 474f633b31
commit f8a7e7f924

View File

@ -22,17 +22,19 @@ pub struct AptUpdateProgressSocket<'a> {
pulse_interval: usize, pulse_interval: usize,
max: usize, max: usize,
progress: f32, progress: f32,
socket_path: &'a str, percent_socket_path: &'a str,
status_socket_path: &'a str,
} }
impl<'a> AptUpdateProgressSocket<'a> { impl<'a> AptUpdateProgressSocket<'a> {
/// Returns a new default progress instance. /// Returns a new default progress instance.
pub fn new(socket_path: &'a str) -> Self { pub fn new(percent_socket_path: &'a str, status_socket_path: &'a str) -> Self {
let mut progress = Self { let mut progress = Self {
pulse_interval: 0, pulse_interval: 0,
max: 0, max: 0,
progress: 0.0, progress: 0.0,
socket_path: socket_path percent_socket_path: percent_socket_path,
status_socket_path: status_socket_path
}; };
progress progress
} }
@ -56,21 +58,27 @@ impl<'a> DynAcquireProgress for AptUpdateProgressSocket<'a> {
/// ///
/// Prints out the short description and the expected size. /// Prints out the short description and the expected size.
fn hit(&mut self, item: &ItemDesc) { fn hit(&mut self, item: &ItemDesc) {
println!("Download: {}: {}", item.uri(), item.description()); let message = format!("Up-to-date: {} {}", item.description(), item.short_desc());
println!("{}", message);
Runtime::new().unwrap().block_on(send_progress_status(message, self.status_socket_path));
} }
/// Called when an Item has started to download /// Called when an Item has started to download
/// ///
/// Prints out the short description and the expected size. /// Prints out the short description and the expected size.
fn fetch(&mut self, item: &ItemDesc) { fn fetch(&mut self, item: &ItemDesc) {
println!("Fetch: {}: {}", item.uri(), item.description()); let message = format!("Downloading: {} {}", item.description(), item.short_desc());
println!("{}", message);
Runtime::new().unwrap().block_on(send_progress_status(message, self.status_socket_path));
} }
/// Called when an item is successfully and completely fetched. /// Called when an item is successfully and completely fetched.
/// ///
/// We don't print anything here to remain consistent with apt. /// We don't print anything here to remain consistent with apt.
fn done(&mut self, _: &ItemDesc) { fn done(&mut self, item: &ItemDesc) {
println!("APT Cache Update Complete!") let message = format!("Download Successful: {} {}", item.description(), item.short_desc());
println!("{}", message);
Runtime::new().unwrap().block_on(send_progress_status(message, self.status_socket_path));
} }
/// Called when progress has started. /// Called when progress has started.
@ -78,24 +86,22 @@ impl<'a> DynAcquireProgress for AptUpdateProgressSocket<'a> {
/// Start does not pass information into the method. /// Start does not pass information into the method.
/// ///
/// We do not print anything here to remain consistent with apt. /// We do not print anything here to remain consistent with apt.
fn start(&mut self) { fn start(&mut self) {}
println!("Starting APT Cache Update.");
}
/// Called when progress has finished. /// Called when progress has finished.
/// ///
/// Stop does not pass information into the method. /// Stop does not pass information into the method.
/// ///
/// prints out the bytes downloaded and the overall average line speed. /// prints out the bytes downloaded and the overall average line speed.
fn stop(&mut self, status: &AcqTextStatus) { fn stop(&mut self, status: &AcqTextStatus) {}
println!("APT Cache Update Stopped!")
}
/// Called when an Item fails to download. /// Called when an Item fails to download.
/// ///
/// Print out the ErrorText for the Item. /// Print out the ErrorText for the Item.
fn fail(&mut self, item: &ItemDesc) { fn fail(&mut self, item: &ItemDesc) {
println!("APT Cache Update Failed!") let message = format!("Download Failed!: {} {}", item.description(), item.short_desc());
eprintln!("{}", message);
Runtime::new().unwrap().block_on(send_progress_status(message, self.status_socket_path));
} }
/// Called periodically to provide the overall progress information /// Called periodically to provide the overall progress information
@ -105,13 +111,13 @@ impl<'a> DynAcquireProgress for AptUpdateProgressSocket<'a> {
/// meter along with an overall bandwidth and ETA indicator. /// meter along with an overall bandwidth and ETA indicator.
fn pulse(&mut self, status: &AcqTextStatus, owner: &PkgAcquire) { fn pulse(&mut self, status: &AcqTextStatus, owner: &PkgAcquire) {
let progress_percent: f32 = (status.current_bytes() as f32 * 100.0) / status.total_bytes() as f32; let progress_percent: f32 = (status.current_bytes() as f32 * 100.0) / status.total_bytes() as f32;
Runtime::new().unwrap().block_on(send_progress_percent(progress_percent, self.socket_path)); Runtime::new().unwrap().block_on(send_progress_percent(progress_percent, self.percent_socket_path));
} }
} }
fn main() { fn main() {
let update_cache = new_cache!().unwrap(); let update_cache = new_cache!().unwrap();
match update_cache.update(&mut AcquireProgress::new(AptUpdateProgressSocket::new("/tmp/pika_apt_update.sock"))) { match update_cache.update(&mut AcquireProgress::new(AptUpdateProgressSocket::new("/tmp/pika_apt_update.sock", "/tmp/pika_apt_update.sock"))) {
Ok(_) => {} Ok(_) => {}
Err(e) => panic!("{}", e.to_string()) Err(e) => panic!("{}", e.to_string())
}; };
@ -132,11 +138,34 @@ async fn send_progress_percent(progress_f32: f32, socket_path: &str) {
match stream.read(&mut buffer).await { match stream.read(&mut buffer).await {
Ok(size) => { Ok(size) => {
// Print the received response // Print the received response
println!("Response from Server on GTK4: {}", String::from_utf8_lossy(&buffer[..size])); //println!("Response from Server on GTK4: {}", String::from_utf8_lossy(&buffer[..size]));
} }
Err(e) => { Err(e) => {
// Print error message if reading fails // Print error message if reading fails
eprintln!("Failed to read Server on GTK4 with Error: {}", e); //eprintln!("Failed to read Server on GTK4 with Error: {}", e);
}
}
}
async fn send_progress_status(message: String, socket_path: &str) {
// Connect to the Unix socket
let mut stream = UnixStream::connect(socket_path).await.expect("Could not connect to server");
// Send the message to the server
stream.write_all(message.as_bytes()).await.expect("Failed to write to stream");
// Buffer to store the server's response
let mut buffer = [0; 2024];
// Read the response from the server
match stream.read(&mut buffer).await {
Ok(size) => {
// Print the received response
//println!("Response from Server on GTK4: {}", String::from_utf8_lossy(&buffer[..size]));
}
Err(e) => {
// Print error message if reading fails
//eprintln!("Failed to read Server on GTK4 with Error: {}", e);
} }
} }
} }
@ -173,11 +202,11 @@ async fn get_upgradable_packages() {
match stream.read(&mut buffer).await { match stream.read(&mut buffer).await {
Ok(size) => { Ok(size) => {
// Print the received response // Print the received response
println!("Response from Server on GTK4: {}", String::from_utf8_lossy(&buffer[..size])); //println!("Response from Server on GTK4: {}", String::from_utf8_lossy(&buffer[..size]));
} }
Err(e) => { Err(e) => {
// Print error message if reading fails // Print error message if reading fails
eprintln!("Failed to read Server on GTK4 with Error: {}", e); //eprintln!("Failed to read Server on GTK4 with Error: {}", e);
} }
} }
} }