daemon: scaffolding for settings / sync
This commit is contained in:
@@ -1,16 +1,24 @@
|
||||
use directories::ProjectDirs;
|
||||
use std::path::PathBuf;
|
||||
use anyhow::Result;
|
||||
|
||||
use thiserror::Error;
|
||||
use kordophone_db::{
|
||||
database::Database,
|
||||
settings::Settings,
|
||||
models::Conversation,
|
||||
};
|
||||
|
||||
use kordophone::api::http_client::HTTPAPIClient;
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum DaemonError {
|
||||
#[error("Client Not Configured")]
|
||||
ClientNotConfigured,
|
||||
}
|
||||
|
||||
pub struct Daemon {
|
||||
pub version: String,
|
||||
database: Database,
|
||||
client: Option<HTTPAPIClient>,
|
||||
}
|
||||
|
||||
impl Daemon {
|
||||
@@ -23,17 +31,25 @@ impl Daemon {
|
||||
std::fs::create_dir_all(database_dir)?;
|
||||
|
||||
let database = Database::new(&database_path.to_string_lossy())?;
|
||||
Ok(Self { version: "0.1.0".to_string(), database })
|
||||
}
|
||||
|
||||
pub fn get_version(&self) -> String {
|
||||
self.version.clone()
|
||||
// TODO: Check to see if we have client settings in the database
|
||||
|
||||
|
||||
Ok(Self { version: "0.1.0".to_string(), database, client: None })
|
||||
}
|
||||
|
||||
pub fn get_conversations(&mut self) -> Vec<Conversation> {
|
||||
self.database.with_repository(|r| r.all_conversations().unwrap())
|
||||
}
|
||||
|
||||
pub fn sync_all_conversations(&mut self) -> Result<()> {
|
||||
let client = self.client
|
||||
.as_mut()
|
||||
.ok_or(DaemonError::ClientNotConfigured)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn get_database_path() -> PathBuf {
|
||||
if let Some(proj_dirs) = ProjectDirs::from("com", "kordophone", "kordophone") {
|
||||
let data_dir = proj_dirs.data_dir();
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
use log::info;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use crate::{daemon::Daemon, dbus::interface};
|
||||
|
||||
use dbus_crossroads::Crossroads;
|
||||
use dbus_tokio::connection;
|
||||
@@ -11,13 +10,13 @@ use dbus::{
|
||||
Path,
|
||||
};
|
||||
|
||||
pub struct Endpoint {
|
||||
pub struct Endpoint<T: Send + Clone + 'static> {
|
||||
connection: Arc<SyncConnection>,
|
||||
daemon: Arc<Mutex<Daemon>>,
|
||||
implementation: T,
|
||||
}
|
||||
|
||||
impl Endpoint {
|
||||
pub fn new(daemon: Daemon) -> Self {
|
||||
impl<T: Send + Clone + 'static> Endpoint<T> {
|
||||
pub fn new(implementation: T) -> Self {
|
||||
let (resource, connection) = connection::new_session_sync().unwrap();
|
||||
|
||||
// The resource is a task that should be spawned onto a tokio compatible
|
||||
@@ -31,15 +30,24 @@ impl Endpoint {
|
||||
|
||||
Self {
|
||||
connection,
|
||||
daemon: Arc::new(Mutex::new(daemon))
|
||||
implementation
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn start(&self) {
|
||||
use crate::dbus::interface;
|
||||
pub async fn register<F, R>(
|
||||
&self,
|
||||
name: &str,
|
||||
path: &str,
|
||||
register_fn: F
|
||||
)
|
||||
where
|
||||
F: Fn(&mut Crossroads) -> R,
|
||||
R: IntoIterator<Item = dbus_crossroads::IfaceToken<T>>,
|
||||
{
|
||||
let dbus_path = String::from(path);
|
||||
|
||||
self.connection
|
||||
.request_name(interface::NAME, false, true, false)
|
||||
.request_name(name, false, true, false)
|
||||
.await
|
||||
.expect("Unable to acquire dbus name");
|
||||
|
||||
@@ -54,9 +62,9 @@ impl Endpoint {
|
||||
}),
|
||||
)));
|
||||
|
||||
// Register the daemon as a D-Bus object.
|
||||
let token = interface::register_net_buzzert_kordophone_server(&mut cr);
|
||||
cr.insert(interface::OBJECT_PATH, &[token], self.daemon.clone());
|
||||
// Register the daemon as a D-Bus object with multiple interfaces
|
||||
let tokens: Vec<_> = register_fn(&mut cr).into_iter().collect();
|
||||
cr.insert(dbus_path, &tokens, self.implementation.clone());
|
||||
|
||||
// Start receiving messages.
|
||||
self.connection.start_receive(
|
||||
@@ -66,14 +74,14 @@ impl Endpoint {
|
||||
),
|
||||
);
|
||||
|
||||
info!(target: "dbus", "DBus server started");
|
||||
info!(target: "dbus", "Registered endpoint at {} with {} interfaces", path, tokens.len());
|
||||
}
|
||||
|
||||
pub fn send_signal<S>(&self, signal: S) -> Result<u32, ()>
|
||||
pub fn send_signal<S>(&self, path: &str, signal: S) -> Result<u32, ()>
|
||||
where
|
||||
S: dbus::message::SignalArgs + dbus::arg::AppendAll,
|
||||
{
|
||||
let message = signal.to_emit_message(&Path::new(interface::OBJECT_PATH).unwrap());
|
||||
let message = signal.to_emit_message(&Path::new(path).unwrap());
|
||||
self.connection.send(message)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
pub mod endpoint;
|
||||
mod server_impl;
|
||||
pub mod server_impl;
|
||||
|
||||
mod interface {
|
||||
pub mod interface {
|
||||
#![allow(unused)]
|
||||
|
||||
pub const NAME: &str = "net.buzzert.kordophonecd";
|
||||
pub const OBJECT_PATH: &str = "/net/buzzert/kordophonecd";
|
||||
pub const OBJECT_PATH: &str = "/net/buzzert/kordophonecd/daemon";
|
||||
|
||||
include!(concat!(env!("OUT_DIR"), "/kordophone-server.rs"));
|
||||
}
|
||||
@@ -1,19 +1,36 @@
|
||||
use dbus::arg;
|
||||
use dbus_tree::MethodErr;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::sync::{Arc, Mutex, MutexGuard};
|
||||
use log::info;
|
||||
|
||||
use crate::daemon::Daemon;
|
||||
use crate::dbus::interface::NetBuzzertKordophoneServer as DbusServer;
|
||||
use crate::dbus::interface::NetBuzzertKordophoneRepository as DbusRepository;
|
||||
use crate::dbus::interface::NetBuzzertKordophoneSettings as DbusSettings;
|
||||
|
||||
impl DbusServer for Arc<Mutex<Daemon>> {
|
||||
#[derive(Clone)]
|
||||
pub struct ServerImpl {
|
||||
daemon: Arc<Mutex<Daemon>>,
|
||||
}
|
||||
|
||||
impl ServerImpl {
|
||||
pub fn new(daemon: Arc<Mutex<Daemon>>) -> Self {
|
||||
Self { daemon }
|
||||
}
|
||||
|
||||
pub fn get_daemon(&self) -> Result<MutexGuard<'_, Daemon>, MethodErr> {
|
||||
self.daemon.lock().map_err(|_| MethodErr::failed("Failed to lock daemon"))
|
||||
}
|
||||
}
|
||||
|
||||
impl DbusRepository for ServerImpl {
|
||||
fn get_version(&mut self) -> Result<String, MethodErr> {
|
||||
let daemon = self.lock().map_err(|_| MethodErr::failed("Failed to lock daemon"))?;
|
||||
let daemon = self.get_daemon()?;
|
||||
Ok(daemon.version.clone())
|
||||
}
|
||||
|
||||
fn get_conversations(&mut self) -> Result<Vec<arg::PropMap>, dbus::MethodErr> {
|
||||
// Get a repository instance and use it to fetch conversations
|
||||
let mut daemon = self.lock().map_err(|_| MethodErr::failed("Failed to lock daemon"))?;
|
||||
let mut daemon = self.get_daemon()?;
|
||||
let conversations = daemon.get_conversations();
|
||||
|
||||
// Convert conversations to DBus property maps
|
||||
@@ -27,4 +44,49 @@ impl DbusServer for Arc<Mutex<Daemon>> {
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
}
|
||||
|
||||
fn sync_all_conversations(&mut self) -> Result<bool, dbus::MethodErr> {
|
||||
let mut daemon = self.get_daemon()?;
|
||||
daemon.sync_all_conversations().map_err(|e| {
|
||||
log::error!("Failed to sync conversations: {}", e);
|
||||
MethodErr::failed(&format!("Failed to sync conversations: {}", e))
|
||||
})?;
|
||||
|
||||
Ok(true)
|
||||
}
|
||||
}
|
||||
|
||||
impl DbusSettings for ServerImpl {
|
||||
fn set_server(&mut self, url: String, user: String) -> Result<(), dbus::MethodErr> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn set_credential_item_(&mut self, item_path: dbus::Path<'static>) -> Result<(), dbus::MethodErr> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn server_url(&self) -> Result<String, dbus::MethodErr> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn set_server_url(&self, value: String) -> Result<(), dbus::MethodErr> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn username(&self) -> Result<String, dbus::MethodErr> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn set_username(&self, value: String) -> Result<(), dbus::MethodErr> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn credential_item(&self) -> Result<dbus::Path<'static>, dbus::MethodErr> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn set_credential_item(&self, value: dbus::Path<'static>) -> Result<(), dbus::MethodErr> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -2,10 +2,13 @@ mod dbus;
|
||||
mod daemon;
|
||||
|
||||
use std::future;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use log::LevelFilter;
|
||||
|
||||
use daemon::Daemon;
|
||||
use dbus::endpoint::Endpoint as DbusEndpoint;
|
||||
use dbus::interface;
|
||||
use dbus::server_impl::ServerImpl;
|
||||
|
||||
fn initialize_logging() {
|
||||
env_logger::Builder::from_default_env()
|
||||
@@ -19,16 +22,32 @@ async fn main() {
|
||||
initialize_logging();
|
||||
|
||||
// Create the daemon
|
||||
let daemon = Daemon::new()
|
||||
.map_err(|e| {
|
||||
log::error!("Failed to start daemon: {}", e);
|
||||
std::process::exit(1);
|
||||
})
|
||||
.unwrap();
|
||||
let daemon = Arc::new(
|
||||
Mutex::new(
|
||||
Daemon::new()
|
||||
.map_err(|e| {
|
||||
log::error!("Failed to start daemon: {}", e);
|
||||
std::process::exit(1);
|
||||
})
|
||||
.unwrap()
|
||||
)
|
||||
);
|
||||
|
||||
// Create the D-Bus endpoint
|
||||
let endpoint = DbusEndpoint::new(daemon);
|
||||
endpoint.start().await;
|
||||
// Create the server implementation
|
||||
let server = ServerImpl::new(daemon);
|
||||
|
||||
// Register DBus interfaces with endpoint
|
||||
let endpoint = DbusEndpoint::new(server.clone());
|
||||
endpoint.register(
|
||||
interface::NAME,
|
||||
interface::OBJECT_PATH,
|
||||
|cr| {
|
||||
vec![
|
||||
interface::register_net_buzzert_kordophone_repository(cr),
|
||||
interface::register_net_buzzert_kordophone_settings(cr)
|
||||
]
|
||||
}
|
||||
).await;
|
||||
|
||||
future::pending::<()>().await;
|
||||
unreachable!()
|
||||
|
||||
Reference in New Issue
Block a user