Struct zbus::ObjectServer [−][src]
pub struct ObjectServer { /* fields omitted */ }
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, ObjectServer, dbus_interface}; use std::rc::Rc; use std::cell::RefCell; struct Example { // Interfaces are owned by the ObjectServer. They can have // `&mut self` methods. // // If you need a shared state, you can use a RefCell for ex: quit: Rc<RefCell<bool>>, } impl Example { fn new(quit: Rc<RefCell<bool>>) -> Self { Self { quit } } } #[dbus_interface(name = "org.myiface.Example")] impl Example { // This will be the "Quit" D-Bus method. fn quit(&self) { *self.quit.borrow_mut() = true; } // See `dbus_interface` documentation to learn // how to expose properties & signals as well. } let connection = Connection::session()?; let mut object_server = ObjectServer::new(&connection); let quit = Rc::new(RefCell::new(false)); let interface = Example::new(quit.clone()); object_server.at("/org/zbus/path", interface)?; loop { if let Err(err) = object_server.try_handle_next() { eprintln!("{}", err); } if *quit.borrow() { break; } }
Implementations
Creates a new D-Bus ObjectServer
for a given connection.
pub fn request_name<'w, W, E>(self, well_known_name: W) -> Result<Self> where
W: TryInto<WellKnownName<'w>, Error = E>,
E: Into<Error>,
pub fn request_name<'w, W, E>(self, well_known_name: W) -> Result<Self> where
W: TryInto<WellKnownName<'w>, Error = E>,
E: Into<Error>,
Register a well-known name for this service on the bus.
You can request multiple names for the same ObjectServer
. All the names are released
automatically for you when ObjectServer
is dropped. Use ObjectServer::release_name
for
explicitly deregistering names registered through this method.
Note that exclusive ownership without queueing is requested (using
fdo::RequestNameFlags::ReplaceExisting
and fdo::RequestNameFlags::DoNotQueue
flags)
since that is the most typical case. If that is not what you want, you should use
fdo::DBusProxy::request_name
instead (but make sure then that name is requested
after instantiating the ObjectServer
).
pub fn release_name<'w, W, E>(&mut self, well_known_name: W) -> Result<bool> where
W: TryInto<WellKnownName<'w>, Error = E>,
E: Into<Error>,
pub fn release_name<'w, W, E>(&mut self, well_known_name: W) -> Result<bool> where
W: TryInto<WellKnownName<'w>, Error = E>,
E: Into<Error>,
Deregister a previously registered well-known name for this service on the bus.
Use this method to explicitly deregister a well-known name, registered through
ObjectServer::request_name
.
Unless an error is encountered, returns Ok(true)
if name was previously registered with
the bus through self
and it has now been successfully deregistered, Ok(fasle)
if name
was not previously registered or already deregistered.
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.
Run the function func
with the interface at path. If the interface was not found, return
Error::InterfaceNotFound
.
This function is useful to emit signals outside of a dispatched handler:
object_server.with(path, |iface: &MyIface| { iface.emit_signal() })?;
pub fn local_node_emit_signal<'d, 'i, 'm, D, I, M, DE, IE, ME, B>(
destination: Option<D>,
interface_name: I,
signal_name: M,
body: &B
) -> Result<()> where
D: TryInto<BusName<'d>, Error = DE>,
I: TryInto<InterfaceName<'i>, Error = IE>,
M: TryInto<MemberName<'m>, Error = ME>,
DE: Into<Error>,
IE: Into<Error>,
ME: Into<Error>,
B: Serialize + Type,
pub fn local_node_emit_signal<'d, 'i, 'm, D, I, M, DE, IE, ME, B>(
destination: Option<D>,
interface_name: I,
signal_name: M,
body: &B
) -> Result<()> where
D: TryInto<BusName<'d>, Error = DE>,
I: TryInto<InterfaceName<'i>, Error = IE>,
M: TryInto<MemberName<'m>, Error = ME>,
DE: Into<Error>,
IE: Into<Error>,
ME: Into<Error>,
B: Serialize + Type,
Emit a signal on the currently dispatched node.
This is an internal helper function to emit a signal on on the current node. You shouldn’t
call this method directly, rather with the derived signal implementation from
dbus_interface
.
Panics
This method will panic if called from outside of a node context. Use ObjectServer::with
to bring a node into the current context.
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, true if it’s handled, false otherwise.
Receive and handle the next message from the associated connection.
This function will read the incoming message from
receive_message()
of the associated connection and pass it
to dispatch_message()
. If the message was handled by an an
interface, it returns Ok(None)
. If not, it returns the received message.
Returns an error if the message is malformed or an error occurred.