pub mod error;
pub mod handle;
pub mod service;
pub mod storage;
use crate::contacts_service::{
handle::ContactsServiceHandle,
service::ContactsService,
storage::database::{ContactsBackend, ContactsDatabase},
};
use futures::{future, Future};
use log::*;
use tari_service_framework::{
reply_channel,
ServiceInitializationError,
ServiceInitializer,
ServiceInitializerContext,
};
const LOG_TARGET: &str = "wallet::contacts_service::initializer";
pub struct ContactsServiceInitializer<T>
where T: ContactsBackend
{
backend: Option<T>,
}
impl<T> ContactsServiceInitializer<T>
where T: ContactsBackend
{
pub fn new(backend: T) -> Self {
Self { backend: Some(backend) }
}
}
impl<T> ServiceInitializer for ContactsServiceInitializer<T>
where T: ContactsBackend + 'static
{
type Future = impl Future<Output = Result<(), ServiceInitializationError>>;
fn initialize(&mut self, context: ServiceInitializerContext) -> Self::Future {
let (sender, receiver) = reply_channel::unbounded();
let contacts_handle = ContactsServiceHandle::new(sender);
context.register_handle(contacts_handle);
let backend = self
.backend
.take()
.expect("Cannot start Contacts Service without setting a storage backend");
let shutdown_signal = context.get_shutdown_signal();
context.spawn_when_ready(move |handles| async move {
let service =
ContactsService::new(receiver, ContactsDatabase::new(backend), handles.get_shutdown_signal()).start();
futures::pin_mut!(service);
future::select(service, shutdown_signal).await;
info!(target: LOG_TARGET, "Contacts service shutdown");
});
future::ready(Ok(()))
}
}