-
Notifications
You must be signed in to change notification settings - Fork 228
/
syncclient.rs
180 lines (164 loc) · 5.98 KB
/
syncclient.rs
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
use tokio::runtime;
use crate::v5::{
client::get_ack_req,
packet::{Publish, Subscribe, SubscribeFilter, Unsubscribe},
AsyncClient, ClientError, Connection, MqttOptions, QoS, Request,
};
/// `Client` to communicate with MQTT eventloop `Connection`.
///
/// Client is cloneable and can be used to synchronously Publish, Subscribe.
/// Asynchronous channel handle can also be extracted if necessary
pub struct Client {
client: AsyncClient,
}
impl Client {
/// Create a new `Client`
pub fn new(options: MqttOptions, cap: usize) -> (Client, Connection) {
let (client, eventloop) = AsyncClient::new(options, cap);
let client = Client { client };
let runtime = runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap();
let connection = Connection::new(eventloop, runtime);
(client, connection)
}
/// Sends a MQTT Publish to the eventloop
pub fn publish<S, V>(
&self,
topic: S,
qos: QoS,
retain: bool,
payload: V,
) -> Result<u16, ClientError>
where
S: Into<String>,
V: Into<Vec<u8>>,
{
let mut publish = Publish::new(topic, qos, payload);
publish.retain = retain;
let pkid = if qos != QoS::AtMostOnce {
let mut request_buf = self.client.outgoing_buf.lock().unwrap();
if request_buf.buf.len() == request_buf.capacity {
return Err(ClientError::RequestsFull);
}
let pkid = request_buf.increment_pkid();
publish.pkid = pkid;
request_buf.buf.push_back(Request::Publish(publish));
pkid
} else {
0
};
self.client.notify()?;
Ok(pkid)
}
pub fn try_publish<S, V>(
&self,
topic: S,
qos: QoS,
retain: bool,
payload: V,
) -> Result<u16, ClientError>
where
S: Into<String>,
V: Into<Vec<u8>>,
{
self.client.try_publish(topic, qos, retain, payload)
}
/// Sends a MQTT PubAck to the eventloop. Only needed in if `manual_acks` flag is set.
pub fn ack(&self, publish: &Publish) -> Result<(), ClientError> {
if let Some(ack) = get_ack_req(publish.qos, publish.pkid) {
let mut request_buf = self.client.outgoing_buf.lock().unwrap();
if request_buf.buf.len() == request_buf.capacity {
return Err(ClientError::RequestsFull);
}
request_buf.buf.push_back(ack);
self.client.notify()?;
}
Ok(())
}
/// Sends a MQTT PubAck to the eventloop. Only needed in if `manual_acks` flag is set.
pub fn try_ack(&self, publish: &Publish) -> Result<(), ClientError> {
self.client.try_ack(publish)
}
/// Sends a MQTT Subscribe to the eventloop
pub fn subscribe<S: Into<String>>(&self, topic: S, qos: QoS) -> Result<u16, ClientError> {
let mut subscribe = Subscribe::new(topic.into(), qos);
let pkid = if qos != QoS::AtMostOnce {
let mut request_buf = self.client.outgoing_buf.lock().unwrap();
if request_buf.buf.len() == request_buf.capacity {
return Err(ClientError::RequestsFull);
}
let pkid = request_buf.increment_pkid();
subscribe.pkid = pkid;
request_buf.buf.push_back(Request::Subscribe(subscribe));
pkid
} else {
0
};
self.client.notify()?;
Ok(pkid)
}
/// Sends a MQTT Subscribe to the eventloop
pub fn try_subscribe<S: Into<String>>(&self, topic: S, qos: QoS) -> Result<u16, ClientError> {
self.client.try_subscribe(topic, qos)
}
/// Sends a MQTT Subscribe for multiple topics to the eventloop
pub fn subscribe_many<T>(&self, topics: T) -> Result<u16, ClientError>
where
T: IntoIterator<Item = SubscribeFilter>,
{
let mut subscribe = Subscribe::new_many(topics)?;
let pkid = {
let mut request_buf = self.client.outgoing_buf.lock().unwrap();
if request_buf.buf.len() == request_buf.capacity {
return Err(ClientError::RequestsFull);
}
let pkid = request_buf.increment_pkid();
subscribe.pkid = pkid;
request_buf.buf.push_back(Request::Subscribe(subscribe));
pkid
};
self.client.notify()?;
Ok(pkid)
}
pub fn try_subscribe_many<T>(&self, topics: T) -> Result<u16, ClientError>
where
T: IntoIterator<Item = SubscribeFilter>,
{
self.client.try_subscribe_many(topics)
}
/// Sends a MQTT Unsubscribe to the eventloop
pub fn unsubscribe<S: Into<String>>(&self, topic: S) -> Result<u16, ClientError> {
let mut unsubscribe = Unsubscribe::new(topic.into());
let pkid = {
let mut request_buf = self.client.outgoing_buf.lock().unwrap();
if request_buf.buf.len() == request_buf.capacity {
return Err(ClientError::RequestsFull);
}
let pkid = request_buf.increment_pkid();
unsubscribe.pkid = pkid;
request_buf.buf.push_back(Request::Unsubscribe(unsubscribe));
pkid
};
self.client.notify()?;
Ok(pkid)
}
/// Sends a MQTT Unsubscribe to the eventloop
pub fn try_unsubscribe<S: Into<String>>(&self, topic: S) -> Result<u16, ClientError> {
self.client.try_unsubscribe(topic)
}
/// Sends a MQTT disconnect to the eventloop
pub fn disconnect(&self) -> Result<(), ClientError> {
let mut request_buf = self.client.outgoing_buf.lock().unwrap();
if request_buf.buf.len() == request_buf.capacity {
return Err(ClientError::RequestsFull);
}
request_buf.buf.push_back(Request::Disconnect);
self.client.notify()
}
/// Sends a MQTT disconnect to the eventloop
pub fn try_disconnect(&self) -> Result<(), ClientError> {
self.client.try_disconnect()
}
}