diff --git a/Cargo.toml b/Cargo.toml index f48e3f0..96b73f3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,3 +1,3 @@ [workspace] resolver = "2" -members = ["jarust", "client"] +members = ["jarust", "jarust_make_plugin", "jarust_plugins", "client"] diff --git a/client/Cargo.toml b/client/Cargo.toml index 95dae49..2a237c7 100644 --- a/client/Cargo.toml +++ b/client/Cargo.toml @@ -6,6 +6,7 @@ edition = "2021" [dependencies] anyhow = "1.0.75" jarust = { version = "*", path = "../jarust" } +jarust_plugins = { version = "*", path = "../jarust_plugins" } log = "0.4.20" tokio = { version = "1.34.0", features = ["time", "macros", "rt-multi-thread"] } simple_logger = "4.3.0" diff --git a/client/src/main.rs b/client/src/main.rs index 74c97ee..90dcb3c 100644 --- a/client/src/main.rs +++ b/client/src/main.rs @@ -1,8 +1,8 @@ use jarust::jaconfig::JaConfig; use jarust::jaconfig::TransportType; -use jarust::plugins::echotest::events::EchoTestPluginEvent; -use jarust::plugins::echotest::handle::EchoTest; -use jarust::plugins::echotest::messages::EchoTestStartMsg; +use jarust_plugins::echotest::events::EchoTestPluginEvent; +use jarust_plugins::echotest::messages::EchoTestStartMsg; +use jarust_plugins::echotest::EchoTest; use log::LevelFilter; use log::SetLoggerError; use simple_logger::SimpleLogger; @@ -12,17 +12,15 @@ async fn main() -> anyhow::Result<()> { init_logger()?; // To make sure handle is working even after dropping the session and the connection - let (handle, mut event_receiver) = { - let mut connection = jarust::connect(JaConfig::new( - "wss://janus.conf.meetecho.com/ws", - None, - TransportType::Wss, - "janus", - )) - .await?; - let session = connection.create(10).await?; - session.attach_echotest().await? - }; + let mut connection = jarust::connect(JaConfig::new( + "wss://janus.conf.meetecho.com/ws", + None, + TransportType::Wss, + "janus", + )) + .await?; + let session = connection.create(10).await?; + let (handle, mut event_receiver) = session.attach_echo_test().await?; handle .start(EchoTestStartMsg { @@ -32,7 +30,7 @@ async fn main() -> anyhow::Result<()> { .await?; while let Some(event) = event_receiver.recv().await { - match event.event { + match event { EchoTestPluginEvent::Result { result, .. } => { log::info!("result: {result}"); } diff --git a/jarust/Cargo.toml b/jarust/Cargo.toml index 485a090..58fa09b 100644 --- a/jarust/Cargo.toml +++ b/jarust/Cargo.toml @@ -3,6 +3,9 @@ name = "jarust" version = "0.1.0" edition = "2021" +[lib] +doctest = false + [dependencies] async-trait = "0.1.75" futures-util = "0.3.29" @@ -16,7 +19,3 @@ serde_json = "1.0.108" [dev-dependencies] tokio = { version = "1.35.1", features = ["macros"] } - -[features] -default = ["echotest"] -echotest = [] diff --git a/jarust/src/jaconfig.rs b/jarust/src/jaconfig.rs index 92989fe..7bfda4b 100644 --- a/jarust/src/jaconfig.rs +++ b/jarust/src/jaconfig.rs @@ -1,4 +1,4 @@ -pub(crate) const CHANNEL_BUFFER_SIZE: usize = 32; +pub const CHANNEL_BUFFER_SIZE: usize = 32; #[derive(Debug)] pub struct JaConfig { diff --git a/jarust/src/jaconnection.rs b/jarust/src/jaconnection.rs index 03e4ddb..bfb8a89 100644 --- a/jarust/src/jaconnection.rs +++ b/jarust/src/jaconnection.rs @@ -15,13 +15,15 @@ use crate::utils::get_subnamespace_from_response; use serde_json::json; use serde_json::Value; use std::collections::HashMap; +use std::ops::Deref; +use std::ops::DerefMut; use std::sync::Arc; use tokio::sync::mpsc; use tokio::sync::Mutex; -use tokio::task::JoinHandle; +use tokio::task::AbortHandle; struct Shared { - demux_join_handle: JoinHandle>, + demux_abort_handle: AbortHandle, config: JaConfig, } @@ -41,7 +43,7 @@ pub struct InnerConnection { #[derive(Clone)] pub struct JaConnection(Arc); -impl std::ops::Deref for JaConnection { +impl Deref for JaConnection { type Target = Arc; fn deref(&self) -> &Self::Target { @@ -49,7 +51,7 @@ impl std::ops::Deref for JaConnection { } } -impl std::ops::DerefMut for JaConnection { +impl DerefMut for JaConnection { fn deref_mut(&mut self) -> &mut Self::Target { &mut self.0 } @@ -115,7 +117,7 @@ impl JaConnection { }); let shared = Shared { - demux_join_handle, + demux_abort_handle: demux_join_handle.abort_handle(), config, }; let safe = SafeShared { @@ -227,6 +229,6 @@ impl JaConnection { impl Drop for InnerConnection { fn drop(&mut self) { log::trace!("Connection dropped"); - self.shared.demux_join_handle.abort(); + self.shared.demux_abort_handle.abort(); } } diff --git a/jarust/src/jahandle.rs b/jarust/src/jahandle.rs index 61485cd..f9ace8b 100644 --- a/jarust/src/jahandle.rs +++ b/jarust/src/jahandle.rs @@ -7,16 +7,17 @@ use crate::jasession::JaSession; use crate::prelude::*; use serde_json::json; use serde_json::Value; +use std::ops::Deref; use std::sync::Arc; use std::sync::Weak; use tokio::sync::mpsc; use tokio::sync::Mutex; -use tokio::task::JoinHandle; +use tokio::task::AbortHandle; struct Shared { id: u64, session: JaSession, - join_handle: JoinHandle<()>, + abort_handle: AbortHandle, } struct SafeShared { @@ -39,7 +40,7 @@ impl WeakJaHandle { } } -impl std::ops::Deref for JaHandle { +impl Deref for JaHandle { type Target = Arc; fn deref(&self) -> &Self::Target { @@ -73,7 +74,7 @@ impl JaHandle { let shared = Shared { id, session, - join_handle, + abort_handle: join_handle.abort_handle(), }; let safe = SafeShared { ack_receiver }; @@ -132,6 +133,6 @@ impl JaHandle { impl Drop for InnerHandle { fn drop(&mut self) { log::trace!("Dropping handle {{ id: {} }}", self.shared.id); - self.shared.join_handle.abort(); + self.shared.abort_handle.abort(); } } diff --git a/jarust/src/japlugin.rs b/jarust/src/japlugin.rs new file mode 100644 index 0000000..b21e0c1 --- /dev/null +++ b/jarust/src/japlugin.rs @@ -0,0 +1,16 @@ +use crate::japrotocol::JaResponse; +use crate::prelude::JaHandle; +use crate::prelude::JaResult; +use async_trait::async_trait; +use tokio::sync::mpsc; +use tokio::task::AbortHandle; + +pub trait PluginTask { + fn assign_abort(&mut self, abort_handle: AbortHandle); + fn abort_plugin(&mut self); +} + +#[async_trait] +pub trait Attach { + async fn attach(&self, plugin_id: &str) -> JaResult<(JaHandle, mpsc::Receiver)>; +} diff --git a/jarust/src/jasession.rs b/jarust/src/jasession.rs index 86d95bf..e6bd803 100644 --- a/jarust/src/jasession.rs +++ b/jarust/src/jasession.rs @@ -5,15 +5,17 @@ use crate::japrotocol::JaResponse; use crate::japrotocol::JaResponseProtocol; use crate::japrotocol::JaSessionRequestProtocol; use crate::prelude::*; +use async_trait::async_trait; use serde_json::json; use serde_json::Value; use std::collections::HashMap; +use std::ops::Deref; use std::sync::Arc; use std::sync::Weak; use std::time::Duration; use tokio::sync::mpsc; use tokio::sync::Mutex; -use tokio::task::JoinHandle; +use tokio::task::AbortHandle; use tokio::time; pub struct Shared { @@ -24,7 +26,7 @@ pub struct Shared { pub struct SafeShared { receiver: mpsc::Receiver, handles: HashMap, - join_handle: Option>, + abort_handle: Option, } pub struct InnerSession { @@ -43,7 +45,7 @@ impl WeakJaSession { } } -impl std::ops::Deref for JaSession { +impl Deref for JaSession { type Target = Arc; fn deref(&self) -> &Self::Target { @@ -62,7 +64,7 @@ impl JaSession { let safe = SafeShared { receiver, handles: HashMap::new(), - join_handle: None, + abort_handle: None, }; let session = Self(Arc::new(InnerSession { @@ -76,15 +78,49 @@ impl JaSession { let _ = this.keep_alive(ka_interval).await; }); - session.safe.lock().await.join_handle = Some(join_handle); + session.safe.lock().await.abort_handle = Some(join_handle.abort_handle()); session } - pub async fn attach( - &self, - plugin_id: &str, - ) -> JaResult<(JaHandle, mpsc::Receiver)> { + pub(crate) async fn send_request(&self, mut request: Value) -> JaResult<()> { + let mut connection = self.shared.connection.clone(); + request["session_id"] = self.shared.id.into(); + connection.send_request(request).await + } + + async fn keep_alive(self, ka_interval: u32) -> JaResult<()> { + let mut interval = time::interval(Duration::from_secs(ka_interval.into())); + let id = { self.shared.id }; + loop { + interval.tick().await; + log::trace!("Sending keep-alive {{ id: {id}, timeout: {ka_interval}s }}"); + self.send_request(json!({ + "janus": JaSessionRequestProtocol::KeepAlive, + })) + .await?; + self.safe.lock().await.receiver.recv().await.unwrap(); + log::trace!("keep-alive OK {{ id: {id} }}"); + } + } + + pub(crate) fn downgrade(&self) -> WeakJaSession { + WeakJaSession(Arc::downgrade(self)) + } +} + +impl Drop for SafeShared { + fn drop(&mut self) { + if let Some(join_handle) = self.abort_handle.take() { + log::trace!("Keepalive task aborted"); + join_handle.abort(); + } + } +} + +#[async_trait] +impl Attach for JaSession { + async fn attach(&self, plugin_id: &str) -> JaResult<(JaHandle, mpsc::Receiver)> { log::info!("Attaching new handle {{ id: {} }}", self.shared.id); let request = json!({ @@ -132,38 +168,4 @@ impl JaSession { Ok((handle, event_receiver)) } - - pub(crate) async fn send_request(&self, mut request: Value) -> JaResult<()> { - let mut connection = self.shared.connection.clone(); - request["session_id"] = self.shared.id.into(); - connection.send_request(request).await - } - - async fn keep_alive(self, ka_interval: u32) -> JaResult<()> { - let mut interval = time::interval(Duration::from_secs(ka_interval.into())); - let id = { self.shared.id }; - loop { - interval.tick().await; - log::trace!("Sending keep-alive {{ id: {id}, timeout: {ka_interval}s }}"); - self.send_request(json!({ - "janus": JaSessionRequestProtocol::KeepAlive, - })) - .await?; - self.safe.lock().await.receiver.recv().await.unwrap(); - log::trace!("keep-alive OK {{ id: {id} }}"); - } - } - - pub(crate) fn downgrade(&self) -> WeakJaSession { - WeakJaSession(Arc::downgrade(self)) - } -} - -impl Drop for SafeShared { - fn drop(&mut self) { - if let Some(join_handle) = self.join_handle.take() { - log::trace!("Keepalive task aborted"); - join_handle.abort(); - } - } } diff --git a/jarust/src/lib.rs b/jarust/src/lib.rs index 9c5a050..fdd3c3f 100644 --- a/jarust/src/lib.rs +++ b/jarust/src/lib.rs @@ -4,15 +4,15 @@ use jaconnection::JaConnection; use prelude::JaResult; pub mod jaconfig; +pub mod jahandle; +pub mod japlugin; pub mod japrotocol; -pub mod plugins; +pub mod jasession; pub mod prelude; pub mod transport; mod error; mod jaconnection; -mod jahandle; -mod jasession; mod nsp_registry; mod tmanager; mod utils; diff --git a/jarust/src/nsp_registry.rs b/jarust/src/nsp_registry.rs index feda439..3c93419 100644 --- a/jarust/src/nsp_registry.rs +++ b/jarust/src/nsp_registry.rs @@ -2,6 +2,8 @@ use crate::jaconfig::CHANNEL_BUFFER_SIZE; use crate::japrotocol::JaResponse; use crate::prelude::*; use std::collections::HashMap; +use std::ops::Deref; +use std::ops::DerefMut; use std::sync::Arc; use std::sync::RwLock; use tokio::sync::mpsc; @@ -13,7 +15,7 @@ pub(crate) struct Inner { #[derive(Clone)] pub(crate) struct NamespaceRegistry(Arc>); -impl std::ops::Deref for NamespaceRegistry { +impl Deref for NamespaceRegistry { type Target = Arc>; fn deref(&self) -> &Self::Target { @@ -21,7 +23,7 @@ impl std::ops::Deref for NamespaceRegistry { } } -impl std::ops::DerefMut for NamespaceRegistry { +impl DerefMut for NamespaceRegistry { fn deref_mut(&mut self) -> &mut Self::Target { &mut self.0 } diff --git a/jarust/src/plugins/echotest/handle.rs b/jarust/src/plugins/echotest/handle.rs deleted file mode 100644 index 6627b59..0000000 --- a/jarust/src/plugins/echotest/handle.rs +++ /dev/null @@ -1,65 +0,0 @@ -use super::events::EchoTestPluginData; -use super::messages::EchoTestStartMsg; -use crate::jaconfig::CHANNEL_BUFFER_SIZE; -use crate::jahandle::JaHandle; -use crate::japrotocol::JaEventProtocol; -use crate::japrotocol::JaResponseProtocol; -use crate::jasession::JaSession; -use crate::prelude::*; -use async_trait::async_trait; -use tokio::sync::mpsc; - -const PLUGIN_ID: &str = "janus.plugin.echotest"; - -#[async_trait] -pub trait EchoTest { - async fn attach_echotest( - &self, - ) -> JaResult<(EchoTestHandle, mpsc::Receiver)>; -} - -pub struct EchoTestHandle { - handle: JaHandle, -} - -impl From for EchoTestHandle { - fn from(handle: JaHandle) -> Self { - Self { handle } - } -} - -#[async_trait] -impl EchoTest for JaSession { - async fn attach_echotest( - &self, - ) -> JaResult<(EchoTestHandle, mpsc::Receiver)> { - let (handle, mut receiver) = self.attach(PLUGIN_ID).await?; - let (tx, rx) = mpsc::channel(CHANNEL_BUFFER_SIZE); - tokio::spawn(async move { - while let Some(msg) = receiver.recv().await { - let msg = match msg.janus { - JaResponseProtocol::Event(JaEventProtocol::Event { plugin_data, .. }) => { - serde_json::from_value::(plugin_data).unwrap() - } - _ => continue, - }; - let _ = tx.send(msg).await; - } - }); - Ok((handle.into(), rx)) - } -} - -impl EchoTestHandle { - pub async fn start(&self, request: EchoTestStartMsg) -> JaResult<()> { - self.handle.message(serde_json::to_value(request)?).await - } -} - -impl std::ops::Deref for EchoTestHandle { - type Target = JaHandle; - - fn deref(&self) -> &Self::Target { - &self.handle - } -} diff --git a/jarust/src/plugins/echotest/mod.rs b/jarust/src/plugins/echotest/mod.rs deleted file mode 100644 index f16b751..0000000 --- a/jarust/src/plugins/echotest/mod.rs +++ /dev/null @@ -1,3 +0,0 @@ -pub mod events; -pub mod handle; -pub mod messages; diff --git a/jarust/src/plugins/mod.rs b/jarust/src/plugins/mod.rs deleted file mode 100644 index 82973d7..0000000 --- a/jarust/src/plugins/mod.rs +++ /dev/null @@ -1,7 +0,0 @@ -#[cfg(feature = "echotest")] -pub mod echotest; - -pub enum Plugin { - #[cfg(feature = "echotest")] - EchoTest, -} diff --git a/jarust/src/prelude.rs b/jarust/src/prelude.rs index 957b51c..7303938 100644 --- a/jarust/src/prelude.rs +++ b/jarust/src/prelude.rs @@ -1,3 +1,9 @@ pub use crate::error::JaError; +pub use crate::jaconfig::CHANNEL_BUFFER_SIZE; +pub use crate::jahandle::JaHandle; +pub use crate::japlugin::Attach; +pub use crate::japlugin::PluginTask; +pub use crate::japrotocol::JaResponse; +pub use crate::jasession::JaSession; pub type JaResult = core::result::Result; diff --git a/jarust/src/tmanager.rs b/jarust/src/tmanager.rs index 7b8dcbb..53f9b2f 100644 --- a/jarust/src/tmanager.rs +++ b/jarust/src/tmanager.rs @@ -1,4 +1,6 @@ use std::collections::HashMap; +use std::ops::Deref; +use std::ops::DerefMut; use std::sync::Arc; use std::sync::RwLock; @@ -16,7 +18,7 @@ pub(crate) struct Inner { #[derive(Clone)] pub(crate) struct TransactionManager(Arc>); -impl std::ops::Deref for TransactionManager { +impl Deref for TransactionManager { type Target = Arc>; fn deref(&self) -> &Self::Target { @@ -24,7 +26,7 @@ impl std::ops::Deref for TransactionManager { } } -impl std::ops::DerefMut for TransactionManager { +impl DerefMut for TransactionManager { fn deref_mut(&mut self) -> &mut Self::Target { &mut self.0 } diff --git a/jarust/src/transport/wss.rs b/jarust/src/transport/wss.rs index e1ecc69..3f7b063 100644 --- a/jarust/src/transport/wss.rs +++ b/jarust/src/transport/wss.rs @@ -7,7 +7,7 @@ use futures_util::SinkExt; use futures_util::StreamExt; use tokio::net::TcpStream; use tokio::sync::mpsc; -use tokio::task::JoinHandle; +use tokio::task::AbortHandle; use tokio_tungstenite::connect_async; use tokio_tungstenite::tungstenite::client::IntoClientRequest; use tokio_tungstenite::tungstenite::Message; @@ -18,7 +18,7 @@ type WebSocketSender = SplitSink>, Mes pub struct WebsocketTransport { sender: Option, - forward_join_handle: Option>, + abort_handle: Option, } #[async_trait] @@ -26,7 +26,7 @@ impl Transport for WebsocketTransport { fn new() -> Self { Self { sender: None, - forward_join_handle: None, + abort_handle: None, } } @@ -47,7 +47,7 @@ impl Transport for WebsocketTransport { }); self.sender = Some(sender); - self.forward_join_handle = Some(forward_join_handle); + self.abort_handle = Some(forward_join_handle.abort_handle()); Ok(rx) } @@ -65,7 +65,7 @@ impl Transport for WebsocketTransport { impl Drop for WebsocketTransport { fn drop(&mut self) { - if let Some(join_handle) = self.forward_join_handle.take() { + if let Some(join_handle) = self.abort_handle.take() { log::trace!("Dropping wss transport"); join_handle.abort(); } diff --git a/jarust_make_plugin/Cargo.toml b/jarust_make_plugin/Cargo.toml new file mode 100644 index 0000000..9539d37 --- /dev/null +++ b/jarust_make_plugin/Cargo.toml @@ -0,0 +1,20 @@ +[package] +name = "jarust_make_plugin" +version = "0.1.0" +edition = "2021" + +[lib] +proc-macro = true +doctest = false + +[dependencies] +async-trait = "0.1.75" +tokio = { version = "1.35.1", features = ["sync", "time", "rt"] } +jarust = { version = "*", path = "../jarust" } +quote = "1.0.35" +syn = "2.0.46" +convert_case = "0.6.0" + +[features] +default = ["echotest"] +echotest = [] diff --git a/jarust_make_plugin/src/lib.rs b/jarust_make_plugin/src/lib.rs new file mode 100644 index 0000000..275f418 --- /dev/null +++ b/jarust_make_plugin/src/lib.rs @@ -0,0 +1,69 @@ +extern crate proc_macro; + +use convert_case::Case; +use convert_case::Casing; +use proc_macro::TokenStream; +use quote::format_ident; +use quote::quote; +use syn::parse::Parse; +use syn::parse::ParseStream; +use syn::parse_macro_input; +use syn::Ident; +use syn::LitStr; +use syn::Token; + +struct PluginInfo { + name: Ident, + id: LitStr, +} + +impl Parse for PluginInfo { + fn parse(input: ParseStream) -> syn::Result { + let name: Ident = input.parse()?; + input.parse::()?; + let id: LitStr = input.parse()?; + Ok(PluginInfo { name, id }) + } +} + +#[proc_macro] +pub fn make_plugin(input: TokenStream) -> TokenStream { + let PluginInfo { name, id } = parse_macro_input!(input as PluginInfo); + let plugin_name = name.clone(); + let plugin_snake_case_name = plugin_name.to_string().to_case(Case::Snake); + + let parse_fn_name = format_ident!("parse_{plugin_snake_case_name}_message"); + let attach_fn_name = format_ident!("attach_{plugin_snake_case_name}"); + + let expanded = quote! { + use jarust::prelude::*; + + #[async_trait::async_trait] + pub trait #name: Attach { + type Event: Send + Sync + 'static; + type Handle: From + std::ops::Deref + PluginTask; + + fn #parse_fn_name(message: JaResponse) -> JaResult; + + async fn #attach_fn_name( + &self, + ) -> JaResult<(Self::Handle, tokio::sync::mpsc::Receiver)> { + let (handle, mut receiver) = self.attach(#id).await?; + let (tx, rx) = tokio::sync::mpsc::channel(CHANNEL_BUFFER_SIZE); + let join_handle = tokio::spawn(async move { + while let Some(msg) = receiver.recv().await { + let msg = Self::#parse_fn_name(msg)?; + let _ = tx.send(msg).await; + } + Ok::<(), JaError>(()) + }); + let abort_handle = join_handle.abort_handle(); + let mut handle: Self::Handle = handle.into(); + handle.assign_abort(abort_handle); + Ok((handle, rx)) + } + } + }; + + TokenStream::from(expanded) +} diff --git a/jarust_plugins/Cargo.toml b/jarust_plugins/Cargo.toml new file mode 100644 index 0000000..5581dea --- /dev/null +++ b/jarust_plugins/Cargo.toml @@ -0,0 +1,20 @@ +[package] +name = "jarust_plugins" +version = "0.1.0" +edition = "2021" + +[lib] +doctest = false + +[dependencies] +async-trait = "0.1.75" +tokio = { version = "1.35.1", features = ["sync", "time", "rt"] } +jarust = { version = "*", path = "../jarust" } +jarust_make_plugin = { version = "*", path = "../jarust_make_plugin" } +serde = { version = "1.0.193", features = ["derive"] } +serde_json = "1.0.108" +log = "0.4.20" + +[features] +default = ["echotest"] +echotest = [] diff --git a/jarust/src/plugins/echotest/events.rs b/jarust_plugins/src/echotest/events.rs similarity index 100% rename from jarust/src/plugins/echotest/events.rs rename to jarust_plugins/src/echotest/events.rs diff --git a/jarust_plugins/src/echotest/handle.rs b/jarust_plugins/src/echotest/handle.rs new file mode 100644 index 0000000..cc5f48e --- /dev/null +++ b/jarust_plugins/src/echotest/handle.rs @@ -0,0 +1,50 @@ +use super::messages::EchoTestStartMsg; +use jarust::prelude::*; +use std::ops::Deref; +use tokio::task::AbortHandle; + +pub struct EchoTestHandle { + handle: JaHandle, + abort_handle: Option, +} + +impl EchoTestHandle { + pub async fn start(&self, request: EchoTestStartMsg) -> JaResult<()> { + self.handle.message(serde_json::to_value(request)?).await + } +} + +impl PluginTask for EchoTestHandle { + fn assign_abort(&mut self, abort_handle: AbortHandle) { + self.abort_handle = Some(abort_handle); + } + + fn abort_plugin(&mut self) { + if let Some(abort_handle) = self.abort_handle.take() { + abort_handle.abort(); + }; + } +} + +impl From for EchoTestHandle { + fn from(handle: JaHandle) -> Self { + Self { + handle, + abort_handle: None, + } + } +} + +impl Deref for EchoTestHandle { + type Target = JaHandle; + + fn deref(&self) -> &Self::Target { + &self.handle + } +} + +impl Drop for EchoTestHandle { + fn drop(&mut self) { + self.abort_plugin(); + } +} diff --git a/jarust/src/plugins/echotest/messages.rs b/jarust_plugins/src/echotest/messages.rs similarity index 100% rename from jarust/src/plugins/echotest/messages.rs rename to jarust_plugins/src/echotest/messages.rs diff --git a/jarust_plugins/src/echotest/mod.rs b/jarust_plugins/src/echotest/mod.rs new file mode 100644 index 0000000..b46db7a --- /dev/null +++ b/jarust_plugins/src/echotest/mod.rs @@ -0,0 +1,31 @@ +pub mod events; +pub mod handle; +pub mod messages; + +use self::events::EchoTestPluginEvent; +use self::handle::EchoTestHandle; +use crate::echotest::events::EchoTestPluginData; +use jarust::japrotocol::JaEventProtocol; +use jarust::japrotocol::JaResponseProtocol; +use jarust::prelude::*; +use jarust_make_plugin::make_plugin; + +make_plugin!(EchoTest, "janus.plugin.echotest"); + +impl EchoTest for JaSession { + type Event = EchoTestPluginEvent; + type Handle = EchoTestHandle; + + fn parse_echo_test_message(message: JaResponse) -> JaResult { + let msg = match message.janus { + JaResponseProtocol::Event(JaEventProtocol::Event { plugin_data, .. }) => { + serde_json::from_value::(plugin_data)?.event + } + _ => { + log::error!("unexpected response"); + return Err(JaError::UnexpectedResponse); + } + }; + Ok(msg) + } +} diff --git a/jarust_plugins/src/lib.rs b/jarust_plugins/src/lib.rs new file mode 100644 index 0000000..1e685d2 --- /dev/null +++ b/jarust_plugins/src/lib.rs @@ -0,0 +1,2 @@ +#[cfg(feature = "echotest")] +pub mod echotest;