detector 0.4.2

A set of types for service registration and discovery.
Documentation
use crate::meta::Meta;
use crate::service::{Service, ServiceStatus, Services};
use crate::{MetaEvent, OneselfRx, ServiceEvent, WatchRx};
use std::collections::HashMap;
use std::error::Error;

pub trait MetaDetector {
    /// 注册服务元数据, 不需要ttl
    fn register(&mut self) -> impl Future<Output = Result<(), Box<dyn Error + 'static + Send>>>;

    /// 卸载服务元数据
    fn unregister(&mut self) -> impl Future<Output = Result<(), Box<dyn Error + 'static + Send>>>;
    
    /// 获取元数据
    fn fetch(
        &mut self,
    ) -> impl Future<Output = Result<Option<Meta>, Box<dyn Error + 'static + Send>>>;

    /// 获取所有的元数据
    fn fetch_all(
        &mut self,
    ) -> impl Future<Output = Result<Vec<Meta>, Box<dyn Error + 'static + Send>>>;

    /// 监听元数据
    fn watch(
        &mut self,
    ) -> impl Future<Output = Result<WatchRx<MetaEvent>, Box<dyn Error + 'static + Send>>>;

    /// 监听所有的元数据
    fn watch_all(
        &mut self,
    ) -> impl Future<Output = Result<WatchRx<MetaEvent>, Box<dyn Error + 'static + Send>>>;
}

/// 如果对象被释放掉, 则应该取消掉注册
pub trait Detector {
    /// 本服务数据
    fn service(&self) -> &Service;

    /// 服务id编号
    /// 一旦id有值后则永久不变, 即使被抢占了也不能变
    /// 如果id被抢占,那服务状态则会一直是Registering
    fn id(&self) -> Option<u32> {
        self.service().key.id()
    }

    /// 服务状态
    fn status(&self) -> ServiceStatus;

    /// 是否处于已注册
    fn registered(&self) -> bool {
        self.status().registered()
    }

    /// 是否处于注册过期了
    fn unregistered(&self) -> bool {
        self.status().unregistered()
    }

    /// 注册服务, 且自动维护服务的有效
    fn register(&mut self) -> impl Future<Output = Result<(), Box<dyn Error + 'static + Send>>>;

    /// 获取本类型服所有实例
    fn fetch(&mut self) -> impl Future<Output = Result<Services, Box<dyn Error + 'static + Send>>>;

    /// 获取所有服务,一般是路由用
    fn fetch_all(
        &mut self,
    ) -> impl Future<Output = Result<HashMap<String, Services>, Box<dyn Error + 'static + Send>>>;

    /// 监听本服务类型
    fn watch(
        &mut self,
    ) -> impl Future<Output = Result<WatchRx<ServiceEvent>, Box<dyn Error + 'static + Send>>>;

    /// 监听所有类型服,一般是路由用
    fn watch_all(
        &mut self,
    ) -> impl Future<Output = Result<WatchRx<ServiceEvent>, Box<dyn Error + 'static + Send>>>;

    /// 监听服务自己本身状态, 需要在第一次调用register成功后才能使用它
    fn oneself(&mut self) -> Option<OneselfRx>;
}