Struct zbus::blocking::ObjectServer [−][src]
pub struct ObjectServer { /* fields omitted */ }
Expand description
A blocking wrapper of crate::ObjectServer
.
Example
This example exposes the org.myiface.Example.Quit
method on the /org/zbus/path
path.
use zbus::{blocking::{Connection, ObjectServer}, dbus_interface};
use std::sync::{Arc, Mutex};
use event_listener::Event;
struct Example {
// Interfaces are owned by the ObjectServer. They can have
// `&mut self` methods.
quit_event: Event,
}
impl Example {
fn new(quit_event: Event) -> Self {
Self { quit_event }
}
}
#[dbus_interface(name = "org.myiface.Example")]
impl Example {
// This will be the "Quit" D-Bus method.
fn quit(&mut self) {
self.quit_event.notify(1);
}
// See `dbus_interface` documentation to learn
// how to expose properties & signals as well.
}
let connection = Connection::session()?;
let quit_event = Event::new();
let quit_listener = quit_event.listen();
let interface = Example::new(quit_event);
connection
.object_server_mut()
.at("/org/zbus/path", interface)?;
quit_listener.wait();
Implementations
Register a D-Bus Interface
at a given path. (see the example above)
If the interface already exists at this path, returns false.
Unregister a D-Bus Interface
at a given path.
If there are no more interfaces left at that path, destroys the object as well. Returns whether the object was destroyed.
pub fn with<'server, 'p, P, F, I>(&'server self, path: P, func: F) -> Result<()> where
F: FnOnce(InterfaceDeref<'server, I>, SignalContext<'p>) -> Result<()>,
I: Interface,
P: TryInto<ObjectPath<'p>>,
P::Error: Into<Error>,
pub fn with<'server, 'p, P, F, I>(&'server self, path: P, func: F) -> Result<()> where
F: FnOnce(InterfaceDeref<'server, I>, SignalContext<'p>) -> Result<()>,
I: Interface,
P: TryInto<ObjectPath<'p>>,
P::Error: Into<Error>,
Run func
with the given path & interface.
Errors
If the interface is not registered at the given path, Error::InterfaceNotFound
error is
returned.
Examples
The typical use of this is to emit signals outside of a dispatched handler:
struct MyIface;
#[dbus_interface(name = "org.myiface.MyIface")]
impl MyIface {
#[dbus_interface(signal)]
async fn emit_signal(ctxt: &SignalContext<'_>) -> zbus::Result<()>;
}
connection
.object_server()
.with(path, |_iface: InterfaceDeref<'_, MyIface>, signal_ctxt| {
block_on(MyIface::emit_signal(&signal_ctxt))
})?;
pub fn with_mut<'server, 'p, P, F, I>(
&'server self,
path: P,
func: F
) -> Result<()> where
F: FnOnce(InterfaceDerefMut<'server, I>, SignalContext<'p>) -> Result<()>,
I: Interface,
P: TryInto<ObjectPath<'p>>,
P::Error: Into<Error>,
pub fn with_mut<'server, 'p, P, F, I>(
&'server self,
path: P,
func: F
) -> Result<()> where
F: FnOnce(InterfaceDerefMut<'server, I>, SignalContext<'p>) -> Result<()>,
I: Interface,
P: TryInto<ObjectPath<'p>>,
P::Error: Into<Error>,
Run func
with the given path & interface.
Same as ObjectServer::with
, except func
gets a mutable reference.
Examples
The typical use of this is property changes outside of a dispatched handler:
struct MyIface(u32);
#[dbus_interface(name = "org.myiface.MyIface")]
impl MyIface {
#[dbus_interface(property)]
fn count(&self) -> u32 {
self.0
}
}
connection
.object_server()
.with_mut(path, |mut iface: InterfaceDerefMut<'_, MyIface>, signal_ctxt| {
iface.0 = 42;
block_on(iface.count_changed(&signal_ctxt))
})?;
pub fn get_interface<'p, P, I>(&self, path: P) -> Result<InterfaceDeref<'_, I>> where
I: Interface,
P: TryInto<ObjectPath<'p>>,
P::Error: Into<Error>,
pub fn get_interface<'p, P, I>(&self, path: P) -> Result<InterfaceDeref<'_, I>> where
I: Interface,
P: TryInto<ObjectPath<'p>>,
P::Error: Into<Error>,
Get a reference to the interface at the given path.
pub fn get_interface_mut<'p, P, I>(
&self,
path: P
) -> Result<InterfaceDerefMut<'_, I>> where
I: Interface,
P: TryInto<ObjectPath<'p>>,
P::Error: Into<Error>,
pub fn get_interface_mut<'p, P, I>(
&self,
path: P
) -> Result<InterfaceDerefMut<'_, I>> where
I: Interface,
P: TryInto<ObjectPath<'p>>,
P::Error: Into<Error>,
Get a reference to the interface at the given path.
WARNINGS: Since self
will not be able to access the interface in question until the
return value of this method is dropped, it is highly recommended to prefer
ObjectServer::with
or ObjectServer::with_mut
over this method. They are also more
convenient to use for emitting signals and changing properties.
Errors
If the interface is not registered at the given path, Error::InterfaceNotFound
error is
returned.
Examples
struct MyIface(u32);
#[dbus_interface(name = "org.myiface.MyIface")]
impl MyIface {
#[dbus_interface(property)]
fn count(&self) -> u32 {
self.0
}
}
// Setup connection and object_server etc here and then in another part of the code:
let mut object_server = connection.object_server();
let mut iface = object_server.get_interface_mut::<_, MyIface>(path)?;
// Note: This will not be needed when using `ObjectServer::with_mut`
let ctxt = SignalContext::new(connection.inner(), path)?;
iface.0 = 42;
block_on(iface.count_changed(&ctxt))?;
Get a reference to the underlying async ObjectServer.
Get a mutable reference to the underlying async ObjectServer.
Get the underlying async ObjectServer, consuming self
.
Methods from Deref<Target = ObjectServer>
Register a D-Bus Interface
at a given path. (see the example above)
If the interface already exists at this path, returns false.
Unregister a D-Bus Interface
at a given path.
If there are no more interfaces left at that path, destroys the object as well. Returns whether the object was destroyed.
Run func
with the given path & interface.
Errors
If the interface is not registered at the given path, Error::InterfaceNotFound
error is
returned.
Examples
The typical use of this is to emit signals outside of a dispatched handler:
struct MyIface;
#[dbus_interface(name = "org.myiface.MyIface")]
impl MyIface {
#[dbus_interface(signal)]
async fn emit_signal(ctxt: &SignalContext<'_>) -> zbus::Result<()>;
}
connection
.object_server()
.await
.with(path, |_iface: InterfaceDeref<'_, MyIface>, signal_ctxt| async move {
MyIface::emit_signal(&signal_ctxt).await
})
.await?;
Run func
with the given path & interface.
Same as ObjectServer::with
, except func
gets a mutable reference.
Examples
The typical use of this is property changes outside of a dispatched handler:
struct MyIface(u32);
#[dbus_interface(name = "org.myiface.MyIface")]
impl MyIface {
#[dbus_interface(property)]
async fn count(&self) -> u32 {
self.0
}
}
connection
.object_server()
.await
.with_mut(path, |mut iface: InterfaceDerefMut<'_, MyIface>, signal_ctxt| async move {
iface.0 = 42;
iface.count_changed(&signal_ctxt).await
})
.await?;
pub async fn get_interface<'p, P, I>(
&self,
path: P
) -> Result<InterfaceDeref<'_, I>> where
I: Interface,
P: TryInto<ObjectPath<'p>>,
P::Error: Into<Error>,
pub async fn get_interface<'p, P, I>(
&self,
path: P
) -> Result<InterfaceDeref<'_, I>> where
I: Interface,
P: TryInto<ObjectPath<'p>>,
P::Error: Into<Error>,
Get a reference to the interface at the given path.
pub async fn get_interface_mut<'p, P, I>(
&self,
path: P
) -> Result<InterfaceDerefMut<'_, I>> where
I: Interface,
P: TryInto<ObjectPath<'p>>,
P::Error: Into<Error>,
pub async fn get_interface_mut<'p, P, I>(
&self,
path: P
) -> Result<InterfaceDerefMut<'_, I>> where
I: Interface,
P: TryInto<ObjectPath<'p>>,
P::Error: Into<Error>,
Get a reference to the interface at the given path.
WARNINGS: Since self
will not be able to access the interface in question until the
return value of this method is dropped, it is highly recommended to prefer
ObjectServer::with
or ObjectServer::with_mut
over this method. They are also more
convenient to use for emitting signals and changing properties.
Errors
If the interface is not registered at the given path, Error::InterfaceNotFound
error is
returned.
Examples
struct MyIface(u32);
#[dbus_interface(name = "org.myiface.MyIface")]
impl MyIface {
#[dbus_interface(property)]
async fn count(&self) -> u32 {
self.0
}
}
// Setup connection and object_server etc here and then in another part of the code:
let mut object_server = connection.object_server().await;
let mut iface = object_server.get_interface_mut::<_, MyIface>(path).await?;
// Note: This will not be needed when using `ObjectServer::with_mut`
let ctxt = SignalContext::new(&connection, path)?;
iface.0 = 42;
iface.count_changed(&ctxt).await?;
Start listening to incoming method messages and dispatch them to appropriate interfaces.
This is implicit when creating a bus connection (the default) and doesn’t do anything if
self
is already dispatching. You only need to explicitly call it when using peer-to-peer
connections (see zbus::ConnectionBuilder::p2p
).
Trait Implementations
type Target = ObjectServer
type Target = ObjectServer
The resulting type after dereferencing.
Performs the conversion.
Performs the conversion.