Files
telepathy-padfoot/src/main.rs

92 lines
2.3 KiB
Rust
Raw Normal View History

2020-05-08 20:38:30 +01:00
mod padfoot;
mod telepathy;
use anyhow::{anyhow, Result};
2020-05-08 20:38:30 +01:00
use dbus::{
blocking::{stdintf::org_freedesktop_dbus::RequestNameReply, LocalConnection},
tree::Factory,
2020-05-08 20:38:30 +01:00
};
use padfoot::{
Connection, ConnectionManager, Protocol, CM_BUS_NAME, CM_CONN_BUS_NAME, CM_OBJECT_PATH,
CONN_BUS_NAME, PROTO_BUS_NAME, PROTO_OBJECT_PATH,
};
use std::sync::Arc;
2020-05-08 20:38:30 +01:00
use std::time::Duration;
#[derive(Debug, Default)]
struct TData {
cm: ConnectionManager,
p: Protocol,
}
impl dbus::tree::DataType for TData {
type Interface = Arc<TData>;
type Tree = ();
type Property = ();
type ObjectPath = ();
type Method = ();
type Signal = ();
}
2020-05-08 20:38:30 +01:00
fn run() -> Result<()> {
let cm = ConnectionManager::new();
let proto = Protocol {};
let data = Arc::new(TData { cm: cm, p: proto });
let f = Factory::new_fn::<TData>();
2020-05-08 20:38:30 +01:00
let mut tree = f.tree(());
let cm_iface = telepathy::connection_manager_server(&f, Arc::clone(&data), |m| {
let a: &Arc<TData> = &m.iface.get_data();
let b: &TData = &a;
&b.cm
});
let proto_iface = telepathy::protocol_server(&f, Arc::clone(&data), |m| {
let a: &Arc<TData> = &m.iface.get_data();
let b: &TData = &a;
&b.p
});
2020-05-08 20:38:30 +01:00
tree = tree.add(
f.object_path(CM_OBJECT_PATH, ())
2020-05-08 20:38:30 +01:00
.introspectable()
.add(cm_iface),
);
tree = tree.add(
f.object_path(PROTO_OBJECT_PATH, ())
.introspectable()
.add(proto_iface),
2020-05-08 20:38:30 +01:00
);
tree = tree.add(f.object_path("/", ()).introspectable());
2020-05-08 20:38:30 +01:00
// Setup DBus connection
let mut c = LocalConnection::new_session()?;
tree.start_receive(&c);
2020-05-08 20:38:30 +01:00
for name in vec![CM_BUS_NAME, PROTO_BUS_NAME, CM_CONN_BUS_NAME, CONN_BUS_NAME] {
let result = c.request_name(name, false, false, true)?;
match result {
RequestNameReply::Exists => {
return Err(anyhow!("Another process is already registered on {}", name))
}
_ => println!("{} listening on {}", c.unique_name(), name), // All other responses we can get are a success
2020-05-08 20:38:30 +01:00
}
}
2020-05-08 20:38:30 +01:00
loop {
c.process(Duration::from_secs(1))?;
}
}
fn main() {
if let Err(e) = run() {
println!("{}", e);
std::process::exit(1);
}
}