summaryrefslogtreecommitdiffstats
path: root/cloudmqtt-bin/src/bin/client.rs
blob: 0c8ebf06fe87a6b80ca0a986c976e8390e671f4b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
//
//   This Source Code Form is subject to the terms of the Mozilla Public
//   License, v. 2.0. If a copy of the MPL was not distributed with this
//   file, You can obtain one at http://mozilla.org/MPL/2.0/.
//

use std::time::Duration;

use clap::Parser;
use cloudmqtt::client::connect::MqttClientConnector;
use cloudmqtt::client::send::Publish;
use cloudmqtt::client::MqttClient;
use cloudmqtt::transport::MqttConnectTransport;
use futures::FutureExt;
use tokio::net::TcpStream;
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::util::SubscriberInitExt;

#[derive(Debug, Parser)]
#[command(version, about, long_about = None)]
struct Args {
    #[arg(long)]
    hostname: String,
}

#[tokio::main]
async fn main() {
    let args = Args::parse();

    let filter = tracing_subscriber::filter::EnvFilter::from_default_env();
    let fmt_layer = tracing_subscriber::fmt::layer()
        .with_timer(tracing_subscriber::fmt::time::uptime())
        .with_level(true)
        .with_file(true)
        .with_line_number(true)
        .pretty();

    tracing_subscriber::registry()
        .with(filter)
        .with(fmt_layer)
        .init();

    let socket = TcpStream::connect(args.hostname).await.unwrap();

    let connection = MqttConnectTransport::TokioTcp(socket);
    let client_id =
        cloudmqtt::client_identifier::ProposedClientIdentifier::PotentiallyServerProvided;

    let connector = MqttClientConnector::new(
        connection,
        client_id,
        cloudmqtt::client::connect::CleanStart::Yes,
        cloudmqtt::keep_alive::KeepAlive::Seconds(5.try_into().unwrap()),
    );

    let client = MqttClient::builder()
        .with_on_packet_recv(Box::new(|packet| {
            tracing::trace!(?packet, "Received packet")
        }))
        .with_handle_acknowledge(Box::new(|packet| {
            async move {
                tracing::trace!(?packet, "Acknowledging packet");
                cloudmqtt::client::send::Acknowledge::Yes
            }
            .boxed()
        }))
        .build()
        .await
        .unwrap();
    let connected = client.connect(connector).await.unwrap();
    let background = tokio::task::spawn(connected.background_task);

    client
        .publish(Publish {
            topic: "foo/bar".try_into().unwrap(),
            qos: cloudmqtt::qos::QualityOfService::ExactlyOnce,
            retain: false,
            payload: vec![123].try_into().unwrap(),
            on_packet_recv: None,
        })
        .await
        .unwrap()
        .acknowledged()
        .await;

    client.ping().await.unwrap().response().await;

    tokio::time::sleep(Duration::from_secs(3)).await;

    client
        .publish(Publish {
            topic: "foo/bar".try_into().unwrap(),
            qos: cloudmqtt::qos::QualityOfService::AtMostOnce,
            retain: false,
            payload: vec![123].try_into().unwrap(),
            on_packet_recv: None,
        })
        .await
        .unwrap();

    tokio::time::sleep(Duration::from_secs(20)).await;

    println!("Sent message! Bye");
}