Skip to content

Commit

Permalink
added simple sync tcp example for v0.2.2
Browse files Browse the repository at this point in the history
  • Loading branch information
GunnarMorrigan committed Jan 31, 2024
1 parent 4a632dd commit 1882fb4
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
9 changes: 9 additions & 0 deletions examples/sync_tcp_v0.2.2/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "sync_tcp_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 = ["sync"]}
68 changes: 68 additions & 0 deletions examples/sync_tcp_v0.2.2/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
use std::time::Duration;

use mqrstt::{
new_sync, packets::{self, Packet}, sync::NetworkStatus, ConnectOptions, EventHandler, MqttClient
};

pub struct PingPong {
pub client: MqttClient,
}

impl EventHandler for PingPong {
// Handlers only get INCOMING packets. This can change later.
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_blocking(p.topic.clone(), p.qos, p.retain, "pong").unwrap();
println!("Received Ping, Send pong!");
}
}
}
Packet::ConnAck(_) => {
println!("Connected!")
}
_ => (),
}
}
}

fn main() {
let client_id = "SyncTls_MQrsTT_Example".to_string();
let options = ConnectOptions::new(client_id);

let address = "broker.emqx.io";
let port = 1883;

let (mut network, client) = new_sync(options);

let stream = std::net::TcpStream::connect((address, port)).unwrap();
stream.set_nonblocking(true).unwrap();

let mut pingpong = PingPong { client: client.clone() };

network.connect(stream, &mut pingpong).unwrap();

client.subscribe_blocking("mqrstt").unwrap();

let thread = std::thread::spawn(move || {
loop {
match network.poll(&mut pingpong) {
// The client is active but there is no data to be read
Ok(NetworkStatus::ActivePending) => std::thread::sleep(Duration::from_millis(100)),
// The client is active and there is data to be read
Ok(NetworkStatus::ActiveReady) => continue,
// The rest is an error
otherwise => return otherwise,
};
}
});

std::thread::sleep(std::time::Duration::from_secs(30));
client.disconnect_blocking().unwrap();

// Unwrap possible join errors on the thread.
let n = thread.join().unwrap();
assert!(n.is_ok());
}

0 comments on commit 1882fb4

Please sign in to comment.