|
|
@ -8,34 +8,27 @@ use crate::client_api::file::FileApi;
|
|
|
|
use crate::client_api::tag::TagApi;
|
|
|
|
use crate::client_api::tag::TagApi;
|
|
|
|
use crate::types::misc::{check_apis_compatible, get_api_version, InfoResponse};
|
|
|
|
use crate::types::misc::{check_apis_compatible, get_api_version, InfoResponse};
|
|
|
|
use async_trait::async_trait;
|
|
|
|
use async_trait::async_trait;
|
|
|
|
use rmp_ipc::context::{PoolGuard, PooledContext};
|
|
|
|
use bromine::ipc::stream_emitter::EmitMetadata;
|
|
|
|
use rmp_ipc::ipc::context::Context;
|
|
|
|
use bromine::prelude::*;
|
|
|
|
use rmp_ipc::ipc::stream_emitter::EmitMetadata;
|
|
|
|
|
|
|
|
use rmp_ipc::payload::{EventReceivePayload, EventSendPayload};
|
|
|
|
|
|
|
|
use rmp_ipc::prelude::{AsyncProtocolStream, AsyncStreamProtocolListener};
|
|
|
|
|
|
|
|
use rmp_ipc::IPCBuilder;
|
|
|
|
|
|
|
|
use std::time::Duration;
|
|
|
|
use std::time::Duration;
|
|
|
|
|
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
#[async_trait]
|
|
|
|
pub trait IPCApi<S: AsyncProtocolStream> {
|
|
|
|
pub trait IPCApi {
|
|
|
|
fn namespace() -> &'static str;
|
|
|
|
fn namespace() -> &'static str;
|
|
|
|
fn ctx(&self) -> PoolGuard<Context<S>>;
|
|
|
|
fn ctx(&self) -> PoolGuard<Context>;
|
|
|
|
|
|
|
|
|
|
|
|
async fn emit<T: EventSendPayload + Send>(
|
|
|
|
async fn emit<T: IntoPayload + Send>(
|
|
|
|
&self,
|
|
|
|
&self,
|
|
|
|
event_name: &str,
|
|
|
|
event_name: &str,
|
|
|
|
data: T,
|
|
|
|
data: T,
|
|
|
|
) -> ApiResult<EmitMetadata> {
|
|
|
|
) -> ApiResult<EmitMetadata> {
|
|
|
|
let ctx = self.ctx();
|
|
|
|
let ctx = self.ctx();
|
|
|
|
let meta = ctx
|
|
|
|
let meta = ctx.emit_to(Self::namespace(), event_name, data).await?;
|
|
|
|
.emitter
|
|
|
|
|
|
|
|
.emit_to(Self::namespace(), event_name, data)
|
|
|
|
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Ok(meta)
|
|
|
|
Ok(meta)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async fn emit_and_get<T: EventSendPayload + Send, R: EventReceivePayload + Send>(
|
|
|
|
async fn emit_and_get<T: IntoPayload + Send, R: FromPayload + Send>(
|
|
|
|
&self,
|
|
|
|
&self,
|
|
|
|
event_name: &str,
|
|
|
|
event_name: &str,
|
|
|
|
data: T,
|
|
|
|
data: T,
|
|
|
@ -43,19 +36,16 @@ pub trait IPCApi<S: AsyncProtocolStream> {
|
|
|
|
let meta = self.emit(event_name, data).await?;
|
|
|
|
let meta = self.emit(event_name, data).await?;
|
|
|
|
let response = meta.await_reply(&self.ctx()).await?;
|
|
|
|
let response = meta.await_reply(&self.ctx()).await?;
|
|
|
|
|
|
|
|
|
|
|
|
Ok(response.data()?)
|
|
|
|
Ok(response.payload()?)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pub struct ApiClient<L: AsyncStreamProtocolListener> {
|
|
|
|
pub struct ApiClient {
|
|
|
|
ctx: PooledContext<L::Stream>,
|
|
|
|
ctx: PooledContext,
|
|
|
|
pub file: FileApi<L::Stream>,
|
|
|
|
pub file: FileApi,
|
|
|
|
pub tag: TagApi<L::Stream>,
|
|
|
|
pub tag: TagApi,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl<L> Clone for ApiClient<L>
|
|
|
|
impl Clone for ApiClient {
|
|
|
|
where
|
|
|
|
|
|
|
|
L: AsyncStreamProtocolListener,
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Self {
|
|
|
|
Self {
|
|
|
|
ctx: self.ctx.clone(),
|
|
|
|
ctx: self.ctx.clone(),
|
|
|
@ -65,12 +55,9 @@ where
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl<L> ApiClient<L>
|
|
|
|
impl ApiClient {
|
|
|
|
where
|
|
|
|
|
|
|
|
L: AsyncStreamProtocolListener,
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
/// Creates a new client from an existing ipc context
|
|
|
|
/// Creates a new client from an existing ipc context
|
|
|
|
pub fn new(ctx: PooledContext<L::Stream>) -> Self {
|
|
|
|
pub fn new(ctx: PooledContext) -> Self {
|
|
|
|
Self {
|
|
|
|
Self {
|
|
|
|
file: FileApi::new(ctx.clone()),
|
|
|
|
file: FileApi::new(ctx.clone()),
|
|
|
|
tag: TagApi::new(ctx.clone()),
|
|
|
|
tag: TagApi::new(ctx.clone()),
|
|
|
@ -80,7 +67,9 @@ where
|
|
|
|
|
|
|
|
|
|
|
|
/// Connects to the ipc Socket
|
|
|
|
/// Connects to the ipc Socket
|
|
|
|
#[tracing::instrument(level = "debug")]
|
|
|
|
#[tracing::instrument(level = "debug")]
|
|
|
|
pub async fn connect(address: L::AddressType) -> ApiResult<Self> {
|
|
|
|
pub async fn connect<L: AsyncStreamProtocolListener>(
|
|
|
|
|
|
|
|
address: L::AddressType,
|
|
|
|
|
|
|
|
) -> ApiResult<Self> {
|
|
|
|
let ctx = IPCBuilder::<L>::new()
|
|
|
|
let ctx = IPCBuilder::<L>::new()
|
|
|
|
.address(address)
|
|
|
|
.address(address)
|
|
|
|
.timeout(Duration::from_secs(10))
|
|
|
|
.timeout(Duration::from_secs(10))
|
|
|
@ -109,13 +98,8 @@ where
|
|
|
|
#[tracing::instrument(level = "debug", skip(self))]
|
|
|
|
#[tracing::instrument(level = "debug", skip(self))]
|
|
|
|
pub async fn info(&self) -> ApiResult<InfoResponse> {
|
|
|
|
pub async fn info(&self) -> ApiResult<InfoResponse> {
|
|
|
|
let ctx = self.ctx.acquire();
|
|
|
|
let ctx = self.ctx.acquire();
|
|
|
|
let res = ctx
|
|
|
|
let res = ctx.emit("info", ()).await?.await_reply(&ctx).await?;
|
|
|
|
.emitter
|
|
|
|
Ok(res.payload::<InfoResponse>()?)
|
|
|
|
.emit("info", ())
|
|
|
|
|
|
|
|
.await?
|
|
|
|
|
|
|
|
.await_reply(&ctx)
|
|
|
|
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
Ok(res.data::<InfoResponse>()?)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[tracing::instrument(level = "debug", skip(self))]
|
|
|
|
#[tracing::instrument(level = "debug", skip(self))]
|
|
|
|