cleaup code

This commit is contained in:
csehviktor
2025-07-05 15:55:57 +02:00
parent 3bcfc67bdb
commit 81719e5574
14 changed files with 224 additions and 121 deletions

View 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
}
}