Skip to content

Commit

Permalink
refactor(rumqttc): clean-up examples (#794)
Browse files Browse the repository at this point in the history
Co-authored-by: swanandx <[email protected]>
  • Loading branch information
Devdutt Shenoi and swanandx authored Feb 7, 2024
1 parent e63925a commit c719181
Show file tree
Hide file tree
Showing 16 changed files with 44 additions and 61 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

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

23 changes: 20 additions & 3 deletions rumqttc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,24 @@ color-backtrace = "0.6"
matches = "0.1"
pretty_assertions = "1"
pretty_env_logger = "0.5"
rustls = "0.21"
rustls-native-certs = "0.6"
serde = { version = "1", features = ["derive"] }
tokio = { version = "1", features = ["full", "macros"] }

[[example]]
name = "tls"
path = "examples/tls.rs"
required-features = ["use-rustls"]

[[example]]
name = "tls2"
path = "examples/tls2.rs"
required-features = ["use-rustls"]

[[example]]
name = "websocket"
path = "examples/websocket.rs"
required-features = ["websocket"]

[[example]]
name = "websocket_proxy"
path = "examples/websocket_proxy.rs"
required-features = ["websocket", "proxy"]
4 changes: 2 additions & 2 deletions rumqttc/examples/async_manual_acks.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use tokio::{task, time};

use rumqttc::{self, AsyncClient, Event, EventLoop, Incoming, MqttOptions, QoS};
use rumqttc::{AsyncClient, Event, EventLoop, Incoming, MqttOptions, QoS};
use std::error::Error;
use std::time::Duration;

Expand All @@ -14,7 +14,7 @@ fn create_conn() -> (AsyncClient, EventLoop) {
AsyncClient::new(mqttoptions, 10)
}

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

Expand Down
3 changes: 1 addition & 2 deletions rumqttc/examples/async_manual_acks_v5.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#![allow(dead_code, unused_imports)]
use rumqttc::v5::mqttbytes::v5::Packet;
use rumqttc::v5::mqttbytes::QoS;
use tokio::{task, time};
Expand All @@ -17,7 +16,7 @@ fn create_conn() -> (AsyncClient, EventLoop) {
AsyncClient::new(mqttoptions, 10)
}

#[tokio::main(worker_threads = 1)]
#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), Box<dyn Error>> {
// todo!("fix this example with new way of spawning clients")
pretty_env_logger::init();
Expand Down
4 changes: 2 additions & 2 deletions rumqttc/examples/asyncpubsub.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use tokio::{task, time};

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

#[tokio::main(worker_threads = 1)]
#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), Box<dyn Error>> {
pretty_env_logger::init();
// color_backtrace::install();
Expand Down
2 changes: 1 addition & 1 deletion rumqttc/examples/asyncpubsub_v5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use rumqttc::v5::{AsyncClient, MqttOptions};
use std::error::Error;
use std::time::Duration;

#[tokio::main(worker_threads = 1)]
#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), Box<dyn Error>> {
pretty_env_logger::init();
// color_backtrace::install();
Expand Down
4 changes: 2 additions & 2 deletions rumqttc/examples/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ fn main() {
});

// Iterate to poll the eventloop for connection progress
for notification in connection.iter() {
if let Ok(Event::Incoming(Incoming::Publish(packet))) = notification {
for notification in connection.iter().flatten() {
if let Event::Incoming(Incoming::Publish(packet)) = notification {
match Message::try_from(packet.payload.as_ref()) {
Ok(message) => println!("Payload = {message:?}"),
Err(error) => println!("Error = {error}"),
Expand Down
2 changes: 1 addition & 1 deletion rumqttc/examples/subscription_ids.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use rumqttc::v5::{AsyncClient, MqttOptions};
use std::error::Error;
use std::time::Duration;

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

Expand Down
2 changes: 1 addition & 1 deletion rumqttc/examples/syncpubsub.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use rumqttc::{self, Client, LastWill, MqttOptions, QoS};
use rumqttc::{Client, LastWill, MqttOptions, QoS};
use std::thread;
use std::time::Duration;

Expand Down
2 changes: 1 addition & 1 deletion rumqttc/examples/syncrecv.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use rumqttc::{self, Client, LastWill, MqttOptions, QoS};
use rumqttc::{Client, LastWill, MqttOptions, QoS};
use std::thread;
use std::time::Duration;

Expand Down
14 changes: 4 additions & 10 deletions rumqttc/examples/tls.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
//! Example of how to configure rumqttd to connect to a server using TLS and authentication.
use std::error::Error;

#[cfg(feature = "use-rustls")]
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
use rumqttc::{self, AsyncClient, Event, Incoming, MqttOptions, Transport};
use tokio_rustls::rustls::ClientConfig;
use rumqttc::{AsyncClient, Event, Incoming, MqttOptions, Transport};
use tokio_rustls::rustls::ClientConfig;

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

Expand Down Expand Up @@ -45,8 +44,3 @@ async fn main() -> Result<(), Box<dyn Error>> {
}
}
}

#[cfg(not(feature = "use-rustls"))]
fn main() -> Result<(), Box<dyn Error>> {
panic!("Enable feature 'use-rustls'");
}
12 changes: 3 additions & 9 deletions rumqttc/examples/tls2.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
//! Example of how to configure rumqttd to connect to a server using TLS and authentication.
use std::error::Error;

#[cfg(feature = "use-rustls")]
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
use rumqttc::{self, AsyncClient, MqttOptions, TlsConfiguration, Transport};
use rumqttc::{AsyncClient, MqttOptions, TlsConfiguration, Transport};

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

Expand Down Expand Up @@ -44,8 +43,3 @@ async fn main() -> Result<(), Box<dyn Error>> {

Ok(())
}

#[cfg(not(feature = "use-rustls"))]
fn main() -> Result<(), Box<dyn Error>> {
panic!("Enable feature 'use-rustls'");
}
2 changes: 1 addition & 1 deletion rumqttc/examples/topic_alias.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use rumqttc::v5::{AsyncClient, MqttOptions};
use std::error::Error;
use std::time::Duration;

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

Expand Down
14 changes: 2 additions & 12 deletions rumqttc/examples/websocket.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
#[cfg(feature = "websocket")]
use rumqttc::{self, AsyncClient, MqttOptions, QoS, Transport};
#[cfg(feature = "websocket")]
use rumqttc::{AsyncClient, MqttOptions, QoS, Transport};
use std::{error::Error, time::Duration};
#[cfg(feature = "websocket")]
use tokio::{task, time};

#[cfg(feature = "websocket")]
#[tokio::main(worker_threads = 1)]
#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), Box<dyn Error>> {
pretty_env_logger::init();

Expand Down Expand Up @@ -39,7 +35,6 @@ async fn main() -> Result<(), Box<dyn Error>> {
}
}

#[cfg(feature = "websocket")]
async fn requests(client: AsyncClient) {
client
.subscribe("hello/world", QoS::AtMostOnce)
Expand All @@ -57,8 +52,3 @@ async fn requests(client: AsyncClient) {

time::sleep(Duration::from_secs(120)).await;
}

#[cfg(not(feature = "websocket"))]
fn main() {
panic!("Enable websocket feature with `--features=websocket`");
}
14 changes: 2 additions & 12 deletions rumqttc/examples/websocket_proxy.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
#[cfg(all(feature = "websocket", feature = "proxy"))]
use rumqttc::{self, AsyncClient, Proxy, ProxyAuth, ProxyType, QoS, Transport};
#[cfg(all(feature = "websocket", feature = "proxy"))]
use rumqttc::{AsyncClient, Proxy, ProxyAuth, ProxyType, QoS, Transport};
use std::{error::Error, time::Duration};
#[cfg(all(feature = "websocket", feature = "proxy"))]
use tokio::{task, time};

#[cfg(all(feature = "websocket", feature = "proxy"))]
#[tokio::main(worker_threads = 1)]
#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), Box<dyn Error>> {
use rumqttc::MqttOptions;

Expand Down Expand Up @@ -48,7 +44,6 @@ async fn main() -> Result<(), Box<dyn Error>> {
}
}

#[cfg(all(feature = "websocket", feature = "proxy"))]
async fn requests(client: AsyncClient) {
client
.subscribe("hello/world", QoS::AtMostOnce)
Expand All @@ -66,8 +61,3 @@ async fn requests(client: AsyncClient) {

time::sleep(Duration::from_secs(120)).await;
}

#[cfg(not(all(feature = "websocket", feature = "proxy")))]
fn main() {
panic!("Enable websocket and proxy feature with `--features=websocket, proxy`");
}
2 changes: 1 addition & 1 deletion rumqttc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
//! use std::time::Duration;
//! use std::error::Error;
//!
//! # #[tokio::main(worker_threads = 1)]
//! # #[tokio::main(flavor = "current_thread")]
//! # async fn main() {
//! let mut mqttoptions = MqttOptions::new("rumqtt-async", "test.mosquitto.org", 1883);
//! mqttoptions.set_keep_alive(Duration::from_secs(5));
Expand Down

0 comments on commit c719181

Please sign in to comment.