Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

small improvements on the mosquitto implementation #256

Merged
merged 1 commit into from
Nov 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 36 additions & 38 deletions crates/pubsub/src/implementors/mosquitto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ use tokio::{runtime::Handle, task::block_in_place};

#[derive(Clone)]
pub struct MosquittoImplementor {
host: String,
port: i32,
subscriptions: Arc<Mutex<Vec<String>>>,
client: Arc<Mutex<Client>>,
}

// TODO: We need to improve these Debug implementations
Expand All @@ -31,30 +29,33 @@ impl MosquittoImplementor {
.unwrap()
.parse::<i32>()
.unwrap();
Self {
host,
port,
subscriptions: Arc::new(Mutex::new(Vec::new())),
}

let client = block_in_place(|| {
Handle::current().block_on(async move {
let mut client = Client::with_auto_id().unwrap();

client
.connect(&host, port, std::time::Duration::from_secs(5), None)
.await
.unwrap();

Arc::new(Mutex::new(client))
})
});

Self { client }
}
}

// Pub
impl MosquittoImplementor {
pub async fn publish(&self, msg_value: &[u8], topic: &str) -> Result<()> {
let mut mqtt = Client::with_auto_id().unwrap();
block_in_place(|| {
Handle::current().block_on(async move {
mqtt.connect(
&self.host,
self.port,
std::time::Duration::from_secs(5),
None,
)
.await
.unwrap();

mqtt.publish(topic, msg_value, QoS::AtMostOnce, false)
self.client
.lock()
.unwrap()
.publish(topic, msg_value, QoS::AtMostOnce, false)
.await
.unwrap()
})
Expand All @@ -66,32 +67,29 @@ impl MosquittoImplementor {

// Sub
impl MosquittoImplementor {
pub fn subscribe(&self, topic: &str) -> Result<()> {
self.subscriptions.lock().unwrap().push(topic.to_string());
pub async fn subscribe(&self, topic: &str) -> Result<()> {
block_in_place(|| {
Handle::current().block_on(async move {
self.client
.lock()
.unwrap()
.subscribe(topic, QoS::AtMostOnce)
.await
.unwrap();
})
});
Ok(())
}

pub async fn receive(&self) -> Result<Vec<u8>> {
let mut mqtt = Client::with_auto_id().unwrap();
let mut res: Vec<u8> = vec![];

block_in_place(|| {
res = Handle::current().block_on(async move {
mqtt.connect(
&self.host,
self.port,
std::time::Duration::from_secs(5),
None,
)
.await
.unwrap();

let subs_lock = self.subscriptions.lock().unwrap();

for t in subs_lock.iter() {
mqtt.subscribe(t, QoS::AtMostOnce).await.unwrap();
}

mqtt.subscriber()
self.client
.lock()
.unwrap()
.subscriber()
.as_mut()
.unwrap()
.recv()
Expand Down
2 changes: 1 addition & 1 deletion crates/pubsub/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ impl pubsub::Pubsub for Pubsub {
async fn sub_subscribe(&mut self, self_: &Self::Sub, topic: &str) -> Result<(), Error> {
match &self_.sub_implementor {
SubImplementor::ConfluentApacheKafka(si) => si.subscribe(topic)?,
SubImplementor::Mosquitto(si) => si.subscribe(topic)?,
SubImplementor::Mosquitto(si) => si.subscribe(topic).await?,
}

Ok(())
Expand Down