pub struct ObjectServer {
conn: WeakConnection,
root: Arc<RwLock<Node>>,
}
Expand description
An object server, holding server-side D-Bus objects & interfaces.
Object servers hold interfaces on various object paths, and expose them over D-Bus.
All object paths will have the standard interfaces implemented on your behalf, such as
org.freedesktop.DBus.Introspectable
or org.freedesktop.DBus.Properties
.
§Example
This example exposes the org.myiface.Example.Quit
method on the /org/zbus/path
path.
use zbus::{Connection, interface};
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 }
}
}
#[interface(name = "org.myiface.Example")]
impl Example {
// This will be the "Quit" D-Bus method.
async fn quit(&mut self) {
self.quit_event.notify(1);
}
// See `interface` documentation to learn
// how to expose properties & signals as well.
}
let connection = Connection::session().await?;
let quit_event = Event::new();
let quit_listener = quit_event.listen();
let interface = Example::new(quit_event);
connection
.object_server()
.at("/org/zbus/path", interface)
.await?;
quit_listener.await;
Fields§
§conn: WeakConnection
§root: Arc<RwLock<Node>>
Implementations§
Source§impl ObjectServer
impl ObjectServer
Sourcepub(crate) fn new(conn: &Connection) -> Self
pub(crate) fn new(conn: &Connection) -> Self
Create a new D-Bus ObjectServer
.
pub(crate) fn root(&self) -> &RwLock<Node>
Sourcepub async fn at<'p, P, I>(&self, path: P, iface: I) -> Result<bool>
pub async fn at<'p, P, I>(&self, path: P, iface: I) -> Result<bool>
Register a D-Bus Interface
at a given path (see the example above).
Typically you’d want your interfaces to be registered immediately after the associated
connection is established and therefore use zbus::connection::Builder::serve_at
instead.
However, there are situations where you’d need to register interfaces dynamically and that’s
where this method becomes useful.
If the interface already exists at this path, returns false.
pub(crate) async fn add_arc_interface<'p, P>( &self, path: P, name: InterfaceName<'static>, arc_iface: ArcInterface, ) -> Result<bool>
Sourcepub async fn remove<'p, I, P>(&self, path: P) -> Result<bool>
pub async fn remove<'p, I, P>(&self, path: P) -> Result<bool>
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.
Sourcepub async fn interface<'p, P, I>(&self, path: P) -> Result<InterfaceRef<I>>
pub async fn interface<'p, P, I>(&self, path: P) -> Result<InterfaceRef<I>>
Get the interface at the given path.
§Errors
If the interface is not registered at the given path, an Error::InterfaceNotFound
error is
returned.
§Examples
The typical use of this is property changes outside of a dispatched handler:
struct MyIface(u32);
#[interface(name = "org.myiface.MyIface")]
impl MyIface {
#[zbus(property)]
async fn count(&self) -> u32 {
self.0
}
}
let iface_ref = connection
.object_server()
.interface::<_, MyIface>(path).await?;
let mut iface = iface_ref.get_mut().await;
iface.0 = 42;
iface.count_changed(iface_ref.signal_emitter()).await?;
async fn dispatch_call_to_iface( &self, iface: Arc<RwLock<dyn Interface>>, connection: &Connection, msg: &Message, hdr: &Header<'_>, ) -> Result<()>
async fn dispatch_method_call_try( &self, connection: &Connection, msg: &Message, hdr: &Header<'_>, ) -> Result<()>
Sourcepub(crate) async fn dispatch_call(
&self,
msg: &Message,
hdr: &Header<'_>,
) -> Result<()>
pub(crate) async fn dispatch_call( &self, msg: &Message, hdr: &Header<'_>, ) -> Result<()>
Dispatch an incoming message to a registered interface.
The object server will handle the message by:
-
looking up the called object path & interface,
-
calling the associated method if one exists,
-
returning a message (responding to the caller with either a return or error message) to the caller through the associated server connection.
Returns an error if the message is malformed.
pub(crate) fn connection(&self) -> Connection
Trait Implementations§
Source§impl Clone for ObjectServer
impl Clone for ObjectServer
Source§fn clone(&self) -> ObjectServer
fn clone(&self) -> ObjectServer
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more