Refactor: serverimpl -> dbus::agent, clean up main.rs
This commit is contained in:
@@ -4,13 +4,9 @@ mod dbus;
|
||||
use log::LevelFilter;
|
||||
use std::future;
|
||||
|
||||
use daemon::signals::Signal;
|
||||
use daemon::Daemon;
|
||||
|
||||
use dbus::endpoint::DbusRegistry;
|
||||
use dbus::interface;
|
||||
use dbus::server_impl::ServerImpl;
|
||||
use dbus_tokio::connection;
|
||||
use dbus::agent::DBusAgent;
|
||||
|
||||
fn initialize_logging() {
|
||||
// Weird: is this the best way to do this?
|
||||
@@ -31,128 +27,20 @@ async fn main() {
|
||||
// Create the daemon
|
||||
let mut daemon = Daemon::new()
|
||||
.map_err(|e| {
|
||||
log::error!("Failed to start daemon: {}", e);
|
||||
log::error!("Failed to initialize daemon: {}", e);
|
||||
std::process::exit(1);
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
// Initialize dbus session connection
|
||||
let (resource, connection) = connection::new_session_sync().unwrap();
|
||||
|
||||
// The resource is a task that should be spawned onto a tokio compatible
|
||||
// reactor ASAP. If the resource ever finishes, you lost connection to D-Bus.
|
||||
//
|
||||
// To shut down the connection, both call _handle.abort() and drop the connection.
|
||||
let _handle = tokio::spawn(async {
|
||||
let err = resource.await;
|
||||
panic!("Lost connection to D-Bus: {}", err);
|
||||
});
|
||||
|
||||
// Acquire the name
|
||||
connection
|
||||
.request_name(interface::NAME, false, true, false)
|
||||
.await
|
||||
.expect("Unable to acquire dbus name");
|
||||
|
||||
// Create shared D-Bus registry
|
||||
let dbus_registry = DbusRegistry::new(connection.clone());
|
||||
|
||||
// Create and register server implementation
|
||||
let server = ServerImpl::new(daemon.event_sender.clone());
|
||||
|
||||
dbus_registry.register_object(interface::OBJECT_PATH, server, |cr| {
|
||||
vec![
|
||||
interface::register_net_buzzert_kordophone_repository(cr),
|
||||
interface::register_net_buzzert_kordophone_settings(cr),
|
||||
]
|
||||
});
|
||||
|
||||
let mut signal_receiver = daemon.obtain_signal_receiver();
|
||||
// Start the D-Bus agent (events in, signals out).
|
||||
let agent = DBusAgent::new(daemon.event_sender.clone(), daemon.obtain_signal_receiver());
|
||||
tokio::spawn(async move {
|
||||
use dbus::interface::signals as DbusSignals;
|
||||
|
||||
while let Some(signal) = signal_receiver.recv().await {
|
||||
match signal {
|
||||
Signal::ConversationsUpdated => {
|
||||
log::debug!("Sending signal: ConversationsUpdated");
|
||||
dbus_registry
|
||||
.send_signal(interface::OBJECT_PATH, DbusSignals::ConversationsUpdated {})
|
||||
.unwrap_or_else(|_| {
|
||||
log::error!("Failed to send signal");
|
||||
0
|
||||
});
|
||||
}
|
||||
|
||||
Signal::MessagesUpdated(conversation_id) => {
|
||||
log::debug!(
|
||||
"Sending signal: MessagesUpdated for conversation {}",
|
||||
conversation_id
|
||||
);
|
||||
dbus_registry
|
||||
.send_signal(
|
||||
interface::OBJECT_PATH,
|
||||
DbusSignals::MessagesUpdated { conversation_id },
|
||||
)
|
||||
.unwrap_or_else(|_| {
|
||||
log::error!("Failed to send signal");
|
||||
0
|
||||
});
|
||||
}
|
||||
|
||||
Signal::AttachmentDownloaded(attachment_id) => {
|
||||
log::debug!(
|
||||
"Sending signal: AttachmentDownloaded for attachment {}",
|
||||
attachment_id
|
||||
);
|
||||
dbus_registry
|
||||
.send_signal(
|
||||
interface::OBJECT_PATH,
|
||||
DbusSignals::AttachmentDownloadCompleted { attachment_id },
|
||||
)
|
||||
.unwrap_or_else(|_| {
|
||||
log::error!("Failed to send signal");
|
||||
0
|
||||
});
|
||||
}
|
||||
|
||||
Signal::AttachmentUploaded(upload_guid, attachment_guid) => {
|
||||
log::debug!(
|
||||
"Sending signal: AttachmentUploaded for upload {}, attachment {}",
|
||||
upload_guid,
|
||||
attachment_guid
|
||||
);
|
||||
dbus_registry
|
||||
.send_signal(
|
||||
interface::OBJECT_PATH,
|
||||
DbusSignals::AttachmentUploadCompleted {
|
||||
upload_guid,
|
||||
attachment_guid,
|
||||
},
|
||||
)
|
||||
.unwrap_or_else(|_| {
|
||||
log::error!("Failed to send signal");
|
||||
0
|
||||
});
|
||||
}
|
||||
|
||||
Signal::UpdateStreamReconnected => {
|
||||
log::debug!("Sending signal: UpdateStreamReconnected");
|
||||
dbus_registry
|
||||
.send_signal(
|
||||
interface::OBJECT_PATH,
|
||||
DbusSignals::UpdateStreamReconnected {},
|
||||
)
|
||||
.unwrap_or_else(|_| {
|
||||
log::error!("Failed to send signal");
|
||||
0
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
agent.run().await;
|
||||
});
|
||||
|
||||
// Run the main daemon loop.
|
||||
daemon.run().await;
|
||||
|
||||
// Keep the process alive as long as any background tasks are running.
|
||||
future::pending::<()>().await;
|
||||
unreachable!()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user