-
Hi , I'm testing this code in order to update a readonly property from service. But my offset property does not seam to update as expected. use tokio::{task, time::Duration};
use zbus::names::InterfaceName;
use zbus::{connection, interface, SignalContext};
use zbus::fdo::PropertiesProxy;
use zbus::{Connection, Result, zvariant};
use zvariant::{OwnedValue};
#[derive(Clone)]
struct Greeter {
offset: i32,
}
#[interface(name = "org.example.TestInterface")]
impl Greeter {
/// A "GreeterName" property.
#[zbus(property)]
async fn offset(&self) -> i32 {
self.offset
}
//#[zbus(property)]
async fn set_offset(&mut self, o: i32) {
self.offset = o;
println!("Update offset is called with value : {o}");
}
}
#[tokio::main]
async fn main() -> Result<(),> {
let mut greeter = Greeter {
offset: 23
};
let _conn = connection::Builder::session()?
.name("org.example.TestServer")?
.serve_at("/org/example/TestServer", greeter.clone())?
.build()
.await?;
let connect = Connection::session().await?;
tokio::time::sleep(Duration::from_secs(4)).await;
let path = zbus::zvariant::ObjectPath::try_from("/org/example/TestServer")?;
connect
.object_server()
.at(path.clone(), greeter)
.await?;
let iface_ref = connect
.object_server()
.interface::<_, Greeter>(path).await?;
let mut iface = iface_ref.get_mut().await;
iface.offset = 120;
let _ = iface.offset_changed(iface_ref.signal_context()).await;
let _ = task::spawn(async move {
let mut v:i32 = 0;
loop {
println!("offset = {v}");
tokio::time::sleep(Duration::from_secs(2)).await;
v+=1;
}
}).await;
Ok(())
} |
Beta Was this translation helpful? Give feedback.
Answered by
gnulux
Aug 18, 2024
Replies: 1 comment 1 reply
-
This is probably your issue: You shouldn't keep mutable reference to the interface around, as documented in the method docs:
Also:
async move {
let mut v:i32 = 0;
loop {
println!("offset = {v}");
tokio::time::sleep(Duration::from_secs(2)).await;
v+=1;
}
}.await; |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi zeenix,
you were right the scope was the issue it works now . a big thank to you