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,
max: usize,
progress: f32,
socket_path: &'a str,
percent_socket_path: &'a str,
status_socket_path: &'a str,
}
impl<'a> AptUpdateProgressSocket<'a> {
/// 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 {
pulse_interval: 0,
max: 0,
progress: 0.0,
socket_path: socket_path
percent_socket_path: percent_socket_path,
status_socket_path: status_socket_path
};
progress
}
@ -56,21 +58,27 @@ impl<'a> DynAcquireProgress for AptUpdateProgressSocket<'a> {
///
/// Prints out the short description and the expected size.
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
///
/// Prints out the short description and the expected size.
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.
///
/// We don't print anything here to remain consistent with apt.
fn done(&mut self, _: &ItemDesc) {
println!("APT Cache Update Complete!")
fn done(&mut self, item: &ItemDesc) {
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.
@ -78,24 +86,22 @@ impl<'a> DynAcquireProgress for AptUpdateProgressSocket<'a> {
/// Start does not pass information into the method.
///
/// We do not print anything here to remain consistent with apt.
fn start(&mut self) {
println!("Starting APT Cache Update.");
}
fn start(&mut self) {}
/// Called when progress has finished.
///
/// Stop does not pass information into the method.
///
/// prints out the bytes downloaded and the overall average line speed.
fn stop(&mut self, status: &AcqTextStatus) {
println!("APT Cache Update Stopped!")
}
fn stop(&mut self, status: &AcqTextStatus) {}
/// Called when an Item fails to download.
///
/// Print out the ErrorText for the Item.
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
@ -105,13 +111,13 @@ impl<'a> DynAcquireProgress for AptUpdateProgressSocket<'a> {
/// meter along with an overall bandwidth and ETA indicator.
fn pulse(&mut self, status: &AcqTextStatus, owner: &PkgAcquire) {
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() {
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(_) => {}
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 {
Ok(size) => {
// 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) => {
// 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 {
Ok(size) => {
// 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) => {
// 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);
}
}
}