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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
use event_listener::EventListener;
use static_assertions::assert_impl_all;
use std::{convert::TryInto, ops::Deref, sync::Arc};
use zbus_names::{BusName, ErrorName, InterfaceName, MemberName, OwnedUniqueName, WellKnownName};
use zvariant::ObjectPath;
use crate::{blocking::ObjectServer, utils::block_on, DBusError, Error, Message, Result};
#[derive(derivative::Derivative, Clone)]
#[derivative(Debug)]
pub struct Connection {
inner: crate::Connection,
}
assert_impl_all!(Connection: Send, Sync, Unpin);
impl Connection {
pub fn session() -> Result<Self> {
block_on(crate::Connection::session()).map(Self::from)
}
pub fn system() -> Result<Self> {
block_on(crate::Connection::system()).map(Self::from)
}
pub fn max_queued(&self) -> usize {
self.inner.max_queued()
}
pub fn set_max_queued(mut self, max: usize) {
self.inner.set_max_queued(max)
}
pub fn server_guid(&self) -> &str {
self.inner.server_guid()
}
pub fn unique_name(&self) -> Option<&OwnedUniqueName> {
self.inner.unique_name()
}
pub fn send_message(&self, msg: Message) -> Result<u32> {
block_on(self.inner.send_message(msg))
}
pub fn call_method<'d, 'p, 'i, 'm, D, P, I, M, B>(
&self,
destination: Option<D>,
path: P,
iface: Option<I>,
method_name: M,
body: &B,
) -> Result<Arc<Message>>
where
D: TryInto<BusName<'d>>,
P: TryInto<ObjectPath<'p>>,
I: TryInto<InterfaceName<'i>>,
M: TryInto<MemberName<'m>>,
D::Error: Into<Error>,
P::Error: Into<Error>,
I::Error: Into<Error>,
M::Error: Into<Error>,
B: serde::ser::Serialize + zvariant::DynamicType,
{
block_on(
self.inner
.call_method(destination, path, iface, method_name, body),
)
}
pub fn emit_signal<'d, 'p, 'i, 'm, D, P, I, M, B>(
&self,
destination: Option<D>,
path: P,
iface: I,
signal_name: M,
body: &B,
) -> Result<()>
where
D: TryInto<BusName<'d>>,
P: TryInto<ObjectPath<'p>>,
I: TryInto<InterfaceName<'i>>,
M: TryInto<MemberName<'m>>,
D::Error: Into<Error>,
P::Error: Into<Error>,
I::Error: Into<Error>,
M::Error: Into<Error>,
B: serde::ser::Serialize + zvariant::DynamicType,
{
block_on(
self.inner
.emit_signal(destination, path, iface, signal_name, body),
)
}
pub fn reply<B>(&self, call: &Message, body: &B) -> Result<u32>
where
B: serde::ser::Serialize + zvariant::DynamicType,
{
block_on(self.inner.reply(call, body))
}
pub fn reply_error<'e, E, B>(&self, call: &Message, error_name: E, body: &B) -> Result<u32>
where
B: serde::ser::Serialize + zvariant::DynamicType,
E: TryInto<ErrorName<'e>>,
E::Error: Into<Error>,
{
block_on(self.inner.reply_error(call, error_name, body))
}
pub fn reply_dbus_error(
&self,
call: &zbus::MessageHeader<'_>,
err: impl DBusError,
) -> Result<u32> {
block_on(self.inner.reply_dbus_error(call, err))
}
pub fn request_name<'w, W>(&self, well_known_name: W) -> Result<()>
where
W: TryInto<WellKnownName<'w>>,
W::Error: Into<Error>,
{
block_on(self.inner.request_name(well_known_name)).map(Into::into)
}
pub fn release_name<'w, W>(&self, well_known_name: W) -> Result<bool>
where
W: TryInto<WellKnownName<'w>>,
W::Error: Into<Error>,
{
block_on(self.inner.release_name(well_known_name))
}
pub fn is_bus(&self) -> bool {
self.inner.is_bus()
}
pub fn object_server(&self) -> impl Deref<Target = ObjectServer> + '_ {
self.inner.sync_object_server(true)
}
pub fn inner(&self) -> &crate::Connection {
&self.inner
}
pub fn into_inner(self) -> crate::Connection {
self.inner
}
pub fn monitor_activity(&self) -> EventListener {
self.inner.monitor_activity()
}
}
impl From<crate::Connection> for Connection {
fn from(conn: crate::Connection) -> Self {
Self { inner: conn }
}
}
#[cfg(test)]
mod tests {
use ntest::timeout;
#[cfg(all(unix, not(feature = "tokio")))]
use std::os::unix::net::UnixStream;
use std::thread;
use test_log::test;
#[cfg(all(unix, feature = "tokio"))]
use tokio::net::UnixStream;
#[cfg(all(windows, not(feature = "tokio")))]
use uds_windows::UnixStream;
use crate::{
blocking::{ConnectionBuilder, MessageIterator},
Guid,
};
#[cfg(unix)]
#[test]
#[timeout(15000)]
fn unix_p2p() {
let guid = Guid::generate();
let (p0, p1) = crate::utils::block_on(async { UnixStream::pair().unwrap() });
let server_thread = thread::spawn(move || {
let c = ConnectionBuilder::unix_stream(p0)
.server(&guid)
.p2p()
.build()
.unwrap();
let reply = c
.call_method(None::<()>, "/", Some("org.zbus.p2p"), "Test", &())
.unwrap();
assert_eq!(reply.to_string(), "Method return");
let val: String = reply.body().unwrap();
val
});
let c = ConnectionBuilder::unix_stream(p1).p2p().build().unwrap();
let listener = c.monitor_activity();
let mut s = MessageIterator::from(&c);
let m = s.next().unwrap().unwrap();
assert_eq!(m.to_string(), "Method call Test");
c.reply(&m, &("yay")).unwrap();
for _ in s {}
let val = server_thread.join().expect("failed to join server thread");
assert_eq!(val, "yay");
listener.wait();
loop {
let listener = c.monitor_activity();
if !listener.wait_timeout(std::time::Duration::from_millis(10)) {
break;
}
}
}
}