mirror of
https://github.com/csehviktor/status-monitor.git
synced 2025-08-08 18:06:14 +02:00
43 lines
1.1 KiB
Rust
43 lines
1.1 KiB
Rust
use rumqttd::{Broker, Config};
|
|
use std::sync::Arc;
|
|
|
|
use super::subscriber::MqttSubscriber;
|
|
use crate::{bridge::ClientManager, storage::StorageRepositoryImpl};
|
|
|
|
pub struct MqttBroker {
|
|
broker: &'static mut Broker,
|
|
clients: Arc<ClientManager>,
|
|
storage: Arc<StorageRepositoryImpl>,
|
|
}
|
|
|
|
impl MqttBroker {
|
|
pub async fn new(cfg: Config, storage: Arc<StorageRepositoryImpl>) -> Self {
|
|
let clients = Arc::new(ClientManager::new());
|
|
let broker: &'static mut Broker = Box::leak(Box::new(Broker::new(cfg)));
|
|
|
|
Self {
|
|
broker,
|
|
clients,
|
|
storage,
|
|
}
|
|
}
|
|
|
|
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, self.storage.inner());
|
|
|
|
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
|
|
}
|
|
}
|