Can many connections share one interface?
Hi, I'm trying to write a p2p
dbus server (like systemd does, which listens on /run/systemd/private). When a client sends "SayHello" to the server, the server's count increase 1.
But I don't know how to do that. I can only build the server connection by stream, so when a new client tries to connect the server, I have to create a new connection, then I have to create a new interface for this connection. As a result, eachtime a client sends "SayHello", the count it gets is always 1 (not 2, 3, 4...)
Can many connections share one interface? Or could you please provide any suggestion?
Sorry if it's a wrong place to look for help, but I don't know where else can solve my problem. Thank you very much for your time!
The code demo is as follows:
use std::{error::Error};
use zbus::{ConnectionBuilder, Guid, dbus_interface};
#[derive(Clone, Copy)]
struct Greeter {
count: u64
}
#[dbus_interface(name = "net.foo.bar")]
impl Greeter {
fn say_hello(&mut self, name: &str) -> String {
self.count += 1;
format!("Hello {}! I have been called {} times.", name, self.count)
}
}
#[tokio::main]
#[cfg(unix)]
async fn main() -> Result<(), Box<dyn Error>> {
use futures::TryStreamExt;
use tokio::net::UnixListener;
use zbus::MessageStream;
let greeter = Greeter{count: 0};
let listener = UnixListener::bind("/run/user/1000/test").unwrap();
loop {
let (stream, _) = listener.accept().await?;
let guid = Guid::generate();
let server = ConnectionBuilder::unix_stream(stream)
.server(&guid)
.internal_executor(true)
.serve_at("/net/foo/bar", greeter)?
.p2p()
.build().await?;
println!("server built!");
let mut m_stream = MessageStream::from(&server);
let m = m_stream.try_next().await?.unwrap();
println!("Got {}", m.to_string());
}
Ok(())
}