diff --git a/WatcherAgent/src/api.rs b/WatcherAgent/src/api.rs index 19f81ba..1e768ca 100644 --- a/WatcherAgent/src/api.rs +++ b/WatcherAgent/src/api.rs @@ -12,10 +12,12 @@ /// These functions are called from the main agent loop and background tasks. All network operations are asynchronous and robust to transient failures. use std::time::Duration; +use crate::docker::container; use crate::docker::serverclientcomm::handle_server_message; use crate::hardware::HardwareInfo; use crate::models::{ - Acknowledgment, HeartbeatDto, IdResponse, MetricDto, RegistrationDto, ServerMessage, DockerMetricDto + Acknowledgment, DockerMetricDto, DockerRegistrationDto, HeartbeatDto, IdResponse, MetricDto, + RegistrationDto, ServerMessage, }; use anyhow::Result; @@ -151,6 +153,46 @@ async fn get_server_id_by_ip( } } +pub async fn broadcast_docker_containers( + base_url: &str, + server_id: u16, + container_dto: &mut DockerRegistrationDto, +) -> Result<(u16, String), Box> { + // First get local IP + println!("Preparing to broadcast docker containers..."); + // Create HTTP client for registration + let client = Client::builder() + .danger_accept_invalid_certs(true) + .build()?; + + // Prepare registration data + let container_dto = container_dto; + container_dto.server_id = server_id; + + // Try to register (will retry on failure) + loop { + println!("Attempting to broadcast containers..."); + let url = format!("{}/monitoring/service-discovery", base_url); + match client.post(&url).json(&container_dto).send().await { + Ok(resp) if resp.status().is_success() => { + println!("✅ Successfully broadcasted docker container."); + } + Ok(resp) => { + let status = resp.status(); + let text = resp.text().await.unwrap_or_default(); + println!( + "⚠️ Broadcasting failed ({}): {} (will retry in 10 seconds)", + status, text + ); + } + Err(err) => { + println!("⚠️ Broadcasting failed: {} (will retry in 10 seconds)", err); + } + } + sleep(Duration::from_secs(10)).await; + } +} + /// Periodically sends heartbeat signals to the backend server to indicate agent liveness. /// /// This function runs in a background task and will retry on network errors. @@ -359,7 +401,3 @@ pub async fn send_docker_metrics( Ok(()) } - -pub async fn broadcast_docker_containers() { - // Placeholder for future implementation -} \ No newline at end of file diff --git a/WatcherAgent/src/docker/mod.rs b/WatcherAgent/src/docker/mod.rs index 71bf942..c4841ce 100644 --- a/WatcherAgent/src/docker/mod.rs +++ b/WatcherAgent/src/docker/mod.rs @@ -206,10 +206,9 @@ impl DockerManager { &self, ) -> Result> { let containers = self.get_containers().await?; - let container_count = containers.len(); let dto = DockerRegistrationDto { server_id: 0, - container_count, + //container_count, containers: serde_json::to_string(&containers)?, }; diff --git a/WatcherAgent/src/main.rs b/WatcherAgent/src/main.rs index 8460ea6..5db93f6 100644 --- a/WatcherAgent/src/main.rs +++ b/WatcherAgent/src/main.rs @@ -94,7 +94,7 @@ async fn main() -> Result<(), Box> { Ok((id, ip)) => { println!("Registered with server. ID: {}, IP: {}", id, ip); (id, ip) - }, + } Err(e) => { eprintln!("Fehler bei der Registrierung am Server: {e}"); return Err(e); @@ -112,6 +112,19 @@ async fn main() -> Result<(), Box> { }; println!("Client Version: {}", client_version); + // Prepare Docker registration DTO + let container_dto = if let Some(ref docker_manager) = docker_manager { + docker_manager.create_registration_dto().await? + } else { + models::DockerRegistrationDto { + server_id: 0, + //container_count: 0, --- IGNORE --- + containers: "[]".to_string(), + } + }; + let _ = + api::broadcast_docker_containers(server_url, server_id, &mut container_dto.clone()).await?; + // Start background tasks // Start server listening for commands (only if Docker is available) let listening_handle = if let Some(ref docker_manager) = docker_manager { diff --git a/WatcherAgent/src/models.rs b/WatcherAgent/src/models.rs index efb3768..e5cce76 100644 --- a/WatcherAgent/src/models.rs +++ b/WatcherAgent/src/models.rs @@ -190,7 +190,7 @@ pub struct DockerRegistrationDto { /// Unique server identifier (integer) pub server_id: u16, /// Number of currently running containers - pub container_count: usize, + // pub container_count: usize, --- IGNORE --- /// json stringified array of DockerContainer /// /// ## Json Example