You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
2.1 KiB

//! The actual running service
use super::*;
use std::sync::Arc;
use tokio::{
task::JoinHandle,
sync::{
RwLock,
Mutex,
mpsc,
oneshot,
}
};
pub mod command;
use command::{
CommandKind,
CommandFlags,
CommandID,
};
pub mod config;
mod builder;
pub use builder::*;
pub mod cache;
mod host;
/// Handle to a running service. Can be used to join it or create `Channel`s.
#[derive(Debug)]
pub struct Handle
{
task: JoinHandle<()>,
channel: Channel,
}
/// Inner portion of a `Channel`. Also held through `Arc` by the background service and its supervisor.
#[derive(Debug)]
struct ChannelInner
{
/// The settings for the service
// Boxed because this is a large structure.
opt: Box<config::ServiceSettings>,
}
/// The side of a `command::Command` that the running service sees.
#[derive(Debug)]
struct Request
{
/// ID that corresponds to the returned `command::Command` from the `send()` function of `Channel`.
id: CommandID,
/// The actual command sent by the user.
kind: CommandKind,
/// The metadata flags of this `CommandKind`.
///
/// This is looked up by the sender (user), not the receiver (service) to save service batch processing time.
/// Although the lookup should be extremely fast.
metadata: CommandFlags,
/// Optional response sender.
///
/// Just dropping this is the same as sending `None` as far as the user sees.
resp: oneshot::Sender<Option<command::Response>>,
}
/// Communicates with a running service
#[derive(Debug, Clone)]
pub struct Channel {
inner: Arc<ChannelInner>,
tx: mpsc::Sender<Request>,
}
/// The service's counterpart to `Channel`. Contains the metadata `ChannelInner` and the receiver for `Channel`s.
#[derive(Debug)]
struct Service
{
inner: Arc<ChannelInner>,
rx: mpsc::Receiver<Request>,
}
impl Eq for Channel{}
impl PartialEq for Channel {
#[inline] fn eq(&self, other: &Self) -> bool
{
Arc::ptr_eq(&self.inner, &other.inner)
}
}
/// Create a new service
pub fn create() -> ServiceBuilder
{
todo!()
}