diff --git a/mediarepo-api/src/client_api/file.rs b/mediarepo-api/src/client_api/file.rs index cb8abe3..2ca6799 100644 --- a/mediarepo-api/src/client_api/file.rs +++ b/mediarepo-api/src/client_api/file.rs @@ -6,12 +6,13 @@ use crate::types::files::{ }; use crate::types::identifier::FileIdentifier; use async_trait::async_trait; +use rmp_ipc::context::{PoolGuard, PooledContext}; use rmp_ipc::payload::{BytePayload, EventSendPayload}; use rmp_ipc::prelude::Context; #[derive(Clone)] pub struct FileApi { - ctx: Context, + ctx: PooledContext, } #[async_trait] @@ -20,14 +21,14 @@ impl IPCApi for FileApi { "files" } - fn ctx(&self) -> &Context { - &self.ctx + fn ctx(&self) -> PoolGuard { + self.ctx.acquire() } } impl FileApi { /// Creates a new file api client - pub fn new(ctx: Context) -> Self { + pub fn new(ctx: PooledContext) -> Self { Self { ctx } } diff --git a/mediarepo-api/src/client_api/mod.rs b/mediarepo-api/src/client_api/mod.rs index 62ade72..3f27ac5 100644 --- a/mediarepo-api/src/client_api/mod.rs +++ b/mediarepo-api/src/client_api/mod.rs @@ -7,6 +7,7 @@ use crate::client_api::file::FileApi; use crate::client_api::tag::TagApi; use crate::types::misc::InfoResponse; use async_trait::async_trait; +use rmp_ipc::context::{PoolGuard, PooledContext}; use rmp_ipc::ipc::context::Context; use rmp_ipc::ipc::stream_emitter::EmitMetadata; use rmp_ipc::payload::{EventReceivePayload, EventSendPayload}; @@ -16,7 +17,7 @@ use std::fmt::Debug; #[async_trait] pub trait IPCApi { fn namespace() -> &'static str; - fn ctx(&self) -> &Context; + fn ctx(&self) -> PoolGuard; async fn emit( &self, @@ -41,7 +42,7 @@ pub trait IPCApi { data: T, ) -> ApiResult { 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()?) } @@ -49,14 +50,14 @@ pub trait IPCApi { #[derive(Clone)] pub struct ApiClient { - ctx: Context, + ctx: PooledContext, pub file: FileApi, pub tag: TagApi, } impl ApiClient { /// Creates a new client from an existing ipc context - pub fn new(ctx: Context) -> Self { + pub fn new(ctx: PooledContext) -> Self { Self { file: FileApi::new(ctx.clone()), tag: TagApi::new(ctx.clone()), @@ -67,7 +68,10 @@ impl ApiClient { /// Connects to the ipc Socket #[tracing::instrument(level = "debug")] pub async fn connect(address: &str) -> ApiResult { - let ctx = IPCBuilder::new().address(address).build_client().await?; + let ctx = IPCBuilder::new() + .address(address) + .build_pooled_client(8) + .await?; Ok(Self::new(ctx)) } @@ -75,19 +79,20 @@ impl ApiClient { /// Returns information about the connected ipc server #[tracing::instrument(level = "debug", skip(self))] pub async fn info(&self) -> ApiResult { - let res = self - .ctx + let ctx = self.ctx.acquire(); + let res = ctx .emitter .emit("info", ()) .await? - .await_reply(&self.ctx) + .await_reply(&ctx) .await?; Ok(res.data::()?) } #[tracing::instrument(level = "debug", skip(self))] pub async fn exit(self) -> ApiResult<()> { - self.ctx.stop().await?; + let ctx = (*self.ctx.acquire()).clone(); + ctx.stop().await?; Ok(()) } } diff --git a/mediarepo-api/src/client_api/tag.rs b/mediarepo-api/src/client_api/tag.rs index 12242e5..05ceda6 100644 --- a/mediarepo-api/src/client_api/tag.rs +++ b/mediarepo-api/src/client_api/tag.rs @@ -4,11 +4,12 @@ use crate::types::files::GetFileTagsRequest; use crate::types::identifier::FileIdentifier; use crate::types::tags::TagResponse; use async_trait::async_trait; +use rmp_ipc::context::{PoolGuard, PooledContext}; use rmp_ipc::ipc::context::Context; #[derive(Clone)] pub struct TagApi { - ctx: Context, + ctx: PooledContext, } #[async_trait] @@ -17,13 +18,13 @@ impl IPCApi for TagApi { "tags" } - fn ctx(&self) -> &Context { - &self.ctx + fn ctx(&self) -> PoolGuard { + self.ctx.acquire() } } impl TagApi { - pub fn new(ctx: Context) -> Self { + pub fn new(ctx: PooledContext) -> Self { Self { ctx } } diff --git a/mediarepo-api/src/tauri_plugin/error.rs b/mediarepo-api/src/tauri_plugin/error.rs index d1f1375..f5440fd 100644 --- a/mediarepo-api/src/tauri_plugin/error.rs +++ b/mediarepo-api/src/tauri_plugin/error.rs @@ -1,4 +1,5 @@ use crate::client_api::error::ApiError; +use rmp_ipc::error::Error; use serde::Serialize; use std::fmt::{Display, Formatter}; @@ -27,9 +28,19 @@ impl From<&str> for PluginError { impl From for PluginError { fn from(e: ApiError) -> Self { - Self { - message: format!("ApiError: {:?}", e), - } + let message = match e { + ApiError::IPC(ipc_error) => match ipc_error { + Error::Message(message) => message, + Error::SendError => String::from("Failed to send event to daemon"), + Error::ErrorEvent(e) => { + format!("Received error: {}", e.to_string()) + } + e => { + format!("{:?}", e) + } + }, + }; + Self { message } } }