Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Lumeo rust utilities acked #1

Draft
wants to merge 21 commits into
base: master
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions rumqttc/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ To update your code simply remove `Key::ECC()` or `Key::RSA()` from the initiali
`rusttls-pemfile` to `2.0.0`, `async-tungstenite` to `0.24.0`, `ws_stream_tungstenite` to `0.12.0`
and `http` to `1.0.0`. This is a breaking change as types from some of these crates are part of
the public API.
- `publish` / `subscribe` / `unsubscribe` methods on `AsyncClient` and `Client` now return a `PkidPromise` which resolves into the identifier value chosen by the `EventLoop` when handling the packet.

### Deprecated

Expand Down
3 changes: 2 additions & 1 deletion rumqttc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ proxy = ["dep:async-http-proxy"]

[dependencies]
futures-util = { version = "0.3", default-features = false, features = ["std", "sink"] }
tokio = { version = "1.36", features = ["rt", "macros", "io-util", "net", "time"] }
tokio = { version = "1.36", features = ["rt", "macros", "io-util", "net", "time", "sync"] }
tokio-util = { version = "0.7", features = ["codec"] }
bytes = "1.5"
log = "0.4"
Expand Down Expand Up @@ -58,6 +58,7 @@ matches = "0.1"
pretty_assertions = "1"
pretty_env_logger = "0.5"
serde = { version = "1", features = ["derive"] }
tokio-util = { version = "0.7", features = ["time"] }

[[example]]
name = "tls"
Expand Down
65 changes: 65 additions & 0 deletions rumqttc/examples/ack_notif.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use tokio::task::{self, JoinSet};

use rumqttc::{AsyncClient, MqttOptions, QoS};
use std::error::Error;
use std::time::Duration;

#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), Box<dyn Error>> {
pretty_env_logger::init();
// color_backtrace::install();

let mut mqttoptions = MqttOptions::new("test-1", "localhost", 1883);
mqttoptions.set_keep_alive(Duration::from_secs(5));

let (client, mut eventloop) = AsyncClient::new(mqttoptions, 10);
task::spawn(async move {
loop {
let event = eventloop.poll().await;
match &event {
Ok(v) => {
println!("Event = {v:?}");
}
Err(e) => {
println!("Error = {e:?}");
}
}
}
});

// Subscribe and wait for broker acknowledgement
client
.subscribe("hello/world", QoS::AtMostOnce)
.await
.unwrap()
.wait_async()
.await
.unwrap();

// Publish and spawn wait for notification
let mut set = JoinSet::new();

let future = client
.publish("hello/world", QoS::AtMostOnce, false, vec![1; 1024])
.await
.unwrap();
set.spawn(future.wait_async());

let future = client
.publish("hello/world", QoS::AtLeastOnce, false, vec![1; 1024])
.await
.unwrap();
set.spawn(future.wait_async());

let future = client
.publish("hello/world", QoS::ExactlyOnce, false, vec![1; 1024])
.await
.unwrap();
set.spawn(future.wait_async());

while let Some(res) = set.join_next().await {
println!("Acknoledged = {:?}", res?);
}

Ok(())
}
70 changes: 70 additions & 0 deletions rumqttc/examples/pkid_promise.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
use futures_util::stream::StreamExt;
use tokio::{
select,
task::{self, JoinSet},
};
use tokio_util::time::DelayQueue;

use rumqttc::{AsyncClient, MqttOptions, QoS};
use std::error::Error;
use std::time::Duration;

#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), Box<dyn Error>> {
pretty_env_logger::init();
// color_backtrace::install();

let mut mqttoptions = MqttOptions::new("test-1", "broker.emqx.io", 1883);
mqttoptions.set_keep_alive(Duration::from_secs(5));

let (client, mut eventloop) = AsyncClient::new(mqttoptions, 10);
task::spawn(async move {
requests(client).await;
});

loop {
let event = eventloop.poll().await;
match &event {
Ok(v) => {
println!("Event = {v:?}");
}
Err(e) => {
println!("Error = {e:?}");
return Ok(());
}
}
}
}

async fn requests(client: AsyncClient) {
let mut joins = JoinSet::new();
joins.spawn(
client
.subscribe("hello/world", QoS::AtMostOnce)
.await
.unwrap()
.wait_async(),
);

let mut queue = DelayQueue::new();
for i in 1..=10 {
queue.insert(i as usize, Duration::from_secs(i));
}

loop {
select! {
Some(i) = queue.next() => {
joins.spawn(
client
.publish("hello/world", QoS::ExactlyOnce, false, vec![1; i.into_inner()])
.await
.unwrap().wait_async(),
);
}
Some(Ok(Ok(pkid))) = joins.join_next() => {
println!("Pkid: {:?}", pkid);
}
else => break,
}
}
}
70 changes: 70 additions & 0 deletions rumqttc/examples/pkid_promise_v5.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
use futures_util::stream::StreamExt;
use tokio::{
select,
task::{self, JoinSet},
};
use tokio_util::time::DelayQueue;

use rumqttc::v5::{mqttbytes::QoS, AsyncClient, MqttOptions};
use std::error::Error;
use std::time::Duration;

#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), Box<dyn Error>> {
pretty_env_logger::init();
// color_backtrace::install();

let mut mqttoptions = MqttOptions::new("test-1", "broker.emqx.io", 1883);
mqttoptions.set_keep_alive(Duration::from_secs(5));

let (client, mut eventloop) = AsyncClient::new(mqttoptions, 10);
task::spawn(async move {
requests(client).await;
});

loop {
let event = eventloop.poll().await;
match &event {
Ok(v) => {
println!("Event = {v:?}");
}
Err(e) => {
println!("Error = {e:?}");
return Ok(());
}
}
}
}

async fn requests(client: AsyncClient) {
let mut joins = JoinSet::new();
joins.spawn(
client
.subscribe("hello/world", QoS::AtMostOnce)
.await
.unwrap()
.wait_async(),
);

let mut queue = DelayQueue::new();
for i in 1..=10 {
queue.insert(i as usize, Duration::from_secs(i));
}

loop {
select! {
Some(i) = queue.next() => {
joins.spawn(
client
.publish("hello/world", QoS::ExactlyOnce, false, vec![1; i.into_inner()])
.await
.unwrap().wait_async(),
);
}
Some(Ok(Ok(pkid))) = joins.join_next() => {
println!("Pkid: {:?}", pkid);
}
else => break,
}
}
}
Loading