mirror of
				https://github.com/csehviktor/status-monitor.git
				synced 2025-08-08 18:06:14 +02:00 
			
		
		
		
	cleaup code
This commit is contained in:
		
							
								
								
									
										40
									
								
								server/src/broker/manager.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										40
									
								
								server/src/broker/manager.rs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,40 @@ | ||||
| use rumqttd::{Broker, Config}; | ||||
| use std::sync::Arc; | ||||
|  | ||||
| use crate::bridge::ClientManager; | ||||
| use super::subscriber::MqttSubscriber; | ||||
|  | ||||
| pub struct MqttBroker { | ||||
|     broker: &'static mut Broker, | ||||
|     clients: Arc<ClientManager>, | ||||
| } | ||||
|  | ||||
| impl MqttBroker { | ||||
|     pub async fn new(cfg: Config) -> Self { | ||||
|         let clients = Arc::new(ClientManager::new()); | ||||
|         let broker: &'static mut Broker = Box::leak(Box::new(Broker::new(cfg))); | ||||
|  | ||||
|         Self { | ||||
|             broker, | ||||
|             clients, | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     pub fn clients(&self) -> Arc<ClientManager> { | ||||
|         self.clients.clone() | ||||
|     } | ||||
|  | ||||
|     pub async fn run(self) -> anyhow::Result<()> { | ||||
|         let mut subscriber = MqttSubscriber::new(&self.broker, self.clients); | ||||
|  | ||||
|         println!("starting mqtt broker on specified port"); | ||||
|  | ||||
|         tokio::spawn(async move { | ||||
|             if let Err(e) = self.broker.start() { | ||||
|                 eprintln!("broker exited with error: {}", e); | ||||
|             } | ||||
|         }); | ||||
|  | ||||
|         subscriber.run().await | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user