forked from GunnarMorrigan/mqrstt
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added workspace, added v0.2.2 exampels, moved files
- Loading branch information
1 parent
cfe5225
commit 10c4516
Showing
70 changed files
with
1,388 additions
and
864 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,85 +1,6 @@ | ||
[package] | ||
name = "mqrstt" | ||
version = "0.3.0-alpha.5" | ||
homepage = "https://github.com/GunnarMorrigan/mqrstt" | ||
repository = "https://github.com/GunnarMorrigan/mqrstt" | ||
documentation = "https://docs.rs/mqrstt" | ||
categories = ["network-programming"] | ||
readme = "README.md" | ||
edition = "2021" | ||
license = "MPL-2.0" | ||
keywords = ["MQTT", "IoT", "MQTTv5", "messaging", "client"] | ||
description = "Pure rust MQTTv5 client implementation Smol and Tokio" | ||
[workspace] | ||
|
||
rust-version = "1.75" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[features] | ||
default = [ | ||
"smol", | ||
"tokio" | ||
] | ||
sync = [] | ||
tokio = ["dep:tokio", "tokio/rt"] | ||
smol = ["dep:smol"] | ||
logs = ["dep:tracing"] | ||
test = [] | ||
|
||
[dependencies] | ||
# Packets | ||
bytes = "1.5.0" | ||
|
||
# Errors | ||
thiserror = "1.0.53" | ||
tracing = { version = "0.1.40", optional = true } | ||
|
||
async-channel = "2.1.1" | ||
#async-mutex = "1.4.0" | ||
futures = { version = "0.3.30", default-features = false, features = [ | ||
"std", | ||
"async-await", | ||
] } | ||
|
||
# quic feature flag | ||
# quinn = {version = "0.9.0", optional = true } | ||
|
||
# tokio feature flag | ||
tokio = { version = "1.35.1", features = [ | ||
"macros", | ||
"io-util", | ||
"net", | ||
"time", | ||
], optional = true } | ||
|
||
# smol feature flag | ||
smol = { version = "2.0.0", optional = true } | ||
|
||
[dev-dependencies] | ||
criterion = { version = "0.5.1", features = ["async_tokio"] } | ||
|
||
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] } | ||
|
||
smol = { version = "2.0.0" } | ||
tokio = { version = "1.33.0", features = [ | ||
"rt-multi-thread", | ||
"rt", | ||
"macros", | ||
"sync", | ||
"io-util", | ||
"net", | ||
"time", | ||
] } | ||
|
||
rustls = { version = "0.21.7" } | ||
rustls-pemfile = { version = "1.0.3" } | ||
webpki = { version = "0.22.4" } | ||
async-rustls = { version = "0.4.1" } | ||
tokio-rustls = "0.24.1" | ||
rstest = "0.18.2" | ||
rand = "0.8.5" | ||
|
||
|
||
[[bench]] | ||
name = "bench_main" | ||
harness = false | ||
members = [ | ||
"mqrstt", | ||
"examples/*", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
**/Cargo.lock |
4 changes: 2 additions & 2 deletions
4
examples/smol_tls/Cargo.toml → examples/smol_tcp_v0.2.2/Cargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
use std::{io::{BufReader, Cursor}, sync::Arc}; | ||
|
||
use async_trait::async_trait; | ||
use mqrstt::{MqttClient, AsyncEventHandler, packets::{self, Packet}, ConnectOptions, new_smol, smol::NetworkStatus}; | ||
use rustls::{RootCertStore, OwnedTrustAnchor, ClientConfig, Certificate, ServerName}; | ||
|
||
pub struct PingPong { | ||
pub client: MqttClient, | ||
} | ||
|
||
#[async_trait] | ||
impl AsyncEventHandler for PingPong { | ||
// Handlers only get INCOMING packets. This can change later. | ||
async fn handle(&mut self, event: packets::Packet) -> () { | ||
match event { | ||
Packet::Publish(p) => { | ||
if let Ok(payload) = String::from_utf8(p.payload.to_vec()) { | ||
if payload.to_lowercase().contains("ping") { | ||
self.client | ||
.publish( | ||
p.topic.clone(), | ||
p.qos, | ||
p.retain, | ||
"pong", | ||
) | ||
.await | ||
.unwrap(); | ||
println!("Received Ping, Send pong!"); | ||
} | ||
} | ||
}, | ||
Packet::ConnAck(_) => { println!("Connected!") }, | ||
_ => (), | ||
} | ||
} | ||
} | ||
|
||
fn main() { | ||
smol::block_on(async { | ||
let client_id = "SmolTls_MQrsTT_Example".to_string(); | ||
let options = ConnectOptions::new(client_id); | ||
|
||
let address = "broker.emqx.io"; | ||
let port = 8883; | ||
|
||
let (mut network, client) = new_smol(options); | ||
|
||
let stream = smol::net::TcpStream::connect((address, port)).await.unwrap(); | ||
|
||
let mut pingpong = PingPong { client: client.clone() }; | ||
|
||
network.connect(stream, &mut pingpong).await.unwrap(); | ||
|
||
client.subscribe("mqrstt").await.unwrap(); | ||
|
||
let (n, _) = futures::join!( | ||
async { | ||
loop { | ||
return match network.poll(&mut pingpong).await { | ||
Ok(NetworkStatus::Active) => continue, | ||
otherwise => otherwise, | ||
}; | ||
} | ||
}, | ||
async { | ||
smol::Timer::after(std::time::Duration::from_secs(30)).await; | ||
client.disconnect().await.unwrap(); | ||
} | ||
); | ||
assert!(n.is_ok()); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
[package] | ||
name = "smol_tls_v0_2_2" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
mqrstt = { version = "0.2.2", default-features = false, features = ["smol"]} | ||
|
||
smol = { version = "1.3.0" } | ||
futures = { version = "0.3.27", default-features = false, features = ["std", "async-await"] } | ||
|
||
async-trait = "0.1.68" | ||
|
||
rustls = { version = "0.20.7" } | ||
rustls-pemfile = { version = "1.0.1" } | ||
webpki = { version = "0.22.0" } | ||
async-rustls = { version = "0.3.0" } |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
examples/tokio_tls/Cargo.toml → examples/tokio_tcp_v0.2.2/Cargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
use std::{io::{BufReader, Cursor}, sync::Arc, time::Duration}; | ||
|
||
use async_trait::async_trait; | ||
use mqrstt::{MqttClient, AsyncEventHandler, packets::{self, Packet}, ConnectOptions, tokio::NetworkStatus, new_tokio}; | ||
use tokio_rustls::rustls::{ClientConfig, RootCertStore, OwnedTrustAnchor, Certificate, ServerName}; | ||
|
||
pub struct PingPong { | ||
pub client: MqttClient, | ||
} | ||
|
||
#[async_trait] | ||
impl AsyncEventHandler for PingPong { | ||
// Handlers only get INCOMING packets. This can change later. | ||
async fn handle(&mut self, event: packets::Packet) -> () { | ||
match event { | ||
Packet::Publish(p) => { | ||
if let Ok(payload) = String::from_utf8(p.payload.to_vec()) { | ||
if payload.to_lowercase().contains("ping") { | ||
self.client | ||
.publish( | ||
p.topic.clone(), | ||
p.qos, | ||
p.retain, | ||
"pong", | ||
) | ||
.await | ||
.unwrap(); | ||
println!("Received Ping, Send pong!"); | ||
} | ||
} | ||
}, | ||
Packet::ConnAck(_) => { println!("Connected!") }, | ||
_ => (), | ||
} | ||
} | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
let client_id = "TokioTls_MQrsTT_Example".to_string(); | ||
let options = ConnectOptions::new(client_id); | ||
|
||
let address = "broker.emqx.io"; | ||
let port = 8883; | ||
|
||
let (mut network, client) = new_tokio(options); | ||
|
||
let stream = tokio::net::TcpStream::connect((address, port)).await.unwrap(); | ||
|
||
let mut pingpong = PingPong { client: client.clone() }; | ||
|
||
network.connect(stream, &mut pingpong).await.unwrap(); | ||
|
||
client.subscribe("mqrstt").await.unwrap(); | ||
|
||
let (n, _) = tokio::join!( | ||
async { | ||
loop { | ||
return match network.poll(&mut pingpong).await { | ||
Ok(NetworkStatus::Active) => continue, | ||
otherwise => otherwise, | ||
}; | ||
} | ||
}, | ||
async { | ||
tokio::time::sleep(Duration::from_secs(30)).await; | ||
client.disconnect().await.unwrap(); | ||
} | ||
); | ||
assert!(n.is_ok()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
[package] | ||
name = "tokio_tls_v0_2_2" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
mqrstt = { version = "0.2.2", default-features = false, features = ["tokio"]} | ||
|
||
tokio = { version = "1.26.0", features = ["rt-multi-thread", "rt", "macros", "sync", "io-util", "net", "time"] } | ||
tokio-rustls = "0.24.0" | ||
|
||
async-trait = "0.1.68" | ||
|
||
rustls = { version = "0.20.7" } | ||
rustls-pemfile = { version = "1.0.1" } | ||
webpki = { version = "0.22.0" } |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.