use std::sync::Arc; use nxmesh_proto::{AgentMessage, ConfigUpdate, MasterMessage, master_message::Payload}; use crate::service::master_handler::{MasterHandlerError, MessageResult}; #[async_trait::async_trait] pub trait MasterMessageHandler: Send + Sync + 'static { async fn handle_master_message(&self, message: MasterMessage) -> MessageResult<()>; } #[async_trait::async_trait] pub trait OnConfigUpdateHandler: Send + Sync + 'static { // Handle the config update message from master, write the config content to files, validate the new config and reload nginx async fn on_config_update(&self, config_info: ConfigUpdate) -> MessageResult<()>; } pub struct HandlerImpl where OCH: OnConfigUpdateHandler + ?Sized, { on_config_update_handler: Arc, } impl HandlerImpl where OCH: OnConfigUpdateHandler + ?Sized, { pub fn new(on_config_update_handler: Arc) -> Self { Self { on_config_update_handler, } } } #[async_trait::async_trait] impl MasterMessageHandler for HandlerImpl where OCH: OnConfigUpdateHandler + ?Sized, { async fn handle_master_message(&self, message: MasterMessage) -> MessageResult<()> { match message.payload { Some(Payload::ConfigUpdate(config_info)) => { self.on_config_update_handler .on_config_update(config_info) .await } Some(_) => { // We should never receive other types of messages from the master, but we should handle it anyway Err(MasterHandlerError::MessageHandlingError( "Received unsupported master message type".to_string(), )) } None => { // This should never happen as the master should always send a valid message, but we should handle it anyway return Err(MasterHandlerError::MessageHandlingError( "Received master message with empty payload".to_string(), )); } } } }